@@ -14,6 +14,9 @@ local async = require("plenary.async")
14
14
15
15
local M = {}
16
16
17
+ --- how many entries to load per readdir
18
+ local ENTRIES_BATCH_SIZE = 1000
19
+
17
20
local on_directory_loaded = function (context , dir_path )
18
21
local state = context .state
19
22
local scanned_folder = context .folders [dir_path ]
@@ -198,23 +201,28 @@ end
198
201
199
202
local function get_children_sync (path )
200
203
local children = {}
201
- local dir , err = uv .fs_opendir (path , nil , 1000 )
202
- if err then
204
+ local dir , err = uv .fs_opendir (path , nil , ENTRIES_BATCH_SIZE )
205
+ if not dir then
206
+ --- @cast err - nil
203
207
if is_permission_error (err ) then
204
208
log .debug (err )
205
209
else
206
210
log .error (err )
207
211
end
208
212
return children
209
213
end
210
- --- @cast dir uv.luv_dir_t
211
- local stats = uv .fs_readdir (dir )
212
- if stats then
213
- for _ , stat in ipairs (stats ) do
214
+ repeat
215
+ local stats = uv .fs_readdir (dir )
216
+ if not stats then
217
+ break
218
+ end
219
+ local more = false
220
+ for i , stat in ipairs (stats ) do
221
+ more = i == ENTRIES_BATCH_SIZE
214
222
local child_path = utils .path_join (path , stat .name )
215
223
table.insert (children , { path = child_path , type = stat .type })
216
224
end
217
- end
225
+ until not more
218
226
uv .fs_closedir (dir )
219
227
return children
220
228
end
@@ -231,17 +239,27 @@ local function get_children_async(path, callback)
231
239
callback (children )
232
240
return
233
241
end
234
- uv .fs_readdir (dir , function (_ , stats )
242
+ local readdir_batch
243
+ --- @param _ string ?
244
+ --- @param stats uv.fs_readdir.entry[]
245
+ readdir_batch = function (_ , stats )
235
246
if stats then
236
- for _ , stat in ipairs (stats ) do
247
+ local more = false
248
+ for i , stat in ipairs (stats ) do
249
+ more = i == ENTRIES_BATCH_SIZE
237
250
local child_path = utils .path_join (path , stat .name )
238
251
table.insert (children , { path = child_path , type = stat .type })
239
252
end
253
+ if more then
254
+ return uv .fs_readdir (dir , readdir_batch )
255
+ end
240
256
end
241
257
uv .fs_closedir (dir )
242
258
callback (children )
243
- end )
244
- end , 1000 )
259
+ end
260
+
261
+ uv .fs_readdir (dir , readdir_batch )
262
+ end , ENTRIES_BATCH_SIZE )
245
263
end
246
264
247
265
local function scan_dir_sync (context , path )
@@ -399,13 +417,19 @@ local function sync_scan(context, path_to_scan)
399
417
end
400
418
job_complete (context )
401
419
else -- scan_mode == "shallow"
402
- local dir , err = uv .fs_opendir (path_to_scan , nil , 1000 )
420
+ local dir , err = uv .fs_opendir (path_to_scan , nil , ENTRIES_BATCH_SIZE )
403
421
if dir then
404
422
local stats = uv .fs_readdir (dir )
405
- if stats then
406
- for _ , stat in ipairs (stats ) do
423
+ repeat
424
+ if not stats then
425
+ break
426
+ end
427
+
428
+ local more = false
429
+ for i , stat in ipairs (stats ) do
430
+ more = i == ENTRIES_BATCH_SIZE
407
431
local path = utils .path_join (path_to_scan , stat .name )
408
- local success , item = pcall (file_items .create_item , context , path , stat .type )
432
+ local success , _ = pcall (file_items .create_item , context , path , stat .type )
409
433
if success then
410
434
if context .recursive and stat .type == " directory" then
411
435
table.insert (context .paths_to_load , path )
@@ -414,7 +438,7 @@ local function sync_scan(context, path_to_scan)
414
438
log .error (" error creating item for " , path )
415
439
end
416
440
end
417
- end
441
+ until not more
418
442
uv .fs_closedir (dir )
419
443
else
420
444
log .error (" Error opening dir:" , err )
0 commit comments