21 #include <sys/types.h>
32 string DirName(
const string& path) {
34 const char kPathSeparator =
'\\';
36 const char kPathSeparator =
'/';
39 string::size_type slash_pos = path.rfind(kPathSeparator);
40 if (slash_pos == string::npos)
42 while (slash_pos > 0 && path[slash_pos - 1] == kPathSeparator)
44 return path.substr(0, slash_pos);
47 int MakeDir(
const string& path) {
49 return _mkdir(path.c_str());
51 return mkdir(path.c_str(), 0777);
60 string dir = DirName(path);
82 if (!path.empty() && path[0] !=
'\\' && path.size() > MAX_PATH) {
84 Error(
"Stat(%s): Filename longer than %i characters",
85 path.c_str(), MAX_PATH);
89 WIN32_FILE_ATTRIBUTE_DATA attrs;
90 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &attrs)) {
91 DWORD err = GetLastError();
92 if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND)
95 Error(
"GetFileAttributesEx(%s): %s", path.c_str(),
96 GetLastErrorString().c_str());
100 const FILETIME& filetime = attrs.ftLastWriteTime;
106 mtime /= 1000000000LL / 100;
107 mtime -= 12622770400LL;
111 if (stat(path.c_str(), &st) < 0) {
112 if (errno == ENOENT || errno == ENOTDIR)
115 Error(
"stat(%s): %s", path.c_str(), strerror(errno));
124 FILE * fp = fopen(path.c_str(),
"w");
126 Error(
"WriteFile(%s): Unable to create file. %s",
127 path.c_str(), strerror(errno));
131 if (fwrite(contents.data(), 1, contents.length(), fp) < contents.length()) {
132 Error(
"WriteFile(%s): Unable to write to the file. %s",
133 path.c_str(), strerror(errno));
138 if (fclose(fp) == EOF) {
139 Error(
"WriteFile(%s): Unable to close the file. %s",
140 path.c_str(), strerror(errno));
149 Error(
"mkdir(%s): %s", path.c_str(), strerror(errno));
158 if (ret == -ENOENT) {
166 if (
remove(path.c_str()) < 0) {
171 Error(
"remove(%s): %s", path.c_str(), strerror(errno));
virtual string ReadFile(const string &path, string *err)
Read a file to a string. Fill in |err| on error.
bool MakeDirs(const string &path)
Create all the parent directories for path; like mkdir -p basename path.
virtual bool WriteFile(const string &path, const string &contents)
Create a file, with the specified name and contents Returns true on success, false on failure...
virtual bool MakeDir(const string &path)
Create a directory, returning false on failure.
virtual TimeStamp Stat(const string &path)
stat() a file, returning the mtime, or 0 if missing and -1 on other errors.
virtual int RemoveFile(const string &path)
Remove the file named path.
unsigned long long uint64_t
virtual bool MakeDir(const string &path)=0
Create a directory, returning false on failure.
bool quiet_
Whether to print on errors. Used to make a test quieter.
virtual TimeStamp Stat(const string &path)=0
stat() a file, returning the mtime, or 0 if missing and -1 on other errors.
void Error(const char *msg,...)
Log an error message.