Skip to content

SD.open() new feature for creating all folders in path #5721

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 3 commits into from
Oct 1, 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/src/FFat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ size_t F_Fat::freeBytes()

bool F_Fat::exists(const char* path)
{
File f = open(path, "r");
File f = open(path, "r",false);
return (f == true) && !f.isDirectory();
}

Expand Down
8 changes: 4 additions & 4 deletions libraries/FS/src/FS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,18 +186,18 @@ void File::rewindDirectory(void)
_p->rewindDirectory();
}

File FS::open(const String& path, const char* mode)
File FS::open(const String& path, const char* mode, const bool create)
{
return open(path.c_str(), mode);
return open(path.c_str(), mode, create);
}

File FS::open(const char* path, const char* mode)
File FS::open(const char* path, const char* mode, const bool create)
{
if (!_impl) {
return File();
}

return File(_impl->open(path, mode));
return File(_impl->open(path, mode, create));
}

bool FS::exists(const char* path)
Expand Down
4 changes: 2 additions & 2 deletions libraries/FS/src/FS.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ class FS
public:
FS(FSImplPtr impl) : _impl(impl) { }

File open(const char* path, const char* mode = FILE_READ);
File open(const String& path, const char* mode = FILE_READ);
File open(const char* path, const char* mode = FILE_READ, const bool create = false);
File open(const String& path, const char* mode = FILE_READ, const bool create = false);

bool exists(const char* path);
bool exists(const String& path);
Expand Down
2 changes: 1 addition & 1 deletion libraries/FS/src/FSImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class FSImpl
public:
FSImpl() : _mountpoint(NULL) { }
virtual ~FSImpl() { }
virtual FileImplPtr open(const char* path, const char* mode) = 0;
virtual FileImplPtr open(const char* path, const char* mode, const bool create) = 0;
virtual bool exists(const char* path) = 0;
virtual bool rename(const char* pathFrom, const char* pathTo) = 0;
virtual bool remove(const char* path) = 0;
Expand Down
56 changes: 47 additions & 9 deletions libraries/FS/src/vfs_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

using namespace fs;

FileImplPtr VFSImpl::open(const char* fpath, const char* mode)
FileImplPtr VFSImpl::open(const char* fpath, const char* mode, const bool create)
{
if(!_mountpoint) {
log_e("File system is not mounted");
Expand All @@ -37,7 +37,7 @@ FileImplPtr VFSImpl::open(const char* fpath, const char* mode)
sprintf(temp,"%s%s", _mountpoint, fpath);

struct stat st;
//file lound
//file found
if(!stat(temp, &st)) {
free(temp);
if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode)) {
Expand All @@ -47,12 +47,6 @@ FileImplPtr VFSImpl::open(const char* fpath, const char* mode)
return FileImplPtr();
}

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

//try to open this as directory (might be mount point)
DIR * d = opendir(temp);
if(d) {
Expand All @@ -61,7 +55,51 @@ FileImplPtr VFSImpl::open(const char* fpath, const char* mode)
return std::make_shared<VFSFileImpl>(this, fpath, mode);
}

log_e("%s does not exist", temp);
//file not found but mode permits file creation without folder creation
if((mode && mode[0] != 'r') && (!create)){
free(temp);
return std::make_shared<VFSFileImpl>(this, fpath, mode);
}

////file not found but mode permits file creation and folder creation
if((mode && mode[0] != 'r') && create){

char *token;
char *folder = (char *)malloc(strlen(fpath));

int start_index = 0;
int end_index = 0;

token = strchr(fpath+1,'/');
end_index = (token-fpath);

while (token != NULL)
{
memcpy(folder,fpath + start_index, end_index-start_index);
folder[end_index-start_index] = '\0';

if(!VFSImpl::mkdir(folder))
{
log_e("Creating folder: %s failed!",folder);
return FileImplPtr();
}

token=strchr(token+1,'/');
if(token != NULL)
{
end_index = (token-fpath);
memset(folder, 0, strlen(folder));
}

}

free(folder);
free(temp);
return std::make_shared<VFSFileImpl>(this, fpath, mode);

}

log_e("%s does not exist, no permits for creation", temp);
free(temp);
return FileImplPtr();
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/FS/src/vfs_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class VFSImpl : public FSImpl
friend class VFSFileImpl;

public:
FileImplPtr open(const char* path, const char* mode) override;
FileImplPtr open(const char* path, const char* mode, const bool create) override;
bool exists(const char* path) override;
bool rename(const char* pathFrom, const char* pathTo) override;
bool remove(const char* path) override;
Expand Down
2 changes: 1 addition & 1 deletion libraries/LittleFS/src/LittleFS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ LittleFSImpl::LittleFSImpl()

bool LittleFSImpl::exists(const char* path)
{
File f = open(path, "r");
File f = open(path, "r",false);
return (f == true);
}

Expand Down
2 changes: 1 addition & 1 deletion libraries/SPIFFS/src/SPIFFS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ SPIFFSImpl::SPIFFSImpl()

bool SPIFFSImpl::exists(const char* path)
{
File f = open(path, "r");
File f = open(path, "r",false);
return (f == true) && !f.isDirectory();
}

Expand Down