| Geremy Condra | de807f2 | 2013-07-08 14:04:02 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2013 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
|  | 17 | #define _FILE_OFFSET_BITS 64 | 
|  | 18 | #define _LARGEFILE64_SOURCE 1 | 
|  | 19 |  | 
|  | 20 | #include <errno.h> | 
|  | 21 | #include <fcntl.h> | 
|  | 22 | #include <stdio.h> | 
|  | 23 | #include <stdlib.h> | 
|  | 24 | #include <string.h> | 
|  | 25 | #include <unistd.h> | 
|  | 26 |  | 
|  | 27 | #include <sparse/sparse.h> | 
| Geremy Condra | de807f2 | 2013-07-08 14:04:02 -0700 | [diff] [blame] | 28 | #include "backed_block.h" | 
| Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 29 | #include "sparse_file.h" | 
| Geremy Condra | de807f2 | 2013-07-08 14:04:02 -0700 | [diff] [blame] | 30 |  | 
|  | 31 | #ifndef O_BINARY | 
|  | 32 | #define O_BINARY 0 | 
|  | 33 | #endif | 
|  | 34 |  | 
|  | 35 | #if defined(__APPLE__) && defined(__MACH__) | 
|  | 36 | #define lseek64 lseek | 
|  | 37 | #endif | 
|  | 38 | #if defined(__APPLE__) && defined(__MACH__) | 
|  | 39 | #define lseek64 lseek | 
|  | 40 | #define off64_t off_t | 
|  | 41 | #endif | 
|  | 42 |  | 
| Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 43 | void usage() { | 
|  | 44 | fprintf(stderr, "Usage: append2simg <output> <input>\n"); | 
| Geremy Condra | de807f2 | 2013-07-08 14:04:02 -0700 | [diff] [blame] | 45 | } | 
|  | 46 |  | 
| Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 47 | int main(int argc, char* argv[]) { | 
|  | 48 | int output; | 
|  | 49 | int output_block; | 
|  | 50 | char* output_path; | 
|  | 51 | struct sparse_file* sparse_output; | 
| Geremy Condra | de807f2 | 2013-07-08 14:04:02 -0700 | [diff] [blame] | 52 |  | 
| Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 53 | int input; | 
|  | 54 | char* input_path; | 
|  | 55 | off64_t input_len; | 
| Geremy Condra | de807f2 | 2013-07-08 14:04:02 -0700 | [diff] [blame] | 56 |  | 
| Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 57 | int tmp_fd; | 
|  | 58 | char* tmp_path; | 
| Colin Cross | 0e3f47e | 2014-04-25 14:28:54 -0700 | [diff] [blame] | 59 |  | 
| Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 60 | int ret; | 
| Colin Cross | 0e3f47e | 2014-04-25 14:28:54 -0700 | [diff] [blame] | 61 |  | 
| Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 62 | if (argc == 3) { | 
|  | 63 | output_path = argv[1]; | 
|  | 64 | input_path = argv[2]; | 
|  | 65 | } else { | 
|  | 66 | usage(); | 
|  | 67 | exit(-1); | 
|  | 68 | } | 
| Geremy Condra | de807f2 | 2013-07-08 14:04:02 -0700 | [diff] [blame] | 69 |  | 
| Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 70 | ret = asprintf(&tmp_path, "%s.append2simg", output_path); | 
|  | 71 | if (ret < 0) { | 
|  | 72 | fprintf(stderr, "Couldn't allocate filename\n"); | 
|  | 73 | exit(-1); | 
|  | 74 | } | 
| Colin Cross | 0e3f47e | 2014-04-25 14:28:54 -0700 | [diff] [blame] | 75 |  | 
| Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 76 | output = open(output_path, O_RDWR | O_BINARY); | 
|  | 77 | if (output < 0) { | 
|  | 78 | fprintf(stderr, "Couldn't open output file (%s)\n", strerror(errno)); | 
|  | 79 | exit(-1); | 
|  | 80 | } | 
| Geremy Condra | de807f2 | 2013-07-08 14:04:02 -0700 | [diff] [blame] | 81 |  | 
| Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 82 | sparse_output = sparse_file_import_auto(output, false, true); | 
|  | 83 | if (!sparse_output) { | 
|  | 84 | fprintf(stderr, "Couldn't import output file\n"); | 
|  | 85 | exit(-1); | 
|  | 86 | } | 
| Geremy Condra | de807f2 | 2013-07-08 14:04:02 -0700 | [diff] [blame] | 87 |  | 
| Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 88 | input = open(input_path, O_RDONLY | O_BINARY); | 
|  | 89 | if (input < 0) { | 
|  | 90 | fprintf(stderr, "Couldn't open input file (%s)\n", strerror(errno)); | 
|  | 91 | exit(-1); | 
|  | 92 | } | 
| Geremy Condra | de807f2 | 2013-07-08 14:04:02 -0700 | [diff] [blame] | 93 |  | 
| Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 94 | input_len = lseek64(input, 0, SEEK_END); | 
|  | 95 | if (input_len < 0) { | 
|  | 96 | fprintf(stderr, "Couldn't get input file length (%s)\n", strerror(errno)); | 
|  | 97 | exit(-1); | 
|  | 98 | } else if (input_len % sparse_output->block_size) { | 
|  | 99 | fprintf(stderr, "Input file is not a multiple of the output file's block size"); | 
|  | 100 | exit(-1); | 
|  | 101 | } | 
|  | 102 | lseek64(input, 0, SEEK_SET); | 
| Geremy Condra | de807f2 | 2013-07-08 14:04:02 -0700 | [diff] [blame] | 103 |  | 
| Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 104 | output_block = sparse_output->len / sparse_output->block_size; | 
|  | 105 | if (sparse_file_add_fd(sparse_output, input, 0, input_len, output_block) < 0) { | 
|  | 106 | fprintf(stderr, "Couldn't add input file\n"); | 
|  | 107 | exit(-1); | 
|  | 108 | } | 
|  | 109 | sparse_output->len += input_len; | 
| Geremy Condra | de807f2 | 2013-07-08 14:04:02 -0700 | [diff] [blame] | 110 |  | 
| Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 111 | tmp_fd = open(tmp_path, O_WRONLY | O_CREAT | O_BINARY, 0664); | 
|  | 112 | if (tmp_fd < 0) { | 
|  | 113 | fprintf(stderr, "Couldn't open temporary file (%s)\n", strerror(errno)); | 
|  | 114 | exit(-1); | 
|  | 115 | } | 
| Colin Cross | 0e3f47e | 2014-04-25 14:28:54 -0700 | [diff] [blame] | 116 |  | 
| Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 117 | lseek64(output, 0, SEEK_SET); | 
|  | 118 | if (sparse_file_write(sparse_output, tmp_fd, false, true, false) < 0) { | 
|  | 119 | fprintf(stderr, "Failed to write sparse file\n"); | 
|  | 120 | exit(-1); | 
|  | 121 | } | 
| Geremy Condra | de807f2 | 2013-07-08 14:04:02 -0700 | [diff] [blame] | 122 |  | 
| Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 123 | sparse_file_destroy(sparse_output); | 
|  | 124 | close(tmp_fd); | 
|  | 125 | close(output); | 
|  | 126 | close(input); | 
| Colin Cross | 0e3f47e | 2014-04-25 14:28:54 -0700 | [diff] [blame] | 127 |  | 
| Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 128 | ret = rename(tmp_path, output_path); | 
|  | 129 | if (ret < 0) { | 
|  | 130 | fprintf(stderr, "Failed to rename temporary file (%s)\n", strerror(errno)); | 
|  | 131 | exit(-1); | 
|  | 132 | } | 
| Colin Cross | 0e3f47e | 2014-04-25 14:28:54 -0700 | [diff] [blame] | 133 |  | 
| Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 134 | free(tmp_path); | 
| Colin Cross | 0e3f47e | 2014-04-25 14:28:54 -0700 | [diff] [blame] | 135 |  | 
| Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 136 | exit(0); | 
| Geremy Condra | de807f2 | 2013-07-08 14:04:02 -0700 | [diff] [blame] | 137 | } |