1- import 'dart:io' ;
2-
1+ import 'package:files/backend/fs.dart' as fs;
32import 'package:files/backend/providers.dart' ;
43import 'package:flutter/foundation.dart' ;
54import 'package:isar_community/isar.dart' ;
65
76part 'model.g.dart' ;
87
8+ typedef _FileInfoSnapshot = ({
9+ DateTime changed,
10+ DateTime modified,
11+ DateTime accessed,
12+ EntityType type,
13+ int mode,
14+ int size,
15+ });
16+
17+ _FileInfoSnapshot _getSnapshotForInfo (fs.FileInfo info) {
18+ final attributes = info.listAttributes ()! ;
19+
20+ assert (attributes.contains ('standard::type' ));
21+ // assert(attributes.contains('time::modified'));
22+ // assert(attributes.contains('time::access'));
23+ // assert(attributes.contains('time::created'));
24+ assert (attributes.contains ('standard::size' ));
25+
26+ final defaultTime = DateTime .fromMillisecondsSinceEpoch (0 );
27+
28+ return (
29+ changed: info.getCreationTime () ?? defaultTime,
30+ modified: info.getModificationTime () ?? defaultTime,
31+ accessed: info.getAccessTime () ?? defaultTime,
32+ type: switch (info.getFileType ()) {
33+ 1 || 4 || 5 => EntityType .file,
34+ 2 || 6 => EntityType .directory,
35+ 3 => EntityType .link,
36+ final t => throw ArgumentError .value (t),
37+ },
38+ mode: 0 , // TODO
39+ size: info.getSize (),
40+ );
41+ }
42+
943@Collection ()
1044class EntityStat with ChangeNotifier {
1145 EntityStat ();
1246
1347 EntityStat .fastInit ({
1448 required this .path,
49+ required this .info,
1550 required this .changed,
1651 required this .modified,
1752 required this .accessed,
@@ -20,21 +55,29 @@ class EntityStat with ChangeNotifier {
2055 required this .size,
2156 });
2257
23- EntityStat .fromStat (String path, FileStat stat)
24- : this .fastInit (
25- path: path,
26- changed: stat.changed,
27- modified: stat.modified,
28- accessed: stat.accessed,
29- type: EntityType .fromDartIo (stat.type),
30- mode: stat.mode,
31- size: stat.size,
32- );
58+ factory EntityStat .fromFileInfo (String path, fs.FileInfo info) {
59+ final snapshot = _getSnapshotForInfo (info);
60+
61+ return EntityStat .fastInit (
62+ path: path,
63+ info: info,
64+ changed: snapshot.changed,
65+ modified: snapshot.modified,
66+ accessed: snapshot.accessed,
67+ type: snapshot.type,
68+ mode: snapshot.mode,
69+ size: snapshot.size,
70+ );
71+ }
72+
3373 Id ? id;
3474
3575 @Index (unique: true , type: IndexType .hash)
3676 late String path;
3777
78+ @Ignore ()
79+ late fs.FileInfo info;
80+
3881 late DateTime changed;
3982 late DateTime modified;
4083 late DateTime accessed;
@@ -45,60 +88,30 @@ class EntityStat with ChangeNotifier {
4588 late int size;
4689
4790 Future <void > fetchUpdate () async {
48- final ioStat = await FileStat .stat (path);
49-
50- if (! _statIdentical (ioStat)) {
51- changed = ioStat.changed;
52- modified = ioStat.modified;
53- accessed = ioStat.accessed;
54- type = EntityType .fromDartIo (ioStat.type);
55- mode = ioStat.mode;
56- size = ioStat.size;
91+ final file = fs.File .fromPath (path);
92+ final info = await file.queryInfo ().result;
93+ final snapshot = _getSnapshotForInfo (info);
94+
95+ if (! _infoIdentical (snapshot)) {
96+ changed = snapshot.changed;
97+ modified = snapshot.modified;
98+ accessed = snapshot.accessed;
99+ type = snapshot.type;
100+ mode = snapshot.mode;
101+ size = snapshot.size;
57102 await helper.set (this );
58103 notifyListeners ();
59104 }
60105 }
61106
62- bool _statIdentical ( FileStat other) {
107+ bool _infoIdentical ( _FileInfoSnapshot other) {
63108 return changed == other.changed &&
64109 modified == other.modified &&
65110 accessed == other.accessed &&
66- type == EntityType . fromDartIo ( other.type) &&
111+ type == other.type &&
67112 mode == other.mode &&
68113 size == other.size;
69114 }
70115}
71116
72- enum EntityType {
73- file,
74- directory,
75- link,
76- notFound;
77-
78- static EntityType fromDartIo (FileSystemEntityType type) {
79- switch (type) {
80- case FileSystemEntityType .file:
81- return EntityType .file;
82- case FileSystemEntityType .directory:
83- return EntityType .directory;
84- case FileSystemEntityType .link:
85- return EntityType .link;
86- case FileSystemEntityType .notFound:
87- default :
88- return EntityType .notFound;
89- }
90- }
91-
92- FileSystemEntityType get toDartIo {
93- switch (this ) {
94- case EntityType .file:
95- return FileSystemEntityType .file;
96- case EntityType .directory:
97- return FileSystemEntityType .directory;
98- case EntityType .link:
99- return FileSystemEntityType .link;
100- case EntityType .notFound:
101- return FileSystemEntityType .notFound;
102- }
103- }
104- }
117+ enum EntityType { file, directory, link, notFound }
0 commit comments