Skip to content

Commit f6c9faf

Browse files
authored
[2.0.0] FS::name() returns the item name as in Arduino SD (#4892)
* FS::name() returns the item name as in Arduino SD Added method FS::path() that returns the full path * Adjust examples
1 parent 89e7893 commit f6c9faf

File tree

17 files changed

+63
-46
lines changed

17 files changed

+63
-46
lines changed

Diff for: libraries/FFat/examples/FFat_Test/FFat_Test.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
2727
Serial.print(" DIR : ");
2828
Serial.println(file.name());
2929
if(levels){
30-
listDir(fs, file.name(), levels -1);
30+
listDir(fs, file.path(), levels -1);
3131
}
3232
} else {
3333
Serial.print(" FILE: ");

Diff for: libraries/FFat/examples/FFat_time/FFat_time.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
3131
struct tm * tmstruct = localtime(&t);
3232
Serial.printf(" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n",(tmstruct->tm_year)+1900,( tmstruct->tm_mon)+1, tmstruct->tm_mday,tmstruct->tm_hour , tmstruct->tm_min, tmstruct->tm_sec);
3333
if(levels){
34-
listDir(fs, file.name(), levels -1);
34+
listDir(fs, file.path(), levels -1);
3535
}
3636
} else {
3737
Serial.print(" FILE: ");

Diff for: libraries/FS/src/FS.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ File::operator bool() const
143143
return _p != nullptr && *_p != false;
144144
}
145145

146+
const char* File::path() const
147+
{
148+
if (!*this) {
149+
return nullptr;
150+
}
151+
152+
return _p->path();
153+
}
154+
146155
const char* File::name() const
147156
{
148157
if (!*this) {

Diff for: libraries/FS/src/FS.h

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class File : public Stream
7373
void close();
7474
operator bool() const;
7575
time_t getLastWrite();
76+
const char* path() const;
7677
const char* name() const;
7778

7879
boolean isDirectory(void);

Diff for: libraries/FS/src/FSImpl.h

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class FileImpl
3838
virtual size_t size() const = 0;
3939
virtual void close() = 0;
4040
virtual time_t getLastWrite() = 0;
41+
virtual const char* path() const = 0;
4142
virtual const char* name() const = 0;
4243
virtual boolean isDirectory(void) = 0;
4344
virtual FileImplPtr openNextFile(const char* mode) = 0;

Diff for: libraries/FS/src/vfs_api.cpp

+39-34
Original file line numberDiff line numberDiff line change
@@ -16,64 +16,64 @@
1616

1717
using namespace fs;
1818

19-
FileImplPtr VFSImpl::open(const char* path, const char* mode)
19+
FileImplPtr VFSImpl::open(const char* fpath, const char* mode)
2020
{
2121
if(!_mountpoint) {
2222
log_e("File system is not mounted");
2323
return FileImplPtr();
2424
}
2525

26-
if(!path || path[0] != '/') {
27-
log_e("%s does not start with /", path);
26+
if(!fpath || fpath[0] != '/') {
27+
log_e("%s does not start with /", fpath);
2828
return FileImplPtr();
2929
}
3030

31-
char * temp = (char *)malloc(strlen(path)+strlen(_mountpoint)+2);
31+
char * temp = (char *)malloc(strlen(fpath)+strlen(_mountpoint)+2);
3232
if(!temp) {
3333
log_e("malloc failed");
3434
return FileImplPtr();
3535
}
3636

37-
sprintf(temp,"%s%s", _mountpoint, path);
37+
sprintf(temp,"%s%s", _mountpoint, fpath);
3838

3939
struct stat st;
4040
//file lound
4141
if(!stat(temp, &st)) {
4242
free(temp);
4343
if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode)) {
44-
return std::make_shared<VFSFileImpl>(this, path, mode);
44+
return std::make_shared<VFSFileImpl>(this, fpath, mode);
4545
}
46-
log_e("%s has wrong mode 0x%08X", path, st.st_mode);
46+
log_e("%s has wrong mode 0x%08X", fpath, st.st_mode);
4747
return FileImplPtr();
4848
}
4949

5050
//file not found but mode permits creation
5151
if(mode && mode[0] != 'r') {
5252
free(temp);
53-
return std::make_shared<VFSFileImpl>(this, path, mode);
53+
return std::make_shared<VFSFileImpl>(this, fpath, mode);
5454
}
5555

5656
//try to open this as directory (might be mount point)
5757
DIR * d = opendir(temp);
5858
if(d) {
5959
closedir(d);
6060
free(temp);
61-
return std::make_shared<VFSFileImpl>(this, path, mode);
61+
return std::make_shared<VFSFileImpl>(this, fpath, mode);
6262
}
6363

6464
log_e("%s does not exist", temp);
6565
free(temp);
6666
return FileImplPtr();
6767
}
6868

69-
bool VFSImpl::exists(const char* path)
69+
bool VFSImpl::exists(const char* fpath)
7070
{
7171
if(!_mountpoint) {
7272
log_e("File system is not mounted");
7373
return false;
7474
}
7575

76-
VFSFileImpl f(this, path, "r");
76+
VFSFileImpl f(this, fpath, "r");
7777
if(f) {
7878
f.close();
7979
return true;
@@ -115,69 +115,69 @@ bool VFSImpl::rename(const char* pathFrom, const char* pathTo)
115115
return rc == 0;
116116
}
117117

118-
bool VFSImpl::remove(const char* path)
118+
bool VFSImpl::remove(const char* fpath)
119119
{
120120
if(!_mountpoint) {
121121
log_e("File system is not mounted");
122122
return false;
123123
}
124124

125-
if(!path || path[0] != '/') {
125+
if(!fpath || fpath[0] != '/') {
126126
log_e("bad arguments");
127127
return false;
128128
}
129129

130-
VFSFileImpl f(this, path, "r");
130+
VFSFileImpl f(this, fpath, "r");
131131
if(!f || f.isDirectory()) {
132132
if(f) {
133133
f.close();
134134
}
135-
log_e("%s does not exists or is directory", path);
135+
log_e("%s does not exists or is directory", fpath);
136136
return false;
137137
}
138138
f.close();
139139

140-
char * temp = (char *)malloc(strlen(path)+strlen(_mountpoint)+1);
140+
char * temp = (char *)malloc(strlen(fpath)+strlen(_mountpoint)+1);
141141
if(!temp) {
142142
log_e("malloc failed");
143143
return false;
144144
}
145-
sprintf(temp,"%s%s", _mountpoint, path);
145+
sprintf(temp,"%s%s", _mountpoint, fpath);
146146
auto rc = unlink(temp);
147147
free(temp);
148148
return rc == 0;
149149
}
150150

151-
bool VFSImpl::mkdir(const char *path)
151+
bool VFSImpl::mkdir(const char *fpath)
152152
{
153153
if(!_mountpoint) {
154154
log_e("File system is not mounted");
155155
return false;
156156
}
157157

158-
VFSFileImpl f(this, path, "r");
158+
VFSFileImpl f(this, fpath, "r");
159159
if(f && f.isDirectory()) {
160160
f.close();
161-
//log_w("%s already exists", path);
161+
//log_w("%s already exists", fpath);
162162
return true;
163163
} else if(f) {
164164
f.close();
165-
log_e("%s is a file", path);
165+
log_e("%s is a file", fpath);
166166
return false;
167167
}
168168

169-
char * temp = (char *)malloc(strlen(path)+strlen(_mountpoint)+1);
169+
char * temp = (char *)malloc(strlen(fpath)+strlen(_mountpoint)+1);
170170
if(!temp) {
171171
log_e("malloc failed");
172172
return false;
173173
}
174-
sprintf(temp,"%s%s", _mountpoint, path);
174+
sprintf(temp,"%s%s", _mountpoint, fpath);
175175
auto rc = ::mkdir(temp, ACCESSPERMS);
176176
free(temp);
177177
return rc == 0;
178178
}
179179

180-
bool VFSImpl::rmdir(const char *path)
180+
bool VFSImpl::rmdir(const char *fpath)
181181
{
182182
if(!_mountpoint) {
183183
log_e("File system is not mounted");
@@ -189,22 +189,22 @@ bool VFSImpl::rmdir(const char *path)
189189
return false;
190190
}
191191

192-
VFSFileImpl f(this, path, "r");
192+
VFSFileImpl f(this, fpath, "r");
193193
if(!f || !f.isDirectory()) {
194194
if(f) {
195195
f.close();
196196
}
197-
log_e("%s does not exists or is a file", path);
197+
log_e("%s does not exists or is a file", fpath);
198198
return false;
199199
}
200200
f.close();
201201

202-
char * temp = (char *)malloc(strlen(path)+strlen(_mountpoint)+1);
202+
char * temp = (char *)malloc(strlen(fpath)+strlen(_mountpoint)+1);
203203
if(!temp) {
204204
log_e("malloc failed");
205205
return false;
206206
}
207-
sprintf(temp,"%s%s", _mountpoint, path);
207+
sprintf(temp,"%s%s", _mountpoint, fpath);
208208
auto rc = ::rmdir(temp);
209209
free(temp);
210210
return rc == 0;
@@ -213,23 +213,23 @@ bool VFSImpl::rmdir(const char *path)
213213

214214

215215

216-
VFSFileImpl::VFSFileImpl(VFSImpl* fs, const char* path, const char* mode)
216+
VFSFileImpl::VFSFileImpl(VFSImpl* fs, const char* fpath, const char* mode)
217217
: _fs(fs)
218218
, _f(NULL)
219219
, _d(NULL)
220220
, _path(NULL)
221221
, _isDirectory(false)
222222
, _written(false)
223223
{
224-
char * temp = (char *)malloc(strlen(path)+strlen(_fs->_mountpoint)+1);
224+
char * temp = (char *)malloc(strlen(fpath)+strlen(_fs->_mountpoint)+1);
225225
if(!temp) {
226226
return;
227227
}
228-
sprintf(temp,"%s%s", _fs->_mountpoint, path);
228+
sprintf(temp,"%s%s", _fs->_mountpoint, fpath);
229229

230-
_path = strdup(path);
230+
_path = strdup(fpath);
231231
if(!_path) {
232-
log_e("strdup(%s) failed", path);
232+
log_e("strdup(%s) failed", fpath);
233233
free(temp);
234234
return;
235235
}
@@ -377,11 +377,16 @@ size_t VFSFileImpl::size() const
377377
return _stat.st_size;
378378
}
379379

380-
const char* VFSFileImpl::name() const
380+
const char* VFSFileImpl::path() const
381381
{
382382
return (const char*) _path;
383383
}
384384

385+
const char* VFSFileImpl::name() const
386+
{
387+
return pathToFileName(path());
388+
}
389+
385390
//to implement
386391
boolean VFSFileImpl::isDirectory(void)
387392
{

Diff for: libraries/FS/src/vfs_api.h

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class VFSFileImpl : public FileImpl
6666
size_t position() const override;
6767
size_t size() const override;
6868
void close() override;
69+
const char* path() const override;
6970
const char* name() const override;
7071
time_t getLastWrite() override;
7172
boolean isDirectory(void) override;

Diff for: libraries/LITTLEFS/examples/LITTLEFS_test/LITTLEFS_test.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
3232
Serial.print(" DIR : ");
3333
Serial.println(file.name());
3434
if(levels){
35-
listDir(fs, file.name(), levels -1);
35+
listDir(fs, file.path(), levels -1);
3636
}
3737
} else {
3838
Serial.print(" FILE: ");

Diff for: libraries/LITTLEFS/examples/LITTLEFS_time/LITTLEFS_time.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
4343
struct tm * tmstruct = localtime(&t);
4444
Serial.printf(" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n",(tmstruct->tm_year)+1900,( tmstruct->tm_mon)+1, tmstruct->tm_mday,tmstruct->tm_hour , tmstruct->tm_min, tmstruct->tm_sec);
4545
if(levels){
46-
listDir(fs, file.name(), levels -1);
46+
listDir(fs, file.path(), levels -1);
4747
}
4848
} else {
4949
Serial.print(" FILE: ");

Diff for: libraries/SD/examples/SD_Test/SD_Test.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
3535
Serial.print(" DIR : ");
3636
Serial.println(file.name());
3737
if(levels){
38-
listDir(fs, file.name(), levels -1);
38+
listDir(fs, file.path(), levels -1);
3939
}
4040
} else {
4141
Serial.print(" FILE: ");

Diff for: libraries/SD/examples/SD_time/SD_time.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
4747
struct tm * tmstruct = localtime(&t);
4848
Serial.printf(" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n",(tmstruct->tm_year)+1900,( tmstruct->tm_mon)+1, tmstruct->tm_mday,tmstruct->tm_hour , tmstruct->tm_min, tmstruct->tm_sec);
4949
if(levels){
50-
listDir(fs, file.name(), levels -1);
50+
listDir(fs, file.path(), levels -1);
5151
}
5252
} else {
5353
Serial.print(" FILE: ");

Diff for: libraries/SD_MMC/examples/SDMMC_Test/SDMMC_Test.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
3535
Serial.print(" DIR : ");
3636
Serial.println(file.name());
3737
if(levels){
38-
listDir(fs, file.name(), levels -1);
38+
listDir(fs, file.path(), levels -1);
3939
}
4040
} else {
4141
Serial.print(" FILE: ");

Diff for: libraries/SD_MMC/examples/SDMMC_time/SDMMC_time.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
4747
struct tm * tmstruct = localtime(&t);
4848
Serial.printf(" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n",(tmstruct->tm_year)+1900,( tmstruct->tm_mon)+1, tmstruct->tm_mday,tmstruct->tm_hour , tmstruct->tm_min, tmstruct->tm_sec);
4949
if(levels){
50-
listDir(fs, file.name(), levels -1);
50+
listDir(fs, file.path(), levels -1);
5151
}
5252
} else {
5353
Serial.print(" FILE: ");

Diff for: libraries/SPIFFS/examples/SPIFFS_Test/SPIFFS_Test.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
2525
Serial.print(" DIR : ");
2626
Serial.println(file.name());
2727
if(levels){
28-
listDir(fs, file.name(), levels -1);
28+
listDir(fs, file.path(), levels -1);
2929
}
3030
} else {
3131
Serial.print(" FILE: ");

Diff for: libraries/SPIFFS/examples/SPIFFS_time/SPIFFS_time.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
3131
struct tm * tmstruct = localtime(&t);
3232
Serial.printf(" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n",(tmstruct->tm_year)+1900,( tmstruct->tm_mon)+1, tmstruct->tm_mday,tmstruct->tm_hour , tmstruct->tm_min, tmstruct->tm_sec);
3333
if(levels){
34-
listDir(fs, file.name(), levels -1);
34+
listDir(fs, file.path(), levels -1);
3535
}
3636
} else {
3737
Serial.print(" FILE: ");

Diff for: libraries/WebServer/examples/FSBrowser/FSBrowser.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ void handleFileList() {
207207
output += "{\"type\":\"";
208208
output += (file.isDirectory()) ? "dir" : "file";
209209
output += "\",\"name\":\"";
210-
output += String(file.name()).substring(1);
210+
output += String(file.path()).substring(1);
211211
output += "\"}";
212212
file = root.openNextFile();
213213
}

Diff for: libraries/WebServer/examples/SDWebServer/SDWebServer.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ void printDirectory() {
229229
output += "{\"type\":\"";
230230
output += (entry.isDirectory()) ? "dir" : "file";
231231
output += "\",\"name\":\"";
232-
output += entry.name();
232+
output += entry.path();
233233
output += "\"";
234234
output += "}";
235235
server.sendContent(output);

0 commit comments

Comments
 (0)