adoor 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/README.md +2 -2
- package/dist/adoor.mjs +105 -84
- package/native/LICENSE +662 -0
- package/native/README.md +12 -0
- package/native/darwin/ADoorService.app/Contents/Info.plist +57 -0
- package/native/darwin/ADoorService.app/Contents/MacOS/ADoorService +0 -0
- package/native/darwin/ADoorService.app/Contents/PkgInfo +1 -0
- package/native/darwin/ADoorService.app/Contents/_CodeSignature/CodeResources +123 -0
- package/native/darwin/ADoorService.app/Contents/embedded.provisionprofile +0 -0
- package/native/source/Package.swift +6 -0
- package/native/source/sdk/LICENSE +21 -0
- package/native/source/sdk/Package.swift +14 -0
- package/native/source/sdk/Sources/ADoor/AdoorClient.swift +270 -0
- package/native/source/sdk/Sources/ADoor/AdoorError.swift +60 -0
- package/native/source/sdk/Sources/ADoor/Changes.swift +202 -0
- package/native/source/sdk/Sources/ADoor/DesktopSessionState.swift +73 -0
- package/native/source/sdk/Sources/ADoor/DeviceFlow.swift +70 -0
- package/native/source/sdk/Sources/ADoor/FolderCatalog.swift +49 -0
- package/native/source/sdk/Sources/ADoor/JSONValue.swift +70 -0
- package/native/source/sdk/Sources/ADoor/LocalStore.swift +474 -0
- package/native/source/sdk/Sources/ADoor/Models.swift +146 -0
- package/native/source/sdk/Sources/ADoor/MountModel.swift +896 -0
- package/native/source/sdk/Sources/ADoor/OAuth.swift +225 -0
- package/native/source/sdk/Sources/ADoor/Provider.swift +211 -0
- package/native/source/sdk/Sources/ADoor/Storage.swift +41 -0
- package/native/source/sdk/Sources/ADoor/SyncTree.swift +501 -0
- package/native/source/sdk/Sources/ADoor/Trash.swift +283 -0
- package/native/source/sdk/Tests/ADoorTests/ADoorTests.swift +182 -0
- package/native/source/sdk/Tests/ADoorTests/DesktopSessionStateTests.swift +46 -0
- package/native/source/sdk/Tests/ADoorTests/MountTests.swift +560 -0
- package/native/source/sdk/Tests/ADoorTests/RegressionTests.swift +360 -0
- package/native/source/service/DesktopSession.swift +241 -0
- package/native/source/service/DeviceClient.swift +53 -0
- package/native/source/service/Domains.swift +182 -0
- package/native/source/service/FolderRefresh.swift +37 -0
- package/native/source/service/KeychainStore.swift +74 -0
- package/native/source/service/LocalSessionSocket.swift +142 -0
- package/native/source/service/ServiceMain.swift +14 -0
- package/native/source/service/ServiceMounts.swift +34 -0
- package/package.json +12 -3
|
@@ -0,0 +1,501 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
|
|
3
|
+
public protocol ScratchStore: Sendable {
|
|
4
|
+
func data(at path: String) -> Data?
|
|
5
|
+
func set(_ data: Data, at path: String)
|
|
6
|
+
func names(at path: String) -> [String]
|
|
7
|
+
func remove(at path: String)
|
|
8
|
+
func exists(_ path: String) -> Bool
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
public final class MemoryScratch: ScratchStore, @unchecked Sendable {
|
|
12
|
+
private let lock = NSLock()
|
|
13
|
+
private var files: [String: Data] = [:]
|
|
14
|
+
|
|
15
|
+
public init() {}
|
|
16
|
+
|
|
17
|
+
public func data(at path: String) -> Data? {
|
|
18
|
+
lock.lock(); defer { lock.unlock() }
|
|
19
|
+
return files[path]
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public func set(_ data: Data, at path: String) {
|
|
23
|
+
lock.lock(); defer { lock.unlock() }
|
|
24
|
+
files[path] = data
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public func names(at path: String) -> [String] {
|
|
28
|
+
lock.lock(); defer { lock.unlock() }
|
|
29
|
+
let prefix = path.isEmpty ? "" : path + "/"
|
|
30
|
+
var seen: [String] = []
|
|
31
|
+
for key in files.keys {
|
|
32
|
+
guard key.hasPrefix(prefix) else { continue }
|
|
33
|
+
let rest = key.dropFirst(prefix.count)
|
|
34
|
+
guard let name = rest.split(separator: "/").first else { continue }
|
|
35
|
+
let text = String(name)
|
|
36
|
+
if !seen.contains(text) { seen.append(text) }
|
|
37
|
+
}
|
|
38
|
+
return seen.sorted()
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public func remove(at path: String) {
|
|
42
|
+
lock.lock(); defer { lock.unlock() }
|
|
43
|
+
let prefix = path + "/"
|
|
44
|
+
files = files.filter { $0.key != path && !$0.key.hasPrefix(prefix) }
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
public func exists(_ path: String) -> Bool {
|
|
48
|
+
lock.lock(); defer { lock.unlock() }
|
|
49
|
+
if files[path] != nil { return true }
|
|
50
|
+
let prefix = path + "/"
|
|
51
|
+
return files.keys.contains { $0.hasPrefix(prefix) }
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
public protocol MountTree: Sendable {
|
|
56
|
+
func rootIdentifier() async -> UUID
|
|
57
|
+
func attributes(_ id: UUID) async throws -> NodeAttr
|
|
58
|
+
func lookup(_ parent: UUID, name: String) async throws -> NodeAttr
|
|
59
|
+
func directory(_ parent: UUID) async throws -> [NodeAttr]
|
|
60
|
+
func read(_ id: UUID, offset: Int, count: Int) async throws -> Data
|
|
61
|
+
func write(_ id: UUID, offset: Int, data: Data) async throws -> NodeAttr
|
|
62
|
+
func create(_ parent: UUID, name: String, data: Data) async throws -> NodeAttr
|
|
63
|
+
func makeDirectory(_ parent: UUID, name: String) async throws -> NodeAttr
|
|
64
|
+
func remove(_ parent: UUID, name: String, expectedGeneration: Int) async throws
|
|
65
|
+
func rename(from parent: UUID, name: String, toParent: UUID, toName: String) async throws
|
|
66
|
+
func commit(_ id: UUID) async throws -> NodeAttr
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
public actor SyncTree: MountTree {
|
|
70
|
+
public let spaceId: String
|
|
71
|
+
public let readOnly: Bool
|
|
72
|
+
private let remote: any FileRemote
|
|
73
|
+
private let bytes: any FileBytePlane
|
|
74
|
+
private let scratch: any ScratchStore
|
|
75
|
+
private let excludes: Set<String>
|
|
76
|
+
private var nodes: [UUID: NodeAttr] = [:]
|
|
77
|
+
private var paths: [UUID: String] = [:]
|
|
78
|
+
private var dirty: [UUID: Data] = [:]
|
|
79
|
+
private var localIDs: [String: UUID] = [:]
|
|
80
|
+
private var rootID: UUID?
|
|
81
|
+
private var conflicts: [ConflictCopy] = []
|
|
82
|
+
private var generations = StatCache()
|
|
83
|
+
private var blocks: BlockCache
|
|
84
|
+
|
|
85
|
+
public init(
|
|
86
|
+
spaceId: String, readOnly: Bool, remote: any FileRemote, bytes: any FileBytePlane = UnavailableBytePlane(),
|
|
87
|
+
scratch: any ScratchStore = MemoryScratch(), excludes: Set<String> = MountExcludes.defaults, cacheSlots: Int = 8
|
|
88
|
+
) {
|
|
89
|
+
self.spaceId = spaceId
|
|
90
|
+
self.readOnly = readOnly
|
|
91
|
+
self.remote = remote
|
|
92
|
+
self.bytes = bytes
|
|
93
|
+
self.scratch = scratch
|
|
94
|
+
self.excludes = excludes
|
|
95
|
+
self.blocks = BlockCache(capacity: cacheSlots)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
public func open(path: String) async throws {
|
|
99
|
+
let remotePath = path.isEmpty ? "/" : path
|
|
100
|
+
let node = try await remote.stat(spaceId: spaceId, path: remotePath, inodeId: nil)
|
|
101
|
+
nodes[node.id] = node
|
|
102
|
+
paths[node.id] = ""
|
|
103
|
+
rootID = node.id
|
|
104
|
+
generations.store(inodeId: node.idString, generation: node.generation)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
public func rootIdentifier() async -> UUID { rootID ?? UUID() }
|
|
108
|
+
|
|
109
|
+
public func conflictCopies() -> [ConflictCopy] { conflicts }
|
|
110
|
+
|
|
111
|
+
public func apply(change: FileChange) {
|
|
112
|
+
generations.invalidate(inodeId: change.inodeId)
|
|
113
|
+
if let id = UUID(uuidString: change.inodeId) {
|
|
114
|
+
nodes.removeValue(forKey: id)
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
public func dropAuthority() {
|
|
119
|
+
generations.invalidateAll()
|
|
120
|
+
blocks.removeAll()
|
|
121
|
+
nodes.removeAll()
|
|
122
|
+
if let rootID { nodes[rootID] = nil }
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
public func attributes(_ id: UUID) async throws -> NodeAttr {
|
|
126
|
+
if let path = localPath(id), MountExcludes.excluded(path, names: excludes) {
|
|
127
|
+
return localAttr(id: id, path: path)
|
|
128
|
+
}
|
|
129
|
+
if let data = dirty[id], var node = nodes[id] {
|
|
130
|
+
node.size = data.count
|
|
131
|
+
return node
|
|
132
|
+
}
|
|
133
|
+
return try await refresh(id)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
public func lookup(_ parent: UUID, name: String) async throws -> NodeAttr {
|
|
137
|
+
try checkName(name)
|
|
138
|
+
let path = childPath(parent, name)
|
|
139
|
+
if MountExcludes.excluded(path, names: excludes) {
|
|
140
|
+
guard scratch.exists(path) else { throw TreeError.notFound }
|
|
141
|
+
return try bindLocal(path: path, name: name, parent: parent)
|
|
142
|
+
}
|
|
143
|
+
if let cached = try await directory(parent).first(where: { $0.name == name }) {
|
|
144
|
+
return try await refresh(cached.id)
|
|
145
|
+
}
|
|
146
|
+
throw TreeError.notFound
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
public func directory(_ parent: UUID) async throws -> [NodeAttr] {
|
|
150
|
+
let parentPath = paths[parent] ?? ""
|
|
151
|
+
if MountExcludes.excluded(parentPath, names: excludes) || localIDs[parentPath] == parent {
|
|
152
|
+
return scratch.names(at: parentPath).map { name in
|
|
153
|
+
let path = parentPath.isEmpty ? name : parentPath + "/" + name
|
|
154
|
+
return (try? bindLocal(path: path, name: name, parent: parent)) ?? localAttr(id: UUID(), path: path)
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
let parentNode = try await attributes(parent)
|
|
158
|
+
guard parentNode.kind == .directory else { throw TreeError.invalid }
|
|
159
|
+
var cursor: String?
|
|
160
|
+
var entries: [NodeAttr] = []
|
|
161
|
+
repeat {
|
|
162
|
+
let page = try await remote.list(spaceId: spaceId, inodeId: parentNode.idString, cursor: cursor)
|
|
163
|
+
for entry in page.entries where !excludes.contains(entry.name) {
|
|
164
|
+
let child = entry
|
|
165
|
+
let path = parentPath.isEmpty ? entry.name : parentPath + "/" + entry.name
|
|
166
|
+
paths[child.id] = path
|
|
167
|
+
nodes[child.id] = child
|
|
168
|
+
generations.store(inodeId: child.idString, generation: child.generation)
|
|
169
|
+
entries.append(child)
|
|
170
|
+
}
|
|
171
|
+
cursor = page.nextCursor
|
|
172
|
+
} while cursor != nil
|
|
173
|
+
for name in scratch.names(at: parentPath) where excludes.contains(name) {
|
|
174
|
+
let path = parentPath.isEmpty ? name : parentPath + "/" + name
|
|
175
|
+
entries.append(try bindLocal(path: path, name: name, parent: parent))
|
|
176
|
+
}
|
|
177
|
+
return entries.sorted { $0.name < $1.name }
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
public func read(_ id: UUID, offset: Int, count: Int) async throws -> Data {
|
|
181
|
+
if let path = localPath(id) {
|
|
182
|
+
let data = scratch.data(at: path) ?? Data()
|
|
183
|
+
return slice(data, offset: offset, count: count)
|
|
184
|
+
}
|
|
185
|
+
let data = try await buffer(id)
|
|
186
|
+
return slice(data, offset: offset, count: count)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
public func write(_ id: UUID, offset: Int, data: Data) async throws -> NodeAttr {
|
|
190
|
+
try requireWrite()
|
|
191
|
+
if let path = localPath(id) {
|
|
192
|
+
var body = scratch.data(at: path) ?? Data()
|
|
193
|
+
splice(&body, offset: offset, data: data)
|
|
194
|
+
scratch.set(body, at: path)
|
|
195
|
+
return localAttr(id: id, path: path)
|
|
196
|
+
}
|
|
197
|
+
var body = try await buffer(id)
|
|
198
|
+
splice(&body, offset: offset, data: data)
|
|
199
|
+
dirty[id] = body
|
|
200
|
+
var node = try await attributes(id)
|
|
201
|
+
node.size = body.count
|
|
202
|
+
nodes[id] = node
|
|
203
|
+
return node
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
public func create(_ parent: UUID, name: String, data: Data) async throws -> NodeAttr {
|
|
207
|
+
try requireWrite()
|
|
208
|
+
try checkName(name)
|
|
209
|
+
let path = childPath(parent, name)
|
|
210
|
+
if MountExcludes.excluded(path, names: excludes) {
|
|
211
|
+
scratch.set(data, at: path)
|
|
212
|
+
return try bindLocal(path: path, name: name, parent: parent)
|
|
213
|
+
}
|
|
214
|
+
let parentNode = try await attributes(parent)
|
|
215
|
+
guard parentNode.access.canWrite else { throw TreeError.access }
|
|
216
|
+
return try await storeNew(parent: parentNode, name: name, path: path, data: data)
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
public func makeDirectory(_ parent: UUID, name: String) async throws -> NodeAttr {
|
|
220
|
+
try requireWrite()
|
|
221
|
+
try checkName(name)
|
|
222
|
+
let path = childPath(parent, name)
|
|
223
|
+
if MountExcludes.excluded(path, names: excludes) {
|
|
224
|
+
scratch.set(Data(), at: path + "/")
|
|
225
|
+
return try bindLocal(path: path, name: name, parent: parent, directory: true)
|
|
226
|
+
}
|
|
227
|
+
let parentNode = try await attributes(parent)
|
|
228
|
+
guard parentNode.access.canWrite else { throw TreeError.access }
|
|
229
|
+
do {
|
|
230
|
+
let created = try await remote.mkdir(spaceId: spaceId, parentId: parentNode.idString, name: name)
|
|
231
|
+
remember(created, path: path)
|
|
232
|
+
return created
|
|
233
|
+
} catch {
|
|
234
|
+
try rethrow(error)
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
public func remove(_ parent: UUID, name: String, expectedGeneration: Int) async throws {
|
|
239
|
+
try requireWrite()
|
|
240
|
+
let path = childPath(parent, name)
|
|
241
|
+
if MountExcludes.excluded(path, names: excludes) {
|
|
242
|
+
scratch.remove(at: path)
|
|
243
|
+
localIDs.removeValue(forKey: path)
|
|
244
|
+
return
|
|
245
|
+
}
|
|
246
|
+
let node = try await lookup(parent, name: name)
|
|
247
|
+
if node.kind == .directory {
|
|
248
|
+
let children = try await directory(node.id)
|
|
249
|
+
if !children.isEmpty { throw TreeError.notEmpty }
|
|
250
|
+
}
|
|
251
|
+
do {
|
|
252
|
+
try await remote.remove(spaceId: spaceId, inodeId: node.idString, expectedGeneration: expectedGeneration)
|
|
253
|
+
nodes.removeValue(forKey: node.id)
|
|
254
|
+
dirty.removeValue(forKey: node.id)
|
|
255
|
+
} catch {
|
|
256
|
+
try rethrow(error)
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
public func rename(from parent: UUID, name: String, toParent: UUID, toName: String) async throws {
|
|
261
|
+
try requireWrite()
|
|
262
|
+
try checkName(toName)
|
|
263
|
+
let fromPath = childPath(parent, name)
|
|
264
|
+
let toPath = childPath(toParent, toName)
|
|
265
|
+
if MountExcludes.excluded(fromPath, names: excludes) || MountExcludes.excluded(toPath, names: excludes) {
|
|
266
|
+
let data = scratch.data(at: fromPath) ?? Data()
|
|
267
|
+
scratch.remove(at: fromPath)
|
|
268
|
+
scratch.set(data, at: toPath)
|
|
269
|
+
localIDs.removeValue(forKey: fromPath)
|
|
270
|
+
_ = try bindLocal(path: toPath, name: toName, parent: toParent)
|
|
271
|
+
return
|
|
272
|
+
}
|
|
273
|
+
let node = try await lookup(parent, name: name)
|
|
274
|
+
let destination = try await attributes(toParent)
|
|
275
|
+
do {
|
|
276
|
+
let moved = try await remote.rename(
|
|
277
|
+
spaceId: spaceId, inodeId: node.idString, name: toName, expectedGeneration: node.generation,
|
|
278
|
+
parentId: parent == toParent ? nil : destination.idString)
|
|
279
|
+
paths[moved.id] = toPath
|
|
280
|
+
nodes[moved.id] = moved
|
|
281
|
+
} catch {
|
|
282
|
+
try rethrow(error)
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
public func commit(_ id: UUID) async throws -> NodeAttr {
|
|
287
|
+
if localPath(id) != nil { return try await attributes(id) }
|
|
288
|
+
guard let data = dirty[id] else { return try await attributes(id) }
|
|
289
|
+
let node = try await attributes(id)
|
|
290
|
+
do {
|
|
291
|
+
let saved = try await put(node, data: data)
|
|
292
|
+
dirty.removeValue(forKey: id)
|
|
293
|
+
remember(saved, path: paths[id] ?? "")
|
|
294
|
+
return saved
|
|
295
|
+
} catch let error as TreeError where error == .conflict {
|
|
296
|
+
return try await conflicted(id, local: data)
|
|
297
|
+
} catch {
|
|
298
|
+
try rethrow(error)
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
public func resolve(_ id: String, keep: ConflictChoice) async throws {
|
|
303
|
+
guard let index = conflicts.firstIndex(where: { $0.id == id }) else { throw TreeError.notFound }
|
|
304
|
+
let conflict = conflicts[index]
|
|
305
|
+
switch keep {
|
|
306
|
+
case .server:
|
|
307
|
+
conflicts.remove(at: index)
|
|
308
|
+
case .mine:
|
|
309
|
+
guard let inode = UUID(uuidString: conflict.inodeId) else { throw TreeError.notFound }
|
|
310
|
+
let node = NodeAttr(
|
|
311
|
+
id: inode, name: conflict.originalName, kind: .file, size: conflict.localBytes.count,
|
|
312
|
+
generation: conflict.baseGeneration, mode: 0o644, mtime: Date(timeIntervalSince1970: 0))
|
|
313
|
+
let saved = try await put(node, data: conflict.localBytes)
|
|
314
|
+
if let fresh = conflicts.firstIndex(where: { $0.id == id }) { conflicts.remove(at: fresh) }
|
|
315
|
+
remember(saved, path: paths[inode] ?? paths[saved.id] ?? "")
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
private func put(_ node: NodeAttr, data: Data) async throws -> NodeAttr {
|
|
320
|
+
try await publish(data, inodeId: node.idString, directory: nil, name: nil, generation: node.generation, path: nil)
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
private func storeNew(parent: NodeAttr, name: String, path: String, data: Data) async throws -> NodeAttr {
|
|
324
|
+
try await publish(
|
|
325
|
+
data, inodeId: nil, directory: apiDirectory(parent), name: name, generation: nil, path: path)
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
private func publish(
|
|
329
|
+
_ data: Data, inodeId: String?, directory: String?, name: String?, generation: Int?, path: String?
|
|
330
|
+
) async throws -> NodeAttr {
|
|
331
|
+
if utf8Text(data) != nil, data.count <= MountLayout.inlineBytes {
|
|
332
|
+
do {
|
|
333
|
+
let saved = try await remote.writeInline(
|
|
334
|
+
spaceId: spaceId, path: directory, name: name, inodeId: inodeId, bytes: data,
|
|
335
|
+
expectedGeneration: generation, idempotencyKey: UUID().uuidString)
|
|
336
|
+
if let path { remember(saved, path: path) }
|
|
337
|
+
return saved
|
|
338
|
+
} catch {
|
|
339
|
+
try rethrow(error)
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
guard let packed = FilePacker.pack(data) else { throw TreeError.unsupported }
|
|
343
|
+
for object in packed.objects {
|
|
344
|
+
do { try await bytes.putSlice(spaceId: spaceId, sha256: object.sha, body: object.body) }
|
|
345
|
+
catch { try rethrow(error) }
|
|
346
|
+
}
|
|
347
|
+
do {
|
|
348
|
+
let saved = try await remote.commitSlices(
|
|
349
|
+
spaceId: spaceId, inodeId: inodeId, path: directory, name: name, expectedGeneration: generation, packed: packed)
|
|
350
|
+
if let path { remember(saved, path: path) }
|
|
351
|
+
return saved
|
|
352
|
+
} catch {
|
|
353
|
+
try rethrow(error)
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
private func conflicted(_ id: UUID, local: Data) async throws -> NodeAttr {
|
|
358
|
+
let previous = nodes[id]
|
|
359
|
+
let name = previous?.name ?? "file"
|
|
360
|
+
nodes.removeValue(forKey: id)
|
|
361
|
+
generations.invalidate(inodeId: id.uuidString.lowercased())
|
|
362
|
+
let current = try await refresh(id)
|
|
363
|
+
let copy = ConflictName.make(original: name, id: UUID().uuidString)
|
|
364
|
+
let record = ConflictCopy(
|
|
365
|
+
id: UUID().uuidString, inodeId: id.uuidString.lowercased(), baseGeneration: current.generation,
|
|
366
|
+
originalName: name, copyName: copy, localBytes: local)
|
|
367
|
+
conflicts.append(record)
|
|
368
|
+
dirty.removeValue(forKey: id)
|
|
369
|
+
if utf8Text(local) != nil, let parent = paths[id].map({ parentPath($0) }), let parentID = nodes.first(where: { paths[$0.key] == parent })?.key {
|
|
370
|
+
_ = try? await create(parentID, name: copy, data: local)
|
|
371
|
+
}
|
|
372
|
+
return current
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
private func buffer(_ id: UUID) async throws -> Data {
|
|
376
|
+
if let data = dirty[id] { return data }
|
|
377
|
+
let node = try await attributes(id)
|
|
378
|
+
guard node.kind == .file else { throw TreeError.invalid }
|
|
379
|
+
if node.size == 0 { return Data() }
|
|
380
|
+
if node.size > MountLayout.inlineBytes {
|
|
381
|
+
return try await cachedRange(node, offset: 0, count: node.size)
|
|
382
|
+
}
|
|
383
|
+
do { return try await remote.readInline(spaceId: spaceId, inodeId: node.idString) }
|
|
384
|
+
catch { try rethrow(error) }
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
private func cachedRange(_ node: NodeAttr, offset: Int, count: Int) async throws -> Data {
|
|
388
|
+
let key = "\(node.contentId ?? node.idString):\(offset / MountLayout.sliceBytes)"
|
|
389
|
+
if let hit = blocks.get(key), offset == 0, count <= hit.count { return hit.prefix(count) }
|
|
390
|
+
do {
|
|
391
|
+
let data = try await bytes.readRange(spaceId: spaceId, inodeId: node.idString, offset: offset, length: count)
|
|
392
|
+
blocks.set(key, data)
|
|
393
|
+
return data
|
|
394
|
+
} catch {
|
|
395
|
+
try rethrow(error)
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
private func refresh(_ id: UUID) async throws -> NodeAttr {
|
|
400
|
+
do {
|
|
401
|
+
let node = try await remote.stat(spaceId: spaceId, path: nil, inodeId: id.uuidString.lowercased())
|
|
402
|
+
let path = paths[id] ?? ""
|
|
403
|
+
remember(node, path: path)
|
|
404
|
+
return node
|
|
405
|
+
} catch {
|
|
406
|
+
try rethrow(error)
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
private func remember(_ node: NodeAttr, path: String) {
|
|
411
|
+
nodes[node.id] = node
|
|
412
|
+
paths[node.id] = path
|
|
413
|
+
generations.store(inodeId: node.idString, generation: node.generation)
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
private func localPath(_ id: UUID) -> String? {
|
|
417
|
+
localIDs.first { $0.value == id }?.key
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
private func bindLocal(path: String, name: String, parent: UUID, directory: Bool = false) throws -> NodeAttr {
|
|
421
|
+
let id = localIDs[path] ?? UUID()
|
|
422
|
+
localIDs[path] = id
|
|
423
|
+
paths[id] = path
|
|
424
|
+
let data = scratch.data(at: path) ?? Data()
|
|
425
|
+
let marker = scratch.data(at: path + "/") != nil
|
|
426
|
+
let listed = scratch.names(at: path).isEmpty == false && scratch.data(at: path) == nil
|
|
427
|
+
let kind: NodeKind = directory || marker || listed ? .directory : .file
|
|
428
|
+
let node = NodeAttr(
|
|
429
|
+
id: id, name: name, kind: kind, size: kind == .directory ? 0 : data.count, generation: 1, mode: 0o644,
|
|
430
|
+
mtime: Date(timeIntervalSince1970: 0))
|
|
431
|
+
nodes[id] = node
|
|
432
|
+
_ = parent
|
|
433
|
+
return node
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
private func localAttr(id: UUID, path: String) -> NodeAttr {
|
|
437
|
+
let name = path.split(separator: "/").last.map(String.init) ?? path
|
|
438
|
+
let data = scratch.data(at: path) ?? Data()
|
|
439
|
+
let directory = scratch.names(at: path).isEmpty == false && data.isEmpty
|
|
440
|
+
return NodeAttr(
|
|
441
|
+
id: id, name: name, kind: directory ? .directory : .file, size: data.count, generation: 1, mode: 0o644,
|
|
442
|
+
mtime: Date(timeIntervalSince1970: 0))
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
private func childPath(_ parent: UUID, _ name: String) -> String {
|
|
446
|
+
let base = paths[parent] ?? ""
|
|
447
|
+
return base.isEmpty ? name : base + "/" + name
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
private func apiDirectory(_ parent: NodeAttr) -> String {
|
|
451
|
+
let path = paths[parent.id] ?? ""
|
|
452
|
+
return path.isEmpty ? "/" : "/" + path
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
private func parentPath(_ path: String) -> String {
|
|
456
|
+
guard let slash = path.lastIndex(of: "/") else { return "" }
|
|
457
|
+
return String(path[..<slash])
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
private func requireWrite() throws {
|
|
461
|
+
if readOnly { throw TreeError.readOnly }
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
private func checkName(_ name: String) throws {
|
|
465
|
+
if name.isEmpty || name == "." || name == ".." || name.contains("/") || name.utf8.count > 255 {
|
|
466
|
+
throw TreeError.invalid
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
private func rethrow(_ error: Error) throws -> Never {
|
|
471
|
+
if let tree = error as? TreeError {
|
|
472
|
+
if tree == .access { generations.invalidateAll(); blocks.removeAll() }
|
|
473
|
+
throw tree
|
|
474
|
+
}
|
|
475
|
+
if let adoor = error as? AdoorError {
|
|
476
|
+
let tree = TreeError(adoor: adoor)
|
|
477
|
+
if tree == .access { generations.invalidateAll(); blocks.removeAll() }
|
|
478
|
+
throw tree
|
|
479
|
+
}
|
|
480
|
+
throw TreeError.unavailable("NETWORK")
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
private func utf8Text(_ data: Data) -> String? {
|
|
485
|
+
guard let text = String(data: data, encoding: .utf8), Data(text.utf8) == data else { return nil }
|
|
486
|
+
return text
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
private func slice(_ data: Data, offset: Int, count: Int) -> Data {
|
|
490
|
+
guard offset < data.count, count > 0 else { return Data() }
|
|
491
|
+
let end = min(data.count, offset + count)
|
|
492
|
+
guard offset < end else { return Data() }
|
|
493
|
+
return data.subdata(in: offset..<end)
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
private func splice(_ body: inout Data, offset: Int, data: Data) {
|
|
497
|
+
if offset > body.count { body.append(Data(repeating: 0, count: offset - body.count)) }
|
|
498
|
+
let end = min(body.count, offset + data.count)
|
|
499
|
+
if offset < body.count { body.replaceSubrange(offset..<end, with: data.prefix(end - offset)) }
|
|
500
|
+
if offset + data.count > body.count { body.append(data.dropFirst(max(0, body.count - offset))) }
|
|
501
|
+
}
|