forked from blinksh/blink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CopyFiles.swift
241 lines (213 loc) · 9.04 KB
/
CopyFiles.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
//////////////////////////////////////////////////////////////////////////////////
//
// B L I N K
//
// Copyright (C) 2016-2021 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 Foundation
import Combine
import Dispatch
public struct CopyError : Error {
public let msg: String
}
public struct CopyAttributesFlag: OptionSet {
public var rawValue: UInt
public static let none = CopyAttributesFlag(rawValue: 0)
public static let timestamp = CopyAttributesFlag(rawValue: 1 << 0)
public static let permissions = CopyAttributesFlag(rawValue: 1 << 1)
public init(rawValue: UInt) {
self.rawValue = rawValue
}
func filter(_ attrs: FileAttributes) -> FileAttributes {
var newAttrs: FileAttributes = [:]
if self.contains(.timestamp) {
newAttrs[.creationDate] = attrs[.creationDate]
newAttrs[.modificationDate] = attrs[.modificationDate]
}
if self.contains(.permissions) {
newAttrs[.posixPermissions] = attrs[.posixPermissions]
}
return newAttrs
}
}
public struct CopyArguments {
public let inplace: Bool
public var preserve: CopyAttributesFlag // attributes. Check how FileManager passes this.
public let checkTimes: Bool
public init(inplace: Bool = true,
preserve: CopyAttributesFlag = [.permissions],
checkTimes: Bool = false) {
self.inplace = inplace
self.preserve = preserve
self.checkTimes = checkTimes
if checkTimes {
self.preserve.insert(.timestamp)
}
}
}
extension Translator {
public func copy(from ts: [Translator], args: CopyArguments = CopyArguments()) -> CopyProgressInfoPublisher {
print("Copying \(ts.count) elements")
return ts.publisher.compactMap { t in
t.fileType == .typeDirectory || t.fileType == .typeRegular ? t : nil
}.flatMap(maxPublishers: .max(1)) { t in
copyElement(from: t, args: args)
}.eraseToAnyPublisher()
}
// Self can be a File or a directory.
fileprivate func copyElement(from t: Translator, args: CopyArguments) -> CopyProgressInfoPublisher {
return Just(t)
.flatMap() { $0.stat() }
.tryMap { attrs -> (String, NSNumber, FileAttributes) in
guard let name = attrs[FileAttributeKey.name] as? String else {
throw CopyError(msg: "No name provided")
}
let passingAttributes = args.preserve.filter(attrs)
// TODO Two ways to set permissions. Should be part of the CopyArguments
// The equivalent of -P is simpler for now.
// https://serverfault.com/questions/639042/does-openssh-sftp-server-use-umask-or-preserve-client-side-permissions-after-put
// let mode = attrs[FileAttributeKey.posixPermissions] as? NSNumber ??
// (t.fileType == .typeDirectory ? NSNumber(value: Int16(0o755)) : NSNumber(value: Int16(0o644)))
guard let size = attrs[FileAttributeKey.size] as? NSNumber else {
throw CopyError(msg: "No size provided")
}
return (name, size, passingAttributes)
}.flatMap { (name, size, passingAttributes) -> CopyProgressInfoPublisher in
print("Processing \(name)")
switch t.fileType {
case .typeDirectory:
let mode = passingAttributes[FileAttributeKey.posixPermissions] as? NSNumber ?? NSNumber(value: Int16(0o755))
return self.copyDirectory(as: name, from: t, mode: mode, args: args)
default:
let copyFilePublisher = self.copyFile(from: t, name: name, size: size, attributes: passingAttributes)
// When checkTimes, copy the file only if the modificationDate is different
if args.checkTimes {
let fileTranslator = self.isDirectory ? self.cloneWalkTo(name) : .just(self)
return fileTranslator
.flatMap { $0.stat() }
.catch { _ in Just([:]) }
.flatMap { localAttributes -> CopyProgressInfoPublisher in
if let localModificationDate = localAttributes[.modificationDate] as? NSDate,
localModificationDate == (passingAttributes[.modificationDate] as? NSDate) {
let fullFile = (self.current as NSString).appendingPathComponent(name)
return .just(CopyProgressInfo(name: fullFile, written: 0, size: size.uint64Value))
}
return copyFilePublisher
}.eraseToAnyPublisher()
}
return copyFilePublisher
}
}.eraseToAnyPublisher()
}
fileprivate func copyDirectory(as name: String,
from t: Translator,
mode: NSNumber,
args: CopyArguments) -> CopyProgressInfoPublisher {
print("Copying directory \(t.current)")
let directory: AnyPublisher<Translator, Error>
if args.checkTimes {
// Walk or create
directory = self.cloneWalkTo(name)
.tryCatch { _ in self.clone().mkdir(name: name, mode: mode_t(truncating: mode)) }
.eraseToAnyPublisher()
} else {
directory = self.clone().mkdir(name: name, mode: mode_t(truncating: mode))
}
return directory
.flatMap { dir -> CopyProgressInfoPublisher in
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 { dir.copy(from: $0, args: args) }.eraseToAnyPublisher()
}.eraseToAnyPublisher()
// 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 { self.copy(from: $0) }.eraseToAnyPublisher()
}
}
fileprivate enum FileState {
case copy(File)
case attributes(File)
}
extension Translator {
fileprivate func copyFile(from t: Translator,
name: String,
size: NSNumber,
attributes: FileAttributes) -> CopyProgressInfoPublisher {
let fullFile: String
let file: AnyPublisher<File, Error>
// If we are in a directory, we create the file, otherwise we open truncated.
if self.isDirectory {
fullFile = (self.current as NSString).appendingPathComponent(name)
file = self.create(name: name, flags: O_WRONLY, mode: S_IRWXU)
} else {
fullFile = self.current
file = self.open(flags: O_WRONLY | O_TRUNC)
}
return file
.flatMap { destination -> CopyProgressInfoPublisher in
if size == 0 {
return .just(CopyProgressInfo(name: fullFile, written:0, size: 0))
}
return t.open(flags: O_RDONLY)
.flatMap { [FileState.copy($0), FileState.attributes($0)].publisher }
.flatMap(maxPublishers: .max(1)) { state -> CopyProgressInfoPublisher in
switch state {
case .copy(let source):
return (source as! WriterTo)
.writeTo(destination)
.map { CopyProgressInfo(name: fullFile, written: UInt64($0), size: size.uint64Value) }
.eraseToAnyPublisher()
case .attributes(let source):
return Publishers.Zip(source.close(), destination.close())
// TODO From the File, we could offer the Translator itself.
.flatMap { _ in self.isDirectory ?
self.cloneWalkTo(name).flatMap { $0.wstat(attributes) }.eraseToAnyPublisher() :
self.wstat(attributes)
}
.map { _ in CopyProgressInfo(name: fullFile, written: 0, size: size.uint64Value) }
.eraseToAnyPublisher()
}
}.eraseToAnyPublisher()
}.eraseToAnyPublisher()
}
}