Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 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 | |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame^] | 17 | #include <sparse/sparse.h> |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 18 | |
| 19 | #include <fcntl.h> |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame^] | 20 | #include <stdbool.h> |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 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> |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 27 | #include <unistd.h> |
| 28 | |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 29 | void usage() |
| 30 | { |
| 31 | fprintf(stderr, "Usage: simg2img <sparse_image_file> <raw_image_file>\n"); |
| 32 | } |
| 33 | |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 34 | int main(int argc, char *argv[]) |
| 35 | { |
| 36 | int in; |
| 37 | int out; |
| 38 | unsigned int i; |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 39 | int ret; |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame^] | 40 | struct sparse_file *s; |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 41 | |
| 42 | if (argc != 3) { |
| 43 | usage(); |
| 44 | exit(-1); |
| 45 | } |
| 46 | |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 47 | if (strcmp(argv[1], "-") == 0) { |
| 48 | in = STDIN_FILENO; |
| 49 | } else { |
| 50 | if ((in = open(argv[1], O_RDONLY)) == 0) { |
| 51 | fprintf(stderr, "Cannot open input file %s\n", argv[1]); |
| 52 | exit(-1); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | if (strcmp(argv[2], "-") == 0) { |
| 57 | out = STDOUT_FILENO; |
| 58 | } else { |
| 59 | if ((out = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0666)) == 0) { |
| 60 | fprintf(stderr, "Cannot open output file %s\n", argv[2]); |
| 61 | exit(-1); |
| 62 | } |
| 63 | } |
| 64 | |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame^] | 65 | s = sparse_file_import(in, true, false); |
| 66 | if (!s) { |
| 67 | fprintf(stderr, "Failed to read sparse file\n"); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 68 | exit(-1); |
| 69 | } |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame^] | 70 | ret = sparse_file_write(s, out, false, false, false); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 71 | |
| 72 | close(in); |
| 73 | close(out); |
| 74 | |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 75 | exit(0); |
| 76 | } |
| 77 | |