[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:
parent
89e7893b1a
commit
f6c9faf4da
@ -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: ");
|
||||
|
@ -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: ");
|
||||
|
@ -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) {
|
||||
|
@ -73,6 +73,7 @@ public:
|
||||
void close();
|
||||
operator bool() const;
|
||||
time_t getLastWrite();
|
||||
const char* path() const;
|
||||
const char* name() const;
|
||||
|
||||
boolean isDirectory(void);
|
||||
|
@ -38,6 +38,7 @@ public:
|
||||
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;
|
||||
|
@ -16,41 +16,41 @@
|
||||
|
||||
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)
|
||||
@ -58,7 +58,7 @@ FileImplPtr VFSImpl::open(const char* path, const char* mode)
|
||||
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);
|
||||
@ -66,14 +66,14 @@ FileImplPtr VFSImpl::open(const char* path, const char* mode)
|
||||
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;
|
||||
@ -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");
|
||||
@ -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;
|
||||
@ -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)
|
||||
, _f(NULL)
|
||||
, _d(NULL)
|
||||
@ -221,15 +221,15 @@ VFSFileImpl::VFSFileImpl(VFSImpl* fs, const char* path, const char* mode)
|
||||
, _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;
|
||||
}
|
||||
@ -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)
|
||||
{
|
||||
|
@ -66,6 +66,7 @@ public:
|
||||
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;
|
||||
|
@ -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: ");
|
||||
|
@ -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: ");
|
||||
|
@ -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: ");
|
||||
|
@ -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: ");
|
||||
|
@ -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: ");
|
||||
|
@ -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: ");
|
||||
|
@ -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: ");
|
||||
|
@ -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: ");
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user