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