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