forked from IBM/mac-ibm-migration-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkConnection.swift
More file actions
835 lines (767 loc) · 48.8 KB
/
NetworkConnection.swift
File metadata and controls
835 lines (767 loc) · 48.8 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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
//
// NetworkConnection.swift
// IBM Data Shift
//
// Created by Simone Martorelli on 15/11/2023.
// © Copyright IBM Corp. 2023, 2025
// SPDX-License-Identifier: Apache2.0
//
// swiftlint:disable function_body_length type_body_length file_length
import Foundation
import Network
import Combine
/// Represents and manages a network connection, handling both incoming and outgoing data transfer.
final class NetworkConnection {
// MARK: - Constants
/// The actual network connection being managed.
let connection: NWConnection
/// Subject for publishing hostname changes of the connected device.
let onHostNameChange = PassthroughSubject<String, Never>()
/// Subject for publishing available space changes on the connected device.
let onAvailableSpaceChange = PassthroughSubject<Int, Never>()
/// Subject for publishing availability of the connected device to receive data.
let onReadyToReceive = PassthroughSubject<Bool, Never>()
/// Subject for publishing the receive of the migration metadata from the source device.
let onMigrationMetadataReceived = PassthroughSubject<Int, Never>()
/// Subject for publishing size of bytes received from file migration.
let onBytesReceived = PassthroughSubject<Int, Never>()
/// Subject for publishing size of bytes sent during file migration.
let onBytesSent = PassthroughSubject<Int, Never>()
/// Subject for publishing the completion of the migration.
let onMigrationCompleted = PassthroughSubject<Bool, Never>()
/// Subject for publishing new connection states.
let onNewConnectionState = PassthroughSubject<NWConnection.State, Never>()
/// Subject for publishing when a file have been sent.
let onFileSent = PassthroughSubject<Int, Never>()
// MARK: - Variables
/// Stores the hostname of the connected device and publishes changes to subscribers.
var connectedDeviceHostName: String = "" {
didSet {
self.onHostNameChange.send(connectedDeviceHostName)
}
}
/// Stores the available space on the connected device and publishes changes to subscribers.
var connectedDeviceAvailableSpace: Int = 0 {
didSet {
self.onAvailableSpaceChange.send(connectedDeviceAvailableSpace)
}
}
/// Stores the size of the migration in bytes.
var migrationSizeInBytes: Int = 0 {
didSet {
self.onMigrationMetadataReceived.send(migrationSizeInBytes)
}
}
var isMigrationCompleted: Bool = false {
didSet {
self.onMigrationCompleted.send(isMigrationCompleted)
if isMigrationCompleted {
self.connection.cancel()
}
}
}
/// Stores symbolic link messages that need to be sent to the connected device.
var symlinks: [SymbolicLinkMessage] = []
/// Provides the current state of the network connection.
var state: NWConnection.State {
return connection.state
}
var currentInterfaceType: NWInterface.InterfaceType? {
guard let currentPath = connection.currentPath else { return nil }
guard let currentInterface = currentPath.availableInterfaces.first(where: { currentPath.usesInterfaceType($0.type) }) else { return nil }
return currentInterface.type
}
// MARK: - Private Constants
/// Logger instance.
private let logger: MLogger = MLogger.main
// MARK: - Intializers
/// Initializes a new network connection for outgoing connections.
/// - Parameters:
/// - endpoint: The network endpoint to connect to.
/// - passcode: A passcode required for establishing the connection.
init(endpoint: NWEndpoint, withPasscode passcode: String) {
logger.log("networkConnection.initOutgoingConnection: endpoint \"\(endpoint.debugDescription)\"", type: .default)
let parameters = NWParameters(passcode: passcode)
connection = NWConnection(to: endpoint, using: parameters)
connection.pathUpdateHandler = { path in
self.logger.log("networkConnection.pathUpdateHandler: newPath \"\(path.debugDescription)\"")
}
connection.betterPathUpdateHandler = { betterPathAvailable in
self.logger.log("networkConnection.betterPathUpdateHandler: betterPathAvailable \"\(betterPathAvailable.description)\"")
}
connection.viabilityUpdateHandler = { available in
self.logger.log("networkConnection.viabilityUpdateHandler: isAvailable \"\(available.description)\"")
}
connection.stateUpdateHandler = { newState in
self.logger.log("networkConnection.stateUpdateHandler: newState \"\(String(describing: newState))\"", type: .default)
self.onNewConnectionState.send(newState)
if case .ready = newState {
if let metadata = self.connection.metadata(definition: NWProtocolTLS.definition) as? NWProtocolTLS.Metadata {
let version = sec_protocol_metadata_get_negotiated_tls_protocol_version(metadata.securityProtocolMetadata)
let suite = sec_protocol_metadata_get_negotiated_tls_ciphersuite(metadata.securityProtocolMetadata)
MLogger.main.log("Negotiated TLS version: \(version), suite: \(suite)", type: .debug)
}
if let interfaceTypeString = self.currentInterfaceType?.stringValue {
MigrationReportController.shared.setMigrationTransferMethod(interfaceTypeString)
}
self.receiveNextMessage()
Task {
try? await self.sendHostName()
}
}
}
}
/// Initializes a new network connection for handling incoming connections.
/// - Parameter connection: The incoming network connection.
init(connection: NWConnection) {
logger.log("networkConnection.initIncomingConnection: connection \"\(connection.debugDescription)\"", type: .default)
self.connection = connection
connection.stateUpdateHandler = { newState in
self.logger.log("networkConnection.stateUpdateHandler: new state \"\(String(describing: newState))\"", type: .default)
self.onNewConnectionState.send(newState)
if case .ready = newState {
self.receiveNextMessage()
Task {
try? await self.sendAvailableFreeSpace()
}
}
}
connection.start(queue: .main)
}
// MARK: - Public Methods
/// Asynchronously sends a file to the connected device.
/// - Parameter file: The file to send.
func sendFile(_ file: MigratorFile) async throws {
if file.type == .app {
try await self.sendFile(file.url.fullURL())
MigrationReportController.shared.addMigratedApp(file.name)
} else {
try await _sendFile(file)
try await sendSymlinks()
}
}
/// Asynchronously sends a file to the connected device.
/// - Parameter fileURL: The URL of the file to send.
private func sendFile(_ fileURL: URL) async throws {
try await _sendFile(at: fileURL)
try await sendSymlinks()
}
/// Sends the hostname of the current device to the connected device.
func sendHostName() async throws {
if let data = (Host.current().localizedName ?? "Connected Device").data(using: .utf8) {
let message = NWProtocolFramer.Message(migratorMessageType: .hostname, infoLenght: 0)
let context = NWConnection.ContentContext(identifier: "Hostname",
metadata: [message])
try await sendAsyncWrapper(content: data, contentContext: context)
}
}
/// Sends the available free space of the current device to the connected device.
func sendAvailableFreeSpace() async throws {
if let data = Utils.Common.freeSpaceOnDevice.description.data(using: .utf8) {
let message = NWProtocolFramer.Message(migratorMessageType: .availableSpace, infoLenght: 0)
let context = NWConnection.ContentContext(identifier: "FreeSpace",
metadata: [message])
try await sendAsyncWrapper(content: data, contentContext: context)
}
}
func sendMigrationSize(_ size: Int) async throws {
if let data = size.description.data(using: .utf8) {
let message = NWProtocolFramer.Message(migratorMessageType: .metadata, infoLenght: 0)
let context = NWConnection.ContentContext(identifier: "MigrationSize",
metadata: [message])
try await sendAsyncWrapper(content: data, contentContext: context)
onReadyToReceive.send(true)
}
}
func sendMigrationCompleted() async throws {
if let data = "Completion".data(using: .utf8) {
let message = NWProtocolFramer.Message(migratorMessageType: .result, infoLenght: 0)
let context = NWConnection.ContentContext(identifier: "MigrationCompleted",
metadata: [message])
try await sendAsyncWrapper(content: data, contentContext: context)
isMigrationCompleted = true
}
}
// MARK: - Private Methods
/// A wrapper method to send data asynchronously over the network connection with retry capability.
/// - Parameters:
/// - content: The data to be sent.
/// - contentContext: The context for the data being sent, including any associated metadata.
/// - maxRetries: Maximum number of retry attempts (default: 3)
/// - retryDelay: Delay in seconds between retries (default: 2)
private func sendAsyncWrapper(content: Data,
contentContext: NWConnection.ContentContext = .defaultMessage,
maxRetries: Int = 3,
retryDelay: TimeInterval = 2) async throws {
logger.log("networkConnection.sendAsync: sending message \"\(contentContext.identifier)\"", type: .debug)
var currentRetry = 0
var lastError: Error?
while currentRetry <= maxRetries {
do {
await MigrationController.shared.awaitConnectionReadiness()
await MigrationController.shared.acquireConnectionOperationToken()
return try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in
// Add timeout for send operation
let timeoutTask = Task {
try await Task.sleep(nanoseconds: 120_000_000_000)
continuation.resume(throwing: NSError(domain: "NetworkConnection", code: 1002,
userInfo: [NSLocalizedDescriptionKey: "Send operation timed out"]))
}
connection.send(content: content, contentContext: contentContext, isComplete: true, completion: .contentProcessed({ error in
timeoutTask.cancel()
if let error = error {
MLogger.main.log("networkConnection.sendAsync: error sending message \"\(contentContext.identifier)\", error \"\(error.localizedDescription)\"", type: .error)
continuation.resume(throwing: error)
} else {
MLogger.main.log("networkConnection.sendAsync: done sending message \"\(contentContext.identifier)\"")
MigrationController.shared.releaseConnectionOperationToken()
continuation.resume()
}
}))
}
} catch {
MigrationController.shared.releaseConnectionOperationToken()
lastError = error
currentRetry += 1
if currentRetry <= maxRetries {
MLogger.main.log("networkConnection.sendAsync: retry \(currentRetry)/\(maxRetries) for message \"\(contentContext.identifier)\" after error: \(error.localizedDescription)", type: .fault)
try await Task.sleep(nanoseconds: UInt64(retryDelay * 1_000_000_000))
}
}
}
if let lastError = lastError {
throw lastError
} else {
throw NSError(domain: "NetworkConnection", code: 1000,
userInfo: [NSLocalizedDescriptionKey: "Failed to send after \(maxRetries) retries"])
}
}
private func closeFile(_ fileHandle: FileHandle) throws {
do {
try fileHandle.close()
} catch let error {
throw MigratorError.fileError(type: .failedDuringFileHandling(error: error))
}
}
/// Handles the sending of a file, breaking it into chunks if necessary, and collecting symbolic links as needed.
/// - Parameter file: The file to send.
private func _sendFile(_ file: MigratorFile) async throws {
logger.log("networkConnection.sendfile: preparing file \"\(file.url.fullURL().relativePath)\"")
let chunkSize: UInt64 = 33_554_432
if file.type == .symlink {
do {
let destinationPath = try FileManager.default.destinationOfSymbolicLink(atPath: file.url.fullURL().relativePath)
if destinationPath == file.url.fullURL().relativePath {
logger.log("networkConnection.sendfile: detected circular symlink at \"\(file.url.fullURL().relativePath)\"", type: .error)
MigrationReportController.shared.addError("Skipped circular symbolic link: \(file.url.fullURL().relativePath)")
return
}
guard let destinationURL = URL(string: destinationPath) else {
logger.log("networkConnection.sendfile: impossible to create URL from \"\(destinationPath)\"", type: .error)
MigrationReportController.shared.addError("Skipped invalid symbolic link: \(file.url.fullURL().relativePath) -> \(destinationPath)")
return
}
if !FileManager.default.fileExists(atPath: destinationPath) {
logger.log("networkConnection.sendfile: symbolic link points to non-existent path \"\(destinationPath)\"", type: .fault)
}
logger.log("networkConnection.sendfile: file \"\(file.url.fullURL().relativePath)\" is alias of \"\(destinationPath)\"")
let destinationTrackedURL = MigratorFileURL(with: destinationURL)
let sourceTrackedURL = MigratorFileURL(with: file.url.fullURL())
if destinationTrackedURL.source == .unknown {
let infoData = SymbolicLinkMessage(source: sourceTrackedURL, absoluteDestination: MigratorFileURL(with: file.url.fullURL().deletingLastPathComponent()), relativeDestination: destinationPath)
symlinks.append(infoData)
} else {
let infoData = SymbolicLinkMessage(source: sourceTrackedURL, absoluteDestination: MigratorFileURL(with: URL(string: destinationPath)!))
symlinks.append(infoData)
}
return
} catch {
logger.log("networkConnection.sendfile: impossible to create symlink with error -> \(error.localizedDescription)", type: .error)
MigrationReportController.shared.addError("Failed to process symbolic link: \(file.url.fullURL().relativePath) - \(error.localizedDescription)")
}
}
guard FileManager.default.fileExists(atPath: file.url.fullURL().relativePath) else {
logger.log("networkConnection.sendfile: file \"\(file.url.fullURL().relativePath)\" doesn't exists", type: .error)
throw MigratorError.fileError(type: .noData)
}
var attributes: [FileAttributeKey: Any] = [:]
do {
attributes = try FileManager.default.attributesOfItem(atPath: file.url.fullURL().relativePath) as [FileAttributeKey: Any]
} catch {
logger.log("networkConnection.sendfile: failed to get attributes of file \"\(file.url.fullURL().relativePath)\" - \(error.localizedDescription)", type: .error)
MigrationReportController.shared.addError("Failed to get attributes for file: \(file.url.fullURL().relativePath)")
}
if file.type == .directory || file.type == .app {
let infoData = FileMessage(with: file.url.fullURL(), part: 0, attributes: attributes)
guard var data = "directory".data(using: .utf8),
let infoDataLenght = try? data.include(object: infoData) else {
throw MigratorError.internalError(type: .data)
}
let message = NWProtocolFramer.Message(migratorMessageType: .directory, infoLenght: UInt32(infoDataLenght))
let context = NWConnection.ContentContext(identifier: "Directory",
metadata: [message])
logger.log("networkConnection.sendfile: sending data of directory at \"\(file.url.fullURL().relativePath)\"")
try await sendAsyncWrapper(content: data, contentContext: context)
self.onFileSent.send(1)
self.onBytesSent.send(data.count)
logger.log("networkConnection.sendfile: start sending content of directory \"\(file.url.fullURL().relativePath)\"")
let childs = file.childFiles.isEmpty ? await file.fetchUnretainedChilds() : file.childFiles
for child in childs {
do {
try await sendFile(child)
} catch {
MigrationReportController.shared.addError("migrationViewModel.migrationTask: failed to send file: \(child.url.fullURL().relativePath) - with error: \"\(error.localizedDescription)\"")
}
}
return
}
guard let fileHandle = FileHandle(forReadingAtPath: file.url.fullURL().relativePath) else {
logger.log("networkConnection.sendfile: failed to handle file \"\(file.url.fullURL().relativePath)\", impossible to read", type: .error)
throw MigratorError.fileError(type: .failedDuringFileHandling())
}
var availableBytes = (attributes as NSDictionary).fileSize()
var parsedBytes: UInt64 = 0
logger.log("networkConnection.sendfile: start sending data of file \"\(file.url.fullURL().relativePath)\"")
if availableBytes > chunkSize {
var partNumber: UInt32 = 0
do {
try fileHandle.seek(toOffset: parsedBytes)
} catch let error {
logger.log("networkConnection.sendfile: failed to handle file \"\(file.url.fullURL().relativePath)\", impossible to read", type: .error)
throw MigratorError.fileError(type: .failedDuringFileHandling(error: error))
}
while availableBytes > 0 {
let infoData = FileMessage(with: file.url.fullURL(), part: Int(partNumber), attributes: attributes)
guard var chunk = try? fileHandle.read(upToCount: Int(min(chunkSize, availableBytes))),
let infoDataLenght = try? chunk.include(object: infoData) else {
try closeFile(fileHandle)
logger.log("networkConnection.sendfile: failed to handle file \"\(file.url.fullURL().relativePath)\", impossible to read", type: .error)
throw MigratorError.fileError(type: .noData)
}
let message = NWProtocolFramer.Message(migratorMessageType: .multipartFile, infoLenght: UInt32(infoDataLenght))
let context = NWConnection.ContentContext(identifier: "Chunk",
metadata: [message])
try await sendAsyncWrapper(content: chunk, contentContext: context)
self.onBytesSent.send(chunk.count)
parsedBytes += min(chunkSize, availableBytes)
if availableBytes > chunkSize {
availableBytes -= chunkSize
} else {
availableBytes = 0
}
partNumber += 1
do {
try fileHandle.seek(toOffset: parsedBytes)
} catch let error {
logger.log("networkConnection.sendfile: failed to handle file \"\(file.url.fullURL().relativePath)\", impossible to read", type: .error)
throw MigratorError.fileError(type: .failedDuringFileHandling(error: error))
}
}
} else if availableBytes == 0 {
let infoObject = FileMessage(with: file.url.fullURL(), part: 0, attributes: attributes)
let infoData = try JSONEncoder().encode(infoObject)
let message = NWProtocolFramer.Message(migratorMessageType: .file, infoLenght: UInt32(infoData.count))
let context = NWConnection.ContentContext(identifier: "EmptyFile",
metadata: [message])
try await sendAsyncWrapper(content: infoData, contentContext: context)
self.onBytesSent.send(infoData.count)
self.onFileSent.send(1)
} else {
let infoData = FileMessage(with: file.url.fullURL(), part: 0, attributes: attributes)
guard var data = try? fileHandle.readToEnd(),
let infoDataLenght = try? data.include(object: infoData) else {
try closeFile(fileHandle)
throw MigratorError.fileError(type: .noData)
}
let message = NWProtocolFramer.Message(migratorMessageType: .file, infoLenght: UInt32(infoDataLenght))
let context = NWConnection.ContentContext(identifier: "File",
metadata: [message])
try await sendAsyncWrapper(content: data, contentContext: context)
self.onBytesSent.send(data.count)
self.onFileSent.send(1)
}
try closeFile(fileHandle)
logger.log("networkConnection.sendfile: done sending file \"\(file.url.fullURL().relativePath)\"")
}
/// Handles the sending of a file, breaking it into chunks if necessary, and collecting symbolic links as needed.
/// - Parameter fileURL: The URL of the file to send.
private func _sendFile(at fileURL: URL) async throws {
logger.log("networkConnection.sendfile: preparing file \"\(fileURL.relativePath)\"")
guard !Utils.FileManagerHelpers.shouldIgnorePath(fileURL) else {
logger.log("networkConnection.sendfile: file \"\(fileURL.relativePath)\" needs to be ignored. This should'n happen.", type: .fault)
return
}
let chunkSize: UInt64 = 33_554_432
var isDirectory: ObjCBool = false
if let destinationPath = try? FileManager.default.destinationOfSymbolicLink(atPath: fileURL.relativePath) {
// Check for circular references
if destinationPath == fileURL.relativePath {
logger.log("networkConnection.sendfile: detected circular symlink at \"\(fileURL.relativePath)\"", type: .error)
MigrationReportController.shared.addError("Skipped circular symbolic link: \(fileURL.relativePath)")
return
}
logger.log("networkConnection.sendfile: file \"\(fileURL.relativePath)\" is alias of \"\(destinationPath)\"")
guard let destinationURL = URL(string: destinationPath) else {
logger.log("networkConnection.sendfile: impossible to create URL from \"\(destinationPath)\"", type: .error)
MigrationReportController.shared.addError("Skipped invalid symbolic link: \(fileURL.relativePath) -> \(destinationPath)")
return
}
// Check if destination path exists (non-critical, just log it)
if !FileManager.default.fileExists(atPath: destinationPath) {
logger.log("networkConnection.sendfile: symbolic link points to non-existent path \"\(destinationPath)\"", type: .fault)
}
let destinationTrackedURL = MigratorFileURL(with: destinationURL)
let sourceTrackedURL = MigratorFileURL(with: fileURL)
if destinationTrackedURL.source == .unknown {
let infoData = SymbolicLinkMessage(source: sourceTrackedURL, absoluteDestination: MigratorFileURL(with: fileURL.deletingLastPathComponent()), relativeDestination: destinationPath)
symlinks.append(infoData)
} else {
let infoData = SymbolicLinkMessage(source: sourceTrackedURL, absoluteDestination: MigratorFileURL(with: URL(string: destinationPath)!))
symlinks.append(infoData)
}
return
}
guard FileManager.default.fileExists(atPath: fileURL.relativePath, isDirectory: &isDirectory) else {
logger.log("networkConnection.sendfile: file \"\(fileURL.relativePath)\" doesn't exists", type: .error)
throw MigratorError.fileError(type: .noData)
}
logger.log("networkConnection.sendfile: file \"\(fileURL.relativePath)\" is directory -> \"\(isDirectory.description)\"")
var attributes: [FileAttributeKey: Any] = [:]
do {
attributes = try FileManager.default.attributesOfItem(atPath: fileURL.relativePath) as [FileAttributeKey: Any]
} catch {
logger.log("networkConnection.sendfile: failed to get attributes of file \"\(fileURL.relativePath)\" - \(error.localizedDescription)", type: .error)
MigrationReportController.shared.addError("Failed to get attributes for file: \(fileURL.relativePath)")
}
guard !isDirectory.boolValue else {
let infoData = FileMessage(with: fileURL, part: 0, attributes: attributes)
guard var data = "directory".data(using: .utf8),
let infoDataLenght = try? data.include(object: infoData) else {
throw MigratorError.internalError(type: .data)
}
let message = NWProtocolFramer.Message(migratorMessageType: .directory, infoLenght: UInt32(infoDataLenght))
let context = NWConnection.ContentContext(identifier: "Directory",
metadata: [message])
logger.log("networkConnection.sendfile: sending data of directory at \"\(fileURL.relativePath)\"")
try await sendAsyncWrapper(content: data, contentContext: context)
self.onBytesSent.send(data.count)
let childFilePaths = try FileManager.default.contentsOfDirectory(atPath: fileURL.relativePath)
logger.log("networkConnection.sendfile: start sending content of directory \"\(fileURL.relativePath)\"")
for childFilePath in childFilePaths {
guard !childFilePath.isEmpty else { return }
do {
try await sendFile(fileURL.appendingPathComponent(childFilePath, isDirectory: isDirectory.boolValue))
} catch {
MigrationReportController.shared.addError("migrationViewModel.migrationTask: failed to send file: \(childFilePath) - with error: \"\(error.localizedDescription)\"")
}
}
return
}
guard let fileHandle = FileHandle(forReadingAtPath: fileURL.relativePath) else {
logger.log("networkConnection.sendfile: failed to handle file \"\(fileURL.relativePath)\", impossible to read", type: .error)
throw MigratorError.fileError(type: .failedDuringFileHandling())
}
var availableBytes = (attributes as NSDictionary).fileSize()
var parsedBytes: UInt64 = 0
logger.log("networkConnection.sendfile: start sending data of file \"\(fileURL.relativePath)\"")
if availableBytes > chunkSize {
var partNumber: UInt32 = 0
do {
try fileHandle.seek(toOffset: parsedBytes)
} catch let error {
logger.log("networkConnection.sendfile: failed to handle file \"\(fileURL.relativePath)\", impossible to read", type: .error)
throw MigratorError.fileError(type: .failedDuringFileHandling(error: error))
}
while availableBytes > 0 {
let infoData = FileMessage(with: fileURL, part: Int(partNumber), attributes: attributes)
guard var chunk = try? fileHandle.read(upToCount: Int(min(chunkSize, availableBytes))),
let infoDataLenght = try? chunk.include(object: infoData)else {
try closeFile(fileHandle)
logger.log("networkConnection.sendfile: failed to handle file \"\(fileURL.relativePath)\", impossible to read", type: .error)
throw MigratorError.fileError(type: .noData)
}
let message = NWProtocolFramer.Message(migratorMessageType: .multipartFile, infoLenght: UInt32(infoDataLenght))
let context = NWConnection.ContentContext(identifier: "Chunk",
metadata: [message])
try await sendAsyncWrapper(content: chunk, contentContext: context)
self.onBytesSent.send(chunk.count)
parsedBytes += min(chunkSize, availableBytes)
if availableBytes > chunkSize {
availableBytes -= chunkSize
} else {
availableBytes = 0
}
partNumber += 1
do {
try fileHandle.seek(toOffset: parsedBytes)
} catch let error {
logger.log("networkConnection.sendfile: failed to handle file \"\(fileURL.relativePath)\", impossible to read", type: .error)
throw MigratorError.fileError(type: .failedDuringFileHandling(error: error))
}
}
} else if availableBytes == 0 {
let infoObject = FileMessage(with: fileURL, part: 0, attributes: attributes)
let infoData = try JSONEncoder().encode(infoObject)
let message = NWProtocolFramer.Message(migratorMessageType: .file, infoLenght: UInt32(infoData.count))
let context = NWConnection.ContentContext(identifier: "EmptyFile",
metadata: [message])
try await sendAsyncWrapper(content: infoData, contentContext: context)
self.onBytesSent.send(infoData.count)
} else {
let infoData = FileMessage(with: fileURL, part: 0, attributes: attributes)
guard var data = try? fileHandle.readToEnd(),
let infoDataLenght = try? data.include(object: infoData) else {
try closeFile(fileHandle)
throw MigratorError.fileError(type: .noData)
}
let message = NWProtocolFramer.Message(migratorMessageType: .file, infoLenght: UInt32(infoDataLenght))
let context = NWConnection.ContentContext(identifier: "File",
metadata: [message])
try await sendAsyncWrapper(content: data, contentContext: context)
self.onBytesSent.send(data.count)
}
try closeFile(fileHandle)
logger.log("networkConnection.sendfile: done sending file \"\(fileURL.relativePath)\"")
}
/// Sends collected symbolic links to the connected device.
private func sendSymlinks() async throws {
logger.log("networkConnection.sendSymlinks: start sending collected symlinks")
for message in symlinks {
guard var data = "link".data(using: .utf8),
let infoDataLenght = try? data.include(object: message) else {
throw MigratorError.fileError(type: .noData)
}
let message = NWProtocolFramer.Message(migratorMessageType: .symlink, infoLenght: UInt32(infoDataLenght))
let context = NWConnection.ContentContext(identifier: "SymbolicLink",
metadata: [message])
try await sendAsyncWrapper(content: data, contentContext: context)
}
symlinks = []
logger.log("networkConnection.sendSymlinks: done sending collected symlinks")
}
/// Sends collected symbolic links to the connected device.
func sendDefaults(_ object: DefaultsMessage) async throws {
logger.log("networkConnection.sendDefaults: start sending \(object.key) UserDefaults")
guard var data = "defaults".data(using: .utf8),
let infoDataLenght = try? data.include(object: object) else {
throw MigratorError.fileError(type: .noData)
}
let message = NWProtocolFramer.Message(migratorMessageType: .defaults, infoLenght: UInt32(infoDataLenght))
let context = NWConnection.ContentContext(identifier: "UserDefaults",
metadata: [message])
try await sendAsyncWrapper(content: data, contentContext: context)
logger.log("networkConnection.sendDefaults: done sending \(object.key) UserDefaults")
}
/// Receives and processes the next incoming message from the connected device.
private func receiveNextMessage() {
logger.log("networkConnection.receiveNextMessage: waiting for new messages")
guard connection.state == .ready else {
logger.log("networkConnection.receiveNextMessage: connection not ready, state: \(connection.state)", type: .error)
if connection.state != .cancelled {
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) { [weak self] in
self?.receiveNextMessage()
}
}
return
}
let timeoutWorkItem = DispatchWorkItem { [weak self] in
self?.logger.log("networkConnection.receiveNextMessage: receive operation timed out", type: .error)
self?.receiveNextMessage()
}
DispatchQueue.main.asyncAfter(deadline: .now() + 30.0, execute: timeoutWorkItem)
connection.receiveMessage { [weak self] (content, context, _, error) in
guard let self = self else { return }
timeoutWorkItem.cancel()
if let migratorMessage = context?.protocolMetadata(definition: MigratorNetworkProtocol.definition) as? NWProtocolFramer.Message {
switch migratorMessage.migratorMessageType {
case .hostname:
self.logger.log("networkConnection.receiveNextMessage: hostname message received", type: .default)
if let data = content,
let hostname = String(data: data, encoding: .utf8) {
self.connectedDeviceHostName = hostname
} else {
self.logger.log("networkConnection.receiveNextMessage: impossible to decode hostname", type: .error)
}
case .symlink:
self.logger.log("networkConnection.receiveNextMessage: symlink message received!", type: .default)
do {
if var data = content {
let messageInfo = try data.extractObject(from: 0..<Int(migratorMessage.migratorMessageInfoLenght), ofType: SymbolicLinkMessage.self)
self.logger.log("networkConnection.receiveNextMessage: symlink source \"\(messageInfo.source.fullURL().relativePath)\"")
self.logger.log("networkConnection.receiveNextMessage: symlink relative destination \"\(messageInfo.relativeDestination ?? "nil")\"")
self.logger.log("networkConnection.receiveNextMessage: symlink absolute destination \"\(messageInfo.absoluteDestination.fullURL().relativePath)\"")
var destinationPath = ""
if let relativeDestination = messageInfo.relativeDestination {
destinationPath = relativeDestination
} else {
destinationPath = messageInfo.absoluteDestination.fullURL().relativePath
}
if messageInfo.source.fullURL().relativePath == destinationPath {
self.logger.log("networkConnection.receiveNextMessage: detected circular symlink, skipping", type: .error)
MigrationReportController.shared.addError("Skipped circular symbolic link: \(messageInfo.source.fullURL().relativePath)")
return
}
let parentDir = messageInfo.source.fullURL().deletingLastPathComponent()
if !FileManager.default.fileExists(atPath: parentDir.relativePath) {
try FileManager.default.createDirectory(at: parentDir, withIntermediateDirectories: true)
}
if FileManager.default.fileExists(atPath: messageInfo.source.fullURL().relativePath) {
try FileManager.default.removeItem(atPath: messageInfo.source.fullURL().relativePath)
}
try FileManager.default.createSymbolicLink(atPath: messageInfo.source.fullURL().relativePath, withDestinationPath: destinationPath)
}
} catch let error {
self.logger.log("networkConnection.receiveNextMessage: failed to create symbolik link -> \"\(error.localizedDescription)\"", type: .error)
}
case .file:
self.logger.log("networkConnection.receiveNextMessage: file message received")
do {
if var data = content {
let messageInfo = try data.extractObject(from: 0..<Int(migratorMessage.migratorMessageInfoLenght), ofType: FileMessage.self)
self.onBytesReceived.send(data.count)
guard let directory = URL(string: messageInfo.source.fullURL().relativePath)?.deletingLastPathComponent() else {
throw MigratorError.fileError(type: .failedToWriteFile)
}
self.logger.log("networkConnection.receiveNextMessage: file source \"\(messageInfo.source.fullURL().relativePath)\"")
if FileManager.default.fileExists(atPath: messageInfo.source.fullURL().relativePath) {
switch AppContext.duplicateFilesHandlingPolicy {
case .ignore:
break
case .move:
if !FileManager.default.fileExists(atPath: "\(AppContext.backupPath)/\(directory.relativePath)") {
try? FileManager.default.createDirectory(atPath: "\(AppContext.backupPath)/\(directory.relativePath)", withIntermediateDirectories: true)
}
try? FileManager.default.moveItem(atPath: messageInfo.source.fullURL().relativePath, toPath: "\(AppContext.backupPath)/\(messageInfo.source.fullURL().relativePath)")
fallthrough
case .overwrite:
do {
guard FileManager.default.createFile(atPath: messageInfo.source.fullURL().relativePath, contents: data, attributes: messageInfo.attributes) else {
throw MigratorError.fileError(type: .failedToWriteFile)
}
} catch {
self.logger.log("networkConnection.receiveNextMessage: failed to create file with attributes - \(error.localizedDescription)", type: .error)
guard FileManager.default.createFile(atPath: messageInfo.source.fullURL().relativePath, contents: data, attributes: nil) else {
self.logger.log("networkConnection.receiveNextMessage: failed to create file without attributes - \(error.localizedDescription)", type: .error)
throw MigratorError.fileError(type: .failedToWriteFile)
}
}
}
} else {
try FileManager.default.createDirectory(atPath: directory.relativePath, withIntermediateDirectories: true)
do {
guard FileManager.default.createFile(atPath: messageInfo.source.fullURL().relativePath, contents: data, attributes: messageInfo.attributes) else {
throw MigratorError.fileError(type: .failedToWriteFile)
}
} catch {
self.logger.log("networkConnection.receiveNextMessage: failed to create file with attributes - \(error.localizedDescription)", type: .error)
guard FileManager.default.createFile(atPath: messageInfo.source.fullURL().relativePath, contents: data, attributes: nil) else {
self.logger.log("networkConnection.receiveNextMessage: failed to create file without attributes - \(error.localizedDescription)", type: .error)
throw MigratorError.fileError(type: .failedToWriteFile)
}
}
}
}
} catch let error {
self.logger.log("networkConnection.receiveNextMessage: failed to write file -> \"\(error.localizedDescription)\"", type: .error)
}
case .directory:
self.logger.log("networkConnection.receiveNextMessage: directory message received")
do {
if var data = content {
let messageInfo = try data.extractObject(from: 0..<Int(migratorMessage.migratorMessageInfoLenght), ofType: FileMessage.self)
self.logger.log("networkConnection.receiveNextMessage: directory source \"\(messageInfo.source.fullURL().relativePath)\"")
try FileManager.default.createDirectory(atPath: messageInfo.source.fullURL().relativePath, withIntermediateDirectories: true, attributes: messageInfo.attributes)
}
} catch let error {
self.logger.log("networkConnection.receiveNextMessage: failed to create directory -> \"\(error.localizedDescription)\"", type: .error)
}
case .result:
self.logger.log("networkConnection.receiveNextMessage: result message received", type: .default)
self.isMigrationCompleted = true
return
case .invalid:
self.logger.log("networkConnection.receiveNextMessage: invalid message received", type: .default)
case .metadata:
self.logger.log("networkConnection.receiveNextMessage: metadata message received", type: .default)
if let data = content,
let migrationSize = String(data: data, encoding: .utf8) {
self.migrationSizeInBytes = Int(migrationSize) ?? 0
} else {
self.logger.log("networkConnection.receiveNextMessage: failed to decode metadata", type: .error)
}
case .availableSpace:
self.logger.log("networkConnection.receiveNextMessage: availableSpace message received", type: .default)
if let data = content,
let freeSpace = String(data: data, encoding: .utf8) {
self.connectedDeviceAvailableSpace = Int(freeSpace) ?? 0
} else {
self.logger.log("networkConnection.receiveNextMessage: failed to decode availableSpace", type: .error)
}
case .multipartFile:
self.logger.log("networkConnection.receiveNextMessage: multipart file message received")
guard var data = content else {
self.logger.log("networkConnection.receiveNextMessage: no data in multipart file message", type: .error)
break
}
do {
let messageInfo = try data.extractObject(from: 0..<Int(migratorMessage.migratorMessageInfoLenght), ofType: FileMessage.self)
self.onBytesReceived.send(data.count)
guard let directory = URL(string: messageInfo.source.fullURL().relativePath)?.deletingLastPathComponent() else {
throw MigratorError.fileError(type: .failedToWriteFile)
}
if !FileManager.default.fileExists(atPath: directory.relativePath) {
try FileManager.default.createDirectory(atPath: directory.relativePath, withIntermediateDirectories: true)
}
if !FileManager.default.fileExists(atPath: messageInfo.source.fullURL().relativePath) || messageInfo.partNumber == 0 {
FileManager.default.createFile(atPath: messageInfo.source.fullURL().relativePath, contents: nil, attributes: messageInfo.attributes)
}
guard let fileHandle = FileHandle(forWritingAtPath: messageInfo.source.fullURL().relativePath) else {
throw MigratorError.fileError(type: .failedDuringFileHandling())
}
try fileHandle.seekToEnd()
try fileHandle.write(contentsOf: data)
try fileHandle.close()
do {
try FileManager.default.setAttributes(messageInfo.attributes, ofItemAtPath: messageInfo.source.fullURL().relativePath)
} catch {
self.logger.log("networkConnection.receiveNextMessage: failed to set attributes - \(error.localizedDescription)", type: .error)
}
} catch let error {
self.logger.log("networkConnection.receiveNextMessage: failed to write chunk of data -> \"\(error.localizedDescription)\"", type: .error)
}
case .defaults:
self.logger.log("networkConnection.receiveNextMessage: defaults message received!", type: .default)
do {
if var data = content {
let messageInfo = try data.extractObject(from: 0..<Int(migratorMessage.migratorMessageInfoLenght), ofType: DefaultsMessage.self)
if let boolValue = messageInfo.boolValue {
UserDefaults.standard.setValue(boolValue, forKey: messageInfo.key)
} else if let stringValue = messageInfo.stringValue {
UserDefaults.standard.setValue(stringValue, forKey: messageInfo.key)
}
}
} catch let error {
self.logger.log("networkConnection.receiveNextMessage: failed to save UserDefaults value -> \"\(error.localizedDescription)\"", type: .error)
}
}
}
if let error = error {
self.logger.log("networkConnection.receiveNextMessage: failed to receive message -> \"\(error.localizedDescription)\"", type: .error)
}
self.receiveNextMessage()
}
}
}
// swiftlint:enable function_body_length type_body_length file_length
extension NWInterface.InterfaceType {
var stringValue: String {
switch self {
case .other: return "other"
case .wifi: return "wifi"
case .wiredEthernet: return "wiredEthernet"
case .loopback: return "loopback"
case .cellular: return "cellular"
@unknown default: return "unknown"
}
}
}