blob: 99f433981fba6bad66a5cda130cf2acab2a03a39 [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>
Geremy Condrade807f22013-07-08 14:04:02 -070028#include "backed_block.h"
Jerry Zhang7b444f02018-06-12 16:42:09 -070029#include "sparse_file.h"
Geremy Condrade807f22013-07-08 14:04:02 -070030
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 Zhang7b444f02018-06-12 16:42:09 -070043void usage() {
44 fprintf(stderr, "Usage: append2simg <output> <input>\n");
Geremy Condrade807f22013-07-08 14:04:02 -070045}
46
Jerry Zhang7b444f02018-06-12 16:42:09 -070047int main(int argc, char* argv[]) {
48 int output;
49 int output_block;
50 char* output_path;
51 struct sparse_file* sparse_output;
Geremy Condrade807f22013-07-08 14:04:02 -070052
Jerry Zhang7b444f02018-06-12 16:42:09 -070053 int input;
54 char* input_path;
55 off64_t input_len;
Geremy Condrade807f22013-07-08 14:04:02 -070056
Jerry Zhang7b444f02018-06-12 16:42:09 -070057 int tmp_fd;
58 char* tmp_path;
Colin Cross0e3f47e2014-04-25 14:28:54 -070059
Jerry Zhang7b444f02018-06-12 16:42:09 -070060 int ret;
Colin Cross0e3f47e2014-04-25 14:28:54 -070061
Jerry Zhang7b444f02018-06-12 16:42:09 -070062 if (argc == 3) {
63 output_path = argv[1];
64 input_path = argv[2];
65 } else {
66 usage();
67 exit(-1);
68 }
Geremy Condrade807f22013-07-08 14:04:02 -070069
Jerry Zhang7b444f02018-06-12 16:42:09 -070070 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 Cross0e3f47e2014-04-25 14:28:54 -070075
Jerry Zhang7b444f02018-06-12 16:42:09 -070076 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 Condrade807f22013-07-08 14:04:02 -070081
Jerry Zhang7b444f02018-06-12 16:42:09 -070082 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 Condrade807f22013-07-08 14:04:02 -070087
Jerry Zhang7b444f02018-06-12 16:42:09 -070088 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 Condrade807f22013-07-08 14:04:02 -070093
Jerry Zhang7b444f02018-06-12 16:42:09 -070094 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 Condrade807f22013-07-08 14:04:02 -0700103
Jerry Zhang7b444f02018-06-12 16:42:09 -0700104 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 Condrade807f22013-07-08 14:04:02 -0700110
Jerry Zhang7b444f02018-06-12 16:42:09 -0700111 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 Cross0e3f47e2014-04-25 14:28:54 -0700116
Jerry Zhang7b444f02018-06-12 16:42:09 -0700117 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 Condrade807f22013-07-08 14:04:02 -0700122
Jerry Zhang7b444f02018-06-12 16:42:09 -0700123 sparse_file_destroy(sparse_output);
124 close(tmp_fd);
125 close(output);
126 close(input);
Colin Cross0e3f47e2014-04-25 14:28:54 -0700127
Jerry Zhang7b444f02018-06-12 16:42:09 -0700128 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 Cross0e3f47e2014-04-25 14:28:54 -0700133
Jerry Zhang7b444f02018-06-12 16:42:09 -0700134 free(tmp_path);
Colin Cross0e3f47e2014-04-25 14:28:54 -0700135
Jerry Zhang7b444f02018-06-12 16:42:09 -0700136 exit(0);
Geremy Condrade807f22013-07-08 14:04:02 -0700137}