17 #include <boost/filesystem.hpp>
18 #include <gutil/strings/substitute.h>
26 using namespace strings;
33 Status FileSystemUtil::CreateDirectories(
const vector<string>& directories) {
34 for (
int i = 0; i < directories.size(); ++i) {
39 boost::filesystem::remove_all(directories[i]);
40 }
catch (exception& e) {
43 boost::filesystem::create_directory(directories[i]);
44 }
catch (exception& e) {
46 "Encountered error creating directory $0: $1", directories[i], e.what())));
53 Status FileSystemUtil::RemovePaths(
const vector<string>& directories) {
54 for (
int i = 0; i < directories.size(); ++i) {
56 boost::filesystem::remove_all(directories[i]);
57 }
catch (exception& e) {
59 "Encountered error removing directory $0: $1", directories[i], e.what())));
66 Status FileSystemUtil::CreateFile(
const string& file_path) {
67 int fd = creat(file_path.c_str(), S_IRUSR | S_IWUSR);
71 Substitute(
"Create file $0 failed with errno=$1 description=$2",
75 int success = close(fd);
78 Substitute(
"Close file $0 failed with errno=$1 description=$2",
85 Status FileSystemUtil::ResizeFile(
const string& file_path, int64_t trunc_len) {
86 int success = truncate(file_path.c_str(), trunc_len);
89 "Truncate file $0 to length $1 failed with errno $2 ($3)",
96 Status FileSystemUtil::VerifyIsDirectory(
const string& directory_path) {
98 if (!boost::filesystem::exists(directory_path)) {
100 "Directory path $0 does not exist", directory_path)));
102 }
catch (exception& e) {
104 "Encountered exception while verifying existence of directory path $0: $1",
105 directory_path, e.what())));
107 if (!boost::filesystem::is_directory(directory_path)) {
109 "Path $0 is not a directory", directory_path)));
114 Status FileSystemUtil::GetSpaceAvailable(
const string& directory_path,
118 *available_bytes = info.available;
119 }
catch (exception& e) {
121 "Encountered exception while checking available space for path $0: $1",
122 directory_path, e.what())));