📄 readfs.c
字号:
if (verbose) { do_indent(--indent); /* Let mkfs know we are done */ printf("$\n"); /* with this directory. */ }}int dump_file(special, ip, filename)int special;d1_inode *ip;char *filename;/* Extract given filename from the MINIX-filesystem, * and store it on the local filesystem. */{ int file; block_t b = 0; struct buf bp; off_t size; if (nofiles && (ip->d1_mode & I_TYPE) == I_REGULAR) return(0); if (verbose) show_info(filename, ip, pathname); if (noaction) return(0); if (access(filename, 0) == 0) { /* Should not happen, but just in case .. */ fprintf(stderr, "Will not create %s: file exists\n", filename); return(-1); } if ((file = creat(filename, (ip->d1_mode & ALL_MODES))) < 0) { fprintf(stderr, "cannot create %s\n", filename); return(-1); } /* Don't try to extract /dev/hd0 */ if ((ip->d1_mode & I_TYPE) == I_REGULAR) { size = ip->d1_size; while (size > (off_t) 0) { /* Get next block of file */ if (get_fileblock(special, ip, b++, &bp) < 0) { close(file); return(-1); } /* Write it to the file */ if (size > (off_t) BLOCK_SIZE) write(file, bp.b_data, BLOCK_SIZE); else write(file, bp.b_data, (int) size); size -= (off_t) BLOCK_SIZE; } } close(file); restore(filename, ip); /* Restore mode/owner/filetimes */ return(0);}int get_fileblock(special, ip, b, bp)int special;d1_inode *ip;block_t b;struct buf *bp;/* Read the `b'-th block from the file whose inode is `ip'. */{ zone_t zone, ind_zone; block_t z, zone_index; int r; /* Calculate zone in which the datablock number is contained */ zone = (zone_t) (b >> zone_shift); /* Calculate index of the block number in the zone */ zone_index = b - ((block_t) zone << zone_shift); /* Go get the zone */ if (zone < (zone_t) V1_NR_DZONES) { /* direct block */ zone = ip->d1_zone[(int) zone]; z = ((block_t) zone << zone_shift) + zone_index; r = get_block(special, z, bp, B_DATA); return(r); } /* The zone is not a direct one */ zone -= (zone_t) V1_NR_DZONES; /* Is it single indirect ? */ if (zone < (zone_t) V1_INDIRECTS) { /* single indirect block */ ind_zone = ip->d1_zone[V1_NR_DZONES]; } else { /* double indirect block */ /* Fetch the double indirect block */ ind_zone = ip->d1_zone[V1_NR_DZONES + 1]; z = (block_t) ind_zone << zone_shift; r = get_block(special, z, bp, B_INDIRECT); if (r < 0) return(r); /* Extract the indirect zone number from it */ zone -= (zone_t) V1_INDIRECTS; /* The next line assumes a V1 file system only! */ ind_zone = bp->b_v1_ind[(int) (zone / V1_INDIRECTS)]; zone %= (zone_t) V1_INDIRECTS; } /* Extract the datablock number from the indirect zone */ z = (block_t) ind_zone << zone_shift; r = get_block(special, z, bp, B_INDIRECT); if (r < 0) return(r); /* The next line assumes a V1 file system only! */ zone = bp->b_v1_ind[(int) zone]; /* Calculate datablock number to be fetched */ z = ((block_t) zone << zone_shift) + zone_index; r = get_block(special, z, bp, B_DATA); return(r);}/* The following routines simulate a LRU block cache. * * Definition of a cache block: */struct cache_block { block_t b_block; /* block number of block */ long b_access; /* counter value of last access */ char b_buf[BLOCK_SIZE]; /* buffer for block */};#define NR_CACHES 2 /* total number of caches */#define NR_CBLOCKS 5 /* number of blocks in a cache */static struct cache_block cache[NR_CACHES][NR_CBLOCKS];static long counter = 0L; /* Counter used as a sense of time. */ /* Incremented after each cache operation. */int get_block(fd, block, bp, type)int fd;block_t block;struct buf *bp;int type;/* Get the requested block from the device with filedescriptor fd. * If it is in the cache, no (floppy-) disk access is needed, * if not, allocate a cache block and read the block into it. */{ register int i; register struct cache_block *cache_p, *cp; if (block == (block_t) NO_ZONE) { /* Should never happen in a good filesystem. */ fprintf(stderr, "get_block: NO_ZONE requested !\n"); return(-1); } if (type < 0 || type >= NR_CACHES) /* No cache for this type */ return(get_rawblock(fd, block, (char *) bp)); cache_p = cache[type]; cp = (struct cache_block *) 0; /* First find out if block requested is in the cache */ for (i = 0; i < NR_CBLOCKS; i++) { if (cache_p[i].b_block == block) { /* found right block */ cp = &cache_p[i]; break; } } if (cp == (struct cache_block *) 0) { /* block is not in cache */ cp = cache_p; /* go find oldest buffer */ for (i = 0; i < NR_CBLOCKS; i++) { if (cache_p[i].b_access < cp->b_access) cp = &cache_p[i]; } /* Fill the buffer with the right block */ if (get_rawblock(fd, block, cp->b_buf) < 0) return(-1); } /* Update/store last access counter */ cp->b_access = ++counter; cp->b_block = block; memcpy((void *) bp, (void *) cp->b_buf, BLOCK_SIZE); return(0);}int get_rawblock(special, blockno, bufp)int special;block_t blockno;char *bufp;/* Read a block from the disk. */{ off_t pos; /* Calculate the position of the block on the disk */ pos = (off_t) blockno *(off_t) BLOCK_SIZE; /* Read the block from the disk */ if (lseek(special, pos, SEEK_SET) == pos && read(special, bufp, BLOCK_SIZE) == BLOCK_SIZE) return(0); /* Should never get here .. */ fprintf(stderr, "read block %d failed\n", blockno); return(-1);}void restore(name, ip)char *name;d1_inode *ip;/* Restores given file's attributes. * `ip' contains the attributes of the file on the MINIX filesystem, * `name' is the filename of the extracted file on the local filesystem. */{ long ttime[2]; chown(name, ip->d1_uid, ip->d1_gid); /* Fails if not superuser */ chmod(name, (ip->d1_mode & ALL_MODES)); ttime[0] = ttime[1] = ip->d1_mtime; utime(name, (struct utimbuf *) ttime);}/* Characters to use as prefix to `mkfs' mode field */static char special_chars[] = { '-', /* I_REGULAR */ 'c', /* I_CHAR_SPECIAL */ 'd', /* I_DIRECTORY */ 'b' /* I_BLOCK_SPECIAL */};void show_info(name, ip, path)char *name;d1_inode *ip;char *path;/* Show information about the given file/dir in `mkfs'-format */{ char c1, c2, c3; c1 = special_chars[(ip->d1_mode >> 13) & 03]; c2 = ((ip->d1_mode & ALL_MODES & ~RWX_MODES) == I_SET_UID_BIT) ? 'u' : '-'; c3 = ((ip->d1_mode & ALL_MODES & ~RWX_MODES) == I_SET_GID_BIT) ? 'g' : '-'; if (*name) { do_indent(indent); printf("%-14s ", name); } printf("%c%c%c%03o %d %d", c1, c2, c3, (ip->d1_mode & RWX_MODES), ip->d1_uid, ip->d1_gid); switch (ip->d1_mode & I_TYPE) { case I_DIRECTORY: break; case I_CHAR_SPECIAL: /* Print major and minor dev numbers */ printf(" %d %d", (ip->d1_zone[0] >> MAJOR) & 0377, (ip->d1_zone[0] >> MINOR) & 0377); break; case I_BLOCK_SPECIAL: /* Print major and minor dev numbers */ printf(" %d %d", (ip->d1_zone[0] >> MAJOR) & 0377, (ip->d1_zone[0] >> MINOR) & 0377); /* Also print the number of blocks on the device */ printf(" %ld", (ip->d1_size / (off_t) BLOCK_SIZE)); break; default: /* Just print the pathname */ printf(" %s", path); break; } putchar('\n');}#define INDENT_SIZE 4void do_indent(i)int i;{ i *= INDENT_SIZE; while (i-- > 0) putchar(' ');}int Mkdir(directory)char *directory;/* Make a directory, return exit status. * This routine is not necessary on systems that * have a system call to make directories. */{ int pid, status; if ((pid = fork()) == 0) { execl("/bin/Mkdir", "Mkdir", directory, (char *) 0); execl("/usr/bin/Mkdir", "Mkdir", directory, (char *) 0); exit(1); } else if (pid < 0) return(-1); while (wait(&status) != pid); return(status);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -