Skip to content

[2.0.0] FS::name() returns the item name as in Arduino SD #4892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libraries/FFat/examples/FFat_Test/FFat_Test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
Serial.print(" DIR : ");
Serial.println(file.name());
if(levels){
listDir(fs, file.name(), levels -1);
listDir(fs, file.path(), levels -1);
}
} else {
Serial.print(" FILE: ");
Expand Down
2 changes: 1 addition & 1 deletion libraries/FFat/examples/FFat_time/FFat_time.ino
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
struct tm * tmstruct = localtime(&t);
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);
if(levels){
listDir(fs, file.name(), levels -1);
listDir(fs, file.path(), levels -1);
}
} else {
Serial.print(" FILE: ");
Expand Down
9 changes: 9 additions & 0 deletions libraries/FS/src/FS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ File::operator bool() const
return _p != nullptr && *_p != false;
}

const char* File::path() const
{
if (!*this) {
return nullptr;
}

return _p->path();
}

const char* File::name() const
{
if (!*this) {
Expand Down
1 change: 1 addition & 0 deletions libraries/FS/src/FS.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class File : public Stream
void close();
operator bool() const;
time_t getLastWrite();
const char* path() const;
const char* name() const;

boolean isDirectory(void);
Expand Down
1 change: 1 addition & 0 deletions libraries/FS/src/FSImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class FileImpl
virtual size_t size() const = 0;
virtual void close() = 0;
virtual time_t getLastWrite() = 0;
virtual const char* path() const = 0;
virtual const char* name() const = 0;
virtual boolean isDirectory(void) = 0;
virtual FileImplPtr openNextFile(const char* mode) = 0;
Expand Down
73 changes: 39 additions & 34 deletions libraries/FS/src/vfs_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,64 +16,64 @@

using namespace fs;

FileImplPtr VFSImpl::open(const char* path, const char* mode)
FileImplPtr VFSImpl::open(const char* fpath, const char* mode)
{
if(!_mountpoint) {
log_e("File system is not mounted");
return FileImplPtr();
}

if(!path || path[0] != '/') {
log_e("%s does not start with /", path);
if(!fpath || fpath[0] != '/') {
log_e("%s does not start with /", fpath);
return FileImplPtr();
}

char * temp = (char *)malloc(strlen(path)+strlen(_mountpoint)+2);
char * temp = (char *)malloc(strlen(fpath)+strlen(_mountpoint)+2);
if(!temp) {
log_e("malloc failed");
return FileImplPtr();
}

sprintf(temp,"%s%s", _mountpoint, path);
sprintf(temp,"%s%s", _mountpoint, fpath);

struct stat st;
//file lound
if(!stat(temp, &st)) {
free(temp);
if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode)) {
return std::make_shared<VFSFileImpl>(this, path, mode);
return std::make_shared<VFSFileImpl>(this, fpath, mode);
}
log_e("%s has wrong mode 0x%08X", path, st.st_mode);
log_e("%s has wrong mode 0x%08X", fpath, st.st_mode);
return FileImplPtr();
}

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

//try to open this as directory (might be mount point)
DIR * d = opendir(temp);
if(d) {
closedir(d);
free(temp);
return std::make_shared<VFSFileImpl>(this, path, mode);
return std::make_shared<VFSFileImpl>(this, fpath, mode);
}

log_e("%s does not exist", temp);
free(temp);
return FileImplPtr();
}

bool VFSImpl::exists(const char* path)
bool VFSImpl::exists(const char* fpath)
{
if(!_mountpoint) {
log_e("File system is not mounted");
return false;
}

VFSFileImpl f(this, path, "r");
VFSFileImpl f(this, fpath, "r");
if(f) {
f.close();
return true;
Expand Down Expand Up @@ -115,69 +115,69 @@ bool VFSImpl::rename(const char* pathFrom, const char* pathTo)
return rc == 0;
}

bool VFSImpl::remove(const char* path)
bool VFSImpl::remove(const char* fpath)
{
if(!_mountpoint) {
log_e("File system is not mounted");
return false;
}

if(!path || path[0] != '/') {
if(!fpath || fpath[0] != '/') {
log_e("bad arguments");
return false;
}

VFSFileImpl f(this, path, "r");
VFSFileImpl f(this, fpath, "r");
if(!f || f.isDirectory()) {
if(f) {
f.close();
}
log_e("%s does not exists or is directory", path);
log_e("%s does not exists or is directory", fpath);
return false;
}
f.close();

char * temp = (char *)malloc(strlen(path)+strlen(_mountpoint)+1);
char * temp = (char *)malloc(strlen(fpath)+strlen(_mountpoint)+1);
if(!temp) {
log_e("malloc failed");
return false;
}
sprintf(temp,"%s%s", _mountpoint, path);
sprintf(temp,"%s%s", _mountpoint, fpath);
auto rc = unlink(temp);
free(temp);
return rc == 0;
}

bool VFSImpl::mkdir(const char *path)
bool VFSImpl::mkdir(const char *fpath)
{
if(!_mountpoint) {
log_e("File system is not mounted");
return false;
}

VFSFileImpl f(this, path, "r");
VFSFileImpl f(this, fpath, "r");
if(f && f.isDirectory()) {
f.close();
//log_w("%s already exists", path);
//log_w("%s already exists", fpath);
return true;
} else if(f) {
f.close();
log_e("%s is a file", path);
log_e("%s is a file", fpath);
return false;
}

char * temp = (char *)malloc(strlen(path)+strlen(_mountpoint)+1);
char * temp = (char *)malloc(strlen(fpath)+strlen(_mountpoint)+1);
if(!temp) {
log_e("malloc failed");
return false;
}
sprintf(temp,"%s%s", _mountpoint, path);
sprintf(temp,"%s%s", _mountpoint, fpath);
auto rc = ::mkdir(temp, ACCESSPERMS);
free(temp);
return rc == 0;
}

bool VFSImpl::rmdir(const char *path)
bool VFSImpl::rmdir(const char *fpath)
{
if(!_mountpoint) {
log_e("File system is not mounted");
Expand All @@ -189,22 +189,22 @@ bool VFSImpl::rmdir(const char *path)
return false;
}

VFSFileImpl f(this, path, "r");
VFSFileImpl f(this, fpath, "r");
if(!f || !f.isDirectory()) {
if(f) {
f.close();
}
log_e("%s does not exists or is a file", path);
log_e("%s does not exists or is a file", fpath);
return false;
}
f.close();

char * temp = (char *)malloc(strlen(path)+strlen(_mountpoint)+1);
char * temp = (char *)malloc(strlen(fpath)+strlen(_mountpoint)+1);
if(!temp) {
log_e("malloc failed");
return false;
}
sprintf(temp,"%s%s", _mountpoint, path);
sprintf(temp,"%s%s", _mountpoint, fpath);
auto rc = ::rmdir(temp);
free(temp);
return rc == 0;
Expand All @@ -213,23 +213,23 @@ bool VFSImpl::rmdir(const char *path)



VFSFileImpl::VFSFileImpl(VFSImpl* fs, const char* path, const char* mode)
VFSFileImpl::VFSFileImpl(VFSImpl* fs, const char* fpath, const char* mode)
: _fs(fs)
, _f(NULL)
, _d(NULL)
, _path(NULL)
, _isDirectory(false)
, _written(false)
{
char * temp = (char *)malloc(strlen(path)+strlen(_fs->_mountpoint)+1);
char * temp = (char *)malloc(strlen(fpath)+strlen(_fs->_mountpoint)+1);
if(!temp) {
return;
}
sprintf(temp,"%s%s", _fs->_mountpoint, path);
sprintf(temp,"%s%s", _fs->_mountpoint, fpath);

_path = strdup(path);
_path = strdup(fpath);
if(!_path) {
log_e("strdup(%s) failed", path);
log_e("strdup(%s) failed", fpath);
free(temp);
return;
}
Expand Down Expand Up @@ -377,11 +377,16 @@ size_t VFSFileImpl::size() const
return _stat.st_size;
}

const char* VFSFileImpl::name() const
const char* VFSFileImpl::path() const
{
return (const char*) _path;
}

const char* VFSFileImpl::name() const
{
return pathToFileName(path());
}

//to implement
boolean VFSFileImpl::isDirectory(void)
{
Expand Down
1 change: 1 addition & 0 deletions libraries/FS/src/vfs_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class VFSFileImpl : public FileImpl
size_t position() const override;
size_t size() const override;
void close() override;
const char* path() const override;
const char* name() const override;
time_t getLastWrite() override;
boolean isDirectory(void) override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
Serial.print(" DIR : ");
Serial.println(file.name());
if(levels){
listDir(fs, file.name(), levels -1);
listDir(fs, file.path(), levels -1);
}
} else {
Serial.print(" FILE: ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
struct tm * tmstruct = localtime(&t);
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);
if(levels){
listDir(fs, file.name(), levels -1);
listDir(fs, file.path(), levels -1);
}
} else {
Serial.print(" FILE: ");
Expand Down
2 changes: 1 addition & 1 deletion libraries/SD/examples/SD_Test/SD_Test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
Serial.print(" DIR : ");
Serial.println(file.name());
if(levels){
listDir(fs, file.name(), levels -1);
listDir(fs, file.path(), levels -1);
}
} else {
Serial.print(" FILE: ");
Expand Down
2 changes: 1 addition & 1 deletion libraries/SD/examples/SD_time/SD_time.ino
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
struct tm * tmstruct = localtime(&t);
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);
if(levels){
listDir(fs, file.name(), levels -1);
listDir(fs, file.path(), levels -1);
}
} else {
Serial.print(" FILE: ");
Expand Down
2 changes: 1 addition & 1 deletion libraries/SD_MMC/examples/SDMMC_Test/SDMMC_Test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
Serial.print(" DIR : ");
Serial.println(file.name());
if(levels){
listDir(fs, file.name(), levels -1);
listDir(fs, file.path(), levels -1);
}
} else {
Serial.print(" FILE: ");
Expand Down
2 changes: 1 addition & 1 deletion libraries/SD_MMC/examples/SDMMC_time/SDMMC_time.ino
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
struct tm * tmstruct = localtime(&t);
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);
if(levels){
listDir(fs, file.name(), levels -1);
listDir(fs, file.path(), levels -1);
}
} else {
Serial.print(" FILE: ");
Expand Down
2 changes: 1 addition & 1 deletion libraries/SPIFFS/examples/SPIFFS_Test/SPIFFS_Test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
Serial.print(" DIR : ");
Serial.println(file.name());
if(levels){
listDir(fs, file.name(), levels -1);
listDir(fs, file.path(), levels -1);
}
} else {
Serial.print(" FILE: ");
Expand Down
2 changes: 1 addition & 1 deletion libraries/SPIFFS/examples/SPIFFS_time/SPIFFS_time.ino
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
struct tm * tmstruct = localtime(&t);
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);
if(levels){
listDir(fs, file.name(), levels -1);
listDir(fs, file.path(), levels -1);
}
} else {
Serial.print(" FILE: ");
Expand Down
2 changes: 1 addition & 1 deletion libraries/WebServer/examples/FSBrowser/FSBrowser.ino
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ void handleFileList() {
output += "{\"type\":\"";
output += (file.isDirectory()) ? "dir" : "file";
output += "\",\"name\":\"";
output += String(file.name()).substring(1);
output += String(file.path()).substring(1);
output += "\"}";
file = root.openNextFile();
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/WebServer/examples/SDWebServer/SDWebServer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ void printDirectory() {
output += "{\"type\":\"";
output += (entry.isDirectory()) ? "dir" : "file";
output += "\",\"name\":\"";
output += entry.name();
output += entry.path();
output += "\"";
output += "}";
server.sendContent(output);
Expand Down