]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - misc/util.c
Eliminate unused parameter warnings from Android build
[thirdparty/e2fsprogs.git] / misc / util.c
1 /*
2 * util.c --- helper functions used by tune2fs and mke2fs
3 *
4 * Copyright 1995, 1996, 1997, 1998, 1999, 2000 by Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
10 */
11
12 #ifndef _LARGEFILE_SOURCE
13 #define _LARGEFILE_SOURCE
14 #endif
15 #ifndef _LARGEFILE64_SOURCE
16 #define _LARGEFILE64_SOURCE
17 #endif
18
19 #include "config.h"
20 #include <fcntl.h>
21 #include <setjmp.h>
22 #include <signal.h>
23 #include <stdio.h>
24 #include <string.h>
25 #ifdef HAVE_ERRNO_H
26 #include <errno.h>
27 #endif
28 #ifdef HAVE_LINUX_MAJOR_H
29 #include <linux/major.h>
30 #endif
31 #include <sys/types.h>
32 #ifdef HAVE_SYS_STAT_H
33 #include <sys/stat.h>
34 #endif
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
38 #include <time.h>
39
40 #include "et/com_err.h"
41 #include "e2p/e2p.h"
42 #include "ext2fs/ext2_fs.h"
43 #include "ext2fs/ext2fs.h"
44 #include "support/nls-enable.h"
45 #include "blkid/blkid.h"
46 #include "util.h"
47
48 char *journal_location_string = NULL;
49
50 #ifndef HAVE_STRCASECMP
51 int strcasecmp (char *s1, char *s2)
52 {
53 while (*s1 && *s2) {
54 int ch1 = *s1++, ch2 = *s2++;
55 if (isupper (ch1))
56 ch1 = tolower (ch1);
57 if (isupper (ch2))
58 ch2 = tolower (ch2);
59 if (ch1 != ch2)
60 return ch1 - ch2;
61 }
62 return *s1 ? 1 : *s2 ? -1 : 0;
63 }
64 #endif
65
66 /*
67 * Given argv[0], return the program name.
68 */
69 char *get_progname(char *argv_zero)
70 {
71 char *cp;
72
73 cp = strrchr(argv_zero, '/');
74 if (!cp )
75 return argv_zero;
76 else
77 return cp+1;
78 }
79
80 static jmp_buf alarm_env;
81
82 static void alarm_signal(int signal EXT2FS_ATTR((unused)))
83 {
84 longjmp(alarm_env, 1);
85 }
86
87 void proceed_question(int delay)
88 {
89 char buf[256];
90 const char *short_yes = _("yY");
91
92 fflush(stdout);
93 fflush(stderr);
94 if (delay > 0) {
95 if (setjmp(alarm_env)) {
96 signal(SIGALRM, SIG_IGN);
97 printf("%s", _("<proceeding>\n"));
98 return;
99 }
100 signal(SIGALRM, alarm_signal);
101 printf(_("Proceed anyway (or wait %d seconds) ? (y,n) "),
102 delay);
103 alarm(delay);
104 } else
105 fputs(_("Proceed anyway? (y,n) "), stdout);
106 buf[0] = 0;
107 if (!fgets(buf, sizeof(buf), stdin) ||
108 strchr(short_yes, buf[0]) == 0) {
109 putc('\n', stdout);
110 exit(1);
111 }
112 signal(SIGALRM, SIG_IGN);
113 }
114
115 void check_mount(const char *device, int force, const char *type)
116 {
117 errcode_t retval;
118 int mount_flags;
119
120 retval = ext2fs_check_if_mounted(device, &mount_flags);
121 if (retval) {
122 com_err("ext2fs_check_if_mount", retval,
123 _("while determining whether %s is mounted."),
124 device);
125 return;
126 }
127 if (mount_flags & EXT2_MF_MOUNTED) {
128 fprintf(stderr, _("%s is mounted; "), device);
129 if (force >= 2) {
130 fputs(_("mke2fs forced anyway. Hope /etc/mtab is "
131 "incorrect.\n"), stderr);
132 return;
133 }
134 abort_mke2fs:
135 fprintf(stderr, _("will not make a %s here!\n"), type);
136 exit(1);
137 }
138 if (mount_flags & EXT2_MF_BUSY) {
139 fprintf(stderr, _("%s is apparently in use by the system; "),
140 device);
141 if (force >= 2) {
142 fputs(_("mke2fs forced anyway.\n"), stderr);
143 return;
144 }
145 goto abort_mke2fs;
146 }
147 }
148
149 void parse_journal_opts(const char *opts)
150 {
151 char *buf, *token, *next, *p, *arg;
152 int len;
153 int journal_usage = 0;
154
155 len = strlen(opts);
156 buf = malloc(len+1);
157 if (!buf) {
158 fputs(_("Couldn't allocate memory to parse journal "
159 "options!\n"), stderr);
160 exit(1);
161 }
162 strcpy(buf, opts);
163 for (token = buf; token && *token; token = next) {
164 p = strchr(token, ',');
165 next = 0;
166 if (p) {
167 *p = 0;
168 next = p+1;
169 }
170 arg = strchr(token, '=');
171 if (arg) {
172 *arg = 0;
173 arg++;
174 }
175 #if 0
176 printf("Journal option=%s, argument=%s\n", token,
177 arg ? arg : "NONE");
178 #endif
179 if (strcmp(token, "device") == 0) {
180 journal_device = blkid_get_devname(NULL, arg, NULL);
181 if (!journal_device) {
182 if (arg)
183 fprintf(stderr, _("\nCould not find "
184 "journal device matching %s\n"),
185 arg);
186 journal_usage++;
187 continue;
188 }
189 } else if (strcmp(token, "size") == 0) {
190 if (!arg) {
191 journal_usage++;
192 continue;
193 }
194 journal_size = strtoul(arg, &p, 0);
195 if (*p)
196 journal_usage++;
197 } else if (!strcmp(token, "location")) {
198 if (!arg) {
199 journal_usage++;
200 continue;
201 }
202 journal_location_string = strdup(arg);
203 } else if (strcmp(token, "v1_superblock") == 0) {
204 journal_flags |= EXT2_MKJOURNAL_V1_SUPER;
205 continue;
206 } else
207 journal_usage++;
208 }
209 if (journal_usage) {
210 fputs(_("\nBad journal options specified.\n\n"
211 "Journal options are separated by commas, "
212 "and may take an argument which\n"
213 "\tis set off by an equals ('=') sign.\n\n"
214 "Valid journal options are:\n"
215 "\tsize=<journal size in megabytes>\n"
216 "\tdevice=<journal device>\n"
217 "\tlocation=<journal location>\n\n"
218 "The journal size must be between "
219 "1024 and 10240000 filesystem blocks.\n\n"), stderr);
220 free(buf);
221 exit(1);
222 }
223 free(buf);
224 }
225
226 /*
227 * Determine the number of journal blocks to use, either via
228 * user-specified # of megabytes, or via some intelligently selected
229 * defaults.
230 *
231 * Find a reasonable journal file size (in blocks) given the number of blocks
232 * in the filesystem. For very small filesystems, it is not reasonable to
233 * have a journal that fills more than half of the filesystem.
234 */
235 unsigned int figure_journal_size(int size, ext2_filsys fs)
236 {
237 int j_blocks;
238
239 j_blocks = ext2fs_default_journal_size(ext2fs_blocks_count(fs->super));
240 if (j_blocks < 0) {
241 fputs(_("\nFilesystem too small for a journal\n"), stderr);
242 return 0;
243 }
244
245 if (size > 0) {
246 j_blocks = size * 1024 / (fs->blocksize / 1024);
247 if (j_blocks < 1024 || j_blocks > 10240000) {
248 fprintf(stderr, _("\nThe requested journal "
249 "size is %d blocks; it must be\n"
250 "between 1024 and 10240000 blocks. "
251 "Aborting.\n"),
252 j_blocks);
253 exit(1);
254 }
255 if ((unsigned) j_blocks > ext2fs_free_blocks_count(fs->super) / 2) {
256 fputs(_("\nJournal size too big for filesystem.\n"),
257 stderr);
258 exit(1);
259 }
260 }
261 return j_blocks;
262 }
263
264 void print_check_message(int mnt, unsigned int check)
265 {
266 if (mnt < 0)
267 mnt = 0;
268 if (!mnt && !check)
269 return;
270 printf(_("This filesystem will be automatically "
271 "checked every %d mounts or\n"
272 "%g days, whichever comes first. "
273 "Use tune2fs -c or -i to override.\n"),
274 mnt, ((double) check) / (3600 * 24));
275 }
276
277 void dump_mmp_msg(struct mmp_struct *mmp, const char *msg)
278 {
279
280 if (msg)
281 printf("MMP check failed: %s\n", msg);
282 if (mmp) {
283 time_t t = mmp->mmp_time;
284
285 printf("MMP error info: last update: %s node: %s device: %s\n",
286 ctime(&t), mmp->mmp_nodename, mmp->mmp_bdevname);
287 }
288 }