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