Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f2a20e8

Browse filesBrowse files
authored
SD.open() new feature for creating all folders in path (espressif#5721)
* SD.open() new feature for creating all folders in path This PR adds to the SD.open() function option to create all folders to the file. SD.open(const char* path, const char* mode, const bool create) Default value of create is false. When true folders are created. From issue espressif#5019 * Update vfs_api.cpp memccpy -> memcpy * File f = open() edit added false for create
1 parent c5bb833 commit f2a20e8
Copy full SHA for f2a20e8

File tree

Expand file treeCollapse file tree

8 files changed

+58
-20
lines changed
Filter options
Expand file treeCollapse file tree

8 files changed

+58
-20
lines changed

‎libraries/FFat/src/FFat.cpp

Copy file name to clipboardExpand all lines: libraries/FFat/src/FFat.cpp
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ size_t F_Fat::freeBytes()
168168

169169
bool F_Fat::exists(const char* path)
170170
{
171-
File f = open(path, "r");
171+
File f = open(path, "r",false);
172172
return (f == true) && !f.isDirectory();
173173
}
174174

‎libraries/FS/src/FS.cpp

Copy file name to clipboardExpand all lines: libraries/FS/src/FS.cpp
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,18 +186,18 @@ void File::rewindDirectory(void)
186186
_p->rewindDirectory();
187187
}
188188

189-
File FS::open(const String& path, const char* mode)
189+
File FS::open(const String& path, const char* mode, const bool create)
190190
{
191-
return open(path.c_str(), mode);
191+
return open(path.c_str(), mode, create);
192192
}
193193

194-
File FS::open(const char* path, const char* mode)
194+
File FS::open(const char* path, const char* mode, const bool create)
195195
{
196196
if (!_impl) {
197197
return File();
198198
}
199199

200-
return File(_impl->open(path, mode));
200+
return File(_impl->open(path, mode, create));
201201
}
202202

203203
bool FS::exists(const char* path)

‎libraries/FS/src/FS.h

Copy file name to clipboardExpand all lines: libraries/FS/src/FS.h
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ class FS
8989
public:
9090
FS(FSImplPtr impl) : _impl(impl) { }
9191

92-
File open(const char* path, const char* mode = FILE_READ);
93-
File open(const String& path, const char* mode = FILE_READ);
92+
File open(const char* path, const char* mode = FILE_READ, const bool create = false);
93+
File open(const String& path, const char* mode = FILE_READ, const bool create = false);
9494

9595
bool exists(const char* path);
9696
bool exists(const String& path);

‎libraries/FS/src/FSImpl.h

Copy file name to clipboardExpand all lines: libraries/FS/src/FSImpl.h
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class FSImpl
5353
public:
5454
FSImpl() : _mountpoint(NULL) { }
5555
virtual ~FSImpl() { }
56-
virtual FileImplPtr open(const char* path, const char* mode) = 0;
56+
virtual FileImplPtr open(const char* path, const char* mode, const bool create) = 0;
5757
virtual bool exists(const char* path) = 0;
5858
virtual bool rename(const char* pathFrom, const char* pathTo) = 0;
5959
virtual bool remove(const char* path) = 0;

‎libraries/FS/src/vfs_api.cpp

Copy file name to clipboardExpand all lines: libraries/FS/src/vfs_api.cpp
+47-9Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
using namespace fs;
1818

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

3939
struct stat st;
40-
//file lound
40+
//file found
4141
if(!stat(temp, &st)) {
4242
free(temp);
4343
if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode)) {
@@ -47,12 +47,6 @@ FileImplPtr VFSImpl::open(const char* fpath, const char* mode)
4747
return FileImplPtr();
4848
}
4949

50-
//file not found but mode permits creation
51-
if(mode && mode[0] != 'r') {
52-
free(temp);
53-
return std::make_shared<VFSFileImpl>(this, fpath, mode);
54-
}
55-
5650
//try to open this as directory (might be mount point)
5751
DIR * d = opendir(temp);
5852
if(d) {
@@ -61,7 +55,51 @@ FileImplPtr VFSImpl::open(const char* fpath, const char* mode)
6155
return std::make_shared<VFSFileImpl>(this, fpath, mode);
6256
}
6357

64-
log_e("%s does not exist", temp);
58+
//file not found but mode permits file creation without folder creation
59+
if((mode && mode[0] != 'r') && (!create)){
60+
free(temp);
61+
return std::make_shared<VFSFileImpl>(this, fpath, mode);
62+
}
63+
64+
////file not found but mode permits file creation and folder creation
65+
if((mode && mode[0] != 'r') && create){
66+
67+
char *token;
68+
char *folder = (char *)malloc(strlen(fpath));
69+
70+
int start_index = 0;
71+
int end_index = 0;
72+
73+
token = strchr(fpath+1,'/');
74+
end_index = (token-fpath);
75+
76+
while (token != NULL)
77+
{
78+
memcpy(folder,fpath + start_index, end_index-start_index);
79+
folder[end_index-start_index] = '\0';
80+
81+
if(!VFSImpl::mkdir(folder))
82+
{
83+
log_e("Creating folder: %s failed!",folder);
84+
return FileImplPtr();
85+
}
86+
87+
token=strchr(token+1,'/');
88+
if(token != NULL)
89+
{
90+
end_index = (token-fpath);
91+
memset(folder, 0, strlen(folder));
92+
}
93+
94+
}
95+
96+
free(folder);
97+
free(temp);
98+
return std::make_shared<VFSFileImpl>(this, fpath, mode);
99+
100+
}
101+
102+
log_e("%s does not exist, no permits for creation", temp);
65103
free(temp);
66104
return FileImplPtr();
67105
}

‎libraries/FS/src/vfs_api.h

Copy file name to clipboardExpand all lines: libraries/FS/src/vfs_api.h
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class VFSImpl : public FSImpl
3535
friend class VFSFileImpl;
3636

3737
public:
38-
FileImplPtr open(const char* path, const char* mode) override;
38+
FileImplPtr open(const char* path, const char* mode, const bool create) override;
3939
bool exists(const char* path) override;
4040
bool rename(const char* pathFrom, const char* pathTo) override;
4141
bool remove(const char* path) override;

‎libraries/LittleFS/src/LittleFS.cpp

Copy file name to clipboardExpand all lines: libraries/LittleFS/src/LittleFS.cpp
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ LittleFSImpl::LittleFSImpl()
4545

4646
bool LittleFSImpl::exists(const char* path)
4747
{
48-
File f = open(path, "r");
48+
File f = open(path, "r",false);
4949
return (f == true);
5050
}
5151

‎libraries/SPIFFS/src/SPIFFS.cpp

Copy file name to clipboardExpand all lines: libraries/SPIFFS/src/SPIFFS.cpp
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ SPIFFSImpl::SPIFFSImpl()
3939

4040
bool SPIFFSImpl::exists(const char* path)
4141
{
42-
File f = open(path, "r");
42+
File f = open(path, "r",false);
4343
return (f == true) && !f.isDirectory();
4444
}
4545

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.