int main(int argc, char **argv) { char **av = argv; struct sockaddr_in host; afs_int32 code; struct hostent *hp; char hnamebuf[200]; struct timeval tv; int noAuth = 1; /* Default is authenticated connections */ argc--, av++; if (argc < 1) { printf("usage: pxclient <serverHost>\n"); exit(1); } memset(&host, 0, sizeof(struct sockaddr_in)); host.sin_family = AF_INET; host.sin_addr.s_addr = inet_addr(av[0]); #ifdef STRUCT_SOCKADDR_HAS_SA_LEN host.sin_len = sizeof(struct sockaddr_in); #endif if (host.sin_addr.s_addr != -1) { strcpy(hnamebuf, av[0]); } else { hp = gethostbyname(av[0]); if (hp) { host.sin_family = hp->h_addrtype; memcpy((caddr_t) & host.sin_addr, hp->h_addr, hp->h_length); } else { printf("unknown server host %s\n", av[0]); exit(1); } } if ((code = pxclient_Initialize(noAuth, host.sin_addr.s_addr)) != 0) { printf("Couldn't initialize fs library (code=%d).\n", code); exit(1); } code = ubik_Call(RXAFS_GetTime, cstruct, 0, &tv.tv_sec, &tv.tv_usec); if (!code) printf("AFS_GetTime on %s sec=%ld, usec=%ld\n", av[0], tv.tv_sec, (long int)tv.tv_usec); else printf("return code is %d\n", code); #ifdef notdef while (1) { char line[500]; int nargs; printf("fs> "); if (fgets(line, 499, stdin) != NULL) { char *oper; char **argp = args; GetArgs(line, argp, &nargs); oper = &argp[0][0]; ++argp, --nargs; if (!strcmp(oper, "probe")) { code = ubik_Call(RXAFS_GetTime, cstruct, 0, &tv.tv_sec, &tv.tv_usec); printf("return code is %d\n", code); if (!code) printf("sec=%d\n", tv.tv_sec); } else if (!strcmp(oper, "fsstats")) { struct afsStatistics stats; code = ubik_AFS_GetStatistics(cstruct, 0, &stats); printf("return code is %d\n", code); } else if (!strcmp(oper, "fd")) { code = FetchData(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "fs")) { code = FetchStatus(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "fa")) { code = FetchACL(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "sd")) { code = StoreData(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "ss")) { code = StoreStatus(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "sa")) { code = StoreACL(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "cf")) { code = CreateFile(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "rf")) { code = RemoveFile(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "rn")) { code = Rename(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "sl")) { code = Symlink(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "hl")) { code = HardLink(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "md")) { code = MakeDir(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "rd")) { code = RemoveDir(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "rdd")) { code = Readdir(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "mm")) { code = MakeMountPoint(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "rt")) { code = ReleaseTokens(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "bs")) { code = BulkStatus(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "lk")) { code = Lookup(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "gt")) { code = GetToken(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "ka")) { code = KeepAlive(argp); printf("return code is %d\n", code); } else if ((!strcmp(oper, "q")) || !strcmp(oper, "quit")) exit(0); else { printf("Unknown oper! Available operations: \n\n"); printf("fd <vnode> <unique> <pos> <len>\n"); printf("fs <vnode> <unique>\n"); printf("fa <vnode> <unique>\n"); printf ("sd <vnode> <unique> <pos> <len> <flen> [<mode>|-1] [<owner>|-1] [<length>|-1] <string>\n"); printf ("ss <vnode> <unique> [<mode>|-1] [<owner>|-1] [<length>|-1]\n"); printf("sa <vnode> <unique> <string>\n"); printf("rf <vnode> <unique> <name>\n"); printf ("cf <vnode> <unique> <name> [<mode>|-1] [<owner>|-1] [<length>|-1]\n"); printf ("rn <ovnode> <ounique> <oname> <nvnode> <nunique> <nname>\n"); printf ("sl <vnode> <unique> <name> <contents> [<mode>|-1] [<owner>|-1] [<length>|-1]\n"); printf("hl <dvnode> <dunique> <name> <evnode> <eunique>\n"); printf ("md <vnode> <unique> <name> [<mode>|-1] [<owner>|-1] [<length>|-1]\n"); printf("rd <vnode> <unique> <name>\n"); printf("rdd <vnode> <unique> <pos> <len>\n"); printf("lk <vnode> <unique> <name>\n"); printf("gt <vnode> <unique> <tokenID>\n"); printf("ka <vol.l> <vnode> <unique> <isExec> <kaTime>\n"); } } } #endif return 0; }
void DirEnumerator::Readdir(const fs::Path& path, bool loadOwners) { this->path = RealPath(path); this->loadOwners = loadOwners; Readdir(); }
void DirectoryList::ListPath(const fs::Path& path, std::queue<std::string> masks, int depth) const { if (maxRecursion && depth > maxRecursion) return; fs::DirEnumerator dirEnum; try { Readdir(path, dirEnum); } catch (const util::SystemError& e) { // silent failure - gives empty directory list return; } if (depth > 1) Output("\r\n"); std::ostringstream message; if (!path.Empty() && (options.Recursive() || !masks.empty() || depth > 1)) { message << path << ":\r\n"; Output(message.str()); message.str(""); } if (options.LongFormat()) { message << "total " << std::floor(dirEnum.TotalBytes() / 1024) << "\r\n"; Output(message.str()); message.str(""); } std::string mask; if (!masks.empty()) { mask = masks.front(); masks.pop(); } if (masks.empty()) { for (const auto& de : dirEnum) { const std::string& pathStr = de.Path(); if (pathStr[0] == '.' && !options.All()) continue; if (!mask.empty() && fnmatch(mask.c_str(), pathStr.c_str(), 0)) continue; if (options.LongFormat()) { message << Permissions(de.Status()) << " " << std::setw(3) << de.Status().Native().st_nlink << " " << std::left << std::setw(10) << UIDToName(de.Owner().UID()).substr(0, 10) << " " << std::left << std::setw(10) << GIDToName(de.Owner().GID()).substr(0, 10) << " " << std::right << std::setw(8) << de.Status().Size() << " " << Timestamp(de.Status()) << " " << de.Path(); if (options.SlashDirs() && de.Status().IsDirectory()) message << "/"; message << "\r\n"; Output(message.str()); message.str(""); } else { message << de.Path() << "\r\n"; Output(message.str()); message.str(""); } } } if (options.Recursive() || !mask.empty()) { for (const auto& de : dirEnum) { if (!de.Status().IsDirectory() || de.Status().IsSymLink()) continue; const std::string& pathStr = de.Path(); if (pathStr[0] == '.' && !options.All()) continue; if (!mask.empty() && fnmatch(mask.c_str(), pathStr.c_str(), 0)) continue; fs::Path fullPath(path); fullPath /= de.Path(); ListPath(fullPath, masks, depth + 1); } } }
static void do_FEF( Fchar *fn, void (*proc)(const Fchar *, const char *, const struct stat *), int dev, struct ino_link *inop, Fchar separator, int max_depth ) { struct stat fs; Dir_t *dir; if (proc == 0) return; /* just make sure */ if (LSTAT(fn, &fs) < 0) { (*proc)(fn, strerror(errno), &fs); return; } /* report on file fn */ (*proc)(fn, (char*)0, &fs); if (max_depth == 0) return; if ((fs.st_mode & S_IFMT) != S_IFDIR) return; #ifdef S_IFLNK /* don't follow links */ if ((fs.st_mode & S_IFMT) == S_IFLNK) return; #endif /* treat directory */ if (dev < 0) { /* no device known yet */ dev = fs.st_dev; } if (fs.st_dev != dev) { return; } dir = Opendir(fn); if (dir == 0) { (*proc)(fn, "directory not readable", &fs); } else { /* scan new directory */ int fnl = Fnamelen(fn); Dirent_t *dent; struct ino_link ino; /* worry about loops in the file system */ if (in_ino_list(inop, &fs)) { (*proc)(fn, "loop in file system", &fs); Closedir(dir); return; } link_ino_list(inop, &ino, &fs); /* shape up the directory name */ if (fn[fnl-1] != separator) { /* append separator */ fn[fnl++] = separator; fn[fnl] = '\0'; } /* descend */ while ((dent = Readdir(dir)) != (Dirent_t *)0) { if ( Fnamecmp(dent->d_name, str2Fname(".")) == 0 || Fnamecmp(dent->d_name, str2Fname("..")) == 0 ) continue; if (Fnamecmp(dent->d_name, str2Fname("")) == 0) { (*proc)(fn, "directory contains empty file name", &fs ); continue; } /* append name */ Fnamecat(fn, dent->d_name); do_FEF(fn, proc, dev, &ino, separator, max_depth-1); /* remove name again*/ fn[fnl] = '\0'; } Closedir(dir); } }
static int FtwTraverse(const FtwInfoPtr ftwip, size_t dirPathLen, int depth) { DIR *DIRp; char *cp; size_t fnLen; struct dirent *dentp; mode_t m; char *filename; char *newBuf; char *path = ftwip->curPath; int nSubdirs; FtwSubDirListPtr head = NULL, tail = NULL, sdp, nextsdp; int rc = (-1); int isRootDir; isRootDir = ((dirPathLen == 1) && (IsLocalPathDelim(path[0]))) ? 1 : 0; if ((DIRp = opendir(dirPathLen ? path : ".")) == NULL) { /* Not an error unless the first directory could not be opened. */ return (0); } nSubdirs = 0; ++ftwip->numDirs; ftwip->depth = depth; if (ftwip->maxDepth < ftwip->depth) { ftwip->maxDepth = ftwip->depth; } filename = path + dirPathLen; if (isRootDir == 0) /* Root directory is a separator. */ { *filename++ = (char) ftwip->dirSeparator; dirPathLen++; } *filename = '\0'; /* Path now contains dir/ */ dentp = (struct dirent *) ftwip->direntbuf; for (;;) { if (Readdir(DIRp, dentp, ftwip->direntbufSize) == NULL) break; cp = dentp->d_name; if ((cp[0] == '.') && ((cp[1] == '\0') || ((cp[1] == '.') && (cp[2] == '\0')))) continue; /* Skip . and .. */ *filename = '\0'; fnLen = strlen(cp) + 1 /* include \0 */; if ((fnLen + dirPathLen) > ftwip->curPathAllocSize) { if (ftwip->autoGrow == kFtwNoAutoGrowAndFail) { goto panic; } else if (ftwip->autoGrow == kFtwNoAutoGrowButContinue) { continue; } newBuf = (char *) realloc(ftwip->curPath, fnLen + dirPathLen + 30 + 2 /* room for / and \0 */); if (newBuf == NULL) goto panic; ftwip->curPath = newBuf; ftwip->curPathAllocSize = fnLen + dirPathLen + 30; path = ftwip->curPath; filename = path + dirPathLen; if (isRootDir == 0) /* Root directory is a separator. */ *filename++ = (char) ftwip->dirSeparator; *filename = '\0'; } memcpy(filename, cp, fnLen); ftwip->curPathLen = dirPathLen + fnLen - 1; ftwip->curFile = filename; ftwip->curFileLen = fnLen - 1; if (Lstat(path, &ftwip->curStat) == 0) { m = ftwip->curStat.st_mode; if (S_ISREG(m)) { ++ftwip->numFiles; ftwip->curType = '-'; if ((*ftwip->proc)(ftwip) < 0) { goto panic; } #ifdef S_ISLNK } else if (S_ISLNK(m)) { ftwip->curType = 'l'; ++ftwip->numLinks; if ((*ftwip->proc)(ftwip) < 0) { goto panic; } #endif /* S_ISLNK */ } else if (S_ISDIR(m)) { /* We delay entering the subdirectories * until we have closed this directory. * This will conserve file descriptors * and also have the effect of having * the files processed first. */ sdp = (FtwSubDirListPtr) malloc(sizeof(FtwSubDirList) + fnLen); if (sdp == NULL) goto panic; memcpy(&sdp->st, &ftwip->curStat, sizeof(sdp->st)); memcpy(sdp->name, cp, fnLen); sdp->fnLen = fnLen; sdp->next = NULL; if (head == NULL) { head = tail = sdp; } else { tail->next = sdp; tail = sdp; } nSubdirs++; } } } (void) closedir(DIRp); DIRp = NULL; /* Now enter each subdirectory. */ for (sdp = head; sdp != NULL; sdp = nextsdp) { nextsdp = sdp->next; memcpy(&ftwip->curStat, &sdp->st, sizeof(ftwip->curStat)); fnLen = sdp->fnLen; memcpy(filename, sdp->name, fnLen); ftwip->curPathLen = dirPathLen + fnLen - 1; ftwip->curFile = filename; ftwip->curFileLen = fnLen - 1; head = nextsdp; free(sdp); ftwip->curType = 'd'; if ((*ftwip->proc)(ftwip) < 0) { goto panic; } if (FtwTraverse(ftwip, dirPathLen + fnLen - 1, depth + 1) < 0) goto panic; /* Reset these, since buffer could have * been reallocated. */ path = ftwip->curPath; filename = path + dirPathLen; *filename = '\0'; } head = NULL; rc = 0; panic: if (DIRp != NULL) (void) closedir(DIRp); for (sdp = head; sdp != NULL; sdp = nextsdp) { nextsdp = sdp->next; free(sdp); } return (rc); } /* FtwTraverse */