Process subdirectories after completing the current folder. Otherwise we might exceed the OS limit of 256 open files when nesting.

This commit is contained in:
Ali 2023-01-10 13:26:26 +04:00
parent 3b31d6bbcd
commit 1361467ffe

View File

@ -132,6 +132,8 @@ private final class TempScanDatabase {
private func scanFiles(at path: String, olderThan minTimestamp: Int32, includeSubdirectories: Bool, performSizeMapping: Bool, tempDatabase: TempScanDatabase) -> ScanFilesResult { private func scanFiles(at path: String, olderThan minTimestamp: Int32, includeSubdirectories: Bool, performSizeMapping: Bool, tempDatabase: TempScanDatabase) -> ScanFilesResult {
var result = ScanFilesResult() var result = ScanFilesResult()
var subdirectories: [String] = []
if let dp = opendir(path) { if let dp = opendir(path) {
let pathBuffer = malloc(2048).assumingMemoryBound(to: Int8.self) let pathBuffer = malloc(2048).assumingMemoryBound(to: Int8.self)
defer { defer {
@ -158,9 +160,7 @@ private func scanFiles(at path: String, olderThan minTimestamp: Int32, includeSu
if (((value.st_mode) & S_IFMT) == S_IFDIR) { if (((value.st_mode) & S_IFMT) == S_IFDIR) {
if includeSubdirectories { if includeSubdirectories {
if let subPath = String(data: Data(bytes: pathBuffer, count: strnlen(pathBuffer, 1024)), encoding: .utf8) { if let subPath = String(data: Data(bytes: pathBuffer, count: strnlen(pathBuffer, 1024)), encoding: .utf8) {
let subResult = scanFiles(at: subPath, olderThan: minTimestamp, includeSubdirectories: true, performSizeMapping: performSizeMapping, tempDatabase: tempDatabase) subdirectories.append(subPath)
result.totalSize += subResult.totalSize
result.unlinkedCount += subResult.unlinkedCount
} }
} }
} else { } else {
@ -179,6 +179,14 @@ private func scanFiles(at path: String, olderThan minTimestamp: Int32, includeSu
closedir(dp) closedir(dp)
} }
if includeSubdirectories {
for subPath in subdirectories {
let subResult = scanFiles(at: subPath, olderThan: minTimestamp, includeSubdirectories: true, performSizeMapping: performSizeMapping, tempDatabase: tempDatabase)
result.totalSize += subResult.totalSize
result.unlinkedCount += subResult.unlinkedCount
}
}
return result return result
} }