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