2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define _FILE_OFFSET_BITS 64
18 #define _LARGEFILE64_SOURCE 1
28 #include <sparse/sparse.h>
29 #include "sparse_file.h"
30 #include "backed_block.h"
36 #if defined(__APPLE__) && defined(__MACH__)
39 #if defined(__APPLE__) && defined(__MACH__)
46 fprintf(stderr
, "Usage: append2simg <output> <input>\n");
49 int main(int argc
, char *argv
[])
54 struct sparse_file
*sparse_output
;
66 output_path
= argv
[1];
73 ret
= asprintf(&tmp_path
, "%s.append2simg", output_path
);
75 fprintf(stderr
, "Couldn't allocate filename\n");
79 output
= open(output_path
, O_RDWR
| O_BINARY
);
81 fprintf(stderr
, "Couldn't open output file (%s)\n", strerror(errno
));
85 sparse_output
= sparse_file_import_auto(output
, true, true);
87 fprintf(stderr
, "Couldn't import output file\n");
91 input
= open(input_path
, O_RDONLY
| O_BINARY
);
93 fprintf(stderr
, "Couldn't open input file (%s)\n", strerror(errno
));
97 input_len
= lseek64(input
, 0, SEEK_END
);
99 fprintf(stderr
, "Couldn't get input file length (%s)\n", strerror(errno
));
101 } else if (input_len
% sparse_output
->block_size
) {
102 fprintf(stderr
, "Input file is not a multiple of the output file's block size");
105 lseek64(input
, 0, SEEK_SET
);
107 output_block
= sparse_output
->len
/ sparse_output
->block_size
;
108 if (sparse_file_add_fd(sparse_output
, input
, 0, input_len
, output_block
) < 0) {
109 fprintf(stderr
, "Couldn't add input file\n");
112 sparse_output
->len
+= input_len
;
114 tmp_fd
= open(tmp_path
, O_WRONLY
| O_CREAT
| O_BINARY
, 0664);
116 fprintf(stderr
, "Couldn't open temporary file (%s)\n", strerror(errno
));
120 lseek64(output
, 0, SEEK_SET
);
121 if (sparse_file_write(sparse_output
, tmp_fd
, false, true, false) < 0) {
122 fprintf(stderr
, "Failed to write sparse file\n");
126 sparse_file_destroy(sparse_output
);
131 ret
= rename(tmp_path
, output_path
);
133 fprintf(stderr
, "Failed to rename temporary file (%s)\n", strerror(errno
));