1 #ifndef CROMBIE_FILESYSTEM_H 2 #define CROMBIE_FILESYSTEM_H 16 namespace FileSystem {
18 std::string
basename(
const std::string& name) {
24 std::string
dirname(
const std::string& name) {
25 std::string modname = name;
26 while (modname.back() ==
'/')
29 auto output = modname.substr(0, modname.size() -
basename(modname).size());
36 bool is_xrd(
const std::string& path) {
37 return path.find(
"root://") == 0;
45 auto iter = xrd_dir_contents.find(path);
46 if (iter != xrd_dir_contents.end())
51 if (parts.size() != 3)
52 throw std::runtime_error{std::string(
"xrd path (") + path +
") doesn't seem to have a good pattern"};
55 return xrd_dir_contents[path] =
61 return Misc::comprehension<std::string>
69 unsigned long xrd_get_size(
const std::string& name) {
72 for (
auto& line : xrd_ls(
dirname(name))) {
73 if (line.find(filename) != std::string::npos)
80 bool xrd_exists(
const std::string& path) {
81 return xrd_get_size(path);
87 bool exists(
const std::string& path) {
89 return xrd_exists(path);
91 return stat(path.data(), &buffer) == 0;
95 unsigned long get_size(
const std::string& name) {
97 return xrd_get_size(name);
98 struct stat file_stat;
99 stat(name.data(), &file_stat);
100 return file_stat.st_size;
105 char path_array[512];
107 auto addslash = path;
108 if (addslash.back() !=
'/')
111 strncpy(path_array, addslash.data(),
sizeof(path_array) - 1);
112 auto num_chars = strlen(path_array);
114 for (
unsigned i_char = 1; i_char < num_chars; i_char++) {
116 if (path_array[i_char] !=
'/')
120 path_array[i_char] =
'\0';
121 if (!
exists(path_array)) {
122 std::cout <<
"Making: " << path_array << std::endl;
123 mkdir(path_array, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
126 path_array[i_char] =
'/';
132 if (is_xrd(directory))
133 return xrd_list(directory);
137 auto* indir = opendir(directory.data());
138 while (
auto* dir_ent = readdir(indir)) {
139 if (dir_ent->d_name[0] !=
'.')
140 output.emplace_back(dir_ent->d_name);
154 std::string response;
155 std::cout << path <<
" already exists. Want to overwrite? (y/N)" << std::endl;
156 std::getline(std::cin, response);
157 return response ==
"y";