• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "backed_block.h"
29 #include "sparse_file.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 
usage()43 void usage() {
44   fprintf(stderr, "Usage: append2simg <output> <input>\n");
45 }
46 
main(int argc,char * argv[])47 int main(int argc, char* argv[]) {
48   int output;
49   int output_block;
50   char* output_path;
51   struct sparse_file* sparse_output;
52 
53   int input;
54   char* input_path;
55   off64_t input_len;
56 
57   int tmp_fd;
58   char* tmp_path;
59 
60   int ret;
61 
62   if (argc == 3) {
63     output_path = argv[1];
64     input_path = argv[2];
65   } else {
66     usage();
67     exit(EXIT_FAILURE);
68   }
69 
70   ret = asprintf(&tmp_path, "%s.append2simg", output_path);
71   if (ret < 0) {
72     fprintf(stderr, "Couldn't allocate filename\n");
73     exit(EXIT_FAILURE);
74   }
75 
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(EXIT_FAILURE);
80   }
81 
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(EXIT_FAILURE);
86   }
87 
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(EXIT_FAILURE);
92   }
93 
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(EXIT_FAILURE);
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(EXIT_FAILURE);
101   }
102   lseek64(input, 0, SEEK_SET);
103 
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(EXIT_FAILURE);
108   }
109   sparse_output->len += input_len;
110 
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(EXIT_FAILURE);
115   }
116 
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(EXIT_FAILURE);
121   }
122 
123   sparse_file_destroy(sparse_output);
124   close(tmp_fd);
125   close(output);
126   close(input);
127 
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(EXIT_FAILURE);
132   }
133 
134   free(tmp_path);
135 
136   exit(EXIT_SUCCESS);
137 }
138