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> |
| 28 | #include "sparse_file.h" |
| 29 | #include "backed_block.h" |
| 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 | |
| 43 | void usage() |
| 44 | { |
| 45 | fprintf(stderr, "Usage: append2simg <output> <input>\n"); |
| 46 | } |
| 47 | |
| 48 | int main(int argc, char *argv[]) |
| 49 | { |
| 50 | int output; |
| 51 | int output_block; |
| 52 | char *output_path; |
| 53 | struct sparse_file *sparse_output; |
| 54 | |
| 55 | int input; |
| 56 | char *input_path; |
| 57 | off64_t input_len; |
| 58 | |
Colin Cross | 0e3f47e | 2014-04-25 14:28:54 -0700 | [diff] [blame] | 59 | int tmp_fd; |
| 60 | char *tmp_path; |
| 61 | |
| 62 | int ret; |
| 63 | |
Geremy Condra | de807f2 | 2013-07-08 14:04:02 -0700 | [diff] [blame] | 64 | if (argc == 3) { |
| 65 | output_path = argv[1]; |
| 66 | input_path = argv[2]; |
| 67 | } else { |
| 68 | usage(); |
| 69 | exit(-1); |
| 70 | } |
| 71 | |
Colin Cross | 0e3f47e | 2014-04-25 14:28:54 -0700 | [diff] [blame] | 72 | ret = asprintf(&tmp_path, "%s.append2simg", output_path); |
| 73 | if (ret < 0) { |
| 74 | fprintf(stderr, "Couldn't allocate filename\n"); |
| 75 | exit(-1); |
| 76 | } |
| 77 | |
Geremy Condra | de807f2 | 2013-07-08 14:04:02 -0700 | [diff] [blame] | 78 | output = open(output_path, O_RDWR | O_BINARY); |
| 79 | if (output < 0) { |
| 80 | fprintf(stderr, "Couldn't open output file (%s)\n", strerror(errno)); |
| 81 | exit(-1); |
| 82 | } |
| 83 | |
Colin Cross | b5619d9 | 2015-12-17 13:52:32 -0800 | [diff] [blame] | 84 | sparse_output = sparse_file_import_auto(output, false, true); |
Geremy Condra | de807f2 | 2013-07-08 14:04:02 -0700 | [diff] [blame] | 85 | if (!sparse_output) { |
| 86 | fprintf(stderr, "Couldn't import output file\n"); |
| 87 | exit(-1); |
| 88 | } |
| 89 | |
| 90 | input = open(input_path, O_RDONLY | O_BINARY); |
| 91 | if (input < 0) { |
| 92 | fprintf(stderr, "Couldn't open input file (%s)\n", strerror(errno)); |
| 93 | exit(-1); |
| 94 | } |
| 95 | |
| 96 | input_len = lseek64(input, 0, SEEK_END); |
| 97 | if (input_len < 0) { |
| 98 | fprintf(stderr, "Couldn't get input file length (%s)\n", strerror(errno)); |
| 99 | exit(-1); |
| 100 | } else if (input_len % sparse_output->block_size) { |
| 101 | fprintf(stderr, "Input file is not a multiple of the output file's block size"); |
| 102 | exit(-1); |
| 103 | } |
| 104 | lseek64(input, 0, SEEK_SET); |
| 105 | |
| 106 | output_block = sparse_output->len / sparse_output->block_size; |
| 107 | if (sparse_file_add_fd(sparse_output, input, 0, input_len, output_block) < 0) { |
| 108 | fprintf(stderr, "Couldn't add input file\n"); |
| 109 | exit(-1); |
| 110 | } |
| 111 | sparse_output->len += input_len; |
| 112 | |
Colin Cross | 0e3f47e | 2014-04-25 14:28:54 -0700 | [diff] [blame] | 113 | tmp_fd = open(tmp_path, O_WRONLY | O_CREAT | O_BINARY, 0664); |
| 114 | if (tmp_fd < 0) { |
| 115 | fprintf(stderr, "Couldn't open temporary file (%s)\n", strerror(errno)); |
| 116 | exit(-1); |
| 117 | } |
| 118 | |
Geremy Condra | de807f2 | 2013-07-08 14:04:02 -0700 | [diff] [blame] | 119 | lseek64(output, 0, SEEK_SET); |
Colin Cross | 0e3f47e | 2014-04-25 14:28:54 -0700 | [diff] [blame] | 120 | if (sparse_file_write(sparse_output, tmp_fd, false, true, false) < 0) { |
Geremy Condra | de807f2 | 2013-07-08 14:04:02 -0700 | [diff] [blame] | 121 | fprintf(stderr, "Failed to write sparse file\n"); |
| 122 | exit(-1); |
| 123 | } |
| 124 | |
| 125 | sparse_file_destroy(sparse_output); |
Colin Cross | 0e3f47e | 2014-04-25 14:28:54 -0700 | [diff] [blame] | 126 | close(tmp_fd); |
Geremy Condra | de807f2 | 2013-07-08 14:04:02 -0700 | [diff] [blame] | 127 | close(output); |
| 128 | close(input); |
Colin Cross | 0e3f47e | 2014-04-25 14:28:54 -0700 | [diff] [blame] | 129 | |
| 130 | ret = rename(tmp_path, output_path); |
| 131 | if (ret < 0) { |
| 132 | fprintf(stderr, "Failed to rename temporary file (%s)\n", strerror(errno)); |
| 133 | exit(-1); |
| 134 | } |
| 135 | |
| 136 | free(tmp_path); |
| 137 | |
Geremy Condra | de807f2 | 2013-07-08 14:04:02 -0700 | [diff] [blame] | 138 | exit(0); |
| 139 | } |