forked from apple/swift-package-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileSystem+Extensions.swift
634 lines (549 loc) · 24.4 KB
/
FileSystem+Extensions.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
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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2020-2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import struct Foundation.Data
import class Foundation.FileManager
import struct Foundation.UUID
import SystemPackage
import struct TSCBasic.ByteString
import struct TSCBasic.FileInfo
import class TSCBasic.FileLock
import enum TSCBasic.FileMode
import protocol TSCBasic.FileSystem
import enum TSCBasic.FileSystemAttribute
import var TSCBasic.localFileSystem
import protocol TSCBasic.WritableByteStream
public typealias FileSystem = TSCBasic.FileSystem
public var localFileSystem = TSCBasic.localFileSystem
// MARK: - Custom path
extension FileSystem {
/// Check whether the given path exists and is accessible.
public func exists(_ path: AbsolutePath, followSymlink: Bool) -> Bool {
self.exists(path.underlying, followSymlink: followSymlink)
}
/// exists override with default value.
public func exists(_ path: AbsolutePath) -> Bool {
self.exists(path.underlying)
}
/// Check whether the given path is accessible and a directory.
public func isDirectory(_ path: AbsolutePath) -> Bool {
self.isDirectory(path.underlying)
}
/// Check whether the given path is accessible and a file.
public func isFile(_ path: AbsolutePath) -> Bool {
self.isFile(path.underlying)
}
/// Check whether the given path is an accessible and executable file.
public func isExecutableFile(_ path: AbsolutePath) -> Bool {
self.isExecutableFile(path.underlying)
}
/// Check whether the given path is accessible and is a symbolic link.
public func isSymlink(_ path: AbsolutePath) -> Bool {
self.isSymlink(path.underlying)
}
/// Check whether the given path is accessible and readable.
public func isReadable(_ path: AbsolutePath) -> Bool {
self.isReadable(path.underlying)
}
/// Check whether the given path is accessible and writable.
public func isWritable(_ path: AbsolutePath) -> Bool {
self.isWritable(path.underlying)
}
/// Returns `true` if a given path has a quarantine attribute applied if when file system supports this attribute.
/// Returns `false` if such attribute is not applied or it isn't supported.
public func hasAttribute(_ name: FileSystemAttribute, _ path: AbsolutePath) -> Bool {
self.hasAttribute(name, path.underlying)
}
/// Get the contents of the given directory, in an undefined order.
public func getDirectoryContents(_ path: AbsolutePath) throws -> [String] {
try self.getDirectoryContents(path.underlying)
}
/// Get the current working directory (similar to `getcwd(3)`), which can be
/// different for different (virtualized) implementations of a FileSystem.
/// The current working directory can be empty if e.g. the directory became
/// unavailable while the current process was still working in it.
/// This follows the POSIX `getcwd(3)` semantics.
public var currentWorkingDirectory: AbsolutePath? {
self.currentWorkingDirectory.flatMap { AbsolutePath($0) }
}
/// Change the current working directory.
/// - Parameters:
/// - path: The path to the directory to change the current working directory to.
public func changeCurrentWorkingDirectory(to path: AbsolutePath) throws {
try self.changeCurrentWorkingDirectory(to: path.underlying)
}
/// Get the home directory of current user
public var homeDirectory: AbsolutePath {
get throws {
try AbsolutePath(self.homeDirectory)
}
}
/// Get the caches directory of current user
public var cachesDirectory: AbsolutePath? {
self.cachesDirectory.flatMap { AbsolutePath($0) }
}
/// Get the temp directory
public var tempDirectory: AbsolutePath {
get throws {
try AbsolutePath(self.tempDirectory)
}
}
/// Create the given directory.
public func createDirectory(_ path: AbsolutePath) throws {
try self.createDirectory(path.underlying)
}
/// Create the given directory.
///
/// - recursive: If true, create missing parent directories if possible.
public func createDirectory(_ path: AbsolutePath, recursive: Bool) throws {
try self.createDirectory(path.underlying, recursive: recursive)
}
/// Creates a symbolic link of the source path at the target path
/// - Parameters:
/// - path: The path at which to create the link.
/// - destination: The path to which the link points to.
/// - relative: If `relative` is true, the symlink contents will be a relative path, otherwise it will be
/// absolute.
public func createSymbolicLink(_ path: AbsolutePath, pointingAt destination: AbsolutePath, relative: Bool) throws {
try self.createSymbolicLink(path.underlying, pointingAt: destination.underlying, relative: relative)
}
/// Get the contents of a file.
///
/// - Returns: The file contents as bytes, or nil if missing.
public func readFileContents(_ path: AbsolutePath) throws -> ByteString {
try self.readFileContents(path.underlying)
}
/// Write the contents of a file.
public func writeFileContents(_ path: AbsolutePath, bytes: ByteString) throws {
try self.writeFileContents(path.underlying, bytes: bytes)
}
/// Write the contents of a file.
public func writeFileContents(_ path: AbsolutePath, bytes: ByteString, atomically: Bool) throws {
try self.writeFileContents(path.underlying, bytes: bytes, atomically: atomically)
}
/// Write to a file from a stream producer.
public func writeFileContents(_ path: AbsolutePath, body: (WritableByteStream) -> Void) throws {
try self.writeFileContents(path.underlying, body: body)
}
/// Recursively deletes the file system entity at `path`.
///
/// If there is no file system entity at `path`, this function does nothing (in particular, this is not considered
/// to be an error).
public func removeFileTree(_ path: AbsolutePath) throws {
try self.removeFileTree(path.underlying)
}
/// Change file mode.
public func chmod(_ mode: FileMode, path: AbsolutePath, options: Set<FileMode.Option>) throws {
try self.chmod(mode, path: path.underlying, options: options)
}
// Change file mode.
public func chmod(_ mode: FileMode, path: AbsolutePath) throws {
try self.chmod(mode, path: path.underlying)
}
/// Returns the file info of the given path.
///
/// The method throws if the underlying stat call fails.
public func getFileInfo(_ path: AbsolutePath) throws -> FileInfo {
try self.getFileInfo(path.underlying)
}
/// Copy a file or directory.
public func copy(from source: AbsolutePath, to destination: AbsolutePath) throws {
try self.copy(from: source.underlying, to: destination.underlying)
}
/// Move a file or directory.
public func move(from source: AbsolutePath, to destination: AbsolutePath) throws {
try self.move(from: source.underlying, to: destination.underlying)
}
/// Execute the given block while holding the lock.
public func withLock<T>(on path: AbsolutePath, type: FileLock.LockType, blocking: Bool = true, _ body: () throws -> T) throws -> T {
try self.withLock(on: path.underlying, type: type, blocking: blocking, body)
}
/// Returns any known item replacement directories for a given path. These may be used by platform-specific
/// libraries to handle atomic file system operations, such as deletion.
func itemReplacementDirectories(for path: AbsolutePath) throws -> [AbsolutePath] {
return try self.itemReplacementDirectories(for: path.underlying).compactMap { AbsolutePath($0) }
}
}
// MARK: - user level
extension FileSystem {
/// SwiftPM directory under user's home directory (~/.swiftpm)
/// or under $XDG_CONFIG_HOME/swiftpm if the environmental variable is defined
public var dotSwiftPM: AbsolutePath {
get throws {
if let configurationDirectory = EnvironmentVariables.process()["XDG_CONFIG_HOME"] {
return try AbsolutePath(validating: configurationDirectory).appending("swiftpm")
} else {
return try self.homeDirectory.appending(".swiftpm")
}
}
}
private var idiomaticSwiftPMDirectory: AbsolutePath? {
get throws {
try FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first
.flatMap { try AbsolutePath(validating: $0.path) }?.appending("org.swift.swiftpm")
}
}
}
// MARK: - cache
extension FileSystem {
private var idiomaticUserCacheDirectory: AbsolutePath? {
// in TSC: FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)
self.cachesDirectory
}
/// SwiftPM cache directory under user's caches directory (if exists)
public var swiftPMCacheDirectory: AbsolutePath {
get throws {
if let path = self.idiomaticUserCacheDirectory {
return path.appending("org.swift.swiftpm")
} else {
return try self.dotSwiftPMCachesDirectory
}
}
}
private var dotSwiftPMCachesDirectory: AbsolutePath {
get throws {
try self.dotSwiftPM.appending("cache")
}
}
}
extension FileSystem {
public func getOrCreateSwiftPMCacheDirectory() throws -> AbsolutePath {
let idiomaticCacheDirectory = try self.swiftPMCacheDirectory
// Create idiomatic if necessary
if !self.exists(idiomaticCacheDirectory) {
try self.createDirectory(idiomaticCacheDirectory, recursive: true)
}
// Create ~/.swiftpm if necessary
if !self.exists(try self.dotSwiftPM) {
try self.createDirectory(self.dotSwiftPM, recursive: true)
}
// Create ~/.swiftpm/cache symlink if necessary
// locking ~/.swiftpm to protect from concurrent access
try self.withLock(on: self.dotSwiftPM, type: .exclusive) {
if !self.exists(try self.dotSwiftPMCachesDirectory, followSymlink: false) {
try self.createSymbolicLink(
dotSwiftPMCachesDirectory,
pointingAt: idiomaticCacheDirectory,
relative: false
)
}
}
return idiomaticCacheDirectory
}
}
extension FileSystem {
private var dotSwiftPMInstalledBinsDir: AbsolutePath {
get throws {
try self.dotSwiftPM.appending("bin")
}
}
public func getOrCreateSwiftPMInstalledBinariesDirectory() throws -> AbsolutePath {
let idiomaticInstalledBinariesDirectory = try self.dotSwiftPMInstalledBinsDir
// Create idiomatic if necessary
if !self.exists(idiomaticInstalledBinariesDirectory) {
try self.createDirectory(idiomaticInstalledBinariesDirectory, recursive: true)
}
// Create ~/.swiftpm if necessary
if !self.exists(try self.dotSwiftPM) {
try self.createDirectory(self.dotSwiftPM, recursive: true)
}
// Create ~/.swiftpm/bin symlink if necessary
// locking ~/.swiftpm to protect from concurrent access
try self.withLock(on: self.dotSwiftPM, type: .exclusive) {
if !self.exists(try self.dotSwiftPMInstalledBinsDir, followSymlink: false) {
try self.createSymbolicLink(
self.dotSwiftPMInstalledBinsDir,
pointingAt: idiomaticInstalledBinariesDirectory,
relative: false
)
}
}
return idiomaticInstalledBinariesDirectory
}
}
// MARK: - configuration
extension FileSystem {
/// SwiftPM config directory under user's config directory (if exists)
public var swiftPMConfigurationDirectory: AbsolutePath {
get throws {
if let path = try self.idiomaticSwiftPMDirectory {
return path.appending("configuration")
} else {
return try self.dotSwiftPMConfigurationDirectory
}
}
}
private var dotSwiftPMConfigurationDirectory: AbsolutePath {
get throws {
try self.dotSwiftPM.appending("configuration")
}
}
}
extension FileSystem {
public func getOrCreateSwiftPMConfigurationDirectory(warningHandler: @escaping (String) -> Void) throws
-> AbsolutePath
{
let idiomaticConfigurationDirectory = try self.swiftPMConfigurationDirectory
// temporary 5.6, remove on next version: transition from previous configuration location
if !self.exists(idiomaticConfigurationDirectory) {
try self.createDirectory(idiomaticConfigurationDirectory, recursive: true)
}
let handleExistingFiles = { (configurationFiles: [AbsolutePath]) in
for file in configurationFiles {
let destination = idiomaticConfigurationDirectory.appending(component: file.basename)
if !self.exists(destination) {
try self.copy(from: file, to: destination)
} else {
// Only emit a warning if source and destination file differ in their contents.
let srcContents = try? self.readFileContents(file)
let dstContents = try? self.readFileContents(destination)
if srcContents != dstContents {
warningHandler(
"Usage of \(file) has been deprecated. Please delete it and use the new \(destination) instead."
)
}
}
}
}
// in the case where ~/.swiftpm/configuration is not the idiomatic location (eg on macOS where its
// /Users/<user>/Library/org.swift.swiftpm/configuration)
if try idiomaticConfigurationDirectory != self.dotSwiftPMConfigurationDirectory {
// copy the configuration files from old location (eg /Users/<user>/Library/org.swift.swiftpm) to new one
// (eg /Users/<user>/Library/org.swift.swiftpm/configuration)
// but leave them there for backwards compatibility (eg older xcode)
let oldConfigDirectory = idiomaticConfigurationDirectory.parentDirectory
if self.exists(oldConfigDirectory, followSymlink: false) && self.isDirectory(oldConfigDirectory) {
let configurationFiles = try self.getDirectoryContents(oldConfigDirectory)
.map { oldConfigDirectory.appending(component: $0) }
.filter {
self.isFile($0) && !self.isSymlink($0) && $0
.extension != "lock" && ((try? self.readFileContents($0)) ?? []).count > 0
}
try handleExistingFiles(configurationFiles)
}
// in the case where ~/.swiftpm/configuration is the idiomatic location (eg on Linux)
} else {
// copy the configuration files from old location (~/.swiftpm/config) to new one (~/.swiftpm/configuration)
// but leave them there for backwards compatibility (eg older toolchain)
let oldConfigDirectory = try self.dotSwiftPM.appending("config")
if self.exists(oldConfigDirectory, followSymlink: false) && self.isDirectory(oldConfigDirectory) {
let configurationFiles = try self.getDirectoryContents(oldConfigDirectory)
.map { oldConfigDirectory.appending(component: $0) }
.filter {
self.isFile($0) && !self.isSymlink($0) && $0
.extension != "lock" && ((try? self.readFileContents($0)) ?? []).count > 0
}
try handleExistingFiles(configurationFiles)
}
}
// ~temporary 5.6 migration
// Create idiomatic if necessary
if !self.exists(idiomaticConfigurationDirectory) {
try self.createDirectory(idiomaticConfigurationDirectory, recursive: true)
}
// Create ~/.swiftpm if necessary
if !self.exists(try self.dotSwiftPM) {
try self.createDirectory(self.dotSwiftPM, recursive: true)
}
// Create ~/.swiftpm/configuration symlink if necessary
// locking ~/.swiftpm to protect from concurrent access
try self.withLock(on: self.dotSwiftPM, type: .exclusive) {
if !self.exists(try self.dotSwiftPMConfigurationDirectory, followSymlink: false) {
try self.createSymbolicLink(
dotSwiftPMConfigurationDirectory,
pointingAt: idiomaticConfigurationDirectory,
relative: false
)
}
}
return idiomaticConfigurationDirectory
}
}
// MARK: - security
extension FileSystem {
/// SwiftPM security directory under user's security directory (if exists)
public var swiftPMSecurityDirectory: AbsolutePath {
get throws {
if let path = try self.idiomaticSwiftPMDirectory {
return path.appending("security")
} else {
return try self.dotSwiftPMSecurityDirectory
}
}
}
private var dotSwiftPMSecurityDirectory: AbsolutePath {
get throws {
try self.dotSwiftPM.appending("security")
}
}
}
extension FileSystem {
public func getOrCreateSwiftPMSecurityDirectory() throws -> AbsolutePath {
let idiomaticSecurityDirectory = try self.swiftPMSecurityDirectory
// temporary 5.6, remove on next version: transition from ~/.swiftpm/security to idiomatic location + symbolic
// link
if try idiomaticSecurityDirectory != self.dotSwiftPMSecurityDirectory &&
self.exists(try self.dotSwiftPMSecurityDirectory) &&
self.isDirectory(try self.dotSwiftPMSecurityDirectory)
{
try self.removeFileTree(self.dotSwiftPMSecurityDirectory)
}
// ~temporary 5.6 migration
// Create idiomatic if necessary
if !self.exists(idiomaticSecurityDirectory) {
try self.createDirectory(idiomaticSecurityDirectory, recursive: true)
}
// Create ~/.swiftpm if necessary
if !self.exists(try self.dotSwiftPM) {
try self.createDirectory(self.dotSwiftPM, recursive: true)
}
// Create ~/.swiftpm/security symlink if necessary
// locking ~/.swiftpm to protect from concurrent access
try self.withLock(on: self.dotSwiftPM, type: .exclusive) {
if !self.exists(try self.dotSwiftPMSecurityDirectory, followSymlink: false) {
try self.createSymbolicLink(
dotSwiftPMSecurityDirectory,
pointingAt: idiomaticSecurityDirectory,
relative: false
)
}
}
return idiomaticSecurityDirectory
}
}
// MARK: - Swift SDKs
private let swiftSDKsDirectoryName = "swift-sdks"
extension FileSystem {
/// Path to Swift SDKs directory (if exists)
public var swiftSDKsDirectory: AbsolutePath {
get throws {
if let path = try idiomaticSwiftPMDirectory {
return path.appending(component: swiftSDKsDirectoryName)
} else {
return try dotSwiftPMSwiftSDKsDirectory
}
}
}
private var dotSwiftPMSwiftSDKsDirectory: AbsolutePath {
get throws {
try dotSwiftPM.appending(component: swiftSDKsDirectoryName)
}
}
public func getSharedSwiftSDKsDirectory(explicitDirectory: AbsolutePath?) throws -> AbsolutePath {
if let explicitDirectory {
// Create the explicit SDKs path if necessary
if !exists(explicitDirectory) {
try createDirectory(explicitDirectory, recursive: true)
}
return explicitDirectory
} else {
return try swiftSDKsDirectory
}
}
public func getOrCreateSwiftPMSwiftSDKsDirectory() throws -> AbsolutePath {
let idiomaticSwiftSDKDirectory = try swiftSDKsDirectory
// Create idiomatic if necessary
if !exists(idiomaticSwiftSDKDirectory) {
try createDirectory(idiomaticSwiftSDKDirectory, recursive: true)
}
// Create ~/.swiftpm if necessary
if !exists(try dotSwiftPM) {
try createDirectory(dotSwiftPM, recursive: true)
}
// Create ~/.swiftpm/swift-sdks symlink if necessary
// locking ~/.swiftpm to protect from concurrent access
try withLock(on: dotSwiftPM, type: .exclusive) {
if !exists(try dotSwiftPMSwiftSDKsDirectory, followSymlink: false) {
try createSymbolicLink(
dotSwiftPMSwiftSDKsDirectory,
pointingAt: idiomaticSwiftSDKDirectory,
relative: false
)
}
}
return idiomaticSwiftSDKDirectory
}
}
// MARK: - Utilities
extension FileSystem {
@_disfavoredOverload
public func readFileContents(_ path: AbsolutePath) throws -> Data {
try Data(self.readFileContents(path).contents)
}
@_disfavoredOverload
public func readFileContents(_ path: AbsolutePath) throws -> String {
try String(decoding: self.readFileContents(path), as: UTF8.self)
}
public func writeFileContents(_ path: AbsolutePath, data: Data) throws {
try self._writeFileContents(path, bytes: .init(data))
}
public func writeFileContents(_ path: AbsolutePath, string: String) throws {
try self._writeFileContents(path, bytes: .init(encodingAsUTF8: string))
}
private func _writeFileContents(_ path: AbsolutePath, bytes: ByteString) throws {
// using the "body" variant since it creates the directory first
// we should probably fix TSC to be consistent about this behavior
try self.writeFileContents(path, body: { $0.send(bytes) })
}
}
extension FileSystem {
/// Write bytes to the path if the given contents are different.
public func writeIfChanged(path: AbsolutePath, string: String) throws {
try writeIfChanged(path: path, bytes: .init(encodingAsUTF8: string))
}
public func writeIfChanged(path: AbsolutePath, data: Data) throws {
try writeIfChanged(path: path, bytes: .init(data))
}
/// Write bytes to the path if the given contents are different.
public func writeIfChanged(path: AbsolutePath, bytes: ByteString) throws {
try createDirectory(path.parentDirectory, recursive: true)
// Return if the contents are same.
if isFile(path), try readFileContents(path) == bytes {
return
}
try writeFileContents(path, bytes: bytes)
}
}
extension FileSystem {
public func forceCreateDirectory(at path: AbsolutePath) throws {
try self.createDirectory(path.parentDirectory, recursive: true)
if self.exists(path) {
try self.removeFileTree(path)
}
try self.createDirectory(path, recursive: true)
}
}
extension FileSystem {
public func stripFirstLevel(of path: AbsolutePath) throws {
let topLevelDirectories = try self.getDirectoryContents(path)
.map { path.appending(component: $0) }
.filter { self.isDirectory($0) }
guard topLevelDirectories.count == 1, let rootDirectory = topLevelDirectories.first else {
throw StringError("stripFirstLevel requires single top level directory")
}
let tempDirectory = path.parentDirectory.appending(component: UUID().uuidString)
try self.move(from: rootDirectory, to: tempDirectory)
let rootContents = try self.getDirectoryContents(tempDirectory)
for entry in rootContents {
try self.move(from: tempDirectory.appending(component: entry), to: path.appending(component: entry))
}
try self.removeFileTree(tempDirectory)
}
}
// MARK: - Locking
extension FileLock {
public static func prepareLock(
fileToLock: AbsolutePath,
at lockFilesDirectory: AbsolutePath? = nil
) throws -> FileLock {
return try Self.prepareLock(fileToLock: fileToLock.underlying, at: lockFilesDirectory?.underlying)
}
}