-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilesystem.cpp
More file actions
155 lines (139 loc) · 3.54 KB
/
filesystem.cpp
File metadata and controls
155 lines (139 loc) · 3.54 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* FileSystem.cpp
*
* Created on: May 20, 2017
* Author: kolban
*/
#include <iostream>
#include <sstream>
#include <string>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <esp_log.h>
#include "filesystem.h"
static const char* LOG_TAG = "FileSystem";
/**
* @brief Dump a given directory to the log.
* @param [in] path The path to the directory to dump.
* @return N/A.
*/
void FileSystem::dumpDirectory(std::string path) {
DIR *pDir = ::opendir(path.c_str());
if (pDir == nullptr) {
ESP_LOGD(LOG_TAG, "Unable to open directory: %s [errno=%d]", path.c_str(), errno);
return;
}
struct dirent *pDirent;
ESP_LOGD(LOG_TAG, "Directory dump of %s", path.c_str());
while((pDirent = readdir(pDir)) != nullptr) {
std::string type;
switch(pDirent->d_type) {
case DT_UNKNOWN:
type = "Unknown";
break;
case DT_REG:
type = "Regular";
break;
case DT_DIR:
type = "Directory";
break;
}
ESP_LOGD(LOG_TAG, "Entry: d_ino: %d, d_name: %s, d_type: %s", pDirent->d_ino, pDirent->d_name, type.c_str());
}
::closedir(pDir);
} // dumpDirectory
/**
* @brief Get the contents of a directory.
* @param [in] path The path to the directory.
* @return A vector of Files in the directory.
*/
std::vector<File> FileSystem::getDirectoryContents(std::string path) {
std::vector<File> ret;
DIR *pDir = ::opendir(path.c_str());
if (pDir == nullptr) {
ESP_LOGE(LOG_TAG, "getDirectoryContents:: Unable to open directory: %s [errno=%d]", path.c_str(), errno);
return ret;
}
struct dirent *pDirent;
ESP_LOGD(LOG_TAG, "Directory dump of %s", path.c_str());
while((pDirent = readdir(pDir)) != nullptr) {
File file(path +"/" + std::string(pDirent->d_name), pDirent->d_type);
ret.push_back(file);
}
::closedir(pDir);
return ret;
} // getDirectoryContents
/**
* @brief Does the path refer to a directory?
* @param [in] path The path to the directory.
*/
bool FileSystem::isDirectory(std::string path) {
struct stat statBuf;
int rc = stat(path.c_str(), &statBuf);
if (rc != 0) {
return false;
}
return S_ISDIR(statBuf.st_mode);
} // isDirectory
/**
* @brief Create a directory
* @param [in] path The directory to create.
* @return N/A.
*/
int FileSystem::mkdir(std::string path) {
ESP_LOGD(LOG_TAG, ">> mkdir: %s", path.c_str());
int rc = ::mkdir(path.c_str(), 0);
if (rc != 0) {
ESP_LOGE(LOG_TAG, "mkdir: errno=%d %s", errno, strerror(errno));
rc = errno;
}
return rc;
} // mkdir
/**
* @brief Return the constituent parts of the path.
* If we imagine a path as composed of parts separated by slashes, then this function
* returns a vector composed of the parts. For example:
*
* ```
* /x/y/z
* ```
* will break out to:
*
* ```
* path[0] = ""
* path[1] = "x"
* path[2] = "y"
* path[3] = "z"
* ```
*
* @return A vector of the constituent parts of the path.
*/
std::vector<std::string> FileSystem::pathSplit(std::string path) {
std::istringstream stream(path);
std::vector<std::string> ret;
std::string pathPart;
while(std::getline(stream, pathPart, '/')) {
ret.push_back(pathPart);
}
// Debug
for (int i=0; i<ret.size(); i++) {
ESP_LOGD(LOG_TAG, "part[%d]: %s", i, ret[i].c_str());
}
return ret;
} // pathSplit
/**
* @brief Remove a file from the file system.
* @param [in] path The path to the file to be removed.
* @return The return code of the underlying call.
*/
int FileSystem::remove(std::string path) {
int rc = ::unlink(path.c_str());
if (rc != 0) {
ESP_LOGE(LOG_TAG, "unlink: errno=%d", errno);
rc = errno;
}
return rc;
} // remove