bool FileSystemAbstraction::DirExists(const std::string& dir) { bool dirExists = false; #ifdef _WIN32 struct _stat info; std::string myDir = dir; // for the root dir on a drive (for example C:\) // we need the trailing slash if ((myDir.length() == 3) && (myDir[1] == ':') && ((myDir[2] != '\\') || (myDir[2] != '/'))) { // do nothing } else if ((myDir.length() == 2) && (myDir[1] == ':')) { myDir += "\\"; } else { // for any other dir (for example C:\WINDOWS\), // we need to get rid of it. myDir = StripTrailingSlashes(myDir); } const int ret = _stat(myDir.c_str(), &info); dirExists = ((ret == 0) && (info.st_mode & _S_IFDIR)); #else struct stat info; const int ret = stat(dir.c_str(), &info); dirExists = ((ret == 0) && S_ISDIR(info.st_mode)); #endif return dirExists; }
bool FileSystemAbstraction::IsReadableFile(const std::string& file) { #ifdef WIN32 return (_access(StripTrailingSlashes(file).c_str(), 4) == 0); #else return (access(file.c_str(), R_OK | F_OK) == 0); #endif }
void FileSystemAbstraction::ChDir(const std::string& dir) { #ifndef _WIN32 const int err = chdir(dir.c_str()); #else const int err = _chdir(StripTrailingSlashes(dir).c_str()); #endif if (err) { throw content_error("Could not chdir into " + dir); } }
void FileSystemHandler::Chdir(const std::string& dir) { #ifndef _WIN32 const int err = chdir(dir.c_str()); #else const int err = _chdir(StripTrailingSlashes(dir).c_str()); #endif if (err) { throw content_error("Could not chdir into SPRING_DATADIR"); } }
bool FileSystemAbstraction::IsReadableFile(const std::string& file) { // Exclude directories! if (!FileExists(file)) { return false; } #ifdef WIN32 return (_access(StripTrailingSlashes(file).c_str(), 4) == 0); #else return (access(file.c_str(), R_OK | F_OK) == 0); #endif }
bool FileSystemAbstraction::FileExists(const std::string& file) { bool fileExists = false; #ifdef _WIN32 struct _stat info; const int ret = _stat(StripTrailingSlashes(file).c_str(), &info); fileExists = ((ret == 0 && (info.st_mode & _S_IFREG))); #else struct stat info; const int ret = stat(file.c_str(), &info); fileExists = ((ret == 0 && !S_ISDIR(info.st_mode))); #endif return fileExists; }
/** * @brief creates a rwxr-xr-x dir in the writedir * * Returns true if the postcondition of this function is that dir exists in * the write directory. * * Note that this function does not check access to the dir, ie. if you've * created it manually with 0000 permissions then this function may return * true, subsequent operation on files inside the directory may still fail. * * As a rule of thumb, set identical permissions on identical items in the * data directory, ie. all subdirectories the same perms, all files the same * perms. */ bool FileSystemAbstraction::MkDir(const std::string& dir) { // First check if directory exists. We'll return success if it does. if (DirExists(dir)) { return true; } bool dirCreated = false; // If it doesn't exist we try to mkdir it and return success if that succeeds. #ifndef _WIN32 dirCreated = (::mkdir(dir.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0); #else dirCreated = (::_mkdir(StripTrailingSlashes(dir).c_str()) == 0); #endif if (!dirCreated) { LOG_L(L_WARNING, "Could not create directory %s: %s", dir.c_str(), strerror(errno)); } return dirCreated; }
bool FileSystemAbstraction::DirExists(const std::string& dir) { bool dirExists = false; #ifdef _WIN32 struct _stat info; std::string myDir = dir; // only for the root dir on a drive (for example C:\) // we need the trailing slash if (!IsFSRoot(myDir)) { myDir = StripTrailingSlashes(myDir); } else if ((myDir.length() == 2) && (myDir[1] == ':')) { myDir += "\\"; } const int ret = _stat(myDir.c_str(), &info); dirExists = ((ret == 0) && (info.st_mode & _S_IFDIR)); #else struct stat info; const int ret = stat(dir.c_str(), &info); dirExists = ((ret == 0) && S_ISDIR(info.st_mode)); #endif return dirExists; }