source: mainline/uspace/app/mkext4/mkext4.c@ 6abdef37

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6abdef37 was 6abdef37, checked in by Maurizio Lombardi <mlombard@…>, 7 years ago

mkext4: fix infinite loop when passing an invalid argument.

Also adds the —help parameter.

  • Property mode set to 100644
File size: 4.3 KB
Line 
1/*
2 * Copyright (c) 2018 Jiri Svoboda
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/** @addtogroup mkext4
30 * @{
31 */
32
33/**
34 * @file
35 * @brief Tool for creating new Ext4 file systems.
36 */
37
38#include <errno.h>
39#include <ext4/filesystem.h>
40#include <loc.h>
41#include <stdio.h>
42#include <str.h>
43
44#define NAME "mkext4"
45
46static void syntax_print(void);
47static errno_t ext4_version_parse(const char *, ext4_cfg_ver_t *);
48
49int main(int argc, char **argv)
50{
51 errno_t rc;
52 char *dev_path;
53 service_id_t service_id;
54 ext4_cfg_t cfg;
55 char *endptr;
56 aoff64_t nblocks;
57 const char *label = "";
58
59 cfg.version = ext4_def_fs_version;
60
61 if (argc < 2) {
62 printf(NAME ": Error, argument missing.\n");
63 syntax_print();
64 return 1;
65 }
66
67 --argc;
68 ++argv;
69
70 while (*argv && *argv[0] == '-') {
71 if (str_cmp(*argv, "--size") == 0) {
72 --argc;
73 ++argv;
74 if (*argv == NULL) {
75 printf(NAME ": Error, argument missing.\n");
76 syntax_print();
77 return 1;
78 }
79
80 nblocks = strtol(*argv, &endptr, 10);
81 if (*endptr != '\0') {
82 printf(NAME ": Error, invalid argument.\n");
83 syntax_print();
84 return 1;
85 }
86
87 --argc;
88 ++argv;
89 continue;
90 }
91
92 if (str_cmp(*argv, "--type") == 0) {
93 --argc;
94 ++argv;
95 if (*argv == NULL) {
96 printf(NAME ": Error, argument missing.\n");
97 syntax_print();
98 return 1;
99 }
100
101 rc = ext4_version_parse(*argv, &cfg.version);
102 if (rc != EOK) {
103 printf(NAME ": Error, invalid argument.\n");
104 syntax_print();
105 return 1;
106 }
107
108 --argc;
109 ++argv;
110 continue;
111 }
112
113 if (str_cmp(*argv, "--label") == 0) {
114 --argc;
115 ++argv;
116 if (*argv == NULL) {
117 printf(NAME ": Error, argument missing.\n");
118 syntax_print();
119 return 1;
120 }
121
122 label = *argv;
123
124 --argc;
125 ++argv;
126 continue;
127 }
128
129 if (str_cmp(*argv, "--help") == 0) {
130 syntax_print();
131 return 0;
132 }
133
134 if (str_cmp(*argv, "-") == 0) {
135 --argc;
136 ++argv;
137 break;
138 } else {
139 printf(NAME ": Invalid argument: %s\n", *argv);
140 syntax_print();
141 return 1;
142 }
143 }
144
145 if (argc != 1) {
146 printf(NAME ": Error, unexpected argument.\n");
147 syntax_print();
148 return 1;
149 }
150
151 dev_path = *argv;
152 printf("Device: %s\n", dev_path);
153
154 rc = loc_service_get_id(dev_path, &service_id, 0);
155 if (rc != EOK) {
156 printf(NAME ": Error resolving device `%s'.\n", dev_path);
157 return 2;
158 }
159
160 cfg.volume_name = label;
161 (void) nblocks;
162
163 rc = ext4_filesystem_create(&cfg, service_id);
164 if (rc != EOK) {
165 printf(NAME ": Error initializing file system.\n");
166 return 3;
167 }
168
169 printf("Success.\n");
170
171 return 0;
172}
173
174static void syntax_print(void)
175{
176 printf("syntax: mkext4 [<options>...] <device_name>\n");
177 printf("options:\n"
178 "\t--size <sectors> Filesystem size, overrides device size\n"
179 "\t--label <label> Volume label\n"
180 "\t--type <fstype> Filesystem type (ext2, ext2old)\n");
181}
182
183static errno_t ext4_version_parse(const char *str, ext4_cfg_ver_t *ver)
184{
185 if (str_cmp(str, "ext2old") == 0) {
186 *ver = extver_ext2_old;
187 return EOK;
188 }
189
190 if (str_cmp(str, "ext2") == 0) {
191 *ver = extver_ext2;
192 return EOK;
193 }
194
195 return EINVAL;
196}
197
198/**
199 * @}
200 */
Note: See TracBrowser for help on using the repository browser.