16 #include <glog/logging.h> 29 const char kDirectorySeparator =
'/';
37 if (a.empty() || b[0] == kDirectorySeparator)
39 if (a[a.size() - 1] == kDirectorySeparator)
41 return a + kDirectorySeparator + b;
49 std::string copy = path;
50 return dirname(©[0]);
56 return stat(path.c_str(), &info) == 0 && S_ISREG(info.st_mode);
61 return stat(path.c_str(), &info) == 0 && S_ISDIR(info.st_mode);
65 return unlink(path.c_str()) == 0;
69 std::string::size_type pos = 0;
70 while ((pos = path.find(kDirectorySeparator, pos + 1)) != std::string::npos) {
71 if (mkdir(path.substr(0, pos).c_str(), 0755) != 0 && errno != EEXIST)
75 return mkdir(path.c_str(), 0755) == 0 || errno == EEXIST;
79 std::vector<std::string>* files)
const {
84 if ((dir = opendir(path.c_str())) ==
nullptr) {
85 PLOG(ERROR) <<
"Unable to open directory '" << path <<
"'";
89 while ((entry = readdir(dir)) !=
nullptr) {
90 if (!strcmp(
".", entry->d_name) || !strcmp(
"..", entry->d_name))
93 const std::string sub_path =
PathJoin(path, entry->d_name);
95 CHECK_EQ(0, stat(sub_path.c_str(), &info));
96 if (S_ISREG(info.st_mode))
97 files->push_back(entry->d_name);
98 else if (!S_ISDIR(info.st_mode))
99 LOG(WARNING) <<
"Unable to process folder entry '" << sub_path <<
"'";
static std::string DirName(const std::string &path)
virtual MUST_USE_RESULT bool DeleteFile(const std::string &path) const
virtual bool FileExists(const std::string &path) const
virtual bool DirectoryExists(const std::string &path) const
static std::string PathJoin(const std::string &a, const std::string &b)
virtual MUST_USE_RESULT bool CreateDirectory(const std::string &path) const
virtual MUST_USE_RESULT bool ListFiles(const std::string &path, std::vector< std::string > *files) const