@@ -323,7 +323,7 @@ class LittleFSImpl : public FSImpl
323
323
class LittleFSFileImpl : public FileImpl
324
324
{
325
325
public:
326
- LittleFSFileImpl (LittleFSImpl* fs, const char *name, std::shared_ptr<lfs_file_t > fd, int flags) : _fs(fs), _fd(fd), _opened(true ), _flags(flags) {
326
+ LittleFSFileImpl (LittleFSImpl* fs, const char *name, std::shared_ptr<lfs_file_t > fd, int flags, time_t creation ) : _fs(fs), _fd(fd), _opened(true ), _flags(flags), _creation(creation ) {
327
327
_name = std::shared_ptr<char >(new char [strlen (name) + 1 ], std::default_delete<char []>());
328
328
strcpy (_name.get (), name);
329
329
}
@@ -420,12 +420,19 @@ class LittleFSFileImpl : public FileImpl
420
420
_opened = false ;
421
421
DEBUGV (" lfs_file_close: fd=%p\n " , _getFD ());
422
422
if (timeCallback && (_flags & LFS_O_WRONLY)) {
423
+ // If the file opened with O_CREAT, write the creation time attribute
424
+ if (_creation) {
425
+ int rc = lfs_setattr (_fs->getFS (), _name.get (), ' c' , (const void *)&_creation, sizeof (_creation));
426
+ if (rc < 0 ) {
427
+ DEBUGV (" Unable to set creation time on '%s' to %d\n " , _name.get (), _creation);
428
+ }
429
+ }
423
430
// Add metadata with last write time
424
431
time_t now = timeCallback ();
425
432
int rc = lfs_setattr (_fs->getFS (), _name.get (), ' t' , (const void *)&now, sizeof (now));
426
433
if (rc < 0 ) {
427
- DEBUGV (" Unable to set time on '%s' to %d\n " , _name.get (), now);
428
- }
434
+ DEBUGV (" Unable to set last write time on '%s' to %d\n " , _name.get (), now);
435
+ }
429
436
}
430
437
}
431
438
}
@@ -440,6 +447,16 @@ class LittleFSFileImpl : public FileImpl
440
447
return ftime;
441
448
}
442
449
450
+ time_t getCreation () override {
451
+ time_t ftime = 0 ;
452
+ if (_opened && _fd) {
453
+ int rc = lfs_getattr (_fs->getFS (), _name.get (), ' c' , (void *)&ftime, sizeof (ftime));
454
+ if (rc != sizeof (ftime))
455
+ ftime = 0 ; // Error, so clear read value
456
+ }
457
+ return ftime;
458
+ }
459
+
443
460
const char * name () const override {
444
461
if (!_opened) {
445
462
return nullptr ;
@@ -484,6 +501,7 @@ class LittleFSFileImpl : public FileImpl
484
501
std::shared_ptr<char > _name;
485
502
bool _opened;
486
503
int _flags;
504
+ time_t _creation;
487
505
};
488
506
489
507
class LittleFSDirImpl : public DirImpl
@@ -537,23 +555,11 @@ class LittleFSDirImpl : public DirImpl
537
555
}
538
556
539
557
time_t fileTime () override {
540
- if (!_valid) {
541
- return 0 ;
542
- }
543
- int nameLen = 3 ; // Slashes, terminator
544
- nameLen += _dirPath.get () ? strlen (_dirPath.get ()) : 0 ;
545
- nameLen += strlen (_dirent.name );
546
- char *tmpName = (char *)malloc (nameLen);
547
- if (!tmpName) {
548
- return 0 ;
549
- }
550
- snprintf (tmpName, nameLen, " %s%s%s" , _dirPath.get () ? _dirPath.get () : " " , _dirPath.get ()&&_dirPath.get ()[0 ]?" /" :" " , _dirent.name );
551
- time_t ftime = 0 ;
552
- int rc = lfs_getattr (_fs->getFS (), tmpName, ' t' , (void *)&ftime, sizeof (ftime));
553
- if (rc != sizeof (ftime))
554
- ftime = 0 ; // Error, so clear read value
555
- free (tmpName);
556
- return ftime;
558
+ return (time_t )_getAttr4 (' t' );
559
+ }
560
+
561
+ time_t fileCreation () override {
562
+ return (time_t )_getAttr4 (' c' );
557
563
}
558
564
559
565
@@ -592,6 +598,26 @@ class LittleFSDirImpl : public DirImpl
592
598
return _dir.get ();
593
599
}
594
600
601
+ uint32_t _getAttr4 (char attr) {
602
+ if (!_valid) {
603
+ return 0 ;
604
+ }
605
+ int nameLen = 3 ; // Slashes, terminator
606
+ nameLen += _dirPath.get () ? strlen (_dirPath.get ()) : 0 ;
607
+ nameLen += strlen (_dirent.name );
608
+ char *tmpName = (char *)malloc (nameLen);
609
+ if (!tmpName) {
610
+ return 0 ;
611
+ }
612
+ snprintf (tmpName, nameLen, " %s%s%s" , _dirPath.get () ? _dirPath.get () : " " , _dirPath.get ()&&_dirPath.get ()[0 ]?" /" :" " , _dirent.name );
613
+ time_t ftime = 0 ;
614
+ int rc = lfs_getattr (_fs->getFS (), tmpName, attr, (void *)&ftime, sizeof (ftime));
615
+ if (rc != sizeof (ftime))
616
+ ftime = 0 ; // Error, so clear read value
617
+ free (tmpName);
618
+ return ftime;
619
+ }
620
+
595
621
String _pattern;
596
622
LittleFSImpl *_fs;
597
623
std::shared_ptr<lfs_dir_t > _dir;
0 commit comments