@@ -63,7 +63,7 @@ def __init__(self, *args, **kwargs):
63
63
"is_dir TINYINT NOT NULL"
64
64
")"
65
65
)
66
- self ._index_dir_recursively ( self . root_dir )
66
+ self ._index_all ( )
67
67
self .con .execute ("CREATE INDEX IF NOT EXISTS ix_Files_path ON Files (path)" )
68
68
self .con .execute ("CREATE INDEX IF NOT EXISTS ix_Files_ino ON Files (ino)" )
69
69
self .con .execute ("CREATE INDEX IF NOT EXISTS ix_Files_is_dir ON Files (is_dir)" )
@@ -77,15 +77,18 @@ def _validate_abspath_traits(self, proposal):
77
77
raise TraitError ("FileIdManager : %s must be an absolute path" % proposal ["trait" ].name )
78
78
return self ._normalize_path (proposal ["value" ])
79
79
80
- def _index_dir_recursively (self , dir_path ):
81
- """Recursively indexes all directories under a given path. Used in
82
- __init__() to recursively index all directories under the server
83
- root."""
84
- self .index (dir_path )
80
+ def _index_all (self ):
81
+ """Recursively indexes all directories under the server root."""
82
+ self ._index_dir_recursively (self .root_dir , self ._stat (self .root_dir ))
83
+
84
+ def _index_dir_recursively (self , dir_path , stat_info ):
85
+ """Recursively indexes all directories under a given path."""
86
+ self .index (dir_path , stat_info = stat_info , commit = False )
87
+
85
88
with os .scandir (dir_path ) as scan_iter :
86
89
for entry in scan_iter :
87
90
if entry .is_dir ():
88
- self ._index_dir_recursively (entry .path )
91
+ self ._index_dir_recursively (entry .path , self . _parse_raw_stat ( entry . stat ()) )
89
92
scan_iter .close ()
90
93
91
94
def _sync_all (self ):
@@ -183,12 +186,11 @@ def _parse_raw_stat(self, raw_stat, stat_info=None):
183
186
184
187
return stat_info
185
188
186
- def _stat (self , path , stat_info = None ):
189
+ def _stat (self , path ):
187
190
"""Returns stat info on a path in a StatStruct object. Writes to
188
191
`stat_info` StatStruct arg if passed. Returns None if file does not
189
192
exist at path."""
190
- if stat_info is None :
191
- stat_info = StatStruct ()
193
+ stat_info = StatStruct ()
192
194
193
195
try :
194
196
raw_stat = os .stat (path )
@@ -225,11 +227,11 @@ def _update(self, id, stat_info, path=None):
225
227
(stat_info .ino , stat_info .crtime , stat_info .mtime , id ),
226
228
)
227
229
228
- def index (self , path ):
230
+ def index (self , path , stat_info = None , commit = True ):
229
231
"""Returns the file ID for the file at `path`, creating a new file ID if
230
232
one does not exist. Returns None only if file does not exist at path."""
231
233
path = self ._normalize_path (path )
232
- stat_info = self ._stat (path )
234
+ stat_info = stat_info or self ._stat (path )
233
235
if not stat_info :
234
236
return None
235
237
@@ -240,7 +242,8 @@ def index(self, path):
240
242
241
243
# otherwise, create a new record and return the file ID
242
244
id = self ._create (path , stat_info )
243
- self .con .commit ()
245
+ if commit :
246
+ self .con .commit ()
244
247
return id
245
248
246
249
def get_id (self , path ):
0 commit comments