summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGeremy Condra2013-07-08 16:04:02 -0500
committerGeremy Condra2013-08-07 00:16:07 -0500
commitde807f2ed8af63a75174933c13705a7529a82914 (patch)
tree4c036589e7eede43beb2c87fd04d72e224550129 /libsparse
parent3ad3d1c4b5856d4e314febc5671c74e78a76db00 (diff)
downloadplatform-system-core-de807f2ed8af63a75174933c13705a7529a82914.tar.gz
platform-system-core-de807f2ed8af63a75174933c13705a7529a82914.tar.xz
platform-system-core-de807f2ed8af63a75174933c13705a7529a82914.zip
Add append2simg.
Append2simg is a small utility designed to glue data to the end of a sparse image. Beware that it doesn't do anything clever with that data. Change-Id: I33af330cb4f77e0842375797a35aebbb6af60163
Diffstat (limited to 'libsparse')
-rw-r--r--libsparse/Android.mk4
-rw-r--r--libsparse/append2simg.c112
2 files changed, 114 insertions, 2 deletions
diff --git a/libsparse/Android.mk b/libsparse/Android.mk
index 9025cc025..a21e090c4 100644
--- a/libsparse/Android.mk
+++ b/libsparse/Android.mk
@@ -82,8 +82,8 @@ include $(BUILD_EXECUTABLE)
82 82
83 83
84include $(CLEAR_VARS) 84include $(CLEAR_VARS)
85LOCAL_SRC_FILES := simg2simg.c 85LOCAL_SRC_FILES := append2simg.c
86LOCAL_MODULE := simg2simg 86LOCAL_MODULE := append2simg
87LOCAL_STATIC_LIBRARIES := \ 87LOCAL_STATIC_LIBRARIES := \
88 libsparse_host \ 88 libsparse_host \
89 libz 89 libz
diff --git a/libsparse/append2simg.c b/libsparse/append2simg.c
new file mode 100644
index 000000000..180584f48
--- /dev/null
+++ b/libsparse/append2simg.c
@@ -0,0 +1,112 @@
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 "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}