Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(removeFileInDatabase): Fix stack overflow #1260

Merged
merged 2 commits into from
Aug 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions kDriveCore/Data/Cache/DriveFileManager/DriveFileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1018,22 +1018,31 @@ public final class DriveFileManager {
}

func removeFileInDatabase(fileUid: String, cascade: Bool, writableRealm: Realm) {
guard let liveFile = writableRealm.object(ofType: File.self, forPrimaryKey: fileUid), !liveFile.isInvalidated else {
return
}
var fileUidsToProcess = [fileUid]
var liveFilesToDelete = [File]()

let localContainerUrl = liveFile.localContainerUrl
if fileManager.fileExists(atPath: localContainerUrl.path) {
try? fileManager.removeItem(at: localContainerUrl) // Check that it was correctly removed?
}
while !fileUidsToProcess.isEmpty {
let currentFileUid = fileUidsToProcess.removeLast()
guard let file = writableRealm.object(ofType: File.self, forPrimaryKey: currentFileUid), !file.isInvalidated else {
continue
}

if cascade {
for frozenChild in liveFile.children.freeze() where !frozenChild.isInvalidated {
removeFileInDatabase(fileUid: frozenChild.uid, cascade: cascade, writableRealm: writableRealm)
if fileManager.fileExists(atPath: file.localContainerUrl.path) {
try? fileManager.removeItem(at: file.localContainerUrl)
}

if cascade {
let filesUidsToDelete = liveFilesToDelete.map { $0.uid }
let liveChildren = file.children.filter { child in
// A child should not be a circular reference to an ancestor
return !child.isInvalidated && !filesUidsToDelete.contains(child.uid)
}
fileUidsToProcess.append(contentsOf: liveChildren.map { $0.uid })
liveFilesToDelete.append(contentsOf: liveChildren)
}
}

writableRealm.delete(liveFile)
writableRealm.delete(liveFilesToDelete)
}

private func deleteOrphanFiles(root: File..., newFiles: [File]? = nil, writableRealm: Realm) {
Expand Down
Loading