2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define _FILE_OFFSET_BITS 64
18 #define _LARGEFILE64_SOURCE 1
25 #include <sys/types.h>
27 #include <sys/types.h>
30 #include <sparse/sparse.h>
36 #if defined(__APPLE__) && defined(__MACH__)
43 fprintf(stderr, "Usage: img2simg <raw_image_file> <sparse_image_file> [<block_size>]\n");
46 int main(int argc, char *argv[])
51 struct sparse_file *s;
52 unsigned int block_size = 4096;
55 if (argc < 3 || argc > 4) {
61 block_size = atoi(argv[3]);
64 if (block_size < 1024 || block_size % 4 != 0) {
69 if (strcmp(argv[1], "-") == 0) {
72 in = open(argv[1], O_RDONLY | O_BINARY);
74 fprintf(stderr, "Cannot open input file %s\n", argv[1]);
79 if (strcmp(argv[2], "-") == 0) {
82 out = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0664);
84 fprintf(stderr, "Cannot open output file %s\n", argv[2]);
89 len = lseek64(in, 0, SEEK_END);
90 lseek64(in, 0, SEEK_SET);
92 s = sparse_file_new(block_size, len);
94 fprintf(stderr, "Failed to create sparse file\n");
98 sparse_file_verbose(s);
99 ret = sparse_file_read(s, in, false, false);
101 fprintf(stderr, "Failed to read file\n");
105 ret = sparse_file_write(s, out, false, true, false);
107 fprintf(stderr, "Failed to write sparse file\n");