OSDN Git Service

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