Fix "const const" warning
[lwext4.git] / fs_test / lwext4_mkfs.c
1 /*
2  * Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * - Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  * - Redistributions in binary form must reproduce the above copyright
12  *   notice, this list of conditions and the following disclaimer in the
13  *   documentation and/or other materials provided with the distribution.
14  * - The name of the author may not be used to endorse or promote products
15  *   derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <getopt.h>
34 #include <stdbool.h>
35 #include <inttypes.h>
36 #include <time.h>
37 #include <unistd.h>
38 #include <sys/time.h>
39
40 #include <ext4.h>
41 #include <ext4_mkfs.h>
42 #include "../blockdev/linux/file_dev.h"
43 #include "../blockdev/windows/file_windows.h"
44
45 /**@brief   Input stream name.*/
46 const char *input_name = NULL;
47
48 /**@brief   Block device handle.*/
49 static struct ext4_blockdev *bd;
50
51 /**@brief   Indicates that input is windows partition.*/
52 static bool winpart = false;
53
54 static int fs_type = F_SET_EXT4;
55
56 static struct ext4_fs fs;
57 static struct ext4_mkfs_info info = {
58         .block_size = 1024,
59         .journal = true,
60 };
61
62 static bool verbose = false;
63
64 static const char *usage = "                                    \n\
65 Welcome in lwext4_mkfs tool .                                   \n\
66 Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com)  \n\
67 Usage:                                                          \n\
68 [-i] --input   - input file name (or blockdevice)               \n\
69 [-w] --wpart   - windows partition mode                         \n\
70 [-v] --verbose - verbose mode                                   \n\
71 [-b] --block   - block size: 1024, 2048, 4096 (default 1024)    \n\
72 [-e] --ext     - fs type (ext2: 2, ext3: 3 ext4: 4))            \n\
73 \n";
74
75
76 static bool open_linux(void)
77 {
78         file_dev_name_set(input_name);
79         bd = file_dev_get();
80         if (!bd) {
81                 printf("open_filedev: fail\n");
82                 return false;
83         }
84         return true;
85 }
86
87 static bool open_windows(void)
88 {
89 #ifdef WIN32
90         file_windows_name_set(input_name);
91         bd = file_windows_dev_get();
92         if (!bd) {
93                 printf("open_winpartition: fail\n");
94                 return false;
95         }
96         return true;
97 #else
98         printf("open_winpartition: this mode should be used only under windows "
99                "!\n");
100         return false;
101 #endif
102 }
103
104 static bool open_filedev(void)
105 {
106         return winpart ? open_windows() : open_linux();
107 }
108
109 static bool parse_opt(int argc, char **argv)
110 {
111         int option_index = 0;
112         int c;
113
114         static struct option long_options[] = {
115             {"input", required_argument, 0, 'i'},
116             {"block", required_argument, 0, 'b'},
117             {"ext", required_argument, 0, 'e'},
118             {"wpart", no_argument, 0, 'w'},
119             {"verbose", no_argument, 0, 'v'},
120             {"version", no_argument, 0, 'x'},
121             {0, 0, 0, 0}};
122
123         while (-1 != (c = getopt_long(argc, argv, "i:b:e:wvx",
124                                       long_options, &option_index))) {
125
126                 switch (c) {
127                 case 'i':
128                         input_name = optarg;
129                         break;
130                 case 'b':
131                         info.block_size = atoi(optarg);
132                         break;
133                 case 'e':
134                         fs_type = atoi(optarg);
135                         break;
136                 case 'w':
137                         winpart = true;
138                         break;
139                 case 'v':
140                         verbose = true;
141                         break;
142                 case 'x':
143                         puts(VERSION);
144                         exit(0);
145                         break;
146                 default:
147                         printf("%s", usage);
148                         return false;
149                 }
150         }
151
152         switch (info.block_size) {
153         case 1024:
154         case 2048:
155         case 4096:
156                 break;
157         default:
158                 printf("parse_opt: block_size = %"PRIu32" unsupported\n",
159                                 info.block_size);
160                 return false;
161         }
162
163         switch (fs_type) {
164         case F_SET_EXT2:
165         case F_SET_EXT3:
166         case F_SET_EXT4:
167                 break;
168         default:
169                 printf("parse_opt: fs_type = %"PRIu32" unsupported\n", fs_type);
170                 return false;
171         }
172
173         return true;
174 }
175
176 int main(int argc, char **argv)
177 {
178         int r;
179         if (!parse_opt(argc, argv)){
180                 printf("parse_opt error\n");
181                 return EXIT_FAILURE;
182         }
183
184         if (!open_filedev()) {
185                 printf("open_filedev error\n");
186                 return EXIT_FAILURE;
187         }
188
189         if (verbose)
190                 ext4_dmask_set(DEBUG_ALL);
191
192         printf("ext4_mkfs: ext%d\n", fs_type);
193         r = ext4_mkfs(&fs, bd, &info, fs_type);
194         if (r != EOK) {
195                 printf("ext4_mkfs error: %d\n", r);
196                 return EXIT_FAILURE;
197         }
198
199         memset(&info, 0, sizeof(struct ext4_mkfs_info));
200         r = ext4_mkfs_read_info(bd, &info);
201         if (r != EOK) {
202                 printf("ext4_mkfs_read_info error: %d\n", r);
203                 return EXIT_FAILURE;
204         }
205
206         printf("Created filesystem with parameters:\n");
207         printf("Size: %"PRIu64"\n", info.len);
208         printf("Block size: %"PRIu32"\n", info.block_size);
209         printf("Blocks per group: %"PRIu32"\n", info.blocks_per_group);
210         printf("Inodes per group: %"PRIu32"\n", info.inodes_per_group);
211         printf("Inode size: %"PRIu32"\n", info.inode_size);
212         printf("Inodes: %"PRIu32"\n", info.inodes);
213         printf("Journal blocks: %"PRIu32"\n", info.journal_blocks);
214         printf("Features ro_compat: 0x%x\n", info.feat_ro_compat);
215         printf("Features compat: 0x%x\n", info.feat_compat);
216         printf("Features incompat: 0x%x\n", info.feat_incompat);
217         printf("BG desc reserve: %"PRIu32"\n", info.bg_desc_reserve_blocks);
218         printf("Descriptor size: %"PRIu32"\n",info.dsc_size);
219         printf("Label: %s\n", info.label);
220
221         printf("\nDone ...\n");
222         return EXIT_SUCCESS;
223 }