1 /* 2 * Copyright (C) 2010 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 #include <sparse/sparse.h> 18 19 #include <fcntl.h> 20 #include <stdbool.h> 21 #include <stdio.h> 22 #include <stdlib.h> 23 #include <string.h> 24 #include <sys/types.h> 25 #include <sys/stat.h> 26 #include <sys/types.h> 27 #include <unistd.h> 28 29 #ifndef O_BINARY 30 #define O_BINARY 0 31 #endif 32 33 void usage() 34 { 35 fprintf(stderr, "Usage: simg2img <sparse_image_files> <raw_image_file>\n"); 36 } 37 38 int main(int argc, char *argv[]) 39 { 40 int in; 41 int out; 42 int i; 43 struct sparse_file *s; 44 45 if (argc < 3) { 46 usage(); 47 exit(-1); 48 } 49 50 out = open(argv[argc - 1], O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0664); 51 if (out < 0) { 52 fprintf(stderr, "Cannot open output file %s\n", argv[argc - 1]); 53 exit(-1); 54 } 55 56 for (i = 1; i < argc - 1; i++) { 57 if (strcmp(argv[i], "-") == 0) { 58 in = STDIN_FILENO; 59 } else { 60 in = open(argv[i], O_RDONLY | O_BINARY); 61 if (in < 0) { 62 fprintf(stderr, "Cannot open input file %s\n", argv[i]); 63 exit(-1); 64 } 65 } 66 67 s = sparse_file_import(in, true, false); 68 if (!s) { 69 fprintf(stderr, "Failed to read sparse file\n"); 70 exit(-1); 71 } 72 73 if (lseek(out, 0, SEEK_SET) == -1) { 74 perror("lseek failed"); 75 exit(EXIT_FAILURE); 76 } 77 78 if (sparse_file_write(s, out, false, false, false) < 0) { 79 fprintf(stderr, "Cannot write output file\n"); 80 exit(-1); 81 } 82 sparse_file_destroy(s); 83 close(in); 84 } 85 86 close(out); 87 88 exit(0); 89 } 90 91