|
14 | 14 | #include <dirent.h>
|
15 | 15 | #include <sys/types.h>
|
16 | 16 | #include <sys/stat.h>
|
| 17 | +#include <utime.h> |
17 | 18 | #include <unistd.h>
|
18 | 19 | #include <cstring>
|
19 | 20 | #include <string>
|
@@ -209,6 +210,12 @@ int addFile(char* name, const char* path) {
|
209 | 210 | lfs_file_close(&s_fs, &dst);
|
210 | 211 | fclose(src);
|
211 | 212 |
|
| 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 | + } |
212 | 219 | return 0;
|
213 | 220 | }
|
214 | 221 |
|
@@ -321,7 +328,16 @@ void listFiles(const char *path) {
|
321 | 328 | sprintf(newpath, "%s/%s", path, it.name);
|
322 | 329 | listFiles(newpath);
|
323 | 330 | } 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 | + } |
325 | 341 | }
|
326 | 342 | }
|
327 | 343 | lfs_dir_close(&s_fs, &dir);
|
@@ -407,6 +423,14 @@ bool unpackFile(const char *lfsDir, lfs_info *littlefsFile, const char *destPath
|
407 | 423 | // Close file.
|
408 | 424 | fclose(dst);
|
409 | 425 |
|
| 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 | + } |
410 | 434 |
|
411 | 435 | return true;
|
412 | 436 | }
|
|
0 commit comments