Add lwext4_mkfs tool skeleton
[lwext4.git] / fs_test / lwext4_mkfs.c
1 /*
2  * Copyright (c) 2013 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/ext4_filedev.h"
43 #include "../blockdev/windows/io_raw.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 const char *usage = "                                    \n\
55 Welcome in lwext4_mkfs tool .                                   \n\
56 Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com)  \n\
57 Usage:                                                          \n\
58 [-i] --input  - input file name (or blockdevice)                \n\
59 [-w] --wpart  - windows partition mode                          \n\
60 \n";
61
62
63 static bool open_linux(void)
64 {
65         ext4_filedev_filename(input_name);
66         bd = ext4_filedev_get();
67         if (!bd) {
68                 printf("open_filedev: fail\n");
69                 return false;
70         }
71         return true;
72 }
73
74 static bool open_windows(void)
75 {
76 #ifdef WIN32
77         ext4_io_raw_filename(input_name);
78         bd = ext4_io_raw_dev_get();
79         if (!bd) {
80                 printf("open_winpartition: fail\n");
81                 return false;
82         }
83         return true;
84 #else
85         printf("open_winpartition: this mode should be used only under windows "
86                "!\n");
87         return false;
88 #endif
89 }
90
91 static bool open_filedev(void)
92 {
93         if (winpart) {
94                 if (!open_windows())
95                         return false;
96         } else {
97                 if (!open_linux())
98                         return false;
99         }
100
101         return true;
102 }
103
104
105 static bool parse_opt(int argc, char **argv)
106 {
107         int option_index = 0;
108         int c;
109
110         static struct option long_options[] = {
111             {"input", required_argument, 0, 'i'},
112             {"wpart", no_argument, 0, 'w'},
113             {0, 0, 0, 0}};
114
115         while (-1 != (c = getopt_long(argc, argv, "i:w",
116                                       long_options, &option_index))) {
117
118                 switch (c) {
119                 case 'i':
120                         input_name = optarg;
121                         break;
122                 case 'w':
123                         winpart = true;
124                         break;
125                 default:
126                         printf("%s", usage);
127                         return false;
128                 }
129         }
130         return true;
131 }
132
133 int main(int argc, char **argv)
134 {
135         if (!parse_opt(argc, argv))
136                 return EXIT_FAILURE;
137
138         if (!open_filedev())
139                 return EXIT_FAILURE;
140
141         static struct ext4_mkfs_info info;
142         int r = ext4_mkfs(bd, &info);
143         if (r != EOK)
144                 return EXIT_FAILURE;
145
146         return EXIT_SUCCESS;
147 }