X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=util-linux%2Ffsck_minix.c;h=f958bdfc97ae9c0350da3c9296a1d38f35d3330e;hb=6ced427a6debb71967a8b9cebc12e31c08de0f6a;hp=b90ccc5e3beb1ffb1f4b9989d3a78aa95f74a69c;hpb=8a6254994ce56859f245120726f47bafa8281b2d;p=oweals%2Fbusybox.git diff --git a/util-linux/fsck_minix.c b/util-linux/fsck_minix.c index b90ccc5e3..f958bdfc9 100644 --- a/util-linux/fsck_minix.c +++ b/util-linux/fsck_minix.c @@ -98,9 +98,6 @@ #include #include "busybox.h" -#define BLOCK_SIZE_BITS 10 -#define BLOCK_SIZE (1<> 3] & (1<<(i & 7))) != 0; } @@ -284,18 +274,55 @@ static void die(const char *str) leave(8); } -/* - * This simply goes through the file-name data and prints out the - * current file. - */ -static void print_current_name(void) +/* File-name data */ +enum { MAX_DEPTH = 32 }; +static int name_depth; +static char *current_name; +static char *name_component[MAX_DEPTH+1]; + +/* Wed Feb 9 15:17:06 MST 2000 */ +/* dynamically allocate name_list (instead of making it static) */ +static inline void alloc_current_name(void) { - int i = 0; + current_name = xmalloc(MAX_DEPTH * (BUFSIZ + 1)); + current_name[0] = '/'; + current_name[1] = '\0'; + name_component[0] = ¤t_name[0]; +} - while (i < name_depth) - printf("/%.*s", namelen, name_list[i++]); - if (i == 0) - printf("/"); +#ifdef CONFIG_FEATURE_CLEAN_UP +/* execute this atexit() to deallocate name_list[] */ +/* piptigger was here */ +static inline void free_current_name(void) +{ + free(current_name); +} +#endif + +static void push_filename(const char *name) +{ + // /dir/dir/dir/file + // ^ ^ ^ + // [0] [1] [2] <-name_component[i] + if (name_depth < MAX_DEPTH) { + int len; + char *p = name_component[name_depth]; + *p++ = '/'; + len = sprintf(p, "%.*s", namelen, name); + name_component[name_depth + 1] = p + len; + } + name_depth++; +} + +static void pop_filename(void) { + name_depth--; + if (name_depth < MAX_DEPTH) { + *name_component[name_depth] = '\0'; + if (!name_depth) { + current_name[0] = '/'; + current_name[1] = '\0'; + } + } } static int ask(const char *string, int def) @@ -372,7 +399,7 @@ static void check_mount(void) else close(fd); - printf("%s is mounted. ", device_name); + printf("%s is mounted. ", device_name); cont = 0; if (isatty(0) && isatty(1)) cont = ask("Do you really want to continue", 0); @@ -391,16 +418,16 @@ static void check_mount(void) */ static int check_zone_nr2(uint32_t *nr, int *corrected) { + const char *msg; if (!*nr) return 0; if (*nr < FIRSTZONE) - printf("Zone nr < FIRSTZONE in file `"); + msg = "< FIRSTZONE"; else if (*nr >= ZONES) - printf("Zone nr >= ZONES in file `"); + msg = ">= ZONES"; else return *nr; - print_current_name(); - printf("'."); + printf("Zone nr %s in file '%s'. ", msg, current_name); if (ask("Remove block", 1)) { *nr = 0; *corrected = 1; @@ -426,15 +453,13 @@ static void read_block(unsigned int nr, char *addr) return; } if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET)) { - printf("Read error: unable to seek to block in file '"); - print_current_name(); - printf("'\n"); + printf("%s: unable to seek to block in file '%s'\n", + bb_msg_read_error, current_name); errors_uncorrected = 1; memset(addr, 0, BLOCK_SIZE); } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) { - printf("Read error: bad block in file '"); - print_current_name(); - printf("'\n"); + printf("%s: bad block in file '%s'\n", + bb_msg_read_error, current_name); errors_uncorrected = 1; memset(addr, 0, BLOCK_SIZE); } @@ -454,11 +479,10 @@ static void write_block(unsigned int nr, char *addr) return; } if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET)) - die("seek failed in write_block"); + die("Seek failed in write_block"); if (BLOCK_SIZE != write(IN, addr, BLOCK_SIZE)) { - printf("Write error: bad block in file '"); - print_current_name(); - printf("'\n"); + printf("%s: bad block in file '%s'\n", + bb_msg_write_error, current_name); errors_uncorrected = 1; } } @@ -575,9 +599,9 @@ static void write_super_block(void) Super.s_state &= ~MINIX_ERROR_FS; if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET)) - die("seek failed in write_super_block"); + die("Seek failed in write_super_block"); if (BLOCK_SIZE != write(IN, super_block_buffer, BLOCK_SIZE)) - die("unable to write super-block"); + die("Unable to write super-block"); } static void write_tables(void) @@ -618,12 +642,14 @@ static void get_dirsize(void) static void read_superblock(void) { if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET)) - die("seek failed"); + die("Seek failed"); if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE)) - die("unable to read super block"); + die("Unable to read super block"); + /* already initialized to: namelen = 14; dirsize = 16; version2 = 0; + */ if (MAGIC == MINIX_SUPER_MAGIC) { } else if (MAGIC == MINIX_SUPER_MAGIC2) { namelen = 30; @@ -637,21 +663,19 @@ static void read_superblock(void) version2 = 1; #endif } else - die("bad magic number in super-block"); + die("Bad magic number in super-block"); if (ZONESIZE != 0 || BLOCK_SIZE != 1024) die("Only 1k blocks/zones supported"); if (IMAPS * BLOCK_SIZE * 8 < INODES + 1) - die("bad s_imap_blocks field in super-block"); + die("Bad s_imap_blocks field in super-block"); if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1) - die("bad s_zmap_blocks field in super-block"); + die("Bad s_zmap_blocks field in super-block"); } static void read_tables(void) { - inode_map = xmalloc(IMAPS * BLOCK_SIZE); - zone_map = xmalloc(ZMAPS * BLOCK_SIZE); - memset(inode_map, 0, IMAPS * BLOCK_SIZE); - memset(zone_map, 0, ZMAPS * BLOCK_SIZE); + inode_map = xzalloc(IMAPS * BLOCK_SIZE); + zone_map = xzalloc(ZMAPS * BLOCK_SIZE); inode_buffer = xmalloc(INODE_BUFFER_SIZE); inode_count = xmalloc(INODES + 1); zone_count = xmalloc(ZONES); @@ -662,7 +686,7 @@ static void read_tables(void) if (INODE_BUFFER_SIZE != read(IN, inode_buffer, INODE_BUFFER_SIZE)) die("Unable to read inodes"); if (NORM_FIRSTZONE != FIRSTZONE) { - printf("Warning: Firstzone != Norm_firstzone\n"); + printf("Warning: Firstzone!=Norm_firstzone\n"); errors_uncorrected = 1; } get_dirsize(); @@ -694,11 +718,10 @@ static struct minix_inode *get_inode(unsigned int nr) inode = Inode + nr; if (!inode_count[nr]) { if (!inode_in_use(nr)) { - printf("Inode %d marked not used, but used for file '", nr); - print_current_name(); - printf("'\n"); + printf("Inode %d is marked as 'unused', but it is used " + "for file '%s'\n", nr, current_name); if (repair) { - if (ask("Mark in use", 1)) + if (ask("Mark as 'in use'", 1)) mark_inode(nr); } else { errors_uncorrected = 1; @@ -717,8 +740,7 @@ static struct minix_inode *get_inode(unsigned int nr) else if (S_ISSOCK(inode->i_mode)); else if (S_ISFIFO(inode->i_mode)); else { - print_current_name(); - printf(" has mode %05o\n", inode->i_mode); + printf("%s has mode %05o\n", current_name, inode->i_mode); } } else @@ -742,11 +764,10 @@ static struct minix2_inode *get_inode2(unsigned int nr) inode = Inode2 + nr; if (!inode_count[nr]) { if (!inode_in_use(nr)) { - printf("Inode %d marked not used, but used for file '", nr); - print_current_name(); - printf("'\n"); + printf("Inode %d is marked as 'unused', but it is used " + "for file '%s'\n", nr, current_name); if (repair) { - if (ask("Mark in use", 1)) + if (ask("Mark as 'in use'", 1)) mark_inode(nr); else errors_uncorrected = 1; @@ -765,8 +786,7 @@ static struct minix2_inode *get_inode2(unsigned int nr) else if (S_ISSOCK(inode->i_mode)); else if (S_ISFIFO(inode->i_mode)); else { - print_current_name(); - printf(" has mode %05o\n", inode->i_mode); + printf("%s has mode %05o\n", current_name, inode->i_mode); } } else links++; @@ -784,7 +804,7 @@ static void check_root(void) struct minix_inode *inode = Inode + ROOT_INO; if (!inode || !S_ISDIR(inode->i_mode)) - die("root inode isn't a directory"); + die("Root inode isn't a directory"); } #ifdef CONFIG_FEATURE_MINIX2 @@ -793,7 +813,7 @@ static void check_root2(void) struct minix2_inode *inode = Inode2 + ROOT_INO; if (!inode || !S_ISDIR(inode->i_mode)) - die("root inode isn't a directory"); + die("Root inode isn't a directory"); } #endif @@ -807,9 +827,8 @@ static int add_zone(uint16_t *znr, int *corrected) if (!block) return 0; if (zone_count[block]) { - printf("Block has been used before. Now in file `"); - print_current_name(); - printf("'."); + printf("Already used block is reused in file '%s'. ", + current_name); if (ask("Clear", 1)) { *znr = 0; block = 0; @@ -818,9 +837,8 @@ static int add_zone(uint16_t *znr, int *corrected) } } if (!zone_in_use(block)) { - printf("Block %d in file `", block); - print_current_name(); - printf("' is marked not in use."); + printf("Block %d in file '%s' is marked as 'unused'. ", + block, current_name); if (ask("Correct", 1)) mark_zone(block); } @@ -840,9 +858,8 @@ static int add_zone2(uint32_t *znr, int *corrected) if (!block) return 0; if (zone_count[block]) { - printf("Block has been used before. Now in file `"); - print_current_name(); - printf("'."); + printf("Already used block is reused in file '%s'. ", + current_name); if (ask("Clear", 1)) { *znr = 0; block = 0; @@ -851,9 +868,8 @@ static int add_zone2(uint32_t *znr, int *corrected) } } if (!zone_in_use(block)) { - printf("Block %d in file `", block); - print_current_name(); - printf("' is marked not in use."); + printf("Block %d in file '%s' is marked as 'unused'. ", + block, current_name); if (ask("Correct", 1)) mark_zone(block); } @@ -998,54 +1014,43 @@ static void check_file(struct minix_inode *dir, unsigned int offset) name = blk + (offset % BLOCK_SIZE) + 2; ino = *(uint16_t *) (name - 2); if (ino > INODES) { - print_current_name(); - printf(" contains a bad inode number for file '"); - printf("%.*s'.", namelen, name); - if (ask(" Remove", 1)) { + printf("%s contains a bad inode number for file '%.*s'. ", + current_name, namelen, name); + if (ask("Remove", 1)) { *(uint16_t *) (name - 2) = 0; write_block(block, blk); } ino = 0; } - if (name_depth < MAX_DEPTH) - strncpy(name_list[name_depth], name, namelen); - name_depth++; + push_filename(name); inode = get_inode(ino); - name_depth--; + pop_filename(); if (!offset) { if (!inode || strcmp(".", name)) { - print_current_name(); - printf(": bad directory: '.' isn't first\n"); + printf("%s: bad directory: '.' isn't first\n", current_name); errors_uncorrected = 1; } else return; } if (offset == dirsize) { if (!inode || strcmp("..", name)) { - print_current_name(); - printf(": bad directory: '..' isn't second\n"); + printf("%s: bad directory: '..' isn't second\n", current_name); errors_uncorrected = 1; } else return; } if (!inode) return; - if (name_depth < MAX_DEPTH) - strncpy(name_list[name_depth], name, namelen); - name_depth++; + push_filename(name); if (list) { if (verbose) printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks); - print_current_name(); - if (S_ISDIR(inode->i_mode)) - printf(":\n"); - else - printf("\n"); + printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : ""); } check_zones(ino); if (inode && S_ISDIR(inode->i_mode)) recursive_check(ino); - name_depth--; + pop_filename(); return; } @@ -1063,52 +1068,43 @@ static void check_file2(struct minix2_inode *dir, unsigned int offset) name = blk + (offset % BLOCK_SIZE) + 2; ino = *(uint16_t *) (name - 2); if (ino > INODES) { - print_current_name(); - printf(" contains a bad inode number for file '"); - printf("%.*s'.", namelen, name); - if (ask(" Remove", 1)) { + printf("%s contains a bad inode number for file '%.*s'. ", + current_name, namelen, name); + if (ask("Remove", 1)) { *(uint16_t *) (name - 2) = 0; write_block(block, blk); } ino = 0; } - if (name_depth < MAX_DEPTH) - strncpy(name_list[name_depth], name, namelen); - name_depth++; + push_filename(name); inode = get_inode2(ino); - name_depth--; + pop_filename(); if (!offset) { if (!inode || strcmp(".", name)) { - print_current_name(); - printf(": bad directory: '.' isn't first\n"); + printf("%s: bad directory: '.' isn't first\n", current_name); errors_uncorrected = 1; } else return; } if (offset == dirsize) { if (!inode || strcmp("..", name)) { - print_current_name(); - printf(": bad directory: '..' isn't second\n"); + printf("%s: bad directory: '..' isn't second\n", current_name); errors_uncorrected = 1; } else return; } if (!inode) return; - name_depth++; + push_filename(name); if (list) { if (verbose) printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks); - print_current_name(); - if (S_ISDIR(inode->i_mode)) - printf(":\n"); - else - printf("\n"); + printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : ""); } check_zones2(ino); if (inode && S_ISDIR(inode->i_mode)) recursive_check2(ino); - name_depth--; + pop_filename(); return; } #endif @@ -1120,10 +1116,9 @@ static void recursive_check(unsigned int ino) dir = Inode + ino; if (!S_ISDIR(dir->i_mode)) - die("internal error"); + die("Internal error"); if (dir->i_size < 2 * dirsize) { - print_current_name(); - printf(": bad directory: size<32"); + printf("%s: bad directory: size<32", current_name); errors_uncorrected = 1; } for (offset = 0; offset < dir->i_size; offset += dirsize) @@ -1138,10 +1133,9 @@ static void recursive_check2(unsigned int ino) dir = Inode2 + ino; if (!S_ISDIR(dir->i_mode)) - die("internal error"); + die("Internal error"); if (dir->i_size < 2 * dirsize) { - print_current_name(); - printf(": bad directory: size < 32"); + printf("%s: bad directory: size<32", current_name); errors_uncorrected = 1; } for (offset = 0; offset < dir->i_size; offset += dirsize) @@ -1154,7 +1148,7 @@ static int bad_zone(int i) char buffer[1024]; if (BLOCK_SIZE * i != lseek(IN, BLOCK_SIZE * i, SEEK_SET)) - die("seek failed in bad_zone"); + die("Seek failed in bad_zone"); return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE)); } @@ -1164,7 +1158,7 @@ static void check_counts(void) for (i = 1; i <= INODES; i++) { if (warn_mode && Inode[i].i_mode && !inode_in_use(i)) { - printf("Inode %d mode not cleared.", i); + printf("Inode %d has non-zero mode. ", i); if (ask("Clear", 1)) { Inode[i].i_mode = 0; changed = 1; @@ -1173,18 +1167,18 @@ static void check_counts(void) if (!inode_count[i]) { if (!inode_in_use(i)) continue; - printf("Inode %d not used, marked used in the bitmap.", i); + printf("Unused inode %d is marked as 'used' in the bitmap. ", i); if (ask("Clear", 1)) unmark_inode(i); continue; } if (!inode_in_use(i)) { - printf("Inode %d used, marked unused in the bitmap.", i); + printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i); if (ask("Set", 1)) mark_inode(i); } if (Inode[i].i_nlinks != inode_count[i]) { - printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.", + printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ", i, Inode[i].i_mode, Inode[i].i_nlinks, inode_count[i]); if (ask("Set i_nlinks to count", 1)) { Inode[i].i_nlinks = inode_count[i]; @@ -1198,7 +1192,7 @@ static void check_counts(void) if (!zone_count[i]) { if (bad_zone(i)) continue; - printf("Zone %d: marked in use, no file uses it.", i); + printf("Zone %d is marked 'in use', but no file uses it. ", i); if (ask("Unmark", 1)) unmark_zone(i); continue; @@ -1215,7 +1209,7 @@ static void check_counts2(void) for (i = 1; i <= INODES; i++) { if (warn_mode && Inode2[i].i_mode && !inode_in_use(i)) { - printf("Inode %d mode not cleared.", i); + printf("Inode %d has non-zero mode. ", i); if (ask("Clear", 1)) { Inode2[i].i_mode = 0; changed = 1; @@ -1224,18 +1218,18 @@ static void check_counts2(void) if (!inode_count[i]) { if (!inode_in_use(i)) continue; - printf("Inode %d not used, marked used in the bitmap.", i); + printf("Unused inode %d is marked as 'used' in the bitmap. ", i); if (ask("Clear", 1)) unmark_inode(i); continue; } if (!inode_in_use(i)) { - printf("Inode %d used, marked unused in the bitmap.", i); + printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i); if (ask("Set", 1)) mark_inode(i); } if (Inode2[i].i_nlinks != inode_count[i]) { - printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.", + printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ", i, Inode2[i].i_mode, Inode2[i].i_nlinks, inode_count[i]); if (ask("Set i_nlinks to count", 1)) { @@ -1250,7 +1244,7 @@ static void check_counts2(void) if (!zone_count[i]) { if (bad_zone(i)) continue; - printf("Zone %d: marked in use, no file uses it.", i); + printf("Zone %d is marked 'in use', but no file uses it. ", i); if (ask("Unmark", 1)) unmark_zone(i); continue; @@ -1281,50 +1275,23 @@ static void check2(void) } #endif -/* Wed Feb 9 15:17:06 MST 2000 */ -/* dynamically allocate name_list (instead of making it static) */ -static void alloc_name_list(void) -{ - int i; - - name_list = xmalloc(sizeof(char *) * MAX_DEPTH); - for (i = 0; i < MAX_DEPTH; i++) - name_list[i] = xmalloc(sizeof(char) * (BUFSIZ + 1)); -} - -#ifdef CONFIG_FEATURE_CLEAN_UP -/* execute this atexit() to deallocate name_list[] */ -/* piptigger was here */ -static void free_name_list(void) -{ - int i; - - if (name_list) { - for (i = 0; i < MAX_DEPTH; i++) { - free(name_list[i]); - } - free(name_list); - } -} -#endif - int fsck_minix_main(int argc, char **argv) { struct termios tmp; int retcode = 0; - alloc_name_list(); + alloc_current_name(); #ifdef CONFIG_FEATURE_CLEAN_UP /* Don't bother to free memory. Exit does * that automagically, so we can save a few bytes */ - atexit(free_name_list); + atexit(free_current_name); #endif if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE) - die("bad inode size"); + die("Bad inode size"); #ifdef CONFIG_FEATURE_MINIX2 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE) - die("bad v2 inode size"); + die("Bad v2 inode size"); #endif while (argc-- > 1) { argv++; @@ -1368,11 +1335,11 @@ int fsck_minix_main(int argc, char **argv) check_mount(); /* trying to check a mounted filesystem? */ if (repair && !automatic) { if (!isatty(0) || !isatty(1)) - die("need terminal for interactive repairs"); + die("Need terminal for interactive repairs"); } IN = open(device_name, repair ? O_RDWR : O_RDONLY); if (IN < 0){ - fprintf(stderr,"unable to open device '%s'\n",device_name); + printf("Unable to open device '%s'\n", device_name); leave(8); } sync(); /* paranoia? */ @@ -1388,7 +1355,7 @@ int fsck_minix_main(int argc, char **argv) if (!(Super.s_state & MINIX_ERROR_FS) && (Super.s_state & MINIX_VALID_FS) && !force) { if (repair) - printf("%s is clean, no check\n", device_name); + printf("%s is clean, check is skipped\n", device_name); return retcode; } else if (force) printf("Forcing filesystem check on %s\n", device_name); @@ -1426,9 +1393,8 @@ int fsck_minix_main(int argc, char **argv) for (i = FIRSTZONE, free_cnt = 0; i < ZONES; i++) if (!zone_in_use(i)) free_cnt++; - printf("%6ld zones used (%ld%%)\n", (ZONES - free_cnt), - 100 * (ZONES - free_cnt) / ZONES); - printf("\n%6d regular files\n" + printf("%6ld zones used (%ld%%)\n\n" + "%6d regular files\n" "%6d directories\n" "%6d character device files\n" "%6d block device files\n" @@ -1436,15 +1402,14 @@ int fsck_minix_main(int argc, char **argv) "%6d symbolic links\n" "------\n" "%6d files\n", + (ZONES - free_cnt), 100 * (ZONES - free_cnt) / ZONES, regular, directory, chardev, blockdev, links - 2 * directory + 1, symlinks, total - 2 * directory + 1); } if (changed) { write_tables(); - printf("----------------------------\n" - "FILE SYSTEM HAS BEEN CHANGED\n" - "----------------------------\n"); + printf("FILE SYSTEM HAS BEEN CHANGED\n"); sync(); } else if (repair) write_super_block();