static struct ufs1_dinode * get_inode(int fd, struct fs *super, ino_t ino) { static struct ufs1_dinode *ip; static ino_t last; if (fd < 0) { /* flush cache */ if (ip) { free(ip); ip = NULL; } return 0; } if (!ip || ino < last || ino >= last + INOCNT(super)) { if (!ip && !(ip = (struct ufs1_dinode *)malloc(INOSZ(super)))) errx(1, "allocate inodes"); last = (ino / INOCNT(super)) * INOCNT(super); if (lseek(fd, (off_t)ino_to_fsba(super, last) << super->fs_fshift, 0) < (off_t)0 || read(fd,ip,INOSZ(super)) != (ssize_t)INOSZ(super)) err(1, "read inodes"); } return ip + ino % INOCNT(super); }
static union dinode * get_inode(int fd, struct fs *super, ino_t ino) { static caddr_t ipbuf; static struct cg *cgp; static ino_t last; static int cg; struct ufs2_dinode *di2; if (fd < 0) { /* flush cache */ if (ipbuf) { free(ipbuf); ipbuf = NULL; if (super != NULL && super->fs_magic == FS_UFS2_MAGIC) { free(cgp); cgp = NULL; } } return 0; } if (!ipbuf || ino < last || ino >= last + INOCNT(super)) { if (super->fs_magic == FS_UFS2_MAGIC && (!cgp || cg != ino_to_cg(super, ino))) { cg = ino_to_cg(super, ino); if (!cgp && !(cgp = malloc(super->fs_cgsize))) errx(1, "allocate cg"); if (pread(fd, cgp, super->fs_cgsize, (off_t)cgtod(super, cg) << super->fs_fshift) != super->fs_cgsize) if (read(fd, cgp, super->fs_cgsize) != super->fs_cgsize) err(1, "read cg"); if (!cg_chkmagic(cgp)) errx(1, "cg has bad magic"); } if (!ipbuf && !(ipbuf = malloc(INOSZ(super)))) err(1, "allocate inodes"); last = (ino / INOCNT(super)) * INOCNT(super); if (lseek(fd, (off_t)ino_to_fsba(super, last) << super->fs_fshift, SEEK_SET) < 0 || read(fd, ipbuf, INOSZ(super)) != INOSZ(super)) { err(1, "read inodes"); } } if (super->fs_magic == FS_UFS1_MAGIC) return ((union dinode *) &((struct ufs1_dinode *)ipbuf)[ino % INOCNT(super)]); di2 = &((struct ufs2_dinode *)ipbuf)[ino % INOCNT(super)]; /* If the inode is unused, it might be unallocated too, so zero it. */ if (isclr(cg_inosused(cgp), ino % super->fs_ipg)) memset(di2, 0, sizeof(*di2)); return ((union dinode *)di2); }