[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
This commit is contained in:
Me No Dev 2021-04-15 17:25:01 +03:00 committed by GitHub
parent 89e7893b1a
commit f6c9faf4da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 63 additions and 46 deletions

View File

@ -27,7 +27,7 @@ void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
Serial.print(" DIR : "); Serial.print(" DIR : ");
Serial.println(file.name()); Serial.println(file.name());
if(levels){ if(levels){
listDir(fs, file.name(), levels -1); listDir(fs, file.path(), levels -1);
} }
} else { } else {
Serial.print(" FILE: "); Serial.print(" FILE: ");

View File

@ -31,7 +31,7 @@ void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
struct tm * tmstruct = localtime(&t); 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); 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){ if(levels){
listDir(fs, file.name(), levels -1); listDir(fs, file.path(), levels -1);
} }
} else { } else {
Serial.print(" FILE: "); Serial.print(" FILE: ");

View File

@ -143,6 +143,15 @@ File::operator bool() const
return _p != nullptr && *_p != false; return _p != nullptr && *_p != false;
} }
const char* File::path() const
{
if (!*this) {
return nullptr;
}
return _p->path();
}
const char* File::name() const const char* File::name() const
{ {
if (!*this) { if (!*this) {

View File

@ -73,6 +73,7 @@ public:
void close(); void close();
operator bool() const; operator bool() const;
time_t getLastWrite(); time_t getLastWrite();
const char* path() const;
const char* name() const; const char* name() const;
boolean isDirectory(void); boolean isDirectory(void);

View File

@ -38,6 +38,7 @@ public:
virtual size_t size() const = 0; virtual size_t size() const = 0;
virtual void close() = 0; virtual void close() = 0;
virtual time_t getLastWrite() = 0; virtual time_t getLastWrite() = 0;
virtual const char* path() const = 0;
virtual const char* name() const = 0; virtual const char* name() const = 0;
virtual boolean isDirectory(void) = 0; virtual boolean isDirectory(void) = 0;
virtual FileImplPtr openNextFile(const char* mode) = 0; virtual FileImplPtr openNextFile(const char* mode) = 0;

View File

@ -16,41 +16,41 @@
using namespace fs; using namespace fs;
FileImplPtr VFSImpl::open(const char* path, const char* mode) FileImplPtr VFSImpl::open(const char* fpath, const char* mode)
{ {
if(!_mountpoint) { if(!_mountpoint) {
log_e("File system is not mounted"); log_e("File system is not mounted");
return FileImplPtr(); return FileImplPtr();
} }
if(!path || path[0] != '/') { if(!fpath || fpath[0] != '/') {
log_e("%s does not start with /", path); log_e("%s does not start with /", fpath);
return FileImplPtr(); return FileImplPtr();
} }
char * temp = (char *)malloc(strlen(path)+strlen(_mountpoint)+2); char * temp = (char *)malloc(strlen(fpath)+strlen(_mountpoint)+2);
if(!temp) { if(!temp) {
log_e("malloc failed"); log_e("malloc failed");
return FileImplPtr(); return FileImplPtr();
} }
sprintf(temp,"%s%s", _mountpoint, path); sprintf(temp,"%s%s", _mountpoint, fpath);
struct stat st; struct stat st;
//file lound //file lound
if(!stat(temp, &st)) { if(!stat(temp, &st)) {
free(temp); free(temp);
if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode)) { 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(); return FileImplPtr();
} }
//file not found but mode permits creation //file not found but mode permits creation
if(mode && mode[0] != 'r') { if(mode && mode[0] != 'r') {
free(temp); 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) //try to open this as directory (might be mount point)
@ -58,7 +58,7 @@ FileImplPtr VFSImpl::open(const char* path, const char* mode)
if(d) { if(d) {
closedir(d); closedir(d);
free(temp); 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); log_e("%s does not exist", temp);
@ -66,14 +66,14 @@ FileImplPtr VFSImpl::open(const char* path, const char* mode)
return FileImplPtr(); return FileImplPtr();
} }
bool VFSImpl::exists(const char* path) bool VFSImpl::exists(const char* fpath)
{ {
if(!_mountpoint) { if(!_mountpoint) {
log_e("File system is not mounted"); log_e("File system is not mounted");
return false; return false;
} }
VFSFileImpl f(this, path, "r"); VFSFileImpl f(this, fpath, "r");
if(f) { if(f) {
f.close(); f.close();
return true; return true;
@ -115,69 +115,69 @@ bool VFSImpl::rename(const char* pathFrom, const char* pathTo)
return rc == 0; return rc == 0;
} }
bool VFSImpl::remove(const char* path) bool VFSImpl::remove(const char* fpath)
{ {
if(!_mountpoint) { if(!_mountpoint) {
log_e("File system is not mounted"); log_e("File system is not mounted");
return false; return false;
} }
if(!path || path[0] != '/') { if(!fpath || fpath[0] != '/') {
log_e("bad arguments"); log_e("bad arguments");
return false; return false;
} }
VFSFileImpl f(this, path, "r"); VFSFileImpl f(this, fpath, "r");
if(!f || f.isDirectory()) { if(!f || f.isDirectory()) {
if(f) { if(f) {
f.close(); f.close();
} }
log_e("%s does not exists or is directory", path); log_e("%s does not exists or is directory", fpath);
return false; return false;
} }
f.close(); f.close();
char * temp = (char *)malloc(strlen(path)+strlen(_mountpoint)+1); char * temp = (char *)malloc(strlen(fpath)+strlen(_mountpoint)+1);
if(!temp) { if(!temp) {
log_e("malloc failed"); log_e("malloc failed");
return false; return false;
} }
sprintf(temp,"%s%s", _mountpoint, path); sprintf(temp,"%s%s", _mountpoint, fpath);
auto rc = unlink(temp); auto rc = unlink(temp);
free(temp); free(temp);
return rc == 0; return rc == 0;
} }
bool VFSImpl::mkdir(const char *path) bool VFSImpl::mkdir(const char *fpath)
{ {
if(!_mountpoint) { if(!_mountpoint) {
log_e("File system is not mounted"); log_e("File system is not mounted");
return false; return false;
} }
VFSFileImpl f(this, path, "r"); VFSFileImpl f(this, fpath, "r");
if(f && f.isDirectory()) { if(f && f.isDirectory()) {
f.close(); f.close();
//log_w("%s already exists", path); //log_w("%s already exists", fpath);
return true; return true;
} else if(f) { } else if(f) {
f.close(); f.close();
log_e("%s is a file", path); log_e("%s is a file", fpath);
return false; return false;
} }
char * temp = (char *)malloc(strlen(path)+strlen(_mountpoint)+1); char * temp = (char *)malloc(strlen(fpath)+strlen(_mountpoint)+1);
if(!temp) { if(!temp) {
log_e("malloc failed"); log_e("malloc failed");
return false; return false;
} }
sprintf(temp,"%s%s", _mountpoint, path); sprintf(temp,"%s%s", _mountpoint, fpath);
auto rc = ::mkdir(temp, ACCESSPERMS); auto rc = ::mkdir(temp, ACCESSPERMS);
free(temp); free(temp);
return rc == 0; return rc == 0;
} }
bool VFSImpl::rmdir(const char *path) bool VFSImpl::rmdir(const char *fpath)
{ {
if(!_mountpoint) { if(!_mountpoint) {
log_e("File system is not mounted"); log_e("File system is not mounted");
@ -189,22 +189,22 @@ bool VFSImpl::rmdir(const char *path)
return false; return false;
} }
VFSFileImpl f(this, path, "r"); VFSFileImpl f(this, fpath, "r");
if(!f || !f.isDirectory()) { if(!f || !f.isDirectory()) {
if(f) { if(f) {
f.close(); 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; return false;
} }
f.close(); f.close();
char * temp = (char *)malloc(strlen(path)+strlen(_mountpoint)+1); char * temp = (char *)malloc(strlen(fpath)+strlen(_mountpoint)+1);
if(!temp) { if(!temp) {
log_e("malloc failed"); log_e("malloc failed");
return false; return false;
} }
sprintf(temp,"%s%s", _mountpoint, path); sprintf(temp,"%s%s", _mountpoint, fpath);
auto rc = ::rmdir(temp); auto rc = ::rmdir(temp);
free(temp); free(temp);
return rc == 0; return rc == 0;
@ -213,7 +213,7 @@ 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) : _fs(fs)
, _f(NULL) , _f(NULL)
, _d(NULL) , _d(NULL)
@ -221,15 +221,15 @@ VFSFileImpl::VFSFileImpl(VFSImpl* fs, const char* path, const char* mode)
, _isDirectory(false) , _isDirectory(false)
, _written(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) { if(!temp) {
return; return;
} }
sprintf(temp,"%s%s", _fs->_mountpoint, path); sprintf(temp,"%s%s", _fs->_mountpoint, fpath);
_path = strdup(path); _path = strdup(fpath);
if(!_path) { if(!_path) {
log_e("strdup(%s) failed", path); log_e("strdup(%s) failed", fpath);
free(temp); free(temp);
return; return;
} }
@ -377,11 +377,16 @@ size_t VFSFileImpl::size() const
return _stat.st_size; return _stat.st_size;
} }
const char* VFSFileImpl::name() const const char* VFSFileImpl::path() const
{ {
return (const char*) _path; return (const char*) _path;
} }
const char* VFSFileImpl::name() const
{
return pathToFileName(path());
}
//to implement //to implement
boolean VFSFileImpl::isDirectory(void) boolean VFSFileImpl::isDirectory(void)
{ {

View File

@ -66,6 +66,7 @@ public:
size_t position() const override; size_t position() const override;
size_t size() const override; size_t size() const override;
void close() override; void close() override;
const char* path() const override;
const char* name() const override; const char* name() const override;
time_t getLastWrite() override; time_t getLastWrite() override;
boolean isDirectory(void) override; boolean isDirectory(void) override;

View File

@ -32,7 +32,7 @@ void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
Serial.print(" DIR : "); Serial.print(" DIR : ");
Serial.println(file.name()); Serial.println(file.name());
if(levels){ if(levels){
listDir(fs, file.name(), levels -1); listDir(fs, file.path(), levels -1);
} }
} else { } else {
Serial.print(" FILE: "); Serial.print(" FILE: ");

View File

@ -43,7 +43,7 @@ void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
struct tm * tmstruct = localtime(&t); 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); 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){ if(levels){
listDir(fs, file.name(), levels -1); listDir(fs, file.path(), levels -1);
} }
} else { } else {
Serial.print(" FILE: "); Serial.print(" FILE: ");

View File

@ -35,7 +35,7 @@ void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
Serial.print(" DIR : "); Serial.print(" DIR : ");
Serial.println(file.name()); Serial.println(file.name());
if(levels){ if(levels){
listDir(fs, file.name(), levels -1); listDir(fs, file.path(), levels -1);
} }
} else { } else {
Serial.print(" FILE: "); Serial.print(" FILE: ");

View File

@ -47,7 +47,7 @@ void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
struct tm * tmstruct = localtime(&t); 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); 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){ if(levels){
listDir(fs, file.name(), levels -1); listDir(fs, file.path(), levels -1);
} }
} else { } else {
Serial.print(" FILE: "); Serial.print(" FILE: ");

View File

@ -35,7 +35,7 @@ void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
Serial.print(" DIR : "); Serial.print(" DIR : ");
Serial.println(file.name()); Serial.println(file.name());
if(levels){ if(levels){
listDir(fs, file.name(), levels -1); listDir(fs, file.path(), levels -1);
} }
} else { } else {
Serial.print(" FILE: "); Serial.print(" FILE: ");

View File

@ -47,7 +47,7 @@ void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
struct tm * tmstruct = localtime(&t); 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); 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){ if(levels){
listDir(fs, file.name(), levels -1); listDir(fs, file.path(), levels -1);
} }
} else { } else {
Serial.print(" FILE: "); Serial.print(" FILE: ");

View File

@ -25,7 +25,7 @@ void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
Serial.print(" DIR : "); Serial.print(" DIR : ");
Serial.println(file.name()); Serial.println(file.name());
if(levels){ if(levels){
listDir(fs, file.name(), levels -1); listDir(fs, file.path(), levels -1);
} }
} else { } else {
Serial.print(" FILE: "); Serial.print(" FILE: ");

View File

@ -31,7 +31,7 @@ void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
struct tm * tmstruct = localtime(&t); 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); 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){ if(levels){
listDir(fs, file.name(), levels -1); listDir(fs, file.path(), levels -1);
} }
} else { } else {
Serial.print(" FILE: "); Serial.print(" FILE: ");

View File

@ -207,7 +207,7 @@ void handleFileList() {
output += "{\"type\":\""; output += "{\"type\":\"";
output += (file.isDirectory()) ? "dir" : "file"; output += (file.isDirectory()) ? "dir" : "file";
output += "\",\"name\":\""; output += "\",\"name\":\"";
output += String(file.name()).substring(1); output += String(file.path()).substring(1);
output += "\"}"; output += "\"}";
file = root.openNextFile(); file = root.openNextFile();
} }

View File

@ -229,7 +229,7 @@ void printDirectory() {
output += "{\"type\":\""; output += "{\"type\":\"";
output += (entry.isDirectory()) ? "dir" : "file"; output += (entry.isDirectory()) ? "dir" : "file";
output += "\",\"name\":\""; output += "\",\"name\":\"";
output += entry.name(); output += entry.path();
output += "\""; output += "\"";
output += "}"; output += "}";
server.sendContent(output); server.sendContent(output);