blob: a2c296e89ba8f8e8de2ef0603531c55ccd2839c2 [file] [log] [blame]
Colin Crossbdc6d392012-05-02 15:18:22 -07001/*
2 * Copyright (C) 2012 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
Colin Crossbdc6d392012-05-02 15:18:22 -070019
20#include <fcntl.h>
21#include <stdbool.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
Colin Crossbdc6d392012-05-02 15:18:22 -070025#include <sys/stat.h>
26#include <sys/types.h>
27#include <unistd.h>
28
29#include <sparse/sparse.h>
30
31#ifndef O_BINARY
32#define O_BINARY 0
33#endif
34
Jerry Zhang7b444f02018-06-12 16:42:09 -070035void usage() {
Colin Crossbdc6d392012-05-02 15:18:22 -070036 fprintf(stderr, "Usage: simg2simg <sparse image file> <sparse_image_file> <max_size>\n");
37}
38
Jerry Zhang7b444f02018-06-12 16:42:09 -070039int main(int argc, char* argv[]) {
40 int in;
41 int out;
42 int i;
43 int ret;
44 struct sparse_file* s;
45 int64_t max_size;
46 struct sparse_file** out_s;
47 int files;
48 char filename[4096];
Colin Crossbdc6d392012-05-02 15:18:22 -070049
Jerry Zhang7b444f02018-06-12 16:42:09 -070050 if (argc != 4) {
51 usage();
52 exit(-1);
53 }
Colin Crossbdc6d392012-05-02 15:18:22 -070054
Jerry Zhang7b444f02018-06-12 16:42:09 -070055 max_size = atoll(argv[3]);
Colin Crossbdc6d392012-05-02 15:18:22 -070056
Jerry Zhang7b444f02018-06-12 16:42:09 -070057 in = open(argv[1], O_RDONLY | O_BINARY);
58 if (in < 0) {
59 fprintf(stderr, "Cannot open input file %s\n", argv[1]);
60 exit(-1);
61 }
Colin Crossbdc6d392012-05-02 15:18:22 -070062
Jerry Zhang7b444f02018-06-12 16:42:09 -070063 s = sparse_file_import(in, true, false);
64 if (!s) {
65 fprintf(stderr, "Failed to import sparse file\n");
66 exit(-1);
67 }
Colin Crossbdc6d392012-05-02 15:18:22 -070068
Yi Kong17ba95e2018-07-23 16:31:11 -070069 files = sparse_file_resparse(s, max_size, nullptr, 0);
Jerry Zhang7b444f02018-06-12 16:42:09 -070070 if (files < 0) {
71 fprintf(stderr, "Failed to resparse\n");
72 exit(-1);
73 }
Colin Crossbdc6d392012-05-02 15:18:22 -070074
Jerry Zhang7b444f02018-06-12 16:42:09 -070075 out_s = calloc(sizeof(struct sparse_file*), files);
76 if (!out_s) {
77 fprintf(stderr, "Failed to allocate sparse file array\n");
78 exit(-1);
79 }
Colin Crossbdc6d392012-05-02 15:18:22 -070080
Jerry Zhang7b444f02018-06-12 16:42:09 -070081 files = sparse_file_resparse(s, max_size, out_s, files);
82 if (files < 0) {
83 fprintf(stderr, "Failed to resparse\n");
84 exit(-1);
85 }
Colin Crossbdc6d392012-05-02 15:18:22 -070086
Jerry Zhang7b444f02018-06-12 16:42:09 -070087 for (i = 0; i < files; i++) {
88 ret = snprintf(filename, sizeof(filename), "%s.%d", argv[2], i);
89 if (ret >= (int)sizeof(filename)) {
90 fprintf(stderr, "Filename too long\n");
91 exit(-1);
92 }
Colin Crossbdc6d392012-05-02 15:18:22 -070093
Jerry Zhang7b444f02018-06-12 16:42:09 -070094 out = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0664);
95 if (out < 0) {
96 fprintf(stderr, "Cannot open output file %s\n", argv[2]);
97 exit(-1);
98 }
Colin Crossbdc6d392012-05-02 15:18:22 -070099
Jerry Zhang7b444f02018-06-12 16:42:09 -0700100 ret = sparse_file_write(out_s[i], out, false, true, false);
101 if (ret) {
102 fprintf(stderr, "Failed to write sparse file\n");
103 exit(-1);
104 }
105 close(out);
106 }
Colin Crossbdc6d392012-05-02 15:18:22 -0700107
Jerry Zhang7b444f02018-06-12 16:42:09 -0700108 close(in);
Colin Crossbdc6d392012-05-02 15:18:22 -0700109
Jerry Zhang7b444f02018-06-12 16:42:09 -0700110 exit(0);
Colin Crossbdc6d392012-05-02 15:18:22 -0700111}