forked from blinksh/blink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileSystem.swift
550 lines (486 loc) · 18.5 KB
/
FileSystem.swift
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
//////////////////////////////////////////////////////////////////////////////////
//
// B L I N K
//
// Copyright (C) 2016-2019 Blink Mobile Shell Project
//
// This file is part of Blink.
//
// Blink 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 3 of the License, or
// (at your option) any later version.
//
// Blink 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.
//
// You should have received a copy of the GNU General Public License
// along with Blink. If not, see <http://www.gnu.org/licenses/>.
//
// In addition, Blink is also subject to certain additional terms under
// GNU GPL version 3 section 7.
//
// You should have received a copy of these additional terms immediately
// following the terms and conditions of the GNU General Public License
// which accompanied the Blink Source Code. If not, see
// <http://www.github.com/blinksh/blink>.
//
////////////////////////////////////////////////////////////////////////////////
import Combine
import Foundation
import Network
import BlinkFiles
import BlinkConfig
import System
import SwiftUI
struct MountEntry: Codable {
let name: String
let root: String
}
class TranslatorReference {
let translator: Translator
let cancel: () -> Void
init(_ translator: Translator, cancel: @escaping (() -> Void)) {
self.translator = translator
self.cancel = cancel
}
deinit {
cancel()
}
}
public class CodeFileSystemService: CodeSocketDelegate {
let server: WebSocketServer
let log: BlinkLogger
public let port: UInt16
var tokens: [Int: MountEntry] = [:]
var tokenIdx = 0;
private var translators: [String: TranslatorReference] = [:]
private let finishedCallback: ((Error?) -> ())
func finished(_ error: Error?) { finishedCallback(error) }
public var state: NWListener.State {
server.listener.state
}
public func registerMount(name: String, root: String) -> Int {
tokenIdx += 1
tokens[tokenIdx] = MountEntry(name: name, root: root)
log.info("Registered mount \(tokenIdx) for \(name) at \(root)")
return tokenIdx
}
public func deregisterMount(_ token: Int) {
log.info("De-registering mount \(tokenIdx)")
// If no other token is using the same translator, trash it
guard let token = tokens.removeValue(forKey: tokenIdx) else {
return
}
let root = URL(string: token.root)!
// If we have no host, there is no remote translator
guard let host = root.host,
let _ = translators[host] else {
return
}
// Remove the Translator if no other mounts use it.
if let _ = tokens.first(where: { (_, tk) in
let url = URL(string: tk.root)!
return host == url.host
}) {
return
}
translators.removeValue(forKey: host)
}
public init(listenOn port: NWEndpoint.Port, tls: Bool, finished: @escaping ((Error?) -> ())) throws {
self.port = port.rawValue
self.server = try WebSocketServer(listenOn: port, tls: tls)
self.finishedCallback = finished
self.log = CodeFileSystemLogger.log("FileSystem")
self.server.delegate = self
}
func getRoot(token: Int, version: Int) -> WebSocketServer.ResponsePublisher {
if let mount = self.tokens[token] {
return .just((try! JSONEncoder().encode(mount), nil)).eraseToAnyPublisher()
} else {
return .just((nil, nil)).eraseToAnyPublisher()
}
}
public func handleMessage(encodedData: Data, binaryData: Data?) -> WebSocketServer.ResponsePublisher {
guard let request = try? JSONDecoder().decode(BaseFileSystemRequest.self, from: encodedData) else {
log.error(String(data: encodedData, encoding: .utf8) ?? "Error decoding data")
return .fail(error: WebSocketError(message: "Bad request"))
}
do {
switch request.op {
case .getRoot:
let msg: GetRootRequest = try decode(encodedData)
return try getRoot(token: msg.token, version: msg.version)
case .stat:
let msg: StatFileSystemRequest = try decode(encodedData)
return try fileSystem(for: msg.uri).stat()
case .readDirectory:
let msg: ReadDirectoryFileSystemRequest = try decode(encodedData)
return try fileSystem(for: msg.uri).readDirectory()
case .readFile:
let msg: ReadFileFileSystemRequest = try decode(encodedData)
return try fileSystem(for: msg.uri).readFile()
case .writeFile:
let msg: WriteFileSystemRequest = try decode(encodedData)
return try fileSystem(for: msg.uri).writeFile(options: msg.options,
content: binaryData ?? Data())
case .createDirectory:
let msg: CreateDirectoryFileSystemRequest = try decode(encodedData)
return try fileSystem(for: msg.uri).createDirectory()
case .rename:
let msg: RenameFileSystemRequest = try decode(encodedData)
return try fileSystem(for: msg.oldUri).rename(newUri: msg.newUri,
options: msg.options)
case .delete:
let msg: DeleteFileSystemRequest = try decode(encodedData)
return try fileSystem(for: msg.uri).delete(options: msg.options)
}
} catch {
log.error("\(error)")
log.error("Processing request \(String(data: encodedData, encoding: .utf8) ?? "Error decoding data")")
return .fail(error: error)
}
}
private func fileSystem(for uri: URI) throws -> CodeFileSystem {
let rootPath = uri.rootPath
if let host = rootPath.host,
let tRef = translators[host],
tRef.translator.isConnected {
return CodeFileSystem(.just(tRef.translator), uri: uri)
}
switch(rootPath.protocolIdentifier) {
case "blinksftp":
let builder = TranslatorFactories.sftp
var thread: Thread!
var runLoop: RunLoop? = nil
let threadIsReady = Future<RunLoop, Error> { promise in
thread = Thread {
let timer = Timer(timeInterval: TimeInterval(1), repeats: true) { _ in
//print("timer")
}
runLoop = RunLoop.current
RunLoop.current.add(timer, forMode: .default)
promise(.success(RunLoop.current))
CFRunLoopRun()
// Wrap it up
RunLoop.current.run(until: Date(timeIntervalSinceNow: 0.5))
}
thread.start()
}
guard let hostAlias = rootPath.host else {
throw WebSocketError(message: "Missing host on rootpath")
}
let translator = threadIsReady
.flatMap { builder.buildOn($0, hostAlias: hostAlias) }
.map { t -> Translator in
self.translators[rootPath.host!] = TranslatorReference(t, cancel: {
self.log.debug("Cancelling translator")
let cfRunLoop = runLoop!.getCFRunLoop()
CFRunLoopStop(cfRunLoop)
})
return t
}
.eraseToAnyPublisher()
return CodeFileSystem(translator, uri: uri)
case "blinkfs":
// The local one does not need to be saved.
return CodeFileSystem(TranslatorFactories.local.build(rootPath), uri: uri)
default:
throw WebSocketError(message: "Unknown protocol - \(rootPath.protocolIdentifier)")
}
}
}
class CodeFileSystem {
private let translator: AnyPublisher<Translator, Error>
private let uri: URI
private let log: BlinkLogger
init(_ t: AnyPublisher<Translator, Error>, uri: URI) {
self.translator = t
self.uri = uri
self.log = CodeFileSystemLogger.log("\(uri.rootPath.host)")
}
func stat() -> WebSocketServer.ResponsePublisher {
let path = self.uri.rootPath.filesAtPath
self.log.debug("stat \(path)")
return translator
.flatMap {
$0.cloneWalkTo(path)
.mapError { _ in
return CodeFileSystemError.fileNotFound(uri: self.uri)
}
}
.flatMap { $0.stat() }
.map { attrs -> FileStat in
self.log.debug("stat \(path) called.")
var mtimeMillis = 0
var ctimeMillis = 0
if let mtime = attrs[.modificationDate] as? NSDate {
mtimeMillis = Int(mtime.timeIntervalSince1970)
}
if let createtime = attrs[.creationDate] as? NSDate {
ctimeMillis = Int(createtime.timeIntervalSince1970)
}
return FileStat(type: FileType(posixType: attrs[.type] as? FileAttributeType),
ctime: ctimeMillis,
mtime: mtimeMillis,
size: attrs[.size] as? Int)
}
.tryMap { (try JSONEncoder().encode($0), nil) }
.eraseToAnyPublisher()
}
func readDirectory() -> WebSocketServer.ResponsePublisher {
let path = self.uri.rootPath.filesAtPath
self.log.debug("readDirectory \(path)")
return translator
.flatMap { $0.cloneWalkTo(path) }
.flatMap { $0.directoryFilesAndAttributesResolvingLinks() }
.map { filesAttributes -> [DirectoryTuple] in
self.log.debug("readDirectory \(path) called. \(filesAttributes.count) items.")
return filesAttributes
.filter { ($0[.name] as! String != "." && $0[.name] as! String != "..") }
.map {
DirectoryTuple(name: $0[.name] as! String,
type: FileType(posixType: $0[.type] as? FileAttributeType))
}
}
.tryMap { (try JSONEncoder().encode($0), nil) }
.eraseToAnyPublisher()
}
func readFile() -> WebSocketServer.ResponsePublisher {
let path = self.uri.rootPath.filesAtPath
self.log.debug("readFile \(path)")
return translator
.flatMap {
$0.cloneWalkTo(path)
.mapError { _ in CodeFileSystemError.fileNotFound(uri: self.uri) }
}
.flatMap { $0.open(flags: O_RDONLY) }
.flatMap { file -> AnyPublisher<DispatchData, Error> in
var content = DispatchData.empty
return file
.read(max: Int(INT32_MAX))
.flatMap { dd -> AnyPublisher<Bool, Error> in
content = dd
return file.close()
}
.map { _ in content }.eraseToAnyPublisher()
}
.map { dd -> (Data?, Data?) in
var result = Data(count: dd.count)
result.withUnsafeMutableBytes { buf in
_ = dd.copyBytes(to: buf)
}
self.log.debug("readFile \(path) completed. Read \(dd.count) bytes.")
return (nil, result)
}
.eraseToAnyPublisher()
}
func writeFile(options: FileSystemOperationOptions, content: Data) -> WebSocketServer.ResponsePublisher {
let path = self.uri.rootPath.filesAtPath
self.log.debug("writeFile \(path)")
let parentDir = (path as NSString).deletingLastPathComponent
let fileName = (path as NSString).lastPathComponent
return translator
.flatMap { $0.cloneWalkTo(parentDir) }
// 1. If the file exists, then check overwrite. Otherwise create if create flag is set.
.flatMap { parentT -> WebSocketServer.ResponsePublisher in
parentT.cloneWalkTo(fileName)
.flatMap { fileT -> AnyPublisher<File, Error> in
// [`FileExists`](#FileSystemError.FileExists) when `uri` already exists and `overwrite` is set.
// NOTE From testing, looks like docs should say 'overwrite' is NOT set.
if !(options.overwrite ?? false) {
return .fail(error: CodeFileSystemError.fileExists(uri: self.uri))
}
return fileT.open(flags: O_WRONLY | O_TRUNC)
}
.tryCatch { error -> AnyPublisher<BlinkFiles.File, Error> in
if case CodeFileSystemError.fileExists = error {
throw error
}
// [`FileNotFound`](#FileSystemError.FileNotFound) when `uri` doesn't exist and `create` is not set.
if !(options.create ?? false) {
return .fail(error: CodeFileSystemError.fileNotFound(uri: self.uri))
}
return parentT.create(name: fileName, flags: O_WRONLY, mode: 0o644)
}
// 2. Write the content to the file
.flatMap { file -> AnyPublisher<Int, Error> in
if content.isEmpty {
return file.close()
.map { _ in
0 }
.eraseToAnyPublisher()
}
return file.write(content.withUnsafeBytes { DispatchData(bytes: $0) }, max: content.count)
.reduce(0, { count, written -> Int in
return count + written
})
.flatMap { wrote -> AnyPublisher<Int, Error> in
self.log.debug("writeFile \(path) completed. Wrote \(wrote) bytes.")
return file.close().map { _ in wrote }.eraseToAnyPublisher()
}
.eraseToAnyPublisher()
}
// 3. Resolve once everything copied. Just collect but output nothing.
.map { _ in (nil, nil) }
.eraseToAnyPublisher()
}.eraseToAnyPublisher()
}
func createDirectory() -> WebSocketServer.ResponsePublisher {
let path = self.uri.rootPath.filesAtPath
self.log.debug("createDirectory \(path)")
let parentUri = URI(rootPath: self.uri.rootPath.parent)
let parent = parentUri.rootPath.filesAtPath
let dirName = (path as NSString).lastPathComponent
return translator
.flatMap {
$0.cloneWalkTo(parent)
.mapError { _ in CodeFileSystemError.fileNotFound(uri: parentUri) }
}
.flatMap { parentT -> AnyPublisher<Translator, Error> in
parentT.cloneWalkTo(dirName)
.tryMap { _ in throw CodeFileSystemError.fileExists(uri:self.uri) }
.tryCatch { error -> AnyPublisher<Translator, Error> in
if case CodeFileSystemError.fileExists = error {
throw error
}
return parentT
.mkdir(name: dirName, mode: S_IRWXU | S_IRWXG | S_IRWXO)
.mapError { _ in return CodeFileSystemError.noPermissions(uri: parentUri) }
.eraseToAnyPublisher()
}.eraseToAnyPublisher()
}
.map { _ in (nil, nil) }
.eraseToAnyPublisher()
}
func rename(newUri: URI, options: FileSystemOperationOptions) -> WebSocketServer.ResponsePublisher {
let path = self.uri.rootPath.filesAtPath
self.log.debug("rename \(path)")
// Walk to oldURI
// Walk to new parent
// Walk to file and apply overwrite
// Stat to new location. Or fail.
let newParent = newUri.rootPath.parent.filesAtPath
let newName = newUri.rootPath.url.lastPathComponent
return translator
.flatMap {
$0.cloneWalkTo(path)
.mapError { _ in CodeFileSystemError.fileNotFound(uri: self.uri) }
}
.flatMap { oldT in
self.translator.flatMap {
$0.cloneWalkTo(newParent)
.mapError { _ in CodeFileSystemError.fileNotFound(uri: URI(rootPath: newUri.rootPath.parent)) }
}
// Will take the easy route for now.
// We try the stat, and will figure out if in case it is a file, we have to
// remove it or what.
.flatMap { newParentT in
oldT.wstat([.name: (newParentT.current as NSString).appendingPathComponent(newName)])
}
}
.map { _ in (nil, nil) }
.eraseToAnyPublisher()
}
func delete(options: FileSystemOperationOptions) -> WebSocketServer.ResponsePublisher {
let path = self.uri.rootPath.filesAtPath
self.log.debug("delete \(path)")
let recursive = options.recursive ?? false
func delete(_ translators: [Translator]) -> AnyPublisher<Void, Error> {
translators.publisher
.flatMap(maxPublishers: .max(1)) { t -> AnyPublisher<Void, Error> in
print(t.current)
if t.fileType == .typeDirectory {
return [deleteDirectoryContent(t), AnyPublisher(t.rmdir().map {_ in})]
.compactMap { $0 }
.publisher
.flatMap(maxPublishers: .max(1)) { $0 }
.collect()
.map {_ in}
.eraseToAnyPublisher()
}
return AnyPublisher(t.remove().map { _ in })
}.eraseToAnyPublisher()
}
func deleteDirectoryContent(_ t: Translator) -> AnyPublisher<Void, Error>? {
if recursive == false {
return nil
}
return t.directoryFilesAndAttributes().flatMap {
$0.compactMap { i -> FileAttributes? in
if (i[.name] as! String) == "." || (i[.name] as! String) == ".." {
return nil
} else {
return i
}
}.publisher
}
.flatMap {
t.cloneWalkTo($0[.name] as! String) }
.collect()
.flatMap {
delete($0) }
.eraseToAnyPublisher()
}
return translator
.flatMap {
$0.cloneWalkTo(path)
.mapError { _ in CodeFileSystemError.fileNotFound(uri: self.uri) }
.flatMap { delete([$0]) }
}
.map { _ in (nil, nil) }
.eraseToAnyPublisher()
}
}
extension CodeFileSystemService {
func decode<T: Decodable>(_ encodedData: Data) throws -> T {
try JSONDecoder().decode(T.self, from: encodedData)
}
}
extension RootPath {
public var parent: RootPath {
return RootPath(url.deletingLastPathComponent())
// RootPath([protocolIdentifier,
// host,
// (filesAtPath as NSString).deletingLastPathComponent]
// .compactMap { $0 }
// .joined(separator: ":"))
}
}
struct CodeFileSystemLogger {
static var handler = [BlinkLogging.LogHandlerFactory]()
static func log(_ component: String) -> BlinkLogger {
if handler.isEmpty {
handler.append(
{
$0.format { [ $0[.component] as? String ?? "global",
$0[.message] as? String ?? ""
].joined(separator: " ") }
.sinkToOutput()
}
)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM dd YYYY, HH:mm:ss"
if let file = try? FileLogging(to: BlinkPaths.blinkCodeErrorLogURL()) {
handler.append(
{
try $0
.filter(logLevel: .debug)
.format {
[ "[\($0[.logLevel]!)]",
dateFormatter.string(from: Date()),
$0[.component] as? String ?? "global",
$0[.message] as? String ?? ""
].joined(separator: " ") }
.sinkToFile(file)
}
)
} else {
print("File logging not working")
}
}
return BlinkLogger(component, handlers: handler)
}
}