-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFilesDatabaseManager.swift
More file actions
615 lines (551 loc) · 25.1 KB
/
FilesDatabaseManager.swift
File metadata and controls
615 lines (551 loc) · 25.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
/*
* Copyright (C) 2022 by Claudio Cambra <claudio.cambra@nextcloud.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
import FileProvider
import Foundation
import OSLog
import RealmSwift
internal let stable1_0SchemaVersion: UInt64 = 100
internal let stable2_0SchemaVersion: UInt64 = 200 // Major change: deleted LocalFileMetadata type
public let relativeDatabaseFolderPath = "Database/"
public let databaseFilename = "fileproviderextdatabase.realm"
public final class FilesDatabaseManager: Sendable {
public enum ErrorCode: Int {
case metadataNotFound = -1000
case parentMetadataNotFound = -1001
}
public enum ErrorUserInfoKey: String {
case missingParentServerUrlAndFileName = "MissingParentServerUrlAndFileName"
}
static let errorDomain = "FilesDatabaseManager"
static func error(code: ErrorCode, userInfo: [String: String]) -> NSError {
NSError(domain: Self.errorDomain, code: code.rawValue, userInfo: userInfo)
}
static func parentMetadataNotFoundError(itemUrl: String) -> NSError {
error(
code: .parentMetadataNotFound,
userInfo: [ErrorUserInfoKey.missingParentServerUrlAndFileName.rawValue: itemUrl]
)
}
private static let schemaVersion = stable2_0SchemaVersion
static let logger = Logger(subsystem: Logger.subsystem, category: "filesdatabase")
let account: Account
var itemMetadatas: Results<RealmItemMetadata> { ncDatabase().objects(RealmItemMetadata.self) }
public init(
realmConfig: Realm.Configuration = Realm.Configuration.defaultConfiguration,
account: Account,
fileProviderDataDirUrl: URL? = pathForFileProviderExtData(),
relativeDatabaseFolderPath: String = relativeDatabaseFolderPath
) {
self.account = account
let fm = FileManager.default
let dbPath = realmConfig.fileURL?.path
let migrate = dbPath != nil && !fm.fileExists(atPath: dbPath!)
Realm.Configuration.defaultConfiguration = realmConfig
do {
_ = try Realm()
Self.logger.info("Successfully started Realm db for NextcloudFileProviderKit")
} catch let error {
Self.logger.error("Error opening Realm db: \(error, privacy: .public)")
}
// Migrate from old unified database to new per-account DB
guard let fileProviderDataDirUrl, migrate else { return }
let oldRelativeDatabaseFilePath = relativeDatabaseFolderPath + databaseFilename
let oldDatabasePath = fileProviderDataDirUrl.appendingPathComponent(
oldRelativeDatabaseFilePath
)
guard FileManager.default.fileExists(atPath: oldDatabasePath.path) == true else {
Self.logger.debug("No old database found at \(oldDatabasePath.path) skipping migration")
return
}
Self.logger.info(
"Migrating old database to database for \(account.ncKitAccount, privacy: .public)"
)
let oldConfig = Realm.Configuration(
fileURL: oldDatabasePath,
schemaVersion: stable2_0SchemaVersion,
objectTypes: [RealmItemMetadata.self, RemoteFileChunk.self]
)
do {
let oldRealm = try Realm(configuration: oldConfig)
let itemMetadatas = oldRealm
.objects(RealmItemMetadata.self)
.filter { $0.account == account.ncKitAccount }
let remoteFileChunks = oldRealm.objects(RemoteFileChunk.self)
Self.logger.info(
"Migrating \(itemMetadatas.count) metadatas and \(remoteFileChunks.count) chunks"
)
let currentRealm = try Realm()
try currentRealm.write {
itemMetadatas.forEach { currentRealm.create(RealmItemMetadata.self, value: $0) }
remoteFileChunks.forEach { currentRealm.create(RemoteFileChunk.self, value: $0) }
}
} catch let error {
Self.logger.error(
"""
Error migrating old database to account-specific database
for: \(account.ncKitAccount, privacy: .public)
Received error: \(error, privacy: .public)
"""
)
}
}
public convenience init?(account: Account) {
let relativeDatabaseFilePath =
relativeDatabaseFolderPath + account.fileName + "-" + databaseFilename
guard let fileProviderDataDirUrl = pathForFileProviderExtData() else { return nil }
let databasePath = fileProviderDataDirUrl.appendingPathComponent(relativeDatabaseFilePath)
// Disable file protection for directory DB
// https://docs.mongodb.com/realm/sdk/ios/examples/configure-and-open-a-realm/
let dbFolder = fileProviderDataDirUrl.appendingPathComponent(relativeDatabaseFolderPath)
let dbFolderPath = dbFolder.path
do {
try FileManager.default.createDirectory(at: dbFolder, withIntermediateDirectories: true)
try FileManager.default.setAttributes(
[.protectionKey: FileProtectionType.completeUntilFirstUserAuthentication],
ofItemAtPath: dbFolderPath
)
} catch {
Self.logger.error(
"Could not set permission level for db folder: \(error, privacy: .public)"
)
}
let config = Realm.Configuration(
fileURL: databasePath,
schemaVersion: Self.schemaVersion,
migrationBlock: { migration, oldSchemaVersion in
if oldSchemaVersion == stable1_0SchemaVersion {
var localFileMetadataOcIds = Set<String>()
migration.enumerateObjects(ofType: "LocalFileMetadata") { oldObject, _ in
guard let oldObject, let lfmOcId = oldObject["ocId"] as? String else {
return
}
localFileMetadataOcIds.insert(lfmOcId)
}
migration.enumerateObjects(ofType: RealmItemMetadata.className()) { _, newObject in
guard let newObject,
let imOcId = newObject["ocId"] as? String,
localFileMetadataOcIds.contains(imOcId)
else { return }
newObject["downloaded"] = true
newObject["uploaded"] = true
}
}
},
objectTypes: [RealmItemMetadata.self, RemoteFileChunk.self]
)
self.init(realmConfig: config, account: account)
}
func ncDatabase() -> Realm {
let realm = try! Realm()
realm.refresh()
return realm
}
public func anyItemMetadatasForAccount(_ account: String) -> Bool {
!itemMetadatas.where({ $0.account == account }).isEmpty
}
public func itemMetadata(ocId: String) -> SendableItemMetadata? {
// Realm objects are live-fire, i.e. they will be changed and invalidated according to
// changes in the db.
//
// Let's therefore create a copy
if let itemMetadata = itemMetadatas.where({ $0.ocId == ocId }).first {
return SendableItemMetadata(value: itemMetadata)
}
return nil
}
public func itemMetadata(
account: String, locatedAtRemoteUrl remoteUrl: String // Is the URL for the actual item
) -> SendableItemMetadata? {
guard let actualRemoteUrl = URL(string: remoteUrl) else { return nil }
let fileName = actualRemoteUrl.lastPathComponent
guard var serverUrl = actualRemoteUrl
.deletingLastPathComponent()
.absoluteString
.removingPercentEncoding
else { return nil }
if serverUrl.hasSuffix("/") {
serverUrl.removeLast()
}
if let metadata = itemMetadatas.where({
$0.account == account && $0.serverUrl == serverUrl && $0.fileName == fileName
}).first {
return SendableItemMetadata(value: metadata)
}
return nil
}
public func itemMetadatas(account: String) -> [SendableItemMetadata] {
itemMetadatas
.where { $0.account == account }
.toUnmanagedResults()
}
public func itemMetadatas(
account: String, underServerUrl serverUrl: String
) -> [SendableItemMetadata] {
itemMetadatas
.where { $0.account == account && $0.serverUrl.starts(with: serverUrl) }
.toUnmanagedResults()
}
public func itemMetadataFromFileProviderItemIdentifier(
_ identifier: NSFileProviderItemIdentifier
) -> SendableItemMetadata? {
itemMetadata(ocId: identifier.rawValue)
}
private func processItemMetadatasToDelete(
existingMetadatas: Results<RealmItemMetadata>,
updatedMetadatas: [SendableItemMetadata]
) -> [RealmItemMetadata] {
var deletedMetadatas: [RealmItemMetadata] = []
for existingMetadata in existingMetadatas {
guard !updatedMetadatas.contains(where: { $0.ocId == existingMetadata.ocId }),
let metadataToDelete = itemMetadatas.where({ $0.ocId == existingMetadata.ocId }).first
else { continue }
deletedMetadatas.append(metadataToDelete)
Self.logger.debug(
"Deleting item metadata during update. ocID: \(existingMetadata.ocId, privacy: .public), etag: \(existingMetadata.etag, privacy: .public), fileName: \(existingMetadata.fileName, privacy: .public)"
)
}
return deletedMetadatas
}
private func processItemMetadatasToUpdate(
existingMetadatas: Results<RealmItemMetadata>,
updatedMetadatas: [SendableItemMetadata],
updateDirectoryEtags: Bool,
keepExistingDownloadState: Bool
) -> (
newMetadatas: [SendableItemMetadata],
updatedMetadatas: [SendableItemMetadata],
directoriesNeedingRename: [SendableItemMetadata]
) {
var returningNewMetadatas: [SendableItemMetadata] = []
var returningUpdatedMetadatas: [SendableItemMetadata] = []
var directoriesNeedingRename: [SendableItemMetadata] = []
for var updatedMetadata in updatedMetadatas {
if let existingMetadata = existingMetadatas.first(where: {
$0.ocId == updatedMetadata.ocId
}) {
if existingMetadata.status == Status.normal.rawValue,
!existingMetadata.isInSameDatabaseStoreableRemoteState(updatedMetadata)
{
if updatedMetadata.directory {
if updatedMetadata.serverUrl != existingMetadata.serverUrl
|| updatedMetadata.fileName != existingMetadata.fileName
{
directoriesNeedingRename.append(updatedMetadata)
updatedMetadata.etag = "" // Renaming doesn't change the etag so reset
} else if !updateDirectoryEtags {
updatedMetadata.etag = existingMetadata.etag
}
}
if keepExistingDownloadState {
updatedMetadata.downloaded = existingMetadata.downloaded
}
returningUpdatedMetadatas.append(updatedMetadata)
Self.logger.debug(
"""
Updated existing item metadata.
ocID: \(updatedMetadata.ocId, privacy: .public)
etag: \(updatedMetadata.etag, privacy: .public)
fileName: \(updatedMetadata.fileName, privacy: .public)
"""
)
} else {
Self.logger.debug(
"""
Skipping item metadata update; same as existing, or still in transit.
ocID: \(updatedMetadata.ocId, privacy: .public)
etag: \(updatedMetadata.etag, privacy: .public)
fileName: \(updatedMetadata.fileName, privacy: .public)
"""
)
}
} else { // This is a new metadata
if !updateDirectoryEtags, updatedMetadata.directory {
updatedMetadata.etag = ""
}
returningNewMetadatas.append(updatedMetadata)
Self.logger.debug(
"""
Created new item metadata during update.
ocID: \(updatedMetadata.ocId, privacy: .public)
etag: \(updatedMetadata.etag, privacy: .public)
fileName: \(updatedMetadata.fileName, privacy: .public)
"""
)
}
}
return (returningNewMetadatas, returningUpdatedMetadatas, directoriesNeedingRename)
}
// ONLY HANDLES UPDATES FOR IMMEDIATE CHILDREN
// (in case of directory renames/moves, the changes are recursed down)
public func depth1ReadUpdateItemMetadatas(
account: String,
serverUrl: String,
updatedMetadatas: [SendableItemMetadata],
updateDirectoryEtags: Bool,
keepExistingDownloadState: Bool
) -> (
newMetadatas: [SendableItemMetadata]?,
updatedMetadatas: [SendableItemMetadata]?,
deletedMetadatas: [SendableItemMetadata]?
) {
let database = ncDatabase()
do {
// Find the metadatas that we previously knew to be on the server for this account
// (we need to check if they were uploaded to prevent deleting ignored/lock files)
//
// - the ones that do exist remotely still are either the same or have been updated
// - the ones that don't have been deleted
let existingMetadatas = database
.objects(RealmItemMetadata.self)
.where { $0.account == account && $0.serverUrl == serverUrl && $0.uploaded }
// NOTE: These metadatas are managed -- be careful!
let metadatasToDelete = processItemMetadatasToDelete(
existingMetadatas: existingMetadatas,
updatedMetadatas: updatedMetadatas)
let metadatasToDeleteCopy = metadatasToDelete.map { SendableItemMetadata(value: $0) }
let metadatasToChange = processItemMetadatasToUpdate(
existingMetadatas: existingMetadatas,
updatedMetadatas: updatedMetadatas,
updateDirectoryEtags: updateDirectoryEtags,
keepExistingDownloadState: keepExistingDownloadState
)
var metadatasToUpdate = metadatasToChange.updatedMetadatas
let metadatasToCreate = metadatasToChange.newMetadatas
let directoriesNeedingRename = metadatasToChange.directoriesNeedingRename
for metadata in directoriesNeedingRename {
if let updatedDirectoryChildren = renameDirectoryAndPropagateToChildren(
ocId: metadata.ocId,
newServerUrl: metadata.serverUrl,
newFileName: metadata.fileName)
{
metadatasToUpdate += updatedDirectoryChildren
}
}
try database.write {
database.delete(metadatasToDelete)
database.add(metadatasToUpdate.map { RealmItemMetadata(value: $0) }, update: .modified)
database.add(metadatasToCreate.map { RealmItemMetadata(value: $0) }, update: .all)
}
return (metadatasToCreate, metadatasToUpdate, metadatasToDeleteCopy)
} catch {
Self.logger.error(
"""
Could not update any item metadatas.
Received error: \(error.localizedDescription, privacy: .public)
"""
)
return (nil, nil, nil)
}
}
// If setting a downloading or uploading status, also modified the relevant boolean properties
// of the item metadata object
public func setStatusForItemMetadata(
_ metadata: SendableItemMetadata, status: Status
) -> SendableItemMetadata? {
guard let result = itemMetadatas.where({ $0.ocId == metadata.ocId }).first else {
Self.logger.debug(
"""
Did not update status for item metadata as it was not found.
ocID: \(metadata.ocId, privacy: .public)
"""
)
return nil
}
do {
let database = ncDatabase()
try database.write {
result.status = status.rawValue
if result.isDownload {
result.downloaded = false
} else if result.isUpload {
result.uploaded = false
result.chunkUploadId = UUID().uuidString
} else if status == .normal, metadata.isUpload {
result.chunkUploadId = nil
}
Self.logger.debug(
"""
Updated status for item metadata.
ocID: \(metadata.ocId, privacy: .public)
etag: \(metadata.etag, privacy: .public)
fileName: \(metadata.fileName, privacy: .public)
"""
)
}
return SendableItemMetadata(value: result)
} catch {
Self.logger.error(
"""
Could not update status for item metadata.
ocID: \(metadata.ocId, privacy: .public)
etag: \(metadata.etag, privacy: .public)
fileName: \(metadata.fileName, privacy: .public)
received error: \(error.localizedDescription, privacy: .public)
"""
)
}
return nil
}
public func addItemMetadata(_ metadata: SendableItemMetadata) {
let database = ncDatabase()
do {
try database.write {
database.add(RealmItemMetadata(value: metadata), update: .all)
Self.logger.debug(
"""
Added item metadata.
ocID: \(metadata.ocId, privacy: .public)
etag: \(metadata.etag, privacy: .public)
fileName: \(metadata.fileName, privacy: .public)
parentDirectoryUrl: \(metadata.serverUrl, privacy: .public)
account: \(metadata.account, privacy: .public)
content type: \(metadata.contentType, privacy: .public)
creation date: \(metadata.creationDate, privacy: .public)
date: \(metadata.date, privacy: .public)
lock: \(metadata.lock, privacy: .public)
lockTimeOut: \(metadata.lockTimeOut?.description ?? "", privacy: .public)
lockOwner: \(metadata.lockOwner ?? "", privacy: .public)
permissions: \(metadata.permissions, privacy: .public)
size: \(metadata.size, privacy: .public)
trashbinFileName: \(metadata.trashbinFileName, privacy: .public)
downloaded: \(metadata.downloaded, privacy: .public)
uploaded: \(metadata.uploaded, privacy: .public)
"""
)
}
} catch {
Self.logger.error(
"""
Could not add item metadata.
ocID: \(metadata.ocId, privacy: .public)
etag: \(metadata.etag, privacy: .public)
fileName: \(metadata.fileName, privacy: .public)
received error: \(error.localizedDescription, privacy: .public)
"""
)
}
}
@discardableResult public func deleteItemMetadata(ocId: String) -> Bool {
do {
let results = itemMetadatas.where { $0.ocId == ocId }
let database = ncDatabase()
try database.write {
Self.logger.debug("Deleting item metadata. \(ocId, privacy: .public)")
database.delete(results)
}
return true
} catch {
Self.logger.error(
"""
Could not delete item metadata with ocId: \(ocId, privacy: .public)
Received error: \(error.localizedDescription, privacy: .public)
"""
)
return false
}
}
public func renameItemMetadata(ocId: String, newServerUrl: String, newFileName: String) {
guard let itemMetadata = itemMetadatas.where({ $0.ocId == ocId }).first else {
Self.logger.debug(
"""
Could not find an item with ocID \(ocId, privacy: .public)
to rename to \(newFileName, privacy: .public)
"""
)
return
}
do {
let database = ncDatabase()
try database.write {
let oldFileName = itemMetadata.fileName
let oldServerUrl = itemMetadata.serverUrl
itemMetadata.fileName = newFileName
itemMetadata.fileNameView = newFileName
itemMetadata.serverUrl = newServerUrl
database.add(itemMetadata, update: .all)
Self.logger.debug(
"""
Renamed item \(oldFileName, privacy: .public)
to \(newFileName, privacy: .public),
moved from serverUrl: \(oldServerUrl, privacy: .public)
to serverUrl: \(newServerUrl, privacy: .public)
"""
)
}
} catch {
Self.logger.error(
"""
Could not rename filename of item metadata with ocID: \(ocId, privacy: .public)
to proposed name \(newFileName, privacy: .public)
at proposed serverUrl \(newServerUrl, privacy: .public)
received error: \(error.localizedDescription, privacy: .public)
"""
)
}
}
public func parentItemIdentifierFromMetadata(
_ metadata: SendableItemMetadata
) -> NSFileProviderItemIdentifier? {
let homeServerFilesUrl = metadata.urlBase + Account.webDavFilesUrlSuffix + metadata.userId
let trashServerFilesUrl = metadata.urlBase + Account.webDavTrashUrlSuffix + metadata.userId + "/trash"
if metadata.serverUrl == homeServerFilesUrl {
return .rootContainer
} else if metadata.serverUrl == trashServerFilesUrl {
return .trashContainer
}
guard let parentDirectoryMetadata = parentDirectoryMetadataForItem(metadata) else {
Self.logger.error(
"""
Could not get item parent directory item metadata for metadata.
ocID: \(metadata.ocId, privacy: .public),
etag: \(metadata.etag, privacy: .public),
fileName: \(metadata.fileName, privacy: .public),
serverUrl: \(metadata.serverUrl, privacy: .public),
account: \(metadata.account, privacy: .public),
"""
)
return nil
}
return NSFileProviderItemIdentifier(parentDirectoryMetadata.ocId)
}
public func parentItemIdentifierWithRemoteFallback(
fromMetadata metadata: SendableItemMetadata,
remoteInterface: RemoteInterface,
account: Account
) async -> NSFileProviderItemIdentifier? {
if let parentItemIdentifier = parentItemIdentifierFromMetadata(metadata) {
return parentItemIdentifier
}
let (metadatas, _, _, _, error) = await Enumerator.readServerUrl(
metadata.serverUrl,
account: account,
remoteInterface: remoteInterface,
dbManager: self,
depth: .target
)
guard error == nil, let parentMetadata = metadatas?.first else {
Self.logger.error(
"""
Could not retrieve parent item identifier remotely, received error.
target metadata: \(metadata.ocId, privacy: .public)
target filename: \(metadata.fileName, privacy: .public)
received metadatas: \(metadatas?.count ?? 0, privacy: .public)
error: \(error?.errorDescription ?? "NO ERROR", privacy: .public)
"""
)
return nil
}
return NSFileProviderItemIdentifier(parentMetadata.ocId)
}
}