forked from nsoperations/Carthage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Files.swift
163 lines (144 loc) · 8.06 KB
/
Files.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
import Foundation
import ReactiveSwift
import Result
/// Generic file manipulation helper methods
final class Files {
static let tempDirTemplate = "carthage.XXXXXX"
/// Copies a product into the given folder. The folder will be created if it
/// does not already exist, and any pre-existing version of the product in the
/// destination folder will be deleted before the copy of the new version.
///
/// If the `from` URL has the same path as the `to` URL, and there is a resource
/// at the given path, no operation is needed and the returned signal will just
/// send `.success`.
///
/// Returns a signal that will send the URL after copying upon .success.
static func copyFile(from: URL, to: URL) -> SignalProducer<URL, CarthageError> { // swiftlint:disable:this identifier_name
return SignalProducer<URL, CarthageError> { () -> Result<URL, CarthageError> in
let manager = FileManager.default
// This signal deletes `to` before it copies `from` over it.
// If `from` and `to` point to the same resource, there's no need to perform a copy at all
// and deleting `to` will also result in deleting the original resource without copying it.
// When `from` and `to` are the same, we can just return success immediately.
//
// See https://github.com/Carthage/Carthage/pull/1160
if manager.fileExists(atPath: to.path) && from.absoluteURL == to.absoluteURL {
return .success(to)
}
// Although some methods’ documentation say: “YES if createIntermediates
// is set and the directory already exists)”, it seems to rarely
// returns NO and NSFileWriteFileExistsError error. So we should
// ignore that specific error.
// See: https://developer.apple.com/documentation/foundation/filemanager/1415371-createdirectory
func result(allowingErrorCode code: Int, _ result: Result<(), CarthageError>) -> Result<(), CarthageError> {
if case .failure(.writeFailed(_, let error?)) = result, error.code == code {
return .success(())
}
return result
}
let createDirectory = { try manager.createDirectory(at: $0, withIntermediateDirectories: true) }
return result(allowingErrorCode: NSFileWriteFileExistsError, Result(at: to.deletingLastPathComponent(), attempt: createDirectory))
.flatMap { _ in
result(allowingErrorCode: NSFileNoSuchFileError, Result(at: to, attempt: manager.removeItem(at:)))
}
.flatMap { _ in
return Result(at: to, attempt: { destination /* to */ in
try manager.copyItem(at: from, to: destination, avoiding·rdar·32984063: true)
return destination
})
}
}
}
/// Moves the source file at the given URL to the specified destination URL
///
/// Sends the final file URL upon .success.
static func moveFile(from: URL, to: URL) -> SignalProducer<URL, CarthageError> { // swiftlint:disable:this identifier_name
return SignalProducer<URL, CarthageError> { () -> Result<URL, CarthageError> in
let manager = FileManager.default
// This signal deletes `to` before it copies `from` over it.
// If `from` and `to` point to the same resource, there's no need to perform a copy at all
// and deleting `to` will also result in deleting the original resource without copying it.
// When `from` and `to` are the same, we can just return success immediately.
//
// See https://github.com/Carthage/Carthage/pull/1160
if manager.fileExists(atPath: to.path) && from.absoluteURL == to.absoluteURL {
return .success(to)
}
// Although some methods’ documentation say: “YES if createIntermediates
// is set and the directory already exists)”, it seems to rarely
// returns NO and NSFileWriteFileExistsError error. So we should
// ignore that specific error.
// See: https://developer.apple.com/documentation/foundation/filemanager/1415371-createdirectory
func result(allowingErrorCode code: Int, _ result: Result<(), CarthageError>) -> Result<(), CarthageError> {
if case .failure(.writeFailed(_, let error?)) = result, error.code == code {
return .success(())
}
return result
}
let createDirectory = { try manager.createDirectory(at: $0, withIntermediateDirectories: true) }
return result(allowingErrorCode: NSFileWriteFileExistsError, Result(at: to.deletingLastPathComponent(), attempt: createDirectory))
.flatMap { _ in
result(allowingErrorCode: NSFileNoSuchFileError, Result(at: to, attempt: manager.removeItem(at:)))
}
.flatMap { _ in
// Tries `rename()` system call at first.
let result = from.withUnsafeFileSystemRepresentation { old in
to.withUnsafeFileSystemRepresentation { new in
rename(old!, new!)
}
}
if result == 0 {
return .success(to)
}
if errno != EXDEV {
return .failure(.taskError(.posixError(errno)))
}
// If the “Cross-device link” error occurred, then falls back to
// `FileManager.moveItem(at:to:)`.
//
// See https://github.com/Carthage/Carthage/issues/706 and
// https://github.com/Carthage/Carthage/issues/711.
return Result(at: to, attempt: { destination in
try FileManager.default.moveItem(at: from, to: destination)
return destination
})
}
}
}
/// Removes the file located at the given URL
///
/// Sends empty value on successful removal
static func removeItem(at url: URL) -> SignalProducer<(), CarthageError> {
return SignalProducer {
Result(at: url, attempt: FileManager.default.removeItem(at:))
}
}
}
extension SignalProducer where Value == URL, Error == CarthageError {
/// Copies existing files sent from the producer into the given directory.
///
/// Returns a producer that will send locations where the copied files are.
func copyFileURLsIntoDirectory(_ directoryURL: URL) -> SignalProducer<URL, CarthageError> {
return producer
.filter { fileURL in (try? fileURL.checkResourceIsReachable()) ?? false }
.flatMap(.merge) { fileURL -> SignalProducer<URL, CarthageError> in
let fileName = fileURL.lastPathComponent
let destinationURL = directoryURL.appendingPathComponent(fileName, isDirectory: false)
let resolvedDestinationURL = destinationURL.resolvingSymlinksInPath()
return Files.copyFile(from: fileURL, to: resolvedDestinationURL)
}
}
/// Moves existing files sent from the producer into the given directory.
///
/// Returns a producer that will send locations where the moved files are.
func moveFileURLsIntoDirectory(_ directoryURL: URL) -> SignalProducer<URL, CarthageError> {
return producer
.filter { fileURL in (try? fileURL.checkResourceIsReachable()) ?? false }
.flatMap(.merge) { fileURL -> SignalProducer<URL, CarthageError> in
let fileName = fileURL.lastPathComponent
let destinationURL = directoryURL.appendingPathComponent(fileName, isDirectory: false)
let resolvedDestinationURL = destinationURL.resolvingSymlinksInPath()
return Files.moveFile(from: fileURL, to: resolvedDestinationURL)
}
}
}