int main(int argc, char **argv) { int reti; uint16_t vid; struct vol *vol; struct dir *retdir; struct path *path; /* initialize */ printf("Initializing\n============\n"); TEST(setuplog("default:note","/dev/tty")); TEST( afp_options_parse_cmdline(&obj, 3, &args[0]) ); TEST_int( afp_config_parse(&obj, NULL), 0); TEST_int( configinit(&obj), 0); TEST( cnid_init() ); TEST( load_volumes(&obj, NULL) ); TEST_int( dircache_init(8192), 0); obj.afp_version = 32; printf("\n"); /* now run tests */ printf("Running tests\n=============\n"); TEST_expr(vid = openvol(&obj, "test"), vid != 0); TEST_expr(vol = getvolbyvid(vid), vol != NULL); /* test directory.c stuff */ TEST_expr(retdir = dirlookup(vol, DIRDID_ROOT_PARENT), retdir != NULL); TEST_expr(retdir = dirlookup(vol, DIRDID_ROOT), retdir != NULL); TEST_expr(path = cname(vol, retdir, cnamewrap("Network Trash Folder")), path != NULL); TEST_expr(retdir = dirlookup(vol, DIRDID_ROOT), retdir != NULL); TEST_int(getfiledirparms(&obj, vid, DIRDID_ROOT_PARENT, "test"), 0); TEST_int(getfiledirparms(&obj, vid, DIRDID_ROOT, ""), 0); TEST_expr(reti = createdir(&obj, vid, DIRDID_ROOT, "dir1"), reti == 0 || reti == AFPERR_EXIST); TEST_int(getfiledirparms(&obj, vid, DIRDID_ROOT, "dir1"), 0); /* FIXME: this doesn't work although it should. "//" get translated to \000 \000 at means ".." ie this should getfiledirparms for DIRDID_ROOT_PARENT -- at least afair! TEST_int(getfiledirparms(&configs->obj, vid, DIRDID_ROOT, "//"), 0); */ TEST_int(createfile(&obj, vid, DIRDID_ROOT, "dir1/file1"), 0); TEST_int(delete(&obj, vid, DIRDID_ROOT, "dir1/file1"), 0); TEST_int(delete(&obj, vid, DIRDID_ROOT, "dir1"), 0); TEST_int(createfile(&obj, vid, DIRDID_ROOT, "file1"), 0); TEST_int(getfiledirparms(&obj, vid, DIRDID_ROOT, "file1"), 0); TEST_int(delete(&obj, vid, DIRDID_ROOT, "file1"), 0); /* test enumerate.c stuff */ TEST_int(enumerate(&obj, vid, DIRDID_ROOT), 0); }
// Look up and return the inode for a path name. // If parent != 0, return the inode for the parent and copy the final // path element into name, which must have room for DIRSIZ bytes. static struct inode* namex(char *path, int nameiparent, char *name) { struct inode *ip, *next; if(*path == '/') ip = iget(ROOTDEV, ROOTINO); else ip = idup(proc->cwd); while((path = skipelem(path, name)) != 0){ ilock(ip); if(ip->type != T_DIR){ iunlockput(ip); return 0; } if(nameiparent && *path == '\0'){ // Stop one level early. iunlock(ip); return ip; } if((next = dirlookup(ip, name, 0)) == 0){ iunlockput(ip); return 0; } iunlockput(ip); ip = next; } if(nameiparent){ iput(ip); return 0; } return ip; }
/* * Return a tube for name segmentname!tubename, * creating any of them if mkit is true and it is not found. */ Tube* namedtube(char *name, ulong msz, int n, int mkit) { char *dir, *tname; Tdir *tl; Tube *t; dumpdir("dir before namedtube"); name = strdup(name); if(name == nil) return nil; tname = utfrune(name, '!'); if(tname == nil){ dir = "tubes"; tname = name; }else{ dir = name; *tname++ = 0; } t = nil; tl = dirlookup(dir, mkit); if(tl != nil) t = tubelookup(tl, tname, msz, n, mkit); free(name); return t; }
int dirlink(struct inode *dp, char *dirname, uint32_t ino) { if(!ISDIR(dp->mode) || ino < 1) { return -1; } if(dirlookup(dp, dirname, 0)) return -1; struct dirent dir; int off; for(off = 0; off < dp->size; off+=sizeof(struct dirent)) { if(!readi(dp, (char *)&dir, off, sizeof(struct dirent))) { return -1; } // found a new entry if(!dir.ino) break; } dir.ino = ino; strcpy(dir.name, dirname); writei(dp, (char *)&dir, off, sizeof(struct dirent)); return 0; }
// Write a new directory entry (name, inum) into the directory dp. int dirlink(struct inode *dp, char *name, uint inum) { int off; struct dirent de; struct inode *ip; // Check that name is not present. if((ip = dirlookup(dp, name, 0)) != 0){ iput(ip); return -1; } // Look for an empty dirent. for(off = 0; off < dp->size; off += sizeof(de)){ if(readi(dp, (char*)&de, off, sizeof(de)) != sizeof(de)) panic("dirlink read"); if(de.inum == 0) break; } strncpy(de.name, name, DIRSIZ); de.inum = inum; if(writei(dp, (char*)&de, off, sizeof(de)) != sizeof(de)) panic("dirlink"); return 0; }
int kernel_unlink(char* path) { struct inode *ip, *dp; struct dirent de; char name[DIRSIZ]; uint off; // if(argstr(0, &path) < 0) // return -1; if((dp = nameiparent(path, name)) == 0) return -1; begin_trans(); ilock(dp); // Cannot unlink "." or "..". if(namecmp(name, ".") == 0 || namecmp(name, "..") == 0) goto bad; if((ip = dirlookup(dp, name, &off)) == 0) goto bad; ilock(ip); if(ip->nlink < 1) panic("unlink: nlink < 1"); /* if(ip->type == T_DIR && !isdirempty(ip)){ iunlockput(ip); goto bad; } */ memset(&de, 0, sizeof(de)); if(writei(dp, (char*)&de, off, sizeof(de)) != sizeof(de)) panic("unlink: writei"); if(ip->type == T_DIR){ dp->nlink--; iupdate(dp); } iunlockput(dp); ip->nlink--; iupdate(ip); iunlockput(ip); commit_trans(); return 0; bad: iunlockput(dp); commit_trans(); return -1; }
static int getforkparams(struct ofork *ofork, u_int16_t bitmap, char *buf, size_t *buflen) { struct path path; struct stat *st; struct adouble *adp; struct dir *dir; struct vol *vol; /* can only get the length of the opened fork */ if ( ( (bitmap & ((1<<FILPBIT_DFLEN) | (1<<FILPBIT_EXTDFLEN))) && (ofork->of_flags & AFPFORK_RSRC)) || ( (bitmap & ((1<<FILPBIT_RFLEN) | (1<<FILPBIT_EXTRFLEN))) && (ofork->of_flags & AFPFORK_DATA))) { return( AFPERR_BITMAP ); } if ( ad_reso_fileno( ofork->of_ad ) == -1 ) { /* META ? */ adp = NULL; } else { adp = ofork->of_ad; } vol = ofork->of_vol; dir = dirlookup(vol, ofork->of_did); if (NULL == (path.u_name = mtoupath(vol, of_name(ofork), dir->d_did, utf8_encoding()))) { return( AFPERR_MISC ); } path.m_name = of_name(ofork); path.id = 0; st = &path.st; if ( bitmap & ( (1<<FILPBIT_DFLEN) | (1<<FILPBIT_EXTDFLEN) | (1<<FILPBIT_FNUM) | (1 << FILPBIT_CDATE) | (1 << FILPBIT_MDATE) | (1 << FILPBIT_BDATE))) { if ( ad_data_fileno( ofork->of_ad ) <= 0 ) { /* 0 is for symlink */ if (movecwd(vol, dir) < 0) return( AFPERR_NOOBJ ); if ( lstat( path.u_name, st ) < 0 ) return( AFPERR_NOOBJ ); } else { if ( fstat( ad_data_fileno( ofork->of_ad ), st ) < 0 ) { return( AFPERR_BITMAP ); } } } return getmetadata(vol, bitmap, &path, dir, buf, buflen, adp ); }
/* * Simulate the opening of a directory */ RST_DIR * rst_opendir(const char *name) { struct inotab *itp; RST_DIR *dirp; ino_t ino; if ((ino = dirlookup(name)) > 0 && (itp = inotablookup(ino)) != NULL) { dirp = opendirfile(dirfile); rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt); return (dirp); } return (NULL); }
static struct inode *_name(char *p, int nameiparent, char *dirname) { struct inode *in, *next; char *name; char arr[64]; char *path = arr; strcpy(arr, p); if(*path == '/') { in = iget(0, 1); while(*++path == '/') ; } else in = idup(current_proc->cwd); name = strtok(path, "/"); while(name) { // printk("File system search:%s\n", name); ilock(in); if(!ISDIR(in->mode)) { iunlock(in); iput(in); return NULL; } if(nameiparent && !tok_hasnext()) { iunlock(in); if(dirname) strcpy(dirname, name); return in; } if(!(next = dirlookup(in, name, 0))) { iunlock(in); iput(in); return NULL; } iunlock(in); iput(in); in = next; if(!tok_hasnext() && dirname) strcpy(dirname, name); name = strtok(NULL, "/"); } return in; }
// Look up and return the inode for a path name. // If parent != 0, return the inode for the parent and copy the final // path element into name, which must have room for DIRSIZ bytes. static struct inode* namex(char *path, int nameiparent, char *name, int mode) // mode - 0-reference 1-dereference { struct inode *ip, *next; char buf[64]; if(*path == '/') ip = iget(ROOTDEV, ROOTINO); else ip = idup(proc->cwd); while((path = skipelem(path, name)) != 0){ ilock(ip); if(ip->type != T_DIR){ iunlockput(ip); return 0; } if(nameiparent && *path == '\0'){ // Stop one level early. iunlock(ip); return ip; } if((next = dirlookup(ip, name, 0)) == 0){ iunlockput(ip); return 0; } iunlockput(ip); if(mode || *path!='\0') { next= recursive_readlink(buf,next, 16, 0); } ip = next; } if(nameiparent){ iput(ip); return 0; } return ip; }
struct inode* kernel_create(char *path, short type, short major, short minor) { uint off; struct inode *ip, *dp; char name[DIRSIZ]; //cprintf("nameiparent path: %s\n",path); if((dp = nameiparent(path, name)) == 0) return 0; ilock(dp); if((ip = dirlookup(dp, name, &off)) != 0){ iunlockput(dp); ilock(ip); if(type == T_FILE && ip->type == T_FILE) return ip; iunlockput(ip); return 0; } if((ip = ialloc(dp->dev, type)) == 0) panic("create: ialloc"); ilock(ip); ip->major = major; ip->minor = minor; ip->nlink = 1; iupdate(ip); if(type == T_DIR){ // Create . and .. entries. dp->nlink++; // for ".." iupdate(dp); // No ip->nlink++ for ".": avoid cyclic ref count. if(dirlink(ip, ".", ip->inum) < 0 || dirlink(ip, "..", dp->inum) < 0) panic("create dots"); } if(dirlink(dp, name, ip->inum) < 0) panic("create: dirlink"); iunlockput(dp); return ip; }
// Look up and return the inode for a path name. // If parent != 0, return the inode for the parent and copy the final // path element into name, which must have room for DIRSIZ bytes. static struct inode* _namei(char *path, int parent, char *name) { //cprintf("Path: %s\n", path); struct inode *ip, *next; if(*path == '/') ip = iget(ROOTDEV, 1); else ip = idup(cp->cwd); //cprintf("cp name: %s\n", cp->name); while((path = skipelem(path, name)) != 0){ ilock(ip); if(ip->type != T_DIR){ iunlockput(ip); //cprintf("HERE\n"); return 0; } if(parent && *path == '\0'){ // Stop one level early. iunlock(ip); //cprintf("HERE3\n"); return ip; } if((next = dirlookup(ip, name, 0)) == 0){ iunlockput(ip); //cprintf("HERE1\n"); return 0; } iunlockput(ip); ip = next; } if(parent){ iput(ip); //cprintf("HERE2\n"); return 0; } //cprintf("HERE4-2: %d\n", ip); return ip; }
int main(int argc, char *argv[]) { int ch; ino_t ino; char *inputdev; char *symtbl = "./restoresymtable"; char *p, name[MAXPATHLEN]; /* Temp files should *not* be readable. We set permissions later. */ (void) umask(077); if (argc < 2) usage(); (void)setlocale(LC_ALL, ""); inputdev = NULL; obsolete(&argc, &argv); while ((ch = getopt(argc, argv, "b:dDf:himNP:Rrs:tuvxy")) != -1) switch(ch) { case 'b': /* Change default tape blocksize. */ bflag = 1; ntrec = strtol(optarg, &p, 10); if (*p) errx(1, "illegal blocksize -- %s", optarg); if (ntrec <= 0) errx(1, "block size must be greater than 0"); break; case 'd': dflag = 1; break; case 'D': Dflag = 1; break; case 'f': if (pipecmd) errx(1, "-P and -f options are mutually exclusive"); inputdev = optarg; break; case 'P': if (!pipecmd && inputdev) errx(1, "-P and -f options are mutually exclusive"); inputdev = optarg; pipecmd = 1; break; case 'h': hflag = 0; break; case 'i': case 'R': case 'r': case 't': case 'x': if (command != '\0') errx(1, "%c and %c options are mutually exclusive", ch, command); command = ch; break; case 'm': mflag = 0; break; case 'N': Nflag = 1; break; case 's': /* Dumpnum (skip to) for multifile dump tapes. */ dumpnum = strtol(optarg, &p, 10); if (*p) errx(1, "illegal dump number -- %s", optarg); if (dumpnum <= 0) errx(1, "dump number must be greater than 0"); break; case 'u': uflag = 1; break; case 'v': vflag = 1; break; case 'y': yflag = 1; break; default: usage(); } argc -= optind; argv += optind; if (command == '\0') errx(1, "none of i, R, r, t or x options specified"); if (signal(SIGINT, onintr) == SIG_IGN) (void) signal(SIGINT, SIG_IGN); if (signal(SIGTERM, onintr) == SIG_IGN) (void) signal(SIGTERM, SIG_IGN); setlinebuf(stderr); if (inputdev == NULL && (inputdev = getenv("TAPE")) == NULL) inputdev = _PATH_DEFTAPE; setinput(inputdev, pipecmd); if (argc == 0) { argc = 1; *--argv = "."; } switch (command) { /* * Interactive mode. */ case 'i': setup(); extractdirs(1); initsymtable(NULL); runcmdshell(); break; /* * Incremental restoration of a file system. */ case 'r': setup(); if (dumptime > 0) { /* * This is an incremental dump tape. */ vprintf(stdout, "Begin incremental restore\n"); initsymtable(symtbl); extractdirs(1); removeoldleaves(); vprintf(stdout, "Calculate node updates.\n"); treescan(".", ROOTINO, nodeupdates); findunreflinks(); removeoldnodes(); } else { /* * This is a level zero dump tape. */ vprintf(stdout, "Begin level 0 restore\n"); initsymtable((char *)0); extractdirs(1); vprintf(stdout, "Calculate extraction list.\n"); treescan(".", ROOTINO, nodeupdates); } createleaves(symtbl); createlinks(); setdirmodes(FORCE); checkrestore(); if (dflag) { vprintf(stdout, "Verify the directory structure\n"); treescan(".", ROOTINO, verifyfile); } dumpsymtable(symtbl, (long)1); break; /* * Resume an incremental file system restoration. */ case 'R': initsymtable(symtbl); skipmaps(); skipdirs(); createleaves(symtbl); createlinks(); setdirmodes(FORCE); checkrestore(); dumpsymtable(symtbl, (long)1); break; /* * List contents of tape. */ case 't': setup(); extractdirs(0); initsymtable((char *)0); while (argc--) { canon(*argv++, name, sizeof(name)); ino = dirlookup(name); if (ino == 0) continue; treescan(name, ino, listfile); } break; /* * Batch extraction of tape contents. */ case 'x': setup(); extractdirs(1); initsymtable((char *)0); while (argc--) { canon(*argv++, name, sizeof(name)); ino = dirlookup(name); if (ino == 0) continue; if (mflag) pathcheck(name); treescan(name, ino, addfile); } createfiles(); createlinks(); setdirmodes(0); if (dflag) checkrestore(); break; } done(0); /* NOTREACHED */ }
/* * NAME: pathlookup * * FUNCTION: resolve <path> to vnode <vpp> * with a reference acquired. * * PARAMETERS: vfsp - pointer to vfs * plp - path lookup structure * crp - credential * * RETURN: errors from subroutines. * -1 for a create when last component does not exist */ pathlookup( struct vfs * vfsp, pathlook_t * plp, struct ucred * crp) { int rc = 0; struct vnode * vp = NULL; /* map the volume handle to a vfs structure. */ if (vfsp == NULL) return ENOTDIR; /* probably should panic */ if (vfsp->vfs_flag & VFS_ACCEPT) return ENOTDIR; /* We're not really mounted */ /* Get the vnode for the last directory in the path. */ rc = dirlookup(vfsp, plp, crp); if (rc) return rc; /* special handling for last component of name. */ /* if the last component is null, then we are looking at * the root. */ if (*plp->pathlast == '\0') { /* add a hold to the directory vnode so that it * can be released twice by the caller. */ vp = plp->dvp; jfs_hold(vp); plp->statlast = PL_ISDIR; } else { /* if there is a wild card in the last component, then * set status accordingly. Otherwise, call lookup to see * if the name exists and get a vnode. */ /* This could be done more efficiently */ if (UniStrchr(plp->pathlast, '*') || UniStrchr(plp->pathlast, '?')) { plp->statlast = PL_WCARD; rc = 0; } else { rc = jfs_lookup(plp->dvp, &vp, plp->pathlast, 0, NULL, crp); /* set the status of the last component. */ switch (rc) { case(0): if ((VP2IP(vp)->i_mode & IFMT) == IFDIR) plp->statlast = PL_ISDIR; else plp->statlast = PL_EXIST; break; case(ENOENT): plp->statlast = PL_NOENT; rc = 0; break; default: /* general lookup failure; * release the vnodes and report an error */ if (plp->dlim.num_limits == 0) // F226941 jfs_rele(plp->dvp); break; } } } /* set the output pointers and return. */ plp->vp = vp; return rc; }
/* * Extract directory contents, building up a directory structure * on disk for extraction by name. * If genmode is requested, save mode, owner, and times for all * directories on the tape. */ void extractdirs(int genmode) { int i; struct inotab *itp; struct direct nulldir; int fd; Vprintf(stdout, "Extract directories from tape\n"); (void)snprintf(dirfile, sizeof(dirfile), "%s/rstdir%d", tmpdir, dumpdate); if (command != 'r' && command != 'R') { strlcat(dirfile, "-XXXXXXXXXX", sizeof(dirfile)); fd = mkstemp(dirfile); } else fd = open(dirfile, O_RDWR|O_CREAT|O_EXCL, 0666); if (fd == -1 || (df = fdopen(fd, "w")) == NULL) { if (fd != -1) close(fd); err(1, "cannot create directory temporary %s", dirfile); } if (genmode != 0) { (void)snprintf(modefile, sizeof(modefile), "%s/rstmode%d", tmpdir, dumpdate); if (command != 'r' && command != 'R') { strlcat(modefile, "-XXXXXXXXXX", sizeof(modefile)); fd = mkstemp(modefile); } else fd = open(modefile, O_RDWR|O_CREAT|O_EXCL, 0666); if (fd == -1 || (mf = fdopen(fd, "w")) == NULL) { if (fd != -1) close(fd); err(1, "cannot create modefile %s", modefile); } } nulldir.d_ino = 0; nulldir.d_type = DT_DIR; nulldir.d_namlen = 1; nulldir.d_name[0] = '/'; nulldir.d_name[1] = '\0'; nulldir.d_reclen = DIRSIZ(0, &nulldir); for (;;) { curfile.name = "<directory file - name unknown>"; curfile.action = USING; if (curfile.mode == 0 || (curfile.mode & IFMT) != IFDIR) { (void)fclose(df); dirp = opendirfile(dirfile); if (dirp == NULL) warn("opendirfile"); if (mf != NULL) (void)fclose(mf); i = dirlookup(dot); if (i == 0) panic("Root directory is not on tape\n"); return; } itp = allocinotab(mf, &curfile, seekpt); getfile(putdir, xtrnull); putent(&nulldir); flushent(); itp->t_size = seekpt - itp->t_seekpt; } }
/* * build mac. path (backwards) by traversing the directory tree * * The old way: dir and path refer to an app, path is a mac format * pathname. makemacpath() builds something that looks like a cname, * but uses upaths instead of mac format paths. * * The new way: dir and path refer to an app, path is a mac format * pathname. makemacpath() builds a cname. (zero is a path separator * and it's not \0 terminated). * * See afp_getappl() for the backward compatiblity code. */ static char * makemacpath(const struct vol *vol, char *mpath, int mpathlen, struct dir *dir, char *path) { char *p; p = mpath + mpathlen; p -= strlen( path ); memcpy( p, path, strlen( path )); while ( dir->d_did != DIRDID_ROOT ) { p -= blength(dir->d_m_name) + 1; if (p < mpath) { /* FIXME: pathname too long */ return NULL; } memcpy(p, cfrombstr(dir->d_m_name), blength(dir->d_m_name) + 1); if ((dir = dirlookup(vol, dir->d_pdid)) == NULL) return NULL; } return( p ); #if 0 char buffer[12 + MAXPATHLEN + 1]; int buflen = 12 + MAXPATHLEN + 1; char *ret = mpath; char *path = name; char *uname = NULL; struct bstrList *pathlist = NULL; cnid_t cnid = dir->d_pdid; /* Create list for path elements, request 16 list elements for now*/ if ((pathlist = bstListCreateMin(16)) == NULL) { LOG(log_error, logtype_afpd, "makemacpath: OOM: %s", strerror(errno)); return NULL; } while ( cnid != DIRDID_ROOT ) { /* construct path, copy already found uname to path element list*/ if ((bstrListPush(pathlist, bfromcstr(path))) != BSTR_OK) { afp_errno = AFPERR_MISC; ret = NULL; goto exit; } /* next part */ if ((uname = cnid_resolve(vol->v_cdb, &cnid, buffer, buflen)) == NULL ) { afp_errno = AFPERR_NOOBJ; ret = NULL; goto exit; } if ((path = utompath(vol, uname, cnid, utf8_encoding())) == NULL) { afp_errno = AFPERR_MISC; ret = NULL; goto exit; } } exit: if (pathlist) bstrListDestroy(pathlist); return(ret); #endif }
/* ------------------------------------------------------- */ static char * private_demangle(const struct vol *vol, char *mfilename, cnid_t did, cnid_t *osx) { char *t; char *u_name; uint32_t id, file_id; static char buffer[12 + MAXPATHLEN + 1]; int len = 12 + MAXPATHLEN + 1; struct dir *dir; size_t prefix; id = file_id = 0; t = strchr(mfilename, MANGLE_CHAR); if (t == NULL) { return mfilename; } prefix = t - mfilename; /* FIXME * is prefix == 0 a valid mangled filename ? */ /* may be a mangled filename */ t++; if (*t == '0') { /* can't start with a 0 */ return mfilename; } while(isuxdigit(*t)) { id = (id *16) + hextoint(*t); t++; } if ((*t != 0 && *t != '.') || strlen(t) > MAX_EXT_LENGTH || id < 17) { return mfilename; } file_id = id = htonl(id); if (osx) { *osx = id; } /* is it a dir?, there's a conflict with pre OSX 'trash #2' */ if ((dir = dirlookup(vol, id))) { if (dir->d_pdid != did) { /* not in the same folder, there's a race with outdate cache * but we have to live with it, hopefully client will recover */ return mfilename; } if (!osx) { /* it's not from cname so mfilename and dir must be the same */ if (strcmp(cfrombstr(dir->d_m_name), mfilename) == 0) { return cfrombstr(dir->d_u_name); } } else { return demangle_checks(vol, cfrombstr(dir->d_u_name), mfilename, prefix, t); } } else if (NULL != (u_name = cnid_resolve(vol->v_cdb, &id, buffer, len)) ) { if (id != did) { return mfilename; } if (!osx) { /* convert back to mac name and check it's the same */ t = utompath(vol, u_name, file_id, utf8_encoding(vol->v_obj)); if (!strcmp(t, mfilename)) { return u_name; } } else { return demangle_checks (vol, u_name, mfilename, prefix, t); } } return mfilename; }
//part 1 - signature change, and de-referencing symbolic links to the correct inode struct inode* namex(char *path, int nameiparent, char *name, struct inode *prev, int loopCount,int noDeRef) { struct inode *ip, *next; char buf[512]; // part 1 - prevent loop in symbolic links if(loopCount > 16) return 0; if(*path == '/') { ip = iget(ROOTDEV, ROOTINO); } else if(prev) { ip = idup(prev); } else { ip = idup(proc->cwd); } while( (path = skipelem(path, name)) != 0 ) { ilock(ip); if(ip->type != T_DIR){ iunlockput(ip); return 0; } if(nameiparent && *path == '\0'){ // Stop one level early. iunlock(ip); return ip; } if((next = dirlookup(ip, name, 0)) == 0){ iunlockput(ip); return 0; } if(noDeRef) { iunlockput(ip); } else { iunlock(ip); //part 1 ilock(next); if(next->type == T_SYMLINK) { if(readi(next, buf, 0, sizeof(buf)) != next->size) { iunlockput(ip); return 0; } buf[next->size] = 0; iunlock(next); next = namex(buf, 0, name, ip, loopCount++,0); } else { iunlock(next); } // End part 1 iput(ip); } ip = next; } if(nameiparent) { iput(ip); return 0; } return ip; }
/*! * This function performs a CNID db search * * Uses globals c1, c2, the search criteria * * @param vol (r) volume we are searching on ... * @param dir (rw) directory we are starting from ... * @param uname (r) UNIX name of object to search * @param rmatches (r) maximum number of matches we can return * @param pos (r) position we've stopped recently * @param rbuf (w) output buffer * @param nrecs (w) number of matches * @param rsize (w) length of data written to output buffer * @param ext (r) extended search flag */ static int catsearch_db(struct vol *vol, struct dir *dir, const char *uname, int rmatches, uint32_t *pos, char *rbuf, uint32_t *nrecs, int *rsize, int ext) { static char resbuf[DBD_MAX_SRCH_RSLTS * sizeof(cnid_t)]; static uint32_t cur_pos; static int num_matches; int ccr ,r; int result = AFP_OK; struct path path; char *rrbuf = rbuf; char buffer[MAXPATHLEN +2]; uint16_t flags = CONV_TOLOWER; LOG(log_debug, logtype_afpd, "catsearch_db(req pos: %u): {pos: %u, name: %s}", *pos, cur_pos, uname); if (*pos != 0 && *pos != cur_pos) { result = AFPERR_CATCHNG; goto catsearch_end; } if (cur_pos == 0 || *pos == 0) { if (convert_charset(vol->v_volcharset, vol->v_volcharset, vol->v_maccharset, uname, strlen(uname), buffer, MAXPATHLEN, &flags) == (size_t)-1) { LOG(log_error, logtype_afpd, "catsearch_db: conversion error"); result = AFPERR_MISC; goto catsearch_end; } LOG(log_debug, logtype_afpd, "catsearch_db: %s", buffer); if ((num_matches = cnid_find(vol->v_cdb, buffer, strlen(uname), resbuf, sizeof(resbuf))) == -1) { result = AFPERR_MISC; goto catsearch_end; } } while (cur_pos < num_matches) { char *name; cnid_t cnid, did; char resolvebuf[12 + MAXPATHLEN + 1]; struct dir *dir; /* Next CNID to process from buffer */ memcpy(&cnid, resbuf + cur_pos * sizeof(cnid_t), sizeof(cnid_t)); did = cnid; if ((name = cnid_resolve(vol->v_cdb, &did, resolvebuf, 12 + MAXPATHLEN + 1)) == NULL) goto next; LOG(log_debug, logtype_afpd, "catsearch_db: {pos: %u, name:%s, cnid: %u}", cur_pos, name, ntohl(cnid)); if ((dir = dirlookup(vol, did)) == NULL) goto next; if (movecwd(vol, dir) < 0 ) goto next; memset(&path, 0, sizeof(path)); path.u_name = name; path.m_name = utompath(vol, name, cnid, utf8_encoding()); if (of_stat(vol, &path) != 0) { switch (errno) { case EACCES: case ELOOP: goto next; case ENOENT: default: result = AFPERR_MISC; goto catsearch_end; } } /* For files path.d_dir is the parent dir, for dirs its the dir itself */ if (S_ISDIR(path.st.st_mode)) if ((dir = dirlookup(vol, cnid)) == NULL) goto next; path.d_dir = dir; LOG(log_maxdebug, logtype_afpd,"catsearch_db: dir: %s, cwd: %s, name: %s", cfrombstr(dir->d_fullpath), getcwdpath(), path.u_name); /* At last we can check the search criteria */ ccr = crit_check(vol, &path); if ((ccr & 1)) { LOG(log_debug, logtype_afpd,"catsearch_db: match: %s/%s", getcwdpath(), path.u_name); /* bit 1 means that criteria has been met */ r = rslt_add(vol, &path, &rrbuf, ext); if (r == 0) { result = AFPERR_MISC; goto catsearch_end; } *nrecs += r; /* Number of matches limit */ if (--rmatches == 0) goto catsearch_pause; /* Block size limit */ if (rrbuf - rbuf >= 448) goto catsearch_pause; } next: cur_pos++; } /* while */ /* finished */ result = AFPERR_EOF; cur_pos = 0; goto catsearch_end; catsearch_pause: *pos = cur_pos; catsearch_end: /* Exiting catsearch: error condition */ *rsize = rrbuf - rbuf; LOG(log_debug, logtype_afpd, "catsearch_db(req pos: %u): {pos: %u}", *pos, cur_pos); return result; }
static int catsearch(struct vol *vol, struct dir *dir, int rmatches, uint32_t *pos, char *rbuf, uint32_t *nrecs, int *rsize, int ext) { static u_int32_t cur_pos; /* Saved position index (ID) - used to remember "position" across FPCatSearch calls */ static DIR *dirpos; /* UNIX structure describing currently opened directory. */ struct dir *currentdir; /* struct dir of current directory */ int cidx, r; struct dirent *entry; int result = AFP_OK; int ccr; struct path path; char *vpath = vol->v_path; char *rrbuf = rbuf; time_t start_time; int num_rounds = NUM_ROUNDS; int cwd = -1; int error; int unlen; if (*pos != 0 && *pos != cur_pos) { result = AFPERR_CATCHNG; goto catsearch_end; } /* FIXME: Category "offspring count ! */ /* We need to initialize all mandatory structures/variables and change working directory appropriate... */ if (*pos == 0) { clearstack(); if (dirpos != NULL) { closedir(dirpos); dirpos = NULL; } if (addstack(vpath, dir, -1) == -1) { result = AFPERR_MISC; goto catsearch_end; } /* FIXME: Sometimes DID is given by client ! (correct this one above !) */ } /* Save current path */ if ((cwd = open(".", O_RDONLY)) < 0) { result = AFPERR_MISC; goto catsearch_end; } /* So we are beginning... */ start_time = time(NULL); while ((cidx = reducestack()) != -1) { if ((currentdir = dirlookup(vol, dstack[cidx].ds_did)) == NULL) { result = AFPERR_MISC; goto catsearch_end; } LOG(log_debug, logtype_afpd, "catsearch: current struct dir: \"%s\"", cfrombstr(currentdir->d_fullpath)); error = movecwd(vol, currentdir); if (!error && dirpos == NULL) dirpos = opendir("."); if (dirpos == NULL) dirpos = opendir(bdata(currentdir->d_fullpath)); if (error || dirpos == NULL) { switch (errno) { case EACCES: dstack[cidx].ds_checked = 1; continue; case EMFILE: case ENFILE: case ENOENT: result = AFPERR_NFILE; break; case ENOMEM: case ENOTDIR: default: result = AFPERR_MISC; } /* switch (errno) */ goto catsearch_end; } while ((entry = readdir(dirpos)) != NULL) { (*pos)++; if (!check_dirent(vol, entry->d_name)) continue; LOG(log_debug, logtype_afpd, "catsearch(\"%s\"): dirent: \"%s\"", cfrombstr(currentdir->d_fullpath), entry->d_name); memset(&path, 0, sizeof(path)); path.u_name = entry->d_name; if (of_stat(vol, &path) != 0) { switch (errno) { case EACCES: case ELOOP: case ENOENT: continue; case ENOTDIR: case EFAULT: case ENOMEM: case ENAMETOOLONG: default: result = AFPERR_MISC; goto catsearch_end; } } switch (S_IFMT & path.st.st_mode) { case S_IFDIR: /* here we can short cut ie if in the same loop the parent dir wasn't in the cache ALL dirsearch_byname will fail. */ unlen = strlen(path.u_name); path.d_dir = dircache_search_by_name(vol, currentdir, path.u_name, unlen); if (path.d_dir == NULL) { /* path.m_name is set by adddir */ if ((path.d_dir = dir_add(vol, currentdir, &path, unlen)) == NULL) { result = AFPERR_MISC; goto catsearch_end; } } path.m_name = cfrombstr(path.d_dir->d_m_name); if (addstack(path.u_name, path.d_dir, cidx) == -1) { result = AFPERR_MISC; goto catsearch_end; } break; case S_IFREG: path.d_dir = currentdir; break; default: continue; } ccr = crit_check(vol, &path); /* bit 0 means that criteria has been met */ if ((ccr & 1)) { r = rslt_add ( vol, &path, &rrbuf, ext); if (r == 0) { result = AFPERR_MISC; goto catsearch_end; } *nrecs += r; /* Number of matches limit */ if (--rmatches == 0) goto catsearch_pause; /* Block size limit */ if (rrbuf - rbuf >= 448) goto catsearch_pause; } /* MacOS 9 doesn't like servers executing commands longer than few seconds */ if (--num_rounds <= 0) { if (start_time != time(NULL)) { result=AFP_OK; goto catsearch_pause; } num_rounds = NUM_ROUNDS; } } /* while ((entry=readdir(dirpos)) != NULL) */ closedir(dirpos); dirpos = NULL; dstack[cidx].ds_checked = 1; } /* while (current_idx = reducestack()) != -1) */ /* We have finished traversing our tree. Return EOF here. */ result = AFPERR_EOF; goto catsearch_end; catsearch_pause: cur_pos = *pos; save_cidx = cidx; catsearch_end: /* Exiting catsearch: error condition */ *rsize = rrbuf - rbuf; if (cwd != -1) { if ((fchdir(cwd)) != 0) { LOG(log_debug, logtype_afpd, "error chdiring back: %s", strerror(errno)); } close(cwd); } return result; } /* catsearch() */
/* * Extract directory contents, building up a directory structure * on disk for extraction by name. * If genmode is requested, save mode, owner, and times for all * directories on the tape. */ void extractdirs(int genmode) { int i; struct ufs1_dinode *ip; struct inotab *itp; struct direct nulldir; int fd; const char *tmpdir; vprintf(stdout, "Extract directories from tape\n"); if ((tmpdir = getenv("TMPDIR")) == NULL || tmpdir[0] == '\0') tmpdir = _PATH_TMP; sprintf(dirfile, "%s/rstdir%ld", tmpdir, (long)dumpdate); if (command != 'r' && command != 'R') { (void *) strcat(dirfile, "-XXXXXX"); fd = mkstemp(dirfile); } else fd = open(dirfile, O_RDWR|O_CREAT|O_EXCL, 0666); if (fd == -1 || (df = fdopen(fd, "w")) == NULL) { if (fd != -1) close(fd); warn("%s - cannot create directory temporary\nfopen", dirfile); done(1); } if (genmode != 0) { sprintf(modefile, "%s/rstmode%ld", tmpdir, (long)dumpdate); if (command != 'r' && command != 'R') { (void *) strcat(modefile, "-XXXXXX"); fd = mkstemp(modefile); } else fd = open(modefile, O_RDWR|O_CREAT|O_EXCL, 0666); if (fd == -1 || (mf = fdopen(fd, "w")) == NULL) { if (fd != -1) close(fd); warn("%s - cannot create modefile\nfopen", modefile); done(1); } } nulldir.d_ino = 0; nulldir.d_type = DT_DIR; nulldir.d_namlen = 1; strcpy(nulldir.d_name, "/"); nulldir.d_reclen = DIRSIZ(0, &nulldir); for (;;) { curfile.name = "<directory file - name unknown>"; curfile.action = USING; ip = curfile.dip; if (ip == NULL || (ip->di_mode & IFMT) != IFDIR) { fclose(df); dirp = opendirfile(dirfile); if (dirp == NULL) fprintf(stderr, "opendirfile: %s\n", strerror(errno)); if (mf != NULL) fclose(mf); i = dirlookup(dot); if (i == 0) panic("Root directory is not on tape\n"); return; } itp = allocinotab(curfile.ino, ip, seekpt); getfile(putdir, xtrnull); putent(&nulldir); flushent(); itp->t_size = seekpt - itp->t_seekpt; } }
/* * Read and execute commands from the terminal. */ void runcmdshell(void) { struct entry *np; ufs1_ino_t ino; struct arglist arglist; char curdir[MAXPATHLEN]; char name[MAXPATHLEN]; char cmd[BUFSIZ]; arglist.freeglob = 0; arglist.argcnt = 0; arglist.glob.gl_flags = GLOB_ALTDIRFUNC; arglist.glob.gl_opendir = (void *)rst_opendir; arglist.glob.gl_readdir = (void *)glob_readdir; arglist.glob.gl_closedir = (void *)rst_closedir; arglist.glob.gl_lstat = glob_stat; arglist.glob.gl_stat = glob_stat; canon("/", curdir, sizeof(curdir)); loop: if (setjmp(reset) != 0) { if (arglist.freeglob != 0) { arglist.freeglob = 0; arglist.argcnt = 0; globfree(&arglist.glob); } nextarg = NULL; volno = 0; } runshell = 1; getcmd(curdir, cmd, name, sizeof(name), &arglist); switch (cmd[0]) { /* * Add elements to the extraction list. */ case 'a': if (strncmp(cmd, "add", strlen(cmd)) != 0) goto bad; ino = dirlookup(name); if (ino == 0) break; if (mflag) pathcheck(name); treescan(name, ino, addfile); break; /* * Change working directory. */ case 'c': if (strncmp(cmd, "cd", strlen(cmd)) != 0) goto bad; ino = dirlookup(name); if (ino == 0) break; if (inodetype(ino) == LEAF) { fprintf(stderr, "%s: not a directory\n", name); break; } strcpy(curdir, name); break; /* * Delete elements from the extraction list. */ case 'd': if (strncmp(cmd, "delete", strlen(cmd)) != 0) goto bad; np = lookupname(name); if (np == NULL || (np->e_flags & NEW) == 0) { fprintf(stderr, "%s: not on extraction list\n", name); break; } treescan(name, np->e_ino, deletefile); break; /* * Extract the requested list. */ case 'e': if (strncmp(cmd, "extract", strlen(cmd)) != 0) goto bad; createfiles(); createlinks(); setdirmodes(0); if (dflag) checkrestore(); volno = 0; break; /* * List available commands. */ case 'h': if (strncmp(cmd, "help", strlen(cmd)) != 0) goto bad; /* FALLTHROUGH */ case '?': fprintf(stderr, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s", "Available commands are:\n", "\tls [arg] - list directory\n", "\tcd arg - change directory\n", "\tpwd - print current directory\n", "\tadd [arg] - add `arg' to list of", " files to be extracted\n", "\tdelete [arg] - delete `arg' from", " list of files to be extracted\n", "\textract - extract requested files\n", "\tsetmodes - set modes of requested directories\n", "\tquit - immediately exit program\n", "\twhat - list dump header information\n", "\tverbose - toggle verbose flag", " (useful with ``ls'')\n", "\thelp or `?' - print this list\n", "If no `arg' is supplied, the current", " directory is used\n"); break; /* * List a directory. */ case 'l': if (strncmp(cmd, "ls", strlen(cmd)) != 0) goto bad; printlist(name, curdir); break; /* * Print current directory. */ case 'p': if (strncmp(cmd, "pwd", strlen(cmd)) != 0) goto bad; if (curdir[1] == '\0') fprintf(stderr, "/\n"); else fprintf(stderr, "%s\n", &curdir[1]); break; /* * Quit. */ case 'q': if (strncmp(cmd, "quit", strlen(cmd)) != 0) goto bad; return; case 'x': if (strncmp(cmd, "xit", strlen(cmd)) != 0) goto bad; return; /* * Toggle verbose mode. */ case 'v': if (strncmp(cmd, "verbose", strlen(cmd)) != 0) goto bad; if (vflag) { fprintf(stderr, "verbose mode off\n"); vflag = 0; break; } fprintf(stderr, "verbose mode on\n"); vflag++; break; /* * Just restore requested directory modes. */ case 's': if (strncmp(cmd, "setmodes", strlen(cmd)) != 0) goto bad; setdirmodes(FORCE); break; /* * Print out dump header information. */ case 'w': if (strncmp(cmd, "what", strlen(cmd)) != 0) goto bad; printdumpinfo(); break; /* * Turn on debugging. */ case 'D': if (strncmp(cmd, "Debug", strlen(cmd)) != 0) goto bad; if (dflag) { fprintf(stderr, "debugging mode off\n"); dflag = 0; break; } fprintf(stderr, "debugging mode on\n"); dflag++; break; /* * Unknown command. */ default: bad: fprintf(stderr, "%s: unknown command; type ? for help\n", cmd); break; } goto loop; }
// Look up and return the inode for a path name. // If parent != 0, return the inode for the parent and copy the final // path element into name, which must have room for DIRSIZ bytes. // Must be called inside a transaction since it calls iput(). static struct inode* namex(char* path, int nameiparent, int ignoreMounts, char* name) { // cprintf("namex \n"); struct inode* ip, *next; // cprintf("path %s nameparent %d , name %s bootfrom %d\n", path, nameiparent, name, bootfrom); if (*path == '/') ip = iget(ROOTDEV, ROOTINO, bootfrom); else ip = idup(proc->cwd); while ((path = skipelem(path, name)) != 0) { //cprintf("namex path %s \n",path); ilock(ip); if (ip->type != T_DIR) { iunlockput(ip); return 0; } if (nameiparent && *path == '\0') { // Stop one level early. // cprintf("fileread \n"); iunlock(ip); return ip; } if ((next = dirlookup(ip, name, 0)) == 0) { // cprintf("next is zero \n"); iunlockput(ip); return 0; } iunlockput(ip); ilock(next); // cprintf("next %d , type %d major %d minor %d \n",next->inum,next->type,next->major,next->minor); if (!ignoreMounts && next->type == T_DIR && next->major != 0 && next->major != MOUNTING_POINT) { // cprintf("major used ,we are f****d \n"); } // handle mounting points if (!ignoreMounts && next->type == T_DIR && next->major == MOUNTING_POINT) { // cprintf("got into condition \n"); iunlock(next); // iunlockput(ip); uint partitionNumnber = next->minor; ip = iget(ROOTDEV, 1, partitionNumnber); } else{ iunlock(next); // testing ip = next; } } if (nameiparent) { iput(ip); return 0; } // cprintf("ip returned is %d \n", ip->inum); return ip; }
/* * Extract directory contents, building up a directory structure * on disk for extraction by name. * If genmode is requested, save mode, owner, and times for all * directories on the tape. */ void extractdirs(int genmode) { struct inotab *itp; struct direct nulldir; int i, fd; const char *tmpdir; vprintf(stdout, "Extract directories from tape\n"); if ((tmpdir = getenv("TMPDIR")) == NULL || tmpdir[0] == '\0') tmpdir = _PATH_TMP; (void) sprintf(dirfile, "%s/rstdir%jd", tmpdir, (intmax_t)dumpdate); if (command != 'r' && command != 'R') { (void) strcat(dirfile, "-XXXXXX"); fd = mkstemp(dirfile); } else fd = open(dirfile, O_RDWR|O_CREAT|O_EXCL, 0666); if (fd == -1 || (df = fdopen(fd, "w")) == NULL) { if (fd != -1) close(fd); warn("%s: cannot create directory database", dirfile); done(1); } if (genmode != 0) { (void) sprintf(modefile, "%s/rstmode%jd", tmpdir, (intmax_t)dumpdate); if (command != 'r' && command != 'R') { (void) strcat(modefile, "-XXXXXX"); fd = mkstemp(modefile); } else fd = open(modefile, O_RDWR|O_CREAT|O_EXCL, 0666); if (fd == -1 || (mf = fdopen(fd, "w")) == NULL) { if (fd != -1) close(fd); warn("%s: cannot create modefile", modefile); done(1); } } nulldir.d_ino = 0; nulldir.d_type = DT_DIR; nulldir.d_namlen = 1; (void) strcpy(nulldir.d_name, "/"); nulldir.d_reclen = DIRSIZ(0, &nulldir); for (;;) { curfile.name = "<directory file - name unknown>"; curfile.action = USING; if (curfile.mode == 0 || (curfile.mode & IFMT) != IFDIR) break; itp = allocinotab(&curfile, seekpt); getfile(putdir, putdirattrs, xtrnull); putent(&nulldir); flushent(); itp->t_size = seekpt - itp->t_seekpt; } if (fclose(df) != 0) fail_dirtmp(dirfile); dirp = opendirfile(dirfile); if (dirp == NULL) fprintf(stderr, "opendirfile: %s\n", strerror(errno)); if (mf != NULL && fclose(mf) != 0) fail_dirtmp(modefile); i = dirlookup(dot); if (i == 0) panic("Root directory is not on tape\n"); }