31 DirectoryFS(
const char* envName,
const char* defaultRoot)
32 : env_name(envName), root(defaultRoot), default_root(defaultRoot) {}
36 const char* env = getenv(env_name);
37 root = env !=
nullptr && env[0] != 0 ? env : default_root;
39 return stat(root.c_str(), &info) == 0 && (info.st_mode & S_IFDIR);
45 void setRoot(
const char* dir) { root = dir; }
46 const char* getRoot() {
return root.c_str(); }
49 File open(
const char* path,
const char* mode =
"r",
bool create =
false) {
51 file.open(fullPath(path).c_str(), toFlags(mode));
55 bool exists(
const char* path) {
57 return stat(fullPath(path).c_str(), &info) == 0;
60 bool remove(
const char* path) {
return std::remove(fullPath(path).c_str()) == 0; }
62 bool rename(
const char* from,
const char* to) {
63 return std::rename(fullPath(from).c_str(), fullPath(to).c_str()) == 0;
66 bool mkdir(
const char* path) {
68 return ::_mkdir(fullPath(path).c_str()) == 0;
70 return ::mkdir(fullPath(path).c_str(), 0777) == 0;
74 bool rmdir(
const char* path) { return ::rmdir(fullPath(path).c_str()) == 0; }
76 bool format() {
return false; }
78 size_t totalBytes() {
return SD.totalBytes(); }
79 size_t usedBytes() {
return SD.usedBytes(); }
84 const char* default_root;
86 std::string fullPath(
const char* path) {
87 std::string result = root;
88 if (path ==
nullptr)
return result;
89 if (path[0] !=
'/') result +=
"/";
94 static int toFlags(
const char* mode) {
95 if (mode ==
nullptr || strcmp(mode,
"r") == 0 || strcmp(mode,
"rb") == 0)
return O_RDONLY;
96 if (mode[0] ==
'r')
return ios::in | ios::out;
97 if (mode[0] ==
'w')
return mode[1] ==
'+' ? ios::in | ios::out | ios::trunc : ios::out | ios::trunc;
98 if (mode[0] ==
'a')
return mode[1] ==
'+' ? ios::in | ios::out | ios::app : ios::out | ios::app;
File open(const char *path, const char *mode="r", bool create=false)
Opens a file: mode "r", "w", "a", "r+", "w+" or "a+".
Definition DirectoryFS.h:49