make_ext4fs_main.c revision 93eb1dc9e68d2e9dea94f56d8bce478c1a52b354
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 <fcntl.h> 18#include <libgen.h> 19#include <unistd.h> 20 21#if defined(__linux__) 22#include <linux/fs.h> 23#elif defined(__APPLE__) && defined(__MACH__) 24#include <sys/disk.h> 25#endif 26 27#include "make_ext4fs.h" 28 29extern struct fs_info info; 30 31 32static void usage(char *path) 33{ 34 fprintf(stderr, "%s [ -l <len> ] [ -j <journal size> ] [ -b <block_size> ]\n", basename(path)); 35 fprintf(stderr, " [ -g <blocks per group> ] [ -i <inodes> ] [ -I <inode size> ]\n"); 36 fprintf(stderr, " [ -L <label> ] [ -f ] [ -a <android mountpoint> ]\n"); 37 fprintf(stderr, " [ -z | -s ] [ -t ] [ -w ] [ -c ] [ -J ]\n"); 38 fprintf(stderr, " <filename> [<directory>]\n"); 39} 40 41int main(int argc, char **argv) 42{ 43 int opt; 44 const char *filename = NULL; 45 const char *directory = NULL; 46 char *mountpoint = ""; 47 int android = 0; 48 int gzip = 0; 49 int sparse = 0; 50 int crc = 0; 51 int wipe = 0; 52 int init_itabs = 0; 53 int fd; 54 int exitcode; 55 56 while ((opt = getopt(argc, argv, "l:j:b:g:i:I:L:a:fwzJsct")) != -1) { 57 switch (opt) { 58 case 'l': 59 info.len = parse_num(optarg); 60 break; 61 case 'j': 62 info.journal_blocks = parse_num(optarg); 63 break; 64 case 'b': 65 info.block_size = parse_num(optarg); 66 break; 67 case 'g': 68 info.blocks_per_group = parse_num(optarg); 69 break; 70 case 'i': 71 info.inodes = parse_num(optarg); 72 break; 73 case 'I': 74 info.inode_size = parse_num(optarg); 75 break; 76 case 'L': 77 info.label = optarg; 78 break; 79 case 'f': 80 force = 1; 81 break; 82 case 'a': 83 android = 1; 84 mountpoint = optarg; 85 break; 86 case 'w': 87 wipe = 1; 88 break; 89 case 'z': 90 gzip = 1; 91 break; 92 case 'J': 93 info.no_journal = 1; 94 break; 95 case 'c': 96 crc = 1; 97 break; 98 case 's': 99 sparse = 1; 100 break; 101 case 't': 102 init_itabs = 1; 103 break; 104 default: /* '?' */ 105 usage(argv[0]); 106 exit(EXIT_FAILURE); 107 } 108 } 109 110 if (gzip && sparse) { 111 fprintf(stderr, "Cannot specify both gzip and sparse\n"); 112 usage(argv[0]); 113 exit(EXIT_FAILURE); 114 } 115 116 if (wipe && sparse) { 117 fprintf(stderr, "Cannot specifiy both wipe and sparse\n"); 118 usage(argv[0]); 119 exit(EXIT_FAILURE); 120 } 121 122 if (wipe && gzip) { 123 fprintf(stderr, "Cannot specifiy both wipe and gzip\n"); 124 usage(argv[0]); 125 exit(EXIT_FAILURE); 126 } 127 128 if (optind >= argc) { 129 fprintf(stderr, "Expected filename after options\n"); 130 usage(argv[0]); 131 exit(EXIT_FAILURE); 132 } 133 134 filename = argv[optind++]; 135 136 if (optind < argc) 137 directory = argv[optind++]; 138 139 if (optind < argc) { 140 fprintf(stderr, "Unexpected argument: %s\n", argv[optind]); 141 usage(argv[0]); 142 exit(EXIT_FAILURE); 143 } 144 145 if (strcmp(filename, "-")) { 146 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644); 147 if (fd < 0) { 148 error_errno("open"); 149 return EXIT_FAILURE; 150 } 151 } else { 152 fd = STDOUT_FILENO; 153 } 154 155 exitcode = make_ext4fs_internal(fd, directory, mountpoint, android, gzip, 156 sparse, crc, wipe, init_itabs); 157 close(fd); 158 159 return exitcode; 160} 161