blob: 180584f4889541ea605a6bed3d8ca39a2fce9950 [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
59 if (argc == 3) {
60 output_path = argv[1];
61 input_path = argv[2];
62 } else {
63 usage();
64 exit(-1);
65 }
66
67 output = open(output_path, O_RDWR | O_BINARY);
68 if (output < 0) {
69 fprintf(stderr, "Couldn't open output file (%s)\n", strerror(errno));
70 exit(-1);
71 }
72
73 sparse_output = sparse_file_import_auto(output, true);
74 if (!sparse_output) {
75 fprintf(stderr, "Couldn't import output file\n");
76 exit(-1);
77 }
78
79 input = open(input_path, O_RDONLY | O_BINARY);
80 if (input < 0) {
81 fprintf(stderr, "Couldn't open input file (%s)\n", strerror(errno));
82 exit(-1);
83 }
84
85 input_len = lseek64(input, 0, SEEK_END);
86 if (input_len < 0) {
87 fprintf(stderr, "Couldn't get input file length (%s)\n", strerror(errno));
88 exit(-1);
89 } else if (input_len % sparse_output->block_size) {
90 fprintf(stderr, "Input file is not a multiple of the output file's block size");
91 exit(-1);
92 }
93 lseek64(input, 0, SEEK_SET);
94
95 output_block = sparse_output->len / sparse_output->block_size;
96 if (sparse_file_add_fd(sparse_output, input, 0, input_len, output_block) < 0) {
97 fprintf(stderr, "Couldn't add input file\n");
98 exit(-1);
99 }
100 sparse_output->len += input_len;
101
102 lseek64(output, 0, SEEK_SET);
103 if (sparse_file_write(sparse_output, output, false, true, false) < 0) {
104 fprintf(stderr, "Failed to write sparse file\n");
105 exit(-1);
106 }
107
108 sparse_file_destroy(sparse_output);
109 close(output);
110 close(input);
111 exit(0);
112}