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