blob: 83450a0d911805c82bbe07b9f8380169c3eb1b4d [file] [log] [blame]
Geremy Condrade807f22013-07-08 14:04:02 -07001/*
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
43void usage()
44{
45 fprintf(stderr, "Usage: append2simg <output> <input>\n");
46}
47
48int 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 Cross0e3f47e2014-04-25 14:28:54 -070059 int tmp_fd;
60 char *tmp_path;
61
62 int ret;
63
Geremy Condrade807f22013-07-08 14:04:02 -070064 if (argc == 3) {
65 output_path = argv[1];
66 input_path = argv[2];
67 } else {
68 usage();
69 exit(-1);
70 }
71
Colin Cross0e3f47e2014-04-25 14:28:54 -070072 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 Condrade807f22013-07-08 14:04:02 -070078 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 Crossb5619d92015-12-17 13:52:32 -080084 sparse_output = sparse_file_import_auto(output, false, true);
Geremy Condrade807f22013-07-08 14:04:02 -070085 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 Cross0e3f47e2014-04-25 14:28:54 -0700113 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 Condrade807f22013-07-08 14:04:02 -0700119 lseek64(output, 0, SEEK_SET);
Colin Cross0e3f47e2014-04-25 14:28:54 -0700120 if (sparse_file_write(sparse_output, tmp_fd, false, true, false) < 0) {
Geremy Condrade807f22013-07-08 14:04:02 -0700121 fprintf(stderr, "Failed to write sparse file\n");
122 exit(-1);
123 }
124
125 sparse_file_destroy(sparse_output);
Colin Cross0e3f47e2014-04-25 14:28:54 -0700126 close(tmp_fd);
Geremy Condrade807f22013-07-08 14:04:02 -0700127 close(output);
128 close(input);
Colin Cross0e3f47e2014-04-25 14:28:54 -0700129
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 Condrade807f22013-07-08 14:04:02 -0700138 exit(0);
139}