Skip to content

Commit 69bd9e6

Browse files
Merge pull request #4 from earlephilhower/addtime
Add time to filesystem and mkfs utility Fixes #3. Adds a 't' metadata attribute to all files added, and when listing or extracting restore according to the 't' attribute, if present. Compatible w/o an w/ esp8266/Arduino#6315 since LittleFS always has metadata support (and can silently ignore them if present but not used).
2 parents 7f77f2b + f48909b commit 69bd9e6

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

main.cpp

+25-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <dirent.h>
1515
#include <sys/types.h>
1616
#include <sys/stat.h>
17+
#include <utime.h>
1718
#include <unistd.h>
1819
#include <cstring>
1920
#include <string>
@@ -209,6 +210,12 @@ int addFile(char* name, const char* path) {
209210
lfs_file_close(&s_fs, &dst);
210211
fclose(src);
211212

213+
// Add time metadata 't'
214+
struct stat sbuf;
215+
if (!stat(path, &sbuf)) {
216+
uint32_t ftime = sbuf.st_mtime;
217+
lfs_setattr(&s_fs, name, 't', (const void *)&ftime, sizeof(ftime));
218+
}
212219
return 0;
213220
}
214221

@@ -321,7 +328,16 @@ void listFiles(const char *path) {
321328
sprintf(newpath, "%s/%s", path, it.name);
322329
listFiles(newpath);
323330
} else {
324-
std::cout << it.size << '\t' << path << "/" << it.name << std::endl;
331+
uint32_t ftime;
332+
time_t t;
333+
char buff[PATH_MAX];
334+
snprintf(buff, sizeof(buff), "%s/%s", path, it.name);
335+
if (lfs_getattr(&s_fs, buff, 't', (uint8_t *)&ftime, sizeof(ftime)) >= 0) {
336+
t = (time_t)ftime;
337+
std::cout << it.size << '\t' << path << "/" << it.name << '\t' << asctime(gmtime(&t));
338+
} else {
339+
std::cout << it.size << '\t' << path << "/" << it.name << std::endl;
340+
}
325341
}
326342
}
327343
lfs_dir_close(&s_fs, &dir);
@@ -407,6 +423,14 @@ bool unpackFile(const char *lfsDir, lfs_info *littlefsFile, const char *destPath
407423
// Close file.
408424
fclose(dst);
409425

426+
// Adjust time, if present
427+
uint32_t ftime;
428+
if (lfs_getattr(&s_fs, (char *)(filename.c_str()), 't', (uint8_t *)&ftime, sizeof(ftime)) >= 0) {
429+
struct utimbuf ut;
430+
ut.actime = ftime;
431+
ut.modtime = ftime;
432+
utime(destPath, &ut);
433+
}
410434

411435
return true;
412436
}

0 commit comments

Comments
 (0)