1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// cs/fs/fs.cc
#include "fs.hh"
#include <string.h>
#include <filesystem>
#include <fstream>
#include <sstream>
#include <string>
using ::cs::Error;
using ::cs::Ok;
using ::cs::Result;
using ::cs::ResultOr;
cs::ResultOr<std::vector<std::string>> cs::fs::ls(
std::string path) {
std::vector<std::string> files;
try {
for (const auto& entry :
std::filesystem::directory_iterator(path)) {
files.push_back(entry.path().string());
}
} catch (const std::filesystem::filesystem_error& e) {
return TRACE(Error(e.what()));
}
return files;
}
ResultOr<std::string> cs::fs::read(std::string path) {
std::ifstream file(path);
if (!file.is_open()) {
return TRACE(Error(
"Failed to open file for reading. path=" + path +
", strerror(errno)=" + strerror(errno)));
}
std::ostringstream buffer;
buffer << file.rdbuf();
if (file.fail()) {
return TRACE( // LCOV_EXCL_LINE
Error("Failed to read file. path=" + path +
", strerror(errno)=" + strerror(errno)));
}
return buffer.str();
}
Result cs::fs::write(std::string path,
std::string contents) {
std::ofstream file(path);
if (!file.is_open()) {
return TRACE(Error(
"Failed to open file for writing. path=" + path +
", strerror(errno)=" + strerror(errno)));
}
file << contents;
if (file.fail()) {
return TRACE( // LCOV_EXCL_LINE
Error("Failed to write to file. path=" + path +
", strerror(errno)=" + strerror(errno)));
}
return Ok();
}
ResultOr<std::string> cs::fs::cwd() {
try {
std::filesystem::path cwd =
std::filesystem::current_path();
return std::string(cwd.c_str());
} catch (const std::filesystem::filesystem_error& e) {
return TRACE(Error(e.what()));
}
}
Result cs::fs::mkdir(std::string path) {
try {
if (std::filesystem::create_directories(path)) {
return Ok();
} else {
return TRACE(
Error("Directory already exists or could not be "
"created."));
}
} catch (const std::filesystem::filesystem_error& e) {
return TRACE(Error(e.what()));
}
}
Result cs::fs::Delete(std::string path) {
try {
if (!std::filesystem::remove(path)) {
return TRACE(Error("Failed to delete path: " + path));
}
return Ok();
// LCOV_EXCL_START
} catch (const std::filesystem::filesystem_error& e) {
return TRACE(Error(e.what()));
}
// LCOV_EXCL_STOP
}
bool cs::fs::dir_exists(std::string path) {
return std::filesystem::is_directory(path) &&
std::filesystem::exists(path);
}
bool cs::fs::IsDir(std::string path) {
return std::filesystem::is_directory(path) &&
std::filesystem::exists(path);
}
bool cs::fs::IsFile(std::string path) {
return (std::filesystem::is_regular_file(path) ||
std::filesystem::is_block_file(path) ||
std::filesystem::is_character_file(path)) &&
!std::filesystem::is_directory(path) &&
std::filesystem::exists(path);
}