From dc620ee2eca3e481e88123323bfb66c1bb214976 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Coye=20de=20Brune=CC=81lis?= Date: Thu, 22 Aug 2024 10:34:35 +0200 Subject: [PATCH 1/8] fix(DriveFileManager): Make sure liveFile.localContainerUrl is only called once --- .../DriveFileManager/DriveFileManager.swift | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/kDriveCore/Data/Cache/DriveFileManager/DriveFileManager.swift b/kDriveCore/Data/Cache/DriveFileManager/DriveFileManager.swift index 47effd0f5..136b56a21 100644 --- a/kDriveCore/Data/Cache/DriveFileManager/DriveFileManager.swift +++ b/kDriveCore/Data/Cache/DriveFileManager/DriveFileManager.swift @@ -1018,19 +1018,22 @@ public final class DriveFileManager { } func removeFileInDatabase(fileUid: String, cascade: Bool, writableRealm: Realm) { - if let file = writableRealm.object(ofType: File.self, forPrimaryKey: fileUid), !file.isInvalidated { - if fileManager.fileExists(atPath: file.localContainerUrl.path) { - try? fileManager.removeItem(at: file.localContainerUrl) // Check that it was correctly removed? - } + guard let liveFile = writableRealm.object(ofType: File.self, forPrimaryKey: fileUid), !liveFile.isInvalidated else { + return + } - if cascade { - for child in file.children.freeze() where !child.isInvalidated { - removeFileInDatabase(fileUid: child.uid, cascade: cascade, writableRealm: writableRealm) - } - } + let localContainerUrl = liveFile.localContainerUrl + if fileManager.fileExists(atPath: localContainerUrl.path) { + try? fileManager.removeItem(at: localContainerUrl) // Check that it was correctly removed? + } - writableRealm.delete(file) + if cascade { + for frozenChild in liveFile.children.freeze() where !frozenChild.isInvalidated { + removeFileInDatabase(fileUid: frozenChild.uid, cascade: cascade, writableRealm: writableRealm) + } } + + writableRealm.delete(liveFile) } private func deleteOrphanFiles(root: File..., newFiles: [File]? = nil, writableRealm: Realm) { From b92512b58e394a58375e9cf5c660f79937a0f8fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Coye=20de=20Brune=CC=81lis?= Date: Thu, 22 Aug 2024 14:17:06 +0200 Subject: [PATCH 2/8] fix: Close button works in share extension in SaveFileViewController --- .../UI/Controller/Files/Save File/SaveFileViewController.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/kDrive/UI/Controller/Files/Save File/SaveFileViewController.swift b/kDrive/UI/Controller/Files/Save File/SaveFileViewController.swift index e6bed6a6e..131af1b23 100644 --- a/kDrive/UI/Controller/Files/Save File/SaveFileViewController.swift +++ b/kDrive/UI/Controller/Files/Save File/SaveFileViewController.swift @@ -299,6 +299,9 @@ class SaveFileViewController: UIViewController { @IBAction func close(_ sender: Any) { importProgress?.cancel() dismiss(animated: true) + if let extensionContext { + extensionContext.completeRequest(returningItems: nil, completionHandler: nil) + } } } From f22959221df371e6c8473cb6c2a15c3069806af1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Coye=20de=20Brune=CC=81lis?= Date: Fri, 23 Aug 2024 10:23:40 +0200 Subject: [PATCH 3/8] fix(removeFileInDatabase): Prevent stack overflow too many recursion, no longer using a recursion fix(removeFileInDatabase): Prevent cyclical folder references to do an infinite loop on deletion --- .../DriveFileManager/DriveFileManager.swift | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/kDriveCore/Data/Cache/DriveFileManager/DriveFileManager.swift b/kDriveCore/Data/Cache/DriveFileManager/DriveFileManager.swift index 136b56a21..e5ea1791c 100644 --- a/kDriveCore/Data/Cache/DriveFileManager/DriveFileManager.swift +++ b/kDriveCore/Data/Cache/DriveFileManager/DriveFileManager.swift @@ -1018,22 +1018,30 @@ 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() - if cascade { - for frozenChild in liveFile.children.freeze() where !frozenChild.isInvalidated { - removeFileInDatabase(fileUid: frozenChild.uid, cascade: cascade, writableRealm: writableRealm) + if let file = writableRealm.object(ofType: File.self, forPrimaryKey: currentFileUid), !file.isInvalidated { + 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) { From b2acd116ee704d08c89bdb34f19e82a97ce2aa93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Coye=20de=20Brune=CC=81lis?= Date: Fri, 23 Aug 2024 13:46:38 +0200 Subject: [PATCH 4/8] refactor(removeFileInDatabase): Using a guard instead of a if let --- .../DriveFileManager/DriveFileManager.swift | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/kDriveCore/Data/Cache/DriveFileManager/DriveFileManager.swift b/kDriveCore/Data/Cache/DriveFileManager/DriveFileManager.swift index e5ea1791c..f64c9b90b 100644 --- a/kDriveCore/Data/Cache/DriveFileManager/DriveFileManager.swift +++ b/kDriveCore/Data/Cache/DriveFileManager/DriveFileManager.swift @@ -1023,21 +1023,22 @@ public final class DriveFileManager { while !fileUidsToProcess.isEmpty { let currentFileUid = fileUidsToProcess.removeLast() + guard let file = writableRealm.object(ofType: File.self, forPrimaryKey: currentFileUid), !file.isInvalidated else { + continue + } - if let file = writableRealm.object(ofType: File.self, forPrimaryKey: currentFileUid), !file.isInvalidated { - if fileManager.fileExists(atPath: file.localContainerUrl.path) { - try? fileManager.removeItem(at: file.localContainerUrl) - } + 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) + 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) } } From 8710baa097ab3dbd494224d28f22622b91296c55 Mon Sep 17 00:00:00 2001 From: Philippe Weidmann Date: Fri, 23 Aug 2024 15:01:59 +0200 Subject: [PATCH 5/8] fix: Prevent adding parent to itself when handling actions Signed-off-by: Philippe Weidmann --- .../Cache/DriveFileManager/DriveFileManager+Listing.swift | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/kDriveCore/Data/Cache/DriveFileManager/DriveFileManager+Listing.swift b/kDriveCore/Data/Cache/DriveFileManager/DriveFileManager+Listing.swift index fd8c12383..c23245dff 100644 --- a/kDriveCore/Data/Cache/DriveFileManager/DriveFileManager+Listing.swift +++ b/kDriveCore/Data/Cache/DriveFileManager/DriveFileManager+Listing.swift @@ -88,6 +88,11 @@ public extension DriveFileManager { let fileUid = File.uid(driveId: directory.driveId, fileId: fileAction.fileId) + // FIXME: Temporary fix for back-end to prevent adding parent to itself + guard fileUid != directory.uid else { + continue + } + switch fileAction.action { case .fileDelete, .fileTrash: removeFileInDatabase(fileUid: fileUid, cascade: true, writableRealm: writableRealm) From 8595e8d62bcecd73f924f81ddd471e958f64c22b Mon Sep 17 00:00:00 2001 From: Philippe Weidmann Date: Mon, 26 Aug 2024 09:05:47 +0200 Subject: [PATCH 6/8] fix: Prevent adding itself as child Signed-off-by: Philippe Weidmann --- .../DriveFileManager/DriveFileManager+Listing.swift | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/kDriveCore/Data/Cache/DriveFileManager/DriveFileManager+Listing.swift b/kDriveCore/Data/Cache/DriveFileManager/DriveFileManager+Listing.swift index c23245dff..e1ba0e715 100644 --- a/kDriveCore/Data/Cache/DriveFileManager/DriveFileManager+Listing.swift +++ b/kDriveCore/Data/Cache/DriveFileManager/DriveFileManager+Listing.swift @@ -114,7 +114,10 @@ public extension DriveFileManager { let oldParent = existingFile.parent { oldParent.children.remove(existingFile) } - directory.children.insert(actionFile) + + if fileUid != directory.uid { + directory.children.insert(actionFile) + } case .fileRename, .fileFavoriteCreate, .fileUpdate, .fileFavoriteRemove, @@ -134,8 +137,12 @@ public extension DriveFileManager { writableRealm: writableRealm ) writableRealm.add(actionFile, update: .modified) - directory.children.insert(actionFile) + + if fileUid != directory.uid { + directory.children.insert(actionFile) + } actionFile.applyLastModifiedDateToLocalFile() + default: break } From 2e94fa97e814633ec654fb90ded68e64276bbe42 Mon Sep 17 00:00:00 2001 From: Philippe Weidmann Date: Mon, 26 Aug 2024 09:06:29 +0200 Subject: [PATCH 7/8] fix: Add inherited actions Signed-off-by: Philippe Weidmann --- .../FileDetail/FileDetailActivityTableViewCell.swift | 4 ++-- .../DriveFileManager/DriveFileManager+Listing.swift | 10 +++------- kDriveCore/Data/Models/FileActivity.swift | 3 +++ 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/kDrive/UI/View/Files/FileDetail/FileDetailActivityTableViewCell.swift b/kDrive/UI/View/Files/FileDetail/FileDetailActivityTableViewCell.swift index 3d2ae45e1..359b6a7ca 100644 --- a/kDrive/UI/View/Files/FileDetail/FileDetailActivityTableViewCell.swift +++ b/kDrive/UI/View/Files/FileDetail/FileDetailActivityTableViewCell.swift @@ -68,9 +68,9 @@ class FileDetailActivityTableViewCell: InsetTableViewCell { localizedKey = file.isDirectory ? "fileDetailsActivityFolderCreate" : "fileDetailsActivityFileCreate" case .fileRename: localizedKey = file.isDirectory ? "fileDetailsActivityFolderRename" : "fileDetailsActivityFileRename" - case .fileTrash: + case .fileTrash, .fileTrashInherited: localizedKey = file.isDirectory ? "fileDetailsActivityFolderTrash" : "fileDetailsActivityFileTrash" - case .fileRestore: + case .fileRestore, .fileRestoreInherited: localizedKey = file.isDirectory ? "fileDetailsActivityFolderRestore" : "fileDetailsActivityFileRestore" case .fileDelete: localizedKey = file.isDirectory ? "fileDetailsActivityFolderDelete" : "fileDetailsActivityFileDelete" diff --git a/kDriveCore/Data/Cache/DriveFileManager/DriveFileManager+Listing.swift b/kDriveCore/Data/Cache/DriveFileManager/DriveFileManager+Listing.swift index e1ba0e715..2e252c39f 100644 --- a/kDriveCore/Data/Cache/DriveFileManager/DriveFileManager+Listing.swift +++ b/kDriveCore/Data/Cache/DriveFileManager/DriveFileManager+Listing.swift @@ -88,13 +88,8 @@ public extension DriveFileManager { let fileUid = File.uid(driveId: directory.driveId, fileId: fileAction.fileId) - // FIXME: Temporary fix for back-end to prevent adding parent to itself - guard fileUid != directory.uid else { - continue - } - switch fileAction.action { - case .fileDelete, .fileTrash: + case .fileDelete, .fileTrash, .fileTrashInherited: removeFileInDatabase(fileUid: fileUid, cascade: true, writableRealm: writableRealm) case .fileMoveOut: @@ -102,7 +97,8 @@ public extension DriveFileManager { let oldParent = movedOutFile.parent else { continue } oldParent.children.remove(movedOutFile) - case .fileMoveIn, .fileRestore, .fileCreate: + + case .fileMoveIn, .fileRestore, .fileCreate, .fileRestoreInherited: keepCacheAttributesForFile( newFile: actionFile, keepProperties: [.standard, .extras], diff --git a/kDriveCore/Data/Models/FileActivity.swift b/kDriveCore/Data/Models/FileActivity.swift index b246b559a..b13f2fe4a 100644 --- a/kDriveCore/Data/Models/FileActivity.swift +++ b/kDriveCore/Data/Models/FileActivity.swift @@ -29,7 +29,9 @@ public enum FileActivityType: String, Codable, CaseIterable { case fileMoveIn = "file_move" case fileMoveOut = "file_move_out" case fileTrash = "file_trash" + case fileTrashInherited = "file_trash_inherited" case fileRestore = "file_restore" + case fileRestoreInherited = "file_restore_inherited" case fileDelete = "file_delete" case fileUpdate = "file_update" case fileCategorize = "file_categorize" @@ -65,6 +67,7 @@ public enum FileActivityType: String, Codable, CaseIterable { .fileMoveIn, .fileMoveOut, .fileTrash, + .fileTrashInherited, .fileRestore, .fileDelete, .fileUpdate, From e6b62cc466b502dbb31c26ee771146420aacf6a1 Mon Sep 17 00:00:00 2001 From: Philippe Weidmann Date: Tue, 27 Aug 2024 10:33:41 +0200 Subject: [PATCH 8/8] fix: Add real name for diffing Signed-off-by: Philippe Weidmann --- kDriveCore/Data/Models/File.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/kDriveCore/Data/Models/File.swift b/kDriveCore/Data/Models/File.swift index 37f6e8025..4026bbc22 100644 --- a/kDriveCore/Data/Models/File.swift +++ b/kDriveCore/Data/Models/File.swift @@ -842,6 +842,7 @@ extension File: Differentiable { autoreleasepool { lastModifiedAt == source.lastModifiedAt && sortedName == source.sortedName + && name == source.name && isFavorite == source.isFavorite && isAvailableOffline == source.isAvailableOffline && visibility == source.visibility