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 b6f0ba8

Browse filesBrowse files
committed
FS::name() returns the item name as in Arduino SD
Added method FS::path() that returns the full path
1 parent 7a6900a commit b6f0ba8
Copy full SHA for b6f0ba8

File tree

Expand file treeCollapse file tree

5 files changed

+51
-34
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+51
-34
lines changed

‎libraries/FS/src/FS.cpp

Copy file name to clipboardExpand all lines: libraries/FS/src/FS.cpp
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ File::operator bool() const
143143
return _p != nullptr && *_p != false;
144144
}
145145

146+
const char* File::path() const
147+
{
148+
if (!*this) {
149+
return nullptr;
150+
}
151+
152+
return _p->path();
153+
}
154+
146155
const char* File::name() const
147156
{
148157
if (!*this) {

‎libraries/FS/src/FS.h

Copy file name to clipboardExpand all lines: libraries/FS/src/FS.h
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class File : public Stream
7373
void close();
7474
operator bool() const;
7575
time_t getLastWrite();
76+
const char* path() const;
7677
const char* name() const;
7778

7879
boolean isDirectory(void);

‎libraries/FS/src/FSImpl.h

Copy file name to clipboardExpand all lines: libraries/FS/src/FSImpl.h
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class FileImpl
3838
virtual size_t size() const = 0;
3939
virtual void close() = 0;
4040
virtual time_t getLastWrite() = 0;
41+
virtual const char* path() const = 0;
4142
virtual const char* name() const = 0;
4243
virtual boolean isDirectory(void) = 0;
4344
virtual FileImplPtr openNextFile(const char* mode) = 0;

‎libraries/FS/src/vfs_api.cpp

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

1717
using namespace fs;
1818

19-
FileImplPtr VFSImpl::open(const char* path, const char* mode)
19+
FileImplPtr VFSImpl::open(const char* fpath, const char* mode)
2020
{
2121
if(!_mountpoint) {
2222
log_e("File system is not mounted");
2323
return FileImplPtr();
2424
}
2525

26-
if(!path || path[0] != '/') {
27-
log_e("%s does not start with /", path);
26+
if(!fpath || fpath[0] != '/') {
27+
log_e("%s does not start with /", fpath);
2828
return FileImplPtr();
2929
}
3030

31-
char * temp = (char *)malloc(strlen(path)+strlen(_mountpoint)+2);
31+
char * temp = (char *)malloc(strlen(fpath)+strlen(_mountpoint)+2);
3232
if(!temp) {
3333
log_e("malloc failed");
3434
return FileImplPtr();
3535
}
3636

37-
sprintf(temp,"%s%s", _mountpoint, path);
37+
sprintf(temp,"%s%s", _mountpoint, fpath);
3838

3939
struct stat st;
4040
//file lound
4141
if(!stat(temp, &st)) {
4242
free(temp);
4343
if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode)) {
44-
return std::make_shared<VFSFileImpl>(this, path, mode);
44+
return std::make_shared<VFSFileImpl>(this, fpath, mode);
4545
}
46-
log_e("%s has wrong mode 0x%08X", path, st.st_mode);
46+
log_e("%s has wrong mode 0x%08X", fpath, st.st_mode);
4747
return FileImplPtr();
4848
}
4949

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

5656
//try to open this as directory (might be mount point)
5757
DIR * d = opendir(temp);
5858
if(d) {
5959
closedir(d);
6060
free(temp);
61-
return std::make_shared<VFSFileImpl>(this, path, mode);
61+
return std::make_shared<VFSFileImpl>(this, fpath, mode);
6262
}
6363

6464
log_e("%s does not exist", temp);
6565
free(temp);
6666
return FileImplPtr();
6767
}
6868

69-
bool VFSImpl::exists(const char* path)
69+
bool VFSImpl::exists(const char* fpath)
7070
{
7171
if(!_mountpoint) {
7272
log_e("File system is not mounted");
7373
return false;
7474
}
7575

76-
VFSFileImpl f(this, path, "r");
76+
VFSFileImpl f(this, fpath, "r");
7777
if(f) {
7878
f.close();
7979
return true;
@@ -115,69 +115,69 @@ bool VFSImpl::rename(const char* pathFrom, const char* pathTo)
115115
return rc == 0;
116116
}
117117

118-
bool VFSImpl::remove(const char* path)
118+
bool VFSImpl::remove(const char* fpath)
119119
{
120120
if(!_mountpoint) {
121121
log_e("File system is not mounted");
122122
return false;
123123
}
124124

125-
if(!path || path[0] != '/') {
125+
if(!fpath || fpath[0] != '/') {
126126
log_e("bad arguments");
127127
return false;
128128
}
129129

130-
VFSFileImpl f(this, path, "r");
130+
VFSFileImpl f(this, fpath, "r");
131131
if(!f || f.isDirectory()) {
132132
if(f) {
133133
f.close();
134134
}
135-
log_e("%s does not exists or is directory", path);
135+
log_e("%s does not exists or is directory", fpath);
136136
return false;
137137
}
138138
f.close();
139139

140-
char * temp = (char *)malloc(strlen(path)+strlen(_mountpoint)+1);
140+
char * temp = (char *)malloc(strlen(fpath)+strlen(_mountpoint)+1);
141141
if(!temp) {
142142
log_e("malloc failed");
143143
return false;
144144
}
145-
sprintf(temp,"%s%s", _mountpoint, path);
145+
sprintf(temp,"%s%s", _mountpoint, fpath);
146146
auto rc = unlink(temp);
147147
free(temp);
148148
return rc == 0;
149149
}
150150

151-
bool VFSImpl::mkdir(const char *path)
151+
bool VFSImpl::mkdir(const char *fpath)
152152
{
153153
if(!_mountpoint) {
154154
log_e("File system is not mounted");
155155
return false;
156156
}
157157

158-
VFSFileImpl f(this, path, "r");
158+
VFSFileImpl f(this, fpath, "r");
159159
if(f && f.isDirectory()) {
160160
f.close();
161-
//log_w("%s already exists", path);
161+
//log_w("%s already exists", fpath);
162162
return true;
163163
} else if(f) {
164164
f.close();
165-
log_e("%s is a file", path);
165+
log_e("%s is a file", fpath);
166166
return false;
167167
}
168168

169-
char * temp = (char *)malloc(strlen(path)+strlen(_mountpoint)+1);
169+
char * temp = (char *)malloc(strlen(fpath)+strlen(_mountpoint)+1);
170170
if(!temp) {
171171
log_e("malloc failed");
172172
return false;
173173
}
174-
sprintf(temp,"%s%s", _mountpoint, path);
174+
sprintf(temp,"%s%s", _mountpoint, fpath);
175175
auto rc = ::mkdir(temp, ACCESSPERMS);
176176
free(temp);
177177
return rc == 0;
178178
}
179179

180-
bool VFSImpl::rmdir(const char *path)
180+
bool VFSImpl::rmdir(const char *fpath)
181181
{
182182
if(!_mountpoint) {
183183
log_e("File system is not mounted");
@@ -189,22 +189,22 @@ bool VFSImpl::rmdir(const char *path)
189189
return false;
190190
}
191191

192-
VFSFileImpl f(this, path, "r");
192+
VFSFileImpl f(this, fpath, "r");
193193
if(!f || !f.isDirectory()) {
194194
if(f) {
195195
f.close();
196196
}
197-
log_e("%s does not exists or is a file", path);
197+
log_e("%s does not exists or is a file", fpath);
198198
return false;
199199
}
200200
f.close();
201201

202-
char * temp = (char *)malloc(strlen(path)+strlen(_mountpoint)+1);
202+
char * temp = (char *)malloc(strlen(fpath)+strlen(_mountpoint)+1);
203203
if(!temp) {
204204
log_e("malloc failed");
205205
return false;
206206
}
207-
sprintf(temp,"%s%s", _mountpoint, path);
207+
sprintf(temp,"%s%s", _mountpoint, fpath);
208208
auto rc = ::rmdir(temp);
209209
free(temp);
210210
return rc == 0;
@@ -213,23 +213,23 @@ bool VFSImpl::rmdir(const char *path)
213213

214214

215215

216-
VFSFileImpl::VFSFileImpl(VFSImpl* fs, const char* path, const char* mode)
216+
VFSFileImpl::VFSFileImpl(VFSImpl* fs, const char* fpath, const char* mode)
217217
: _fs(fs)
218218
, _f(NULL)
219219
, _d(NULL)
220220
, _path(NULL)
221221
, _isDirectory(false)
222222
, _written(false)
223223
{
224-
char * temp = (char *)malloc(strlen(path)+strlen(_fs->_mountpoint)+1);
224+
char * temp = (char *)malloc(strlen(fpath)+strlen(_fs->_mountpoint)+1);
225225
if(!temp) {
226226
return;
227227
}
228-
sprintf(temp,"%s%s", _fs->_mountpoint, path);
228+
sprintf(temp,"%s%s", _fs->_mountpoint, fpath);
229229

230-
_path = strdup(path);
230+
_path = strdup(fpath);
231231
if(!_path) {
232-
log_e("strdup(%s) failed", path);
232+
log_e("strdup(%s) failed", fpath);
233233
free(temp);
234234
return;
235235
}
@@ -377,11 +377,16 @@ size_t VFSFileImpl::size() const
377377
return _stat.st_size;
378378
}
379379

380-
const char* VFSFileImpl::name() const
380+
const char* VFSFileImpl::path() const
381381
{
382382
return (const char*) _path;
383383
}
384384

385+
const char* VFSFileImpl::name() const
386+
{
387+
return pathToFileName(path());
388+
}
389+
385390
//to implement
386391
boolean VFSFileImpl::isDirectory(void)
387392
{

‎libraries/FS/src/vfs_api.h

Copy file name to clipboardExpand all lines: libraries/FS/src/vfs_api.h
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class VFSFileImpl : public FileImpl
6666
size_t position() const override;
6767
size_t size() const override;
6868
void close() override;
69+
const char* path() const override;
6970
const char* name() const override;
7071
time_t getLastWrite() override;
7172
boolean isDirectory(void) override;

0 commit comments

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