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