bool _LocateEntry(LocatableEntry* entry, const BString& locatedPath, bool implicit, bool locateAncestors) { if (implicit && entry->State() == LOCATABLE_ENTRY_LOCATED_EXPLICITLY) return false; struct stat st; if (stat(locatedPath, &st) != 0) return false; if (S_ISDIR(st.st_mode)) { LocatableDirectory* directory = dynamic_cast<LocatableDirectory*>(entry); if (directory == NULL) return false; entry->SetLocatedPath(locatedPath, implicit); } else if (S_ISREG(st.st_mode)) { LocatableFile* file = dynamic_cast<LocatableFile*>(entry); if (file == NULL) return false; entry->SetLocatedPath(locatedPath, implicit); } // locate the ancestor directories, if requested if (locateAncestors) { BString locatedDirectory; BString locatedName; _SplitPath(locatedPath, locatedDirectory, locatedName); if (locatedName == entry->Name()) _LocateDirectory(entry->Parent(), locatedDirectory, implicit); } return true; }
LocatableFile* GetFile(const BString& path) { BString directoryPath; BString name; _SplitPath(path, directoryPath, name); LocatableFile* file = _GetFile(directoryPath, name); if (file == NULL) return NULL; // try to auto-locate the file if (LocatableDirectory* directory = file->Parent()) { if (directory->State() == LOCATABLE_ENTRY_UNLOCATED) { // parent not yet located -- try locate with the entry's path BString path; file->GetPath(path); _LocateEntry(file, path, true, true); } else { // parent already located -- locate the entry in the parent BString locatedDirectoryPath; if (directory->GetLocatedPath(locatedDirectoryPath)) _LocateEntryInParentDir(file, locatedDirectoryPath, true); } } return file; }
bool CopyFiles(const vector<string>& from, const string& to) { // There are two cases. If there is more than one from file, then the // to argument must designate a directory. If there is exactly one from // file then the to may designate either the destination directory or // the new file name. if (from.size() > 1) { if (!IsDir(to)) return false; bool success = true; for (size_t i = 0; i < from.size(); i++) { string dirname; string basename; _SplitPath(from[i], dirname, basename); if (!CopyFile(from[i], to + "/" + basename)) success = false; } return success; } else if (from.size() == 1) { if (IsDir(to)) { string dirname; string basename; _SplitPath(from[0], dirname, basename); return CopyFile(from[0], to + "/" + basename); } else return CopyFile(from[0], to); } else return false; }
void EntryLocated(const BString& path, const BString& locatedPath) { BString directory; BString name; _SplitPath(path, directory, name); LocatableEntry* entry = _LookupEntry(EntryPath(directory, name)); if (entry == NULL) return; _LocateEntry(entry, locatedPath, false, true); }
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; }
bool MkDirHier(const string& path) { vector<string> components; _SplitPath(path, components); for (size_t i = 0; i < components.size(); i++) { if (!IsDir(components[i])) { if (!MakeDir(components[i].c_str())) return false; } if (!ChangeDir(components[i])) return false; } return true; }