diff options
Diffstat (limited to 'src/disk_interface.cc')
-rw-r--r-- | src/disk_interface.cc | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/disk_interface.cc b/src/disk_interface.cc index 70564d7..6404bd5 100644 --- a/src/disk_interface.cc +++ b/src/disk_interface.cc @@ -14,6 +14,13 @@ #include "disk_interface.h" +#include <errno.h> +#include <stdio.h> +#include <string.h> +#include <sys/stat.h> + +#include "util.h" + namespace { std::string DirName(const std::string& path) { @@ -33,6 +40,8 @@ std::string DirName(const std::string& path) { } // namespace +// DiskInterface --------------------------------------------------------------- + bool DiskInterface::MakeDirs(const std::string& path) { std::string dir = DirName(path); if (dir.empty()) @@ -49,3 +58,53 @@ bool DiskInterface::MakeDirs(const std::string& path) { return false; return MakeDir(dir); } + +// RealDiskInterface ----------------------------------------------------------- + +int RealDiskInterface::Stat(const std::string& path) { + struct stat st; + if (stat(path.c_str(), &st) < 0) { + if (errno == ENOENT) { + return 0; + } else { + Error("stat(%s): %s", path.c_str(), strerror(errno)); + return -1; + } + } + + return st.st_mtime; + return true; +} + +bool RealDiskInterface::MakeDir(const std::string& path) { + if (::MakeDir(path) < 0) { + Error("mkdir(%s): %s", path.c_str(), strerror(errno)); + return false; + } + return true; +} + +std::string RealDiskInterface::ReadFile(const std::string& path, + std::string* err) { + std::string contents; + int ret = ::ReadFile(path, &contents, err); + if (ret == -ENOENT) { + // Swallow ENOENT. + err->clear(); + } + return contents; +} + +int RealDiskInterface::RemoveFile(const std::string& path) { + if (remove(path.c_str()) < 0) { + switch (errno) { + case ENOENT: + return 1; + default: + Error("remove(%s): %s", path.c_str(), strerror(errno)); + return -1; + } + } else { + return 0; + } +} |