Skip to content

Commit 72dd589

Browse files
earlephilhowerd-a-v
authored andcommitted
Add time to filesystem API (#6544)
* Add time to filesystem API Support the ESP32 File::getLastWrite() call and setting the time on all filesystems automatically (assuming the system clock has been set properly and time(NULL) returns the proper time!). Adds Dir::fileTime() to get the time of a file being listed, similar to Dir::fileName() and Dir::fileSize(). Adds ::setTimeCallback(time_t (*cb)()) to File, Dir, and FS, allowing users to override the default timestamp on a per-file, directory, or filesystem basis. By default, a simple callback returning time(nullptr) is implemented. LittleFS uses the 't' attribute and should be backwards compatible. SD/SDFS work and include wrappers for obsolete SdFat timestamp callbacks using the MSDOS time. This PR does not update SPIFFS, due to compatability concerns and a possible massive rewrite which would make it possible to determine if an old-style ot metadata enabled FS is present at mount time. Includes an updated SD/listfiles and LittleFS_time example. Replaces #6315 * Add links to new mklittlefs w/timestamp support Include the update mklittlefs which generated 't' metadata on imported files. ../tools/sdk/lwip2/include/netif/lowpan6_opts.h * Add explicit note about timestamp being local time * Address review concerns Clean up some awkward object instantiations. Remove the _enableTime flag/setter from SPIFFS. Clean up the FSConfig constructors using C++ style init lists.
1 parent b4c28e7 commit 72dd589

File tree

12 files changed

+557
-95
lines changed

12 files changed

+557
-95
lines changed

cores/esp8266/FS.cpp

+47-3
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,19 @@ String File::readString()
180180
return ret;
181181
}
182182

183+
time_t File::getLastWrite() {
184+
if (!_p)
185+
return 0;
186+
187+
return _p->getLastWrite();
188+
}
189+
190+
void File::setTimeCallback(time_t (*cb)(void)) {
191+
if (!_p)
192+
return;
193+
_p->setTimeCallback(cb);
194+
}
195+
183196
File Dir::openFile(const char* mode) {
184197
if (!_impl) {
185198
return File();
@@ -192,7 +205,9 @@ File Dir::openFile(const char* mode) {
192205
return File();
193206
}
194207

195-
return File(_impl->openFile(om, am), _baseFS);
208+
File f(_impl->openFile(om, am), _baseFS);
209+
f.setTimeCallback(timeCallback);
210+
return f;
196211
}
197212

198213
String Dir::fileName() {
@@ -203,6 +218,12 @@ String Dir::fileName() {
203218
return _impl->fileName();
204219
}
205220

221+
time_t Dir::fileTime() {
222+
if (!_impl)
223+
return 0;
224+
return _impl->fileTime();
225+
}
226+
206227
size_t Dir::fileSize() {
207228
if (!_impl) {
208229
return 0;
@@ -241,6 +262,20 @@ bool Dir::rewind() {
241262
return _impl->rewind();
242263
}
243264

265+
time_t Dir::getLastWrite() {
266+
if (!_impl)
267+
return 0;
268+
269+
return _impl->getLastWrite();
270+
}
271+
272+
void Dir::setTimeCallback(time_t (*cb)(void)) {
273+
if (!_impl)
274+
return;
275+
_impl->setTimeCallback(cb);
276+
}
277+
278+
244279
bool FS::setConfig(const FSConfig &cfg) {
245280
if (!_impl) {
246281
return false;
@@ -315,7 +350,9 @@ File FS::open(const char* path, const char* mode) {
315350
DEBUGV("FS::open: invalid mode `%s`\r\n", mode);
316351
return File();
317352
}
318-
return File(_impl->open(path, om, am), this);
353+
File f(_impl->open(path, om, am), this);
354+
f.setTimeCallback(timeCallback);
355+
return f;
319356
}
320357

321358
bool FS::exists(const char* path) {
@@ -334,7 +371,9 @@ Dir FS::openDir(const char* path) {
334371
return Dir();
335372
}
336373
DirImplPtr p = _impl->openDir(path);
337-
return Dir(p, this);
374+
Dir d(p, this);
375+
d.setTimeCallback(timeCallback);
376+
return d;
338377
}
339378

340379
Dir FS::openDir(const String& path) {
@@ -385,6 +424,11 @@ bool FS::rename(const String& pathFrom, const String& pathTo) {
385424
return rename(pathFrom.c_str(), pathTo.c_str());
386425
}
387426

427+
void FS::setTimeCallback(time_t (*cb)(void)) {
428+
if (!_impl)
429+
return;
430+
_impl->setTimeCallback(cb);
431+
}
388432

389433

390434
static bool sflags(const char* mode, OpenMode& om, AccessMode& am) {

cores/esp8266/FS.h

+23-11
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,16 @@ class File : public Stream
110110

111111
String readString() override;
112112

113+
time_t getLastWrite();
114+
void setTimeCallback(time_t (*cb)(void));
115+
113116
protected:
114117
FileImplPtr _p;
115118

116119
// Arduino SD class emulation
117120
std::shared_ptr<Dir> _fakeDir;
118121
FS *_baseFS;
122+
time_t (*timeCallback)(void) = nullptr;
119123
};
120124

121125
class Dir {
@@ -126,15 +130,21 @@ class Dir {
126130

127131
String fileName();
128132
size_t fileSize();
133+
time_t fileTime();
129134
bool isFile() const;
130135
bool isDirectory() const;
131136

132137
bool next();
133138
bool rewind();
134139

140+
time_t getLastWrite();
141+
void setTimeCallback(time_t (*cb)(void));
142+
135143
protected:
136144
DirImplPtr _impl;
137145
FS *_baseFS;
146+
time_t (*timeCallback)(void) = nullptr;
147+
138148
};
139149

140150
// Backwards compatible, <4GB filesystem usage
@@ -161,12 +171,10 @@ struct FSInfo64 {
161171
class FSConfig
162172
{
163173
public:
164-
FSConfig(bool autoFormat = true) {
165-
_type = FSConfig::fsid::FSId;
166-
_autoFormat = autoFormat;
167-
}
174+
static constexpr uint32_t FSId = 0x00000000;
175+
176+
FSConfig(uint32_t type = FSId, bool autoFormat = true) : _type(type), _autoFormat(autoFormat) { }
168177

169-
enum fsid { FSId = 0x00000000 };
170178
FSConfig setAutoFormat(bool val = true) {
171179
_autoFormat = val;
172180
return *this;
@@ -179,17 +187,17 @@ class FSConfig
179187
class SPIFFSConfig : public FSConfig
180188
{
181189
public:
182-
SPIFFSConfig(bool autoFormat = true) {
183-
_type = SPIFFSConfig::fsid::FSId;
184-
_autoFormat = autoFormat;
185-
}
186-
enum fsid { FSId = 0x53504946 };
190+
static constexpr uint32_t FSId = 0x53504946;
191+
SPIFFSConfig(bool autoFormat = true) : FSConfig(FSId, autoFormat) { }
192+
193+
// Inherit _type and _autoFormat
194+
// nothing yet, enableTime TBD when SPIFFS has metadate
187195
};
188196

189197
class FS
190198
{
191199
public:
192-
FS(FSImplPtr impl) : _impl(impl) { }
200+
FS(FSImplPtr impl) : _impl(impl) { timeCallback = _defaultTimeCB; }
193201

194202
bool setConfig(const FSConfig &cfg);
195203

@@ -225,10 +233,14 @@ class FS
225233
bool gc();
226234
bool check();
227235

236+
void setTimeCallback(time_t (*cb)(void));
237+
228238
friend class ::SDClass; // More of a frenemy, but SD needs internal implementation to get private FAT bits
229239
protected:
230240
FSImplPtr _impl;
231241
FSImplPtr getImpl() { return _impl; }
242+
time_t (*timeCallback)(void);
243+
static time_t _defaultTimeCB(void) { return time(NULL); }
232244
};
233245

234246
} // namespace fs

cores/esp8266/FSImpl.h

+35
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ class FileImpl {
4141
virtual const char* fullName() const = 0;
4242
virtual bool isFile() const = 0;
4343
virtual bool isDirectory() const = 0;
44+
45+
// Filesystems *may* support a timestamp per-file, so allow the user to override with
46+
// their own callback for *this specific* file (as opposed to the FSImpl call of the
47+
// same name. The default implementation simply returns time(&null)
48+
virtual void setTimeCallback(time_t (*cb)(void)) { timeCallback = cb; }
49+
50+
// Return the last written time for a file. Undefined when called on a writable file
51+
// as the FS is allowed to return either the time of the last write() operation or the
52+
// time present in the filesystem metadata (often the last time the file was closed)
53+
virtual time_t getLastWrite() { return 0; } // Default is to not support timestamps
54+
55+
protected:
56+
time_t (*timeCallback)(void) = nullptr;
4457
};
4558

4659
enum OpenMode {
@@ -62,10 +75,24 @@ class DirImpl {
6275
virtual FileImplPtr openFile(OpenMode openMode, AccessMode accessMode) = 0;
6376
virtual const char* fileName() = 0;
6477
virtual size_t fileSize() = 0;
78+
virtual time_t fileTime() { return 0; } // By default, FS doesn't report file times
6579
virtual bool isFile() const = 0;
6680
virtual bool isDirectory() const = 0;
6781
virtual bool next() = 0;
6882
virtual bool rewind() = 0;
83+
84+
// Filesystems *may* support a timestamp per-file, so allow the user to override with
85+
// their own callback for *this specific* file (as opposed to the FSImpl call of the
86+
// same name. The default implementation simply returns time(&null)
87+
virtual void setTimeCallback(time_t (*cb)(void)) { timeCallback = cb; }
88+
89+
// Return the last written time for a file. Undefined when called on a writable file
90+
// as the FS is allowed to return either the time of the last write() operation or the
91+
// time present in the filesystem metadata (often the last time the file was closed)
92+
virtual time_t getLastWrite() { return 0; } // Default is to not support timestamps
93+
94+
protected:
95+
time_t (*timeCallback)(void) = nullptr;
6996
};
7097

7198
class FSImpl {
@@ -86,6 +113,14 @@ class FSImpl {
86113
virtual bool rmdir(const char* path) = 0;
87114
virtual bool gc() { return true; } // May not be implemented in all file systems.
88115
virtual bool check() { return true; } // May not be implemented in all file systems.
116+
117+
// Filesystems *may* support a timestamp per-file, so allow the user to override with
118+
// their own callback for all files on this FS. The default implementation simply
119+
// returns the present time as reported by time(&null)
120+
virtual void setTimeCallback(time_t (*cb)(void)) { timeCallback = cb; }
121+
122+
protected:
123+
time_t (*timeCallback)(void) = nullptr;
89124
};
90125

91126
} // namespace fs

cores/esp8266/spiffs_api.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class SPIFFSImpl : public FSImpl
162162

163163
bool setConfig(const FSConfig &cfg) override
164164
{
165-
if ((cfg._type != SPIFFSConfig::fsid::FSId) || (SPIFFS_mounted(&_fs) != 0)) {
165+
if ((cfg._type != SPIFFSConfig::FSId) || (SPIFFS_mounted(&_fs) != 0)) {
166166
return false;
167167
}
168168
_cfg = *static_cast<const SPIFFSConfig *>(&cfg);

0 commit comments

Comments
 (0)