more busybox's style for fsck.c, size reduce

This commit is contained in:
"Vladimir N. Oleynik" 2005-09-29 09:45:22 +00:00
parent 74078685e6
commit 6160d45e05

View File

@ -51,7 +51,7 @@
#define _PATH_MNTTAB "/etc/fstab" #define _PATH_MNTTAB "/etc/fstab"
#endif #endif
static const char *ignored_types[] = { static const char * const ignored_types[] = {
"ignore", "ignore",
"iso9660", "iso9660",
"nfs", "nfs",
@ -63,7 +63,7 @@ static const char *ignored_types[] = {
NULL NULL
}; };
static const char *really_wanted[] = { static const char * const really_wanted[] = {
"minix", "minix",
"ext2", "ext2",
"ext3", "ext3",
@ -83,28 +83,27 @@ static char *devices[MAX_DEVICES];
static char *args[MAX_ARGS]; static char *args[MAX_ARGS];
static int num_devices, num_args; static int num_devices, num_args;
static int verbose = 0; static int verbose;
static int doall = 0; static int doall;
static int noexecute = 0; static int noexecute;
static int serialize = 0; static int serialize;
static int skip_root = 0; static int skip_root;
static int like_mount = 0; static int like_mount;
static int notitle = 0; static int notitle;
static int parallel_root = 0; static int parallel_root;
static int progress = 0; static int progress;
static int progress_fd = 0; static int progress_fd;
static int force_all_parallel = 0; static int force_all_parallel;
static int num_running = 0; static int num_running;
static int max_running = 0; static int max_running;
static volatile int cancel_requested = 0; static volatile int cancel_requested;
static int kill_sent = 0; static int kill_sent;
static char *progname; static char *fstype;
static char *fstype = NULL; static struct fs_info *filesys_info, *filesys_last;
static struct fs_info *filesys_info = NULL, *filesys_last = NULL;
static struct fsck_instance *instance_list; static struct fsck_instance *instance_list;
static const char *fsck_prefix_path = "/sbin:/sbin/fs.d:/sbin/fs:/etc/fs:/etc"; static const char *fsck_prefix_path = "/sbin:/sbin/fs.d:/sbin/fs:/etc/fs:/etc";
static char *fsck_path = 0; static char *fsck_path;
static blkid_cache cache = NULL; static blkid_cache cache;
static char *string_copy(const char *s) static char *string_copy(const char *s)
{ {
@ -112,9 +111,7 @@ static char *string_copy(const char *s)
if (!s) if (!s)
return 0; return 0;
ret = malloc(strlen(s)+1); ret = strdup(s);
if (ret)
strcpy(ret, s);
return ret; return ret;
} }
@ -130,8 +127,6 @@ static int string_to_int(const char *s)
return (int) l; return (int) l;
} }
static int ignore(struct fs_info *);
static char *skip_over_blank(char *cp) static char *skip_over_blank(char *cp)
{ {
while (*cp && isspace(*cp)) while (*cp && isspace(*cp))
@ -177,38 +172,19 @@ static char *parse_word(char **buf)
static void parse_escape(char *word) static void parse_escape(char *word)
{ {
char *p, *q; char *q, c;
int ac, i; const char *p;
if (!word) if (!word)
return; return;
for (p = word, q = word; *p; p++, q++) { for (p = q = word; *p; q++) {
*q = *p; c = *p++;
if (*p != '\\') if (c != '\\') {
continue; *q = c;
if (*++p == 0) } else {
break; *q = bb_process_escape_sequence(&p);
if (*p == 't') {
*q = '\t';
continue;
} }
if (*p == 'n') {
*q = '\n';
continue;
}
if (!isdigit(*p)) {
*q = *p;
continue;
}
ac = 0;
for (i = 0; i < 3; i++, p++) {
if (!isdigit(*p))
break;
ac = (ac * 8) + (*p - '0');
}
*q = ac;
p--;
} }
*q = 0; *q = 0;
} }
@ -329,8 +305,7 @@ static void load_fs_info(const char *filename)
struct fs_info *fs; struct fs_info *fs;
if ((f = fopen(filename, "r")) == NULL) { if ((f = fopen(filename, "r")) == NULL) {
fprintf(stderr, _("WARNING: couldn't open %s: %s\n"), bb_perror_msg("WARNING: couldn't open %s: %m", filename);
filename, strerror(errno));
return; return;
} }
while (!feof(f)) { while (!feof(f)) {
@ -339,8 +314,8 @@ static void load_fs_info(const char *filename)
break; break;
buf[sizeof(buf)-1] = 0; buf[sizeof(buf)-1] = 0;
if (parse_fstab_line(buf, &fs) < 0) { if (parse_fstab_line(buf, &fs) < 0) {
fprintf(stderr, _("WARNING: bad format " bb_error_msg("WARNING: bad format "
"on line %d of %s\n"), lineno, filename); "on line %d of %s\n", lineno, filename);
continue; continue;
} }
if (!fs) if (!fs)
@ -354,10 +329,10 @@ static void load_fs_info(const char *filename)
fclose(f); fclose(f);
if (old_fstab) { if (old_fstab) {
fputs(_("\007\007\007" fputs("\007\007\007"
"WARNING: Your /etc/fstab does not contain the fsck passno\n" "WARNING: Your /etc/fstab does not contain the fsck passno\n"
" field. I will kludge around things for you, but you\n" " field. I will kludge around things for you, but you\n"
" should fix your /etc/fstab file as soon as you can.\n\n"), stderr); " should fix your /etc/fstab file as soon as you can.\n\n", stderr);
for (fs = filesys_info; fs; fs = fs->next) { for (fs = filesys_info; fs; fs = fs->next) {
fs->passno = 1; fs->passno = 1;
@ -388,7 +363,7 @@ static char *find_fsck(char *type)
{ {
char *s; char *s;
const char *tpl; const char *tpl;
static char prog[256]; char *prog;
char *p = string_copy(fsck_path); char *p = string_copy(fsck_path);
struct stat st; struct stat st;
@ -396,8 +371,9 @@ static char *find_fsck(char *type)
tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s"); tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) { for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
sprintf(prog, tpl, s, type); bb_xasprintf(&prog, tpl, s, type);
if (stat(prog, &st) == 0) break; if (stat(prog, &st) == 0) break;
free(prog);
} }
free(p); free(p);
return(s ? prog : NULL); return(s ? prog : NULL);
@ -423,7 +399,8 @@ static int progress_active(void)
static int execute(const char *type, const char *device, const char *mntpt, static int execute(const char *type, const char *device, const char *mntpt,
int interactive) int interactive)
{ {
char *s, *argv[80], prog[80]; char *s, *argv[80];
char *prog;
int argc, i; int argc, i;
struct fsck_instance *inst, *p; struct fsck_instance *inst, *p;
pid_t pid; pid_t pid;
@ -433,8 +410,8 @@ static int execute(const char *type, const char *device, const char *mntpt,
return ENOMEM; return ENOMEM;
memset(inst, 0, sizeof(struct fsck_instance)); memset(inst, 0, sizeof(struct fsck_instance));
sprintf(prog, "fsck.%s", type); bb_xasprintf(&prog, "fsck.%s", type);
argv[0] = string_copy(prog); argv[0] = prog;
argc = 1; argc = 1;
for (i=0; i <num_args; i++) for (i=0; i <num_args; i++)
@ -455,7 +432,7 @@ static int execute(const char *type, const char *device, const char *mntpt,
s = find_fsck(prog); s = find_fsck(prog);
if (s == NULL) { if (s == NULL) {
fprintf(stderr, _("fsck: %s: not found\n"), prog); bb_error_msg("%s: not found", prog);
return ENOENT; return ENOENT;
} }
@ -477,15 +454,15 @@ static int execute(const char *type, const char *device, const char *mntpt,
if (!interactive) if (!interactive)
close(0); close(0);
(void) execv(s, argv); (void) execv(s, argv);
perror(argv[0]); bb_perror_msg_and_die("%s", argv[0]);
exit(EXIT_ERROR);
} }
for (i=0; i < argc; i++) for (i = 1; i < argc; i++)
free(argv[i]); free(argv[i]);
free(s);
inst->pid = pid; inst->pid = pid;
inst->prog = string_copy(prog); inst->prog = prog;
inst->type = string_copy(type); inst->type = string_copy(type);
inst->device = string_copy(device); inst->device = string_copy(device);
inst->base_device = base_device(device); inst->base_device = base_device(device);
@ -567,9 +544,7 @@ static struct fsck_instance *wait_one(int flags)
if ((errno == EINTR) || (errno == EAGAIN)) if ((errno == EINTR) || (errno == EAGAIN))
continue; continue;
if (errno == ECHILD) { if (errno == ECHILD) {
fprintf(stderr, bb_error_msg("wait: No more child process?!?");
_("%s: wait: No more child process?!?\n"),
progname);
return NULL; return NULL;
} }
perror("wait"); perror("wait");
@ -590,13 +565,13 @@ static struct fsck_instance *wait_one(int flags)
if (sig == SIGINT) { if (sig == SIGINT) {
status = EXIT_UNCORRECTED; status = EXIT_UNCORRECTED;
} else { } else {
printf(_("Warning... %s for device %s exited " printf("Warning... %s for device %s exited "
"with signal %d.\n"), "with signal %d.\n",
inst->prog, inst->device, sig); inst->prog, inst->device, sig);
status = EXIT_ERROR; status = EXIT_ERROR;
} }
} else { } else {
printf(_("%s %s: status is %x, should never happen.\n"), printf("%s %s: status is %x, should never happen.\n",
inst->prog, inst->device, status); inst->prog, inst->device, status);
status = EXIT_ERROR; status = EXIT_ERROR;
} }
@ -632,7 +607,7 @@ ret_inst:
else else
instance_list = inst->next; instance_list = inst->next;
if (verbose > 1) if (verbose > 1)
printf(_("Finished with %s (exit status %d)\n"), printf("Finished with %s (exit status %d)\n",
inst->device, inst->exit_status); inst->device, inst->exit_status);
num_running--; num_running--;
return inst; return inst;
@ -692,8 +667,8 @@ static void fsck_device(struct fs_info *fs, int interactive)
num_running++; num_running++;
retval = execute(type, fs->device, fs->mountpt, interactive); retval = execute(type, fs->device, fs->mountpt, interactive);
if (retval) { if (retval) {
fprintf(stderr, _("%s: Error %d while executing fsck.%s " bb_error_msg("Error %d while executing fsck.%s for %s",
"for %s\n"), progname, retval, type, fs->device); retval, type, fs->device);
num_running--; num_running--;
} }
} }
@ -729,15 +704,8 @@ static void compile_fs_type(char *fs_type, struct fs_type_compile *cmp)
} }
} }
cmp->list = malloc(num * sizeof(char *)); cmp->list = xcalloc(num, sizeof(char *));
cmp->type = malloc(num * sizeof(int)); cmp->type = xcalloc(num, sizeof(int));
if (!cmp->list || !cmp->type) {
fputs(_("Couldn't allocate memory for filesystem types\n"),
stderr);
exit(EXIT_ERROR);
}
memset(cmp->list, 0, num * sizeof(char *));
memset(cmp->type, 0, num * sizeof(int));
cmp->negate = 0; cmp->negate = 0;
if (!fs_type) if (!fs_type)
@ -769,8 +737,7 @@ static void compile_fs_type(char *fs_type, struct fs_type_compile *cmp)
} }
if ((negate && !cmp->negate) || if ((negate && !cmp->negate) ||
(!negate && cmp->negate)) { (!negate && cmp->negate)) {
fputs(_(fs_type_syntax_error), stderr); bb_error_msg_and_die("%s", fs_type_syntax_error);
exit(EXIT_USAGE);
} }
} }
#if 0 #if 0
@ -841,8 +808,9 @@ static int fs_match(struct fs_info *fs, struct fs_type_compile *cmp)
/* Check if we should ignore this filesystem. */ /* Check if we should ignore this filesystem. */
static int ignore(struct fs_info *fs) static int ignore(struct fs_info *fs)
{ {
const char **ip; const char * const *ip;
int wanted = 0; int wanted = 0;
char *s;
/* /*
* If the pass number is 0, ignore it. * If the pass number is 0, ignore it.
@ -870,12 +838,14 @@ static int ignore(struct fs_info *fs)
} }
/* See if the <fsck.fs> program is available. */ /* See if the <fsck.fs> program is available. */
if (find_fsck(fs->type) == NULL) { s = find_fsck(fs->type);
if (s == NULL) {
if (wanted) if (wanted)
fprintf(stderr, _("fsck: cannot check %s: fsck.%s not found\n"), bb_error_msg("cannot check %s: fsck.%s not found",
fs->device, fs->type); fs->device, fs->type);
return 1; return 1;
} }
free(s);
/* We can and want to check this file system type. */ /* We can and want to check this file system type. */
return 0; return 0;
@ -928,7 +898,7 @@ static int check_all(void)
int pass_done; int pass_done;
if (verbose) if (verbose)
fputs(_("Checking all file systems.\n"), stdout); fputs("Checking all file systems.\n", stdout);
/* /*
* Do an initial scan over the filesystem; mark filesystems * Do an initial scan over the filesystem; mark filesystems
@ -1014,7 +984,7 @@ static int check_all(void)
if (cancel_requested) if (cancel_requested)
break; break;
if (verbose > 1) if (verbose > 1)
printf(_("--waiting-- (pass %d)\n"), passno); printf("--waiting-- (pass %d)\n", passno);
status |= wait_many(pass_done ? FLAG_WAIT_ALL : status |= wait_many(pass_done ? FLAG_WAIT_ALL :
FLAG_WAIT_ATLEAST_ONE); FLAG_WAIT_ATLEAST_ONE);
if (pass_done) { if (pass_done) {
@ -1035,17 +1005,15 @@ static int check_all(void)
#if 0 #if 0
static void usage(void) static void usage(void)
{ {
fputs(_("Usage: fsck [-ANPRTV] [ -C [ fd ] ] [-t fstype] [fs-options] [filesys ...]\n"), stderr); fputs("Usage: fsck [-ANPRTV] [ -C [ fd ] ] [-t fstype] [fs-options] [filesys ...]\n", stderr);
exit(EXIT_USAGE); exit(EXIT_USAGE);
} }
#endif #endif
#ifdef HAVE_SIGNAL_H
static void signal_cancel(int sig FSCK_ATTR((unused))) static void signal_cancel(int sig FSCK_ATTR((unused)))
{ {
cancel_requested++; cancel_requested++;
} }
#endif
static void PRS(int argc, char *argv[]) static void PRS(int argc, char *argv[])
{ {
@ -1054,7 +1022,6 @@ static void PRS(int argc, char *argv[])
char options[128]; char options[128];
int opt = 0; int opt = 0;
int opts_for_fsck = 0; int opts_for_fsck = 0;
#ifdef HAVE_SIGNAL_H
struct sigaction sa; struct sigaction sa;
/* /*
@ -1064,23 +1031,18 @@ static void PRS(int argc, char *argv[])
sa.sa_handler = signal_cancel; sa.sa_handler = signal_cancel;
sigaction(SIGINT, &sa, 0); sigaction(SIGINT, &sa, 0);
sigaction(SIGTERM, &sa, 0); sigaction(SIGTERM, &sa, 0);
#endif
num_devices = 0; num_devices = 0;
num_args = 0; num_args = 0;
instance_list = 0; instance_list = 0;
progname = argv[0];
for (i=1; i < argc; i++) { for (i=1; i < argc; i++) {
arg = argv[i]; arg = argv[i];
if (!arg) if (!arg)
continue; continue;
if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) { if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
if (num_devices >= MAX_DEVICES) { if (num_devices >= MAX_DEVICES) {
fprintf(stderr, _("%s: too many devices\n"), bb_error_msg_and_die("too many devices");
progname);
exit(EXIT_ERROR);
} }
dev = blkid_get_devname(cache, arg, NULL); dev = blkid_get_devname(cache, arg, NULL);
if (!dev && strchr(arg, '=')) { if (!dev && strchr(arg, '=')) {
@ -1089,31 +1051,26 @@ static void PRS(int argc, char *argv[])
* /proc/partitions isn't found. * /proc/partitions isn't found.
*/ */
if (access("/proc/partitions", R_OK) < 0) { if (access("/proc/partitions", R_OK) < 0) {
fprintf(stderr, "Couldn't open /proc/partitions: %s\n", bb_error_msg_and_die("Couldn't open /proc/partitions: %m\n"
strerror(errno)); "Is /proc mounted?");
fprintf(stderr, "Is /proc mounted?\n");
exit(EXIT_ERROR);
} }
/* /*
* Check to see if this is because * Check to see if this is because
* we're not running as root * we're not running as root
*/ */
if (geteuid()) if (geteuid())
fprintf(stderr, bb_error_msg_and_die(
"Must be root to scan for matching filesystems: %s\n", arg); "Must be root to scan for matching filesystems: %s\n", arg);
else else
fprintf(stderr, bb_error_msg_and_die(
"Couldn't find matching filesystem: %s\n", arg); "Couldn't find matching filesystem: %s", arg);
exit(EXIT_ERROR);
} }
devices[num_devices++] = dev ? dev : string_copy(arg); devices[num_devices++] = dev ? dev : string_copy(arg);
continue; continue;
} }
if (arg[0] != '-' || opts_for_fsck) { if (arg[0] != '-' || opts_for_fsck) {
if (num_args >= MAX_ARGS) { if (num_args >= MAX_ARGS) {
fprintf(stderr, _("%s: too many arguments\n"), bb_error_msg_and_die("too many arguments");
progname);
exit(EXIT_ERROR);
} }
args[num_args++] = string_copy(arg); args[num_args++] = string_copy(arg);
continue; continue;
@ -1196,10 +1153,7 @@ static void PRS(int argc, char *argv[])
options[0] = '-'; options[0] = '-';
options[++opt] = '\0'; options[++opt] = '\0';
if (num_args >= MAX_ARGS) { if (num_args >= MAX_ARGS) {
fprintf(stderr, bb_error_msg("too many arguments");
_("%s: too many arguments\n"),
progname);
exit(EXIT_ERROR);
} }
args[num_args++] = string_copy(options); args[num_args++] = string_copy(options);
opt = 0; opt = 0;
@ -1222,12 +1176,6 @@ int fsck_main(int argc, char *argv[])
setvbuf(stdout, NULL, _IONBF, BUFSIZ); setvbuf(stdout, NULL, _IONBF, BUFSIZ);
setvbuf(stderr, NULL, _IONBF, BUFSIZ); setvbuf(stderr, NULL, _IONBF, BUFSIZ);
#ifdef ENABLE_NLS
setlocale(LC_MESSAGES, "");
setlocale(LC_CTYPE, "");
bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
textdomain(NLS_CAT_NAME);
#endif
blkid_get_cache(&cache, NULL); blkid_get_cache(&cache, NULL);
PRS(argc, argv); PRS(argc, argv);
@ -1241,15 +1189,7 @@ int fsck_main(int argc, char *argv[])
/* Update our search path to include uncommon directories. */ /* Update our search path to include uncommon directories. */
if (oldpath) { if (oldpath) {
fsck_path = malloc (strlen (fsck_prefix_path) + 1 + bb_xasprintf(&fsck_path, "%s:%s", fsck_prefix_path, oldpath);
strlen (oldpath) + 1);
if (!fsck_path) {
fprintf(stderr, "%s: Unable to allocate memory for fsck_path\n", progname);
exit(EXIT_ERROR);
}
strcpy (fsck_path, fsck_prefix_path);
strcat (fsck_path, ":");
strcat (fsck_path, oldpath);
} else { } else {
fsck_path = string_copy(fsck_prefix_path); fsck_path = string_copy(fsck_prefix_path);
} }
@ -1296,6 +1236,7 @@ int fsck_main(int argc, char *argv[])
} }
} }
status |= wait_many(FLAG_WAIT_ALL); status |= wait_many(FLAG_WAIT_ALL);
if (ENABLE_FEATURE_CLEAN_UP)
free(fsck_path); free(fsck_path);
blkid_put_cache(cache); blkid_put_cache(cache);
return status; return status;