Improve logging in FileProviderEnumerator

Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
This commit is contained in:
Claudio Cambra 2023-01-27 02:44:34 +01:00
parent 67de82913e
commit 325878b696
No known key found for this signature in database
GPG key ID: C839200C384636B0

View file

@ -41,6 +41,7 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
}
NSLog("Set up enumerator for user: %@ with serverUrl: %@", ncAccount.username, serverUrl)
super.init()
}
@ -51,6 +52,7 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
// MARK: - Protocol methods
func enumerateItems(for observer: NSFileProviderEnumerationObserver, startingAt page: NSFileProviderPage) {
NSLog("Received enumerate items request for enumerator with user: %@ with serverUrl: %@", ncAccount.username, serverUrl)
/* TODO:
- inspect the page to determine whether this is an initial or a follow-up request
@ -65,6 +67,7 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
*/
if enumeratedItemIdentifier == .workingSet {
NSLog("Enumerating working set for user: %@ with serverUrl: %@", ncAccount.username, serverUrl)
// TODO
observer.finishEnumerating(upTo: nil)
return
@ -73,16 +76,21 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
if page == NSFileProviderPage.initialPageSortedByDate as NSFileProviderPage ||
page == NSFileProviderPage.initialPageSortedByName as NSFileProviderPage {
NSLog("Enumerating initial page for user: %@ with serverUrl: %@", ncAccount.username, serverUrl)
FileProviderEnumerator.readServerUrl(serverUrl, ncAccount: ncAccount) { metadatas in
FileProviderEnumerator.completeObserver(observer, numPage: 1, itemMetadatas: metadatas)
}
} else {
let numPage = Int(String(data: page.rawValue, encoding: .utf8)!)!
FileProviderEnumerator.completeObserver(observer, numPage: numPage, itemMetadatas: nil)
return;
}
let numPage = Int(String(data: page.rawValue, encoding: .utf8)!)!
NSLog("Enumerating page %d for user: %@ with serverUrl: %@", numPage, ncAccount.username, serverUrl)
FileProviderEnumerator.completeObserver(observer, numPage: numPage, itemMetadatas: nil)
}
func enumerateChanges(for observer: NSFileProviderChangeObserver, from anchor: NSFileProviderSyncAnchor) {
NSLog("Received enumerate changes request for enumerator with user: %@ with serverUrl: %@", ncAccount.username, serverUrl)
/* TODO:
- query the server for updates since the passed-in sync anchor
@ -102,22 +110,34 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
// MARK: - Helper methods
private static func completeObserver(_ observer: NSFileProviderEnumerationObserver, numPage: Int, itemMetadatas: [NextcloudItemMetadataTable]?) {
guard itemMetadatas != nil else { observer.finishEnumerating(upTo: nil); return }
guard itemMetadatas != nil else {
NSLog("Received nil metadatas, finish empty enumeration")
observer.finishEnumerating(upTo: nil)
return
}
var items: [NSFileProviderItem] = []
for itemMetadata in itemMetadatas! {
if itemMetadata.e2eEncrypted { continue }
if itemMetadata.e2eEncrypted {
NSLog("Skipping encrypted metadata in enumeration")
continue
}
createFileOrDirectoryLocally(metadata: itemMetadata)
if let parentItemIdentifier = parentItemIdentifierFromMetadata(itemMetadata) {
let item = FileProviderItem(metadata: itemMetadata, parentItemIdentifier: parentItemIdentifier)
NSLog("Will enumerate item with ocId: %@ and name: %@", itemMetadata.ocId, itemMetadata.fileName)
items.append(item)
} else {
NSLog("Could not get valid parentItemIdentifier for item with ocId: %@ and name: %@, skipping enumeration", itemMetadata.ocId, itemMetadata.fileName)
}
}
observer.didEnumerate(items)
NSLog("Did enumerate %d items", items.count)
if items.count == maxItemsPerFileProviderPage {
let nextPage = numPage + 1
@ -130,6 +150,8 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
private static func finishReadServerUrl(_ serverUrlPath: String, ncKitAccount: String, completionHandler: @escaping (_ metadatas: [NextcloudItemMetadataTable]?) -> Void) {
let metadatas = NextcloudFilesDatabaseManager.shared.itemMetadatas(account: ncKitAccount, serverUrl: serverUrlPath)
NSLog("Finished reading serverUrl: %@ for user: %@. Processed %d metadatas", serverUrlPath, ncKitAccount, metadatas.count)
completionHandler(metadatas)
}
@ -142,18 +164,31 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator {
directoryEtag = directoryMetadata.etag
}
NSLog("Starting to read serverUrl: %@ for user: %@ at depth 0", serverUrl, ncKitAccount)
NextcloudKit.shared.readFileOrFolder(serverUrlFileName: serverUrl, depth: "0", showHiddenFiles: true) { account, files, _, error in
guard directoryEtag != files.first?.etag else {
guard error == .success else {
NSLog("0 depth readFileOrFolder of url: %@ did not complete successfully, received error: %@", serverUrl, error.errorDescription)
finishReadServerUrl(serverUrl, ncKitAccount: ncKitAccount, completionHandler: completionHandler)
return
}
guard directoryEtag != files.first?.etag else {
NSLog("Fetched directory etag is same as that stored locally (serverUrl: %@ user: %@). Not fetching child items.", serverUrl, account)
finishReadServerUrl(serverUrl, ncKitAccount: ncKitAccount, completionHandler: completionHandler)
return
}
NSLog("Starting to read serverUrl: %@ for user: %@ at depth 1", serverUrl, ncKitAccount)
NextcloudKit.shared.readFileOrFolder(serverUrlFileName: serverUrl, depth: "1", showHiddenFiles: true) { account, files, _, error in
guard error == .success else {
NSLog("1 depth readFileOrFolder of url: %@ did not complete successfully, received error: %@", serverUrl, error.errorDescription)
finishReadServerUrl(serverUrl, ncKitAccount: ncKitAccount, completionHandler: completionHandler)
return
}
NSLog("Starting async conversion of NKFiles for serverUrl: %@ for user: %@", serverUrl, ncKitAccount)
DispatchQueue.global().async {
dbManager.convertNKFilesToItemMetadatas(files, account: ncKitAccount) { _, childDirectoriesMetadata, metadatas in
dbManager.updateItemMetadatas(account: ncKitAccount, serverUrl: serverUrl, updatedMetadatas: metadatas)