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' ;
5- import 'package:isar/isar.dart' ;
6-
7- export 'package:isar/isar.dart' ;
4+ import 'package:isar_community/isar.dart' ;
85
96part 'model.g.dart' ;
107
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+
1143@Collection ()
1244class EntityStat with ChangeNotifier {
1345 EntityStat ();
1446
1547 EntityStat .fastInit ({
1648 required this .path,
49+ required this .info,
1750 required this .changed,
1851 required this .modified,
1952 required this .accessed,
@@ -22,21 +55,29 @@ class EntityStat with ChangeNotifier {
2255 required this .size,
2356 });
2457
25- EntityStat .fromStat (String path, FileStat stat)
26- : this .fastInit (
27- path: path,
28- changed: stat.changed,
29- modified: stat.modified,
30- accessed: stat.accessed,
31- type: EntityType .fromDartIo (stat.type),
32- mode: stat.mode,
33- size: stat.size,
34- );
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+
3573 Id ? id;
3674
3775 @Index (unique: true , type: IndexType .hash)
3876 late String path;
3977
78+ @Ignore ()
79+ late fs.FileInfo info;
80+
4081 late DateTime changed;
4182 late DateTime modified;
4283 late DateTime accessed;
@@ -47,60 +88,30 @@ class EntityStat with ChangeNotifier {
4788 late int size;
4889
4990 Future <void > fetchUpdate () async {
50- final ioStat = await FileStat .stat (path);
51-
52- if (! _statIdentical (ioStat)) {
53- changed = ioStat.changed;
54- modified = ioStat.modified;
55- accessed = ioStat.accessed;
56- type = EntityType .fromDartIo (ioStat.type);
57- mode = ioStat.mode;
58- 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;
59102 await helper.set (this );
60103 notifyListeners ();
61104 }
62105 }
63106
64- bool _statIdentical ( FileStat other) {
107+ bool _infoIdentical ( _FileInfoSnapshot other) {
65108 return changed == other.changed &&
66109 modified == other.modified &&
67110 accessed == other.accessed &&
68- type == EntityType . fromDartIo ( other.type) &&
111+ type == other.type &&
69112 mode == other.mode &&
70113 size == other.size;
71114 }
72115}
73116
74- enum EntityType {
75- file,
76- directory,
77- link,
78- notFound;
79-
80- static EntityType fromDartIo (FileSystemEntityType type) {
81- switch (type) {
82- case FileSystemEntityType .file:
83- return EntityType .file;
84- case FileSystemEntityType .directory:
85- return EntityType .directory;
86- case FileSystemEntityType .link:
87- return EntityType .link;
88- case FileSystemEntityType .notFound:
89- default :
90- return EntityType .notFound;
91- }
92- }
93-
94- FileSystemEntityType get toDartIo {
95- switch (this ) {
96- case EntityType .file:
97- return FileSystemEntityType .file;
98- case EntityType .directory:
99- return FileSystemEntityType .directory;
100- case EntityType .link:
101- return FileSystemEntityType .link;
102- case EntityType .notFound:
103- return FileSystemEntityType .notFound;
104- }
105- }
106- }
117+ enum EntityType { file, directory, link, notFound }
0 commit comments