Commit c3addc8f authored by Paul Asmuth's avatar Paul Asmuth
Browse files

FileUtil::du_c

parent 528e6043
Loading
Loading
Loading
Loading
+29 −0
Original line number Diff line number Diff line
@@ -51,6 +51,16 @@ bool FileUtil::isDirectory(const std::string& filename) {
  return S_ISDIR(fstat.st_mode);
}

size_t FileUtil::size(const std::string& filename) {
  struct stat fstat;

  if (stat(filename.c_str(), &fstat) < 0) {
    RAISE_ERRNO(kIOError, "fstat('%s') failed", filename.c_str());
  }

  return fstat.st_size;
}

/* The mkdir_p method was adapted from bash 4.1 */
void FileUtil::mkdir_p(const std::string& dirname) {
  char const* begin = dirname.c_str();
@@ -175,4 +185,23 @@ void FileUtil::cp(const std::string& src, const std::string& destination) {
  RAISE(kNotYetImplementedError);
}

size_t FileUtil::du_c(const std::string& path) {
  size_t size = 0;

  FileUtil::ls(path, [&path, &size] (const String& file) -> bool {
    auto filename = FileUtil::joinPaths(path, file);

    if (FileUtil::isDirectory(filename)) {
      size += FileUtil::du_c(filename);
    } else {
      size += FileUtil::size(filename);
    }

    return true;
  });

  return size;
}


}
+10 −0
Original line number Diff line number Diff line
@@ -37,6 +37,11 @@ public:
   */
  static bool isDirectory(const std::string& dirname);

  /**
   * Return the size of the file
   */
  static size_t size(const std::string& filename);

  /**
   * Join two paths
   */
@@ -79,6 +84,11 @@ public:
   */
  static void cp(const std::string& src, const std::string& destination);

  /**
   * Return the size of a directory (like du -c)
   */
  static size_t du_c(const std::string& path);

};

}