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