bool RemoveFile(const string& path, bool recurse) { if (!IsDir(path)) return RemoveFile(path); if (!recurse) return RemoveDir(path); vector<string> filenames; if (GetDirEntries(path, filenames)) { string save_cwd; GetCwd(save_cwd); if (!ChangeDir(path)) return false; for (size_t i = 0; i < filenames.size(); i++) RemoveFile(filenames[i], true); if (!ChangeDir(save_cwd)) return false; } return RemoveDir(path); }
/* * Print all the entries in the specified directory. */ void PrintDirectory(struct unixfilesystem *fs, char *pathname) { int inumber = pathname_lookup(fs, pathname); if (inumber < 0) { fprintf(stderr, "Can't find %s\n", pathname); return; } struct direntv6 direntries[1000]; int numentries = GetDirEntries(fs, inumber, direntries, 1000); if (numentries < 0) { fprintf(stderr, "Can't read entries from %s\n", pathname); return; } struct inode in; int i, rowLength = 0; for (i = 0; i < numentries; i++) { inode_iget(fs, direntries[i].d_inumber, &in); printf("%s",direntries[i].d_name); if ((in.i_mode & IFMT) == IFDIR) printf("/"); printf("\t"); rowLength += strlen(direntries[i].d_name) + 1; if ((i+1) % 6 == 0) puts(""); } }
bool Glob(const string& pattern_, vector<string>& fileNames) { // Remove trailing slashes: string pattern = pattern_; while (pattern.size() > 0 && pattern[pattern.size()-1] == '/') { #ifdef OS_TRU64 pattern.remove(pattern.size() - 1); #else pattern.erase(pattern.end() - 1); #endif } // Split the pattern into directory name and base name: string dirname; string basename; _SplitPath(pattern, dirname, basename); if (!_contains_special_chars(basename)) fileNames.push_back(pattern_); else { // Find all files in the given directory MatchStringing the pattern: bool found = false; vector<string> filenames; if (!GetDirEntries(dirname, filenames)) return false; for (size_t i = 0; i < filenames.size(); i++) { if (MatchString(basename, filenames[i])) { found = true; if (dirname == ".") fileNames.push_back(filenames[i]); else fileNames.push_back(dirname + "/" + filenames[i]); } } if (!found) return false; } return true; }
/* * Print all the entries in the specified directory. */ void PrintDirectory(struct unixfilesystem *fs, char *pathname) { int inumber = pathname_lookup(fs, pathname); if (inumber < 0) { fprintf(stderr, "Can't find %s\n", pathname); return; } struct direntv6 direntries[10000]; int numentries = GetDirEntries(fs, inumber, direntries, 10000); if (numentries < 0) { fprintf(stderr, "Can't read entries from %s\n", pathname); return; } for (int i = 0; i < numentries; i++) { printf("Direntry %s Name %s Inumber %d\n", pathname, direntries[i].d_name, direntries[i].d_inumber); } }
/* * Output to the specified file the checksum of the specified pathname and * inode as well as all its children if it is a directory. * * This is used by the grading script, so be careful not to change its output * format. */ void DumpPathAndChildren(struct unixfilesystem *fs, const char *pathname, int inumber, FILE *f) { struct inode in; if (inode_iget(fs, inumber, &in) < 0) { fprintf(stderr,"Can't read inode %d \n", inumber); return; } assert(in.i_mode & IALLOC); char chksum1[CHKSUMFILE_SIZE]; if (chksumfile_byinumber(fs, inumber, chksum1) < 0) { fprintf(stderr,"Can't checksum inode %d path %s\n", inumber, pathname); return; } char chksum2[CHKSUMFILE_SIZE]; if (chksumfile_bypathname(fs, pathname, chksum2) < 0) { fprintf(stderr,"Can't checksum inode %d path %s\n", inumber, pathname); return; } if (!chksumfile_compare(chksum1, chksum2)) { fprintf(stderr,"Pathname checksum of %s differs from inode %d\n", pathname, inumber); return; } char chksumstring[CHKSUMFILE_STRINGSIZE]; chksumfile_cvt2string(chksum2, chksumstring); int size = inode_getsize(&in); fprintf(f, "Path %s %d mode 0x%x size %d checksum %s\n",pathname,inumber,in.i_mode, size, chksumstring); if (pathname[1] == 0) { /* pathame == "/" */ pathname++; /* Delete extra / character */ } if ((in.i_mode & IFMT) == IFDIR) { const unsigned int MAXPATH = 1024; if (strlen(pathname) > MAXPATH-16) { fprintf(stderr, "Too deep of directories %s\n", pathname); } struct direntv6 direntries[10000]; int numentries = GetDirEntries(fs, inumber, direntries, 10000); for (int i = 0; i < numentries; i++) { char *n; n = direntries[i].d_name; if (n[0] == '.') { if ((n[1] == 0) || ((n[1] == '.') && (n[2] == 0))) { /* Skip over "." and ".." */ continue; } } char nextpath[MAXPATH]; sprintf(nextpath, "%s/%s",pathname, direntries[i].d_name); DumpPathAndChildren(fs, nextpath, direntries[i].d_inumber, f); } } }