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