/* This fixes up a filesystem which has bad data in the last block * of the file out beyond eof. This confuses some applications. * * Copyright Stephen Lord 2002, All Rights Reserved. */ #include #include #include #include #include #include #include int verbose = 0, readonly = 0; int filecount, fixedcount; int checker( const char *file, const struct stat *st, int flag) { int fd, i, n; char *ptr, *ptr2; off_t offset; if (flag != FTW_F) return 0; if (st->st_size % 4096==0) { return (0); } fd = open(file,O_RDWR); if(fd<0) { return(0); } filecount++; offset = 4096 - st->st_size % 4096; ptr = mmap(NULL,st->st_size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0); ptr2 = ptr + st->st_size; for (i = 0; i < offset; i++, ptr2++) { if (*ptr2 != 0) { if (verbose) printf("%s\n", file); if (!readonly) memset(ptr + st->st_size, 0, offset); fixedcount++; if ((fixedcount % 100) == 0) putchar('+'); break; } } munmap(ptr, st->st_size); close(fd); if ((filecount % 100) == 0) putchar('.'); return(0); } void catcher(int sig) { printf("\nInterrupted: %d files scanned %d files fixed\n", filecount, fixedcount); exit(0); } int main(int argc, char **argv) { setbuf(stdout, NULL); signal(SIGINT, catcher); ftw(argv[1], checker, 20); printf("%d files scanned %d files fixed\n", filecount, fixedcount); exit (0); }