e22167fcf4e6754751983fba71318ce59d0c81ed
[project/make_ext4fs.git] / make_ext4fs_main.c
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 <stdio.h>
20 #include <unistd.h>
21
22 #if defined(__linux__)
23 #include <linux/fs.h>
24 #elif defined(__APPLE__) && defined(__MACH__)
25 #include <sys/disk.h>
26 #endif
27
28 #include "ext4_utils.h"
29 #include "canned_fs_config.h"
30
31 extern struct fs_info info;
32
33
34 static void usage(char *path)
35 {
36         fprintf(stderr, "%s [ -l <len> ] [ -j <journal size> ] [ -b <block_size> ]\n", basename(path));
37         fprintf(stderr, "    [ -g <blocks per group> ] [ -i <inodes> ] [ -I <inode size> ]\n");
38         fprintf(stderr, "    [ -L <label> ] [ -f ] [ -a <android mountpoint> ]\n");
39         fprintf(stderr, "    [ -S file_contexts ] [ -C fs_config ] [ -T timestamp ]\n");
40         fprintf(stderr, "    [ -z | -s ] [ -w ] [ -c ] [ -J ] [ -v ] [ -B <block_list_file> ]\n");
41         fprintf(stderr, "    <filename> [<directory>]\n");
42 }
43
44 int main(int argc, char **argv)
45 {
46         int opt;
47         const char *filename = NULL;
48         const char *directory = NULL;
49         char *mountpoint = NULL;
50         fs_config_func_t fs_config_func = NULL;
51         const char *fs_config_file = NULL;
52         int gzip = 0;
53         int sparse = 0;
54         int crc = 0;
55         int wipe = 0;
56         int fd;
57         int exitcode;
58         int verbose = 0;
59         time_t fixed_time = -1;
60         FILE* block_list_file = NULL;
61
62         while ((opt = getopt(argc, argv, "l:j:b:g:i:I:L:T:C:B:fwzJsctv")) != -1) {
63                 switch (opt) {
64                 case 'l':
65                         info.len = parse_num(optarg);
66                         break;
67                 case 'j':
68                         info.journal_blocks = parse_num(optarg);
69                         break;
70                 case 'b':
71                         info.block_size = parse_num(optarg);
72                         break;
73                 case 'g':
74                         info.blocks_per_group = parse_num(optarg);
75                         break;
76                 case 'i':
77                         info.inodes = parse_num(optarg);
78                         break;
79                 case 'I':
80                         info.inode_size = parse_num(optarg);
81                         break;
82                 case 'L':
83                         info.label = optarg;
84                         break;
85                 case 'f':
86                         force = 1;
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                         fprintf(stderr, "Warning: -t (initialize inode tables) is deprecated\n");
105                         break;
106                 case 'v':
107                         verbose = 1;
108                         break;
109                 case 'T':
110                         fixed_time = strtoll(optarg, NULL, 0);
111                         break;
112                 case 'C':
113                         fs_config_file = optarg;
114                         break;
115                 case 'B':
116                         block_list_file = fopen(optarg, "w");
117                         if (block_list_file == NULL) {
118                                 fprintf(stderr, "failed to open block_list_file: %s\n", strerror(errno));
119                                 exit(EXIT_FAILURE);
120                         }
121                         break;
122                 default: /* '?' */
123                         usage(argv[0]);
124                         exit(EXIT_FAILURE);
125                 }
126         }
127
128         if (fs_config_file) {
129                 if (load_canned_fs_config(fs_config_file) < 0) {
130                         fprintf(stderr, "failed to load %s\n", fs_config_file);
131                         exit(EXIT_FAILURE);
132                 }
133                 fs_config_func = canned_fs_config;
134         }
135
136         if (wipe && sparse) {
137                 fprintf(stderr, "Cannot specifiy both wipe and sparse\n");
138                 usage(argv[0]);
139                 exit(EXIT_FAILURE);
140         }
141
142         if (wipe && gzip) {
143                 fprintf(stderr, "Cannot specifiy both wipe and gzip\n");
144                 usage(argv[0]);
145                 exit(EXIT_FAILURE);
146         }
147
148         if (optind >= argc) {
149                 fprintf(stderr, "Expected filename after options\n");
150                 usage(argv[0]);
151                 exit(EXIT_FAILURE);
152         }
153
154         filename = argv[optind++];
155
156         if (optind < argc)
157                 directory = argv[optind++];
158
159         if (optind < argc) {
160                 fprintf(stderr, "Unexpected argument: %s\n", argv[optind]);
161                 usage(argv[0]);
162                 exit(EXIT_FAILURE);
163         }
164
165         if (strcmp(filename, "-")) {
166                 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
167                 if (fd < 0) {
168                         perror("open");
169                         return EXIT_FAILURE;
170                 }
171         } else {
172                 fd = STDOUT_FILENO;
173         }
174
175         exitcode = make_ext4fs_internal(fd, directory, mountpoint, fs_config_func, gzip,
176                 sparse, crc, wipe, verbose, fixed_time, block_list_file);
177         close(fd);
178         if (block_list_file)
179                 fclose(block_list_file);
180         if (exitcode && strcmp(filename, "-"))
181                 unlink(filename);
182         return exitcode;
183 }