@@ -187,13 +187,13 @@ public protocol FileSystem: AnyObject {
187
187
func changeCurrentWorkingDirectory( to path: AbsolutePath ) throws
188
188
189
189
/// Get the home directory of current user
190
- var homeDirectory : AbsolutePath { get }
190
+ func homeDirectory( ) throws -> AbsolutePath
191
191
192
192
/// Get the caches directory of current user
193
193
var cachesDirectory : AbsolutePath ? { get }
194
194
195
195
/// Get the temp directory
196
- var tempDirectory : AbsolutePath { get }
196
+ func tempDirectory( ) throws -> AbsolutePath
197
197
198
198
/// Create the given directory.
199
199
func createDirectory( _ path: AbsolutePath ) throws
@@ -317,7 +317,9 @@ private class LocalFileSystem: FileSystem {
317
317
}
318
318
319
319
func isFile( _ path: AbsolutePath ) -> Bool {
320
- let path = resolveSymlinks ( path)
320
+ guard let path = try ? resolveSymlinks ( path) else {
321
+ return false
322
+ }
321
323
let attrs = try ? FileManager . default. attributesOfItem ( atPath: path. pathString)
322
324
return attrs ? [ . type] as? FileAttributeType == . typeRegular
323
325
}
@@ -348,12 +350,12 @@ private class LocalFileSystem: FileSystem {
348
350
// interoperability in which case the return type of
349
351
// `fileSystemRepresentation` is different from the Swift implementation
350
352
// of Foundation.
351
- return try ? AbsolutePath ( validating : cwdStr)
353
+ return try ? AbsolutePath ( cwdStr)
352
354
#else
353
355
let fsr : UnsafePointer < Int8 > = cwdStr. fileSystemRepresentation
354
356
defer { fsr. deallocate ( ) }
355
357
356
- return try ? AbsolutePath ( validating : String ( cString: fsr) )
358
+ return try ? AbsolutePath ( String ( cString: fsr) )
357
359
#endif
358
360
}
359
361
@@ -367,20 +369,20 @@ private class LocalFileSystem: FileSystem {
367
369
}
368
370
}
369
371
370
- var homeDirectory : AbsolutePath {
371
- return AbsolutePath ( NSHomeDirectory ( ) )
372
+ func homeDirectory( ) throws -> AbsolutePath {
373
+ return try AbsolutePath ( NSHomeDirectory ( ) )
372
374
}
373
375
374
376
var cachesDirectory : AbsolutePath ? {
375
- return FileManager . default. urls ( for: . cachesDirectory, in: . userDomainMask) . first. flatMap { AbsolutePath ( $0. path) }
377
+ return FileManager . default. urls ( for: . cachesDirectory, in: . userDomainMask) . first. flatMap { try ? AbsolutePath ( $0. path) }
376
378
}
377
379
378
- var tempDirectory : AbsolutePath {
380
+ func tempDirectory( ) throws -> AbsolutePath {
379
381
let override = ProcessEnv . vars [ " TMPDIR " ] ?? ProcessEnv . vars [ " TEMP " ] ?? ProcessEnv . vars [ " TMP " ]
380
- if let path = override. flatMap ( { try ? AbsolutePath ( validating : $0) } ) {
382
+ if let path = override. flatMap ( { try ? AbsolutePath ( $0) } ) {
381
383
return path
382
384
}
383
- return AbsolutePath ( NSTemporaryDirectory ( ) )
385
+ return try AbsolutePath ( NSTemporaryDirectory ( ) )
384
386
}
385
387
386
388
func getDirectoryContents( _ path: AbsolutePath ) throws -> [ String ] {
@@ -664,7 +666,7 @@ public class InMemoryFileSystem: FileSystem {
664
666
case . directory, . file:
665
667
return node
666
668
case . symlink( let destination) :
667
- let destination = AbsolutePath ( destination, relativeTo: path. parentDirectory)
669
+ let destination = try AbsolutePath ( destination, relativeTo: path. parentDirectory)
668
670
return followSymlink ? try getNodeInternal ( destination) : node
669
671
case . none:
670
672
return nil
@@ -745,24 +747,24 @@ public class InMemoryFileSystem: FileSystem {
745
747
746
748
/// Virtualized current working directory.
747
749
public var currentWorkingDirectory : AbsolutePath ? {
748
- return AbsolutePath ( " / " )
750
+ return try ? AbsolutePath ( " / " )
749
751
}
750
752
751
753
public func changeCurrentWorkingDirectory( to path: AbsolutePath ) throws {
752
754
throw FileSystemError ( . unsupported, path)
753
755
}
754
756
755
- public var homeDirectory : AbsolutePath {
757
+ public func homeDirectory( ) throws -> AbsolutePath {
756
758
// FIXME: Maybe we should allow setting this when creating the fs.
757
- return AbsolutePath ( " /home/user " )
759
+ return try AbsolutePath ( " /home/user " )
758
760
}
759
761
760
762
public var cachesDirectory : AbsolutePath ? {
761
- return self . homeDirectory. appending ( component: " caches " )
763
+ return try ? self . homeDirectory ( ) . appending ( component: " caches " )
762
764
}
763
765
764
- public var tempDirectory : AbsolutePath {
765
- return AbsolutePath ( " /tmp " )
766
+ public func tempDirectory( ) throws -> AbsolutePath {
767
+ return try AbsolutePath ( " /tmp " )
766
768
}
767
769
768
770
public func getDirectoryContents( _ path: AbsolutePath ) throws -> [ String ] {
@@ -978,7 +980,7 @@ public class InMemoryFileSystem: FileSystem {
978
980
public func withLock< T> ( on path: AbsolutePath , type: FileLock . LockType = . exclusive, _ body: ( ) throws -> T ) throws -> T {
979
981
let resolvedPath : AbsolutePath = try lock. withLock {
980
982
if case let . symlink( destination) = try getNode ( path) ? . contents {
981
- return AbsolutePath ( destination, relativeTo: path. parentDirectory)
983
+ return try AbsolutePath ( destination, relativeTo: path. parentDirectory)
982
984
} else {
983
985
return path
984
986
}
@@ -1023,63 +1025,84 @@ public class RerootedFileSystemView: FileSystem {
1023
1025
}
1024
1026
1025
1027
/// Adjust the input path for the underlying file system.
1026
- private func formUnderlyingPath( _ path: AbsolutePath ) -> AbsolutePath {
1028
+ private func formUnderlyingPath( _ path: AbsolutePath ) throws -> AbsolutePath {
1027
1029
if path == AbsolutePath . root {
1028
1030
return root
1029
1031
} else {
1030
1032
// FIXME: Optimize?
1031
- return AbsolutePath ( String ( path. pathString. dropFirst ( 1 ) ) , relativeTo: root)
1033
+ return try AbsolutePath ( String ( path. pathString. dropFirst ( 1 ) ) , relativeTo: root)
1032
1034
}
1033
1035
}
1034
1036
1035
1037
// MARK: FileSystem Implementation
1036
1038
1037
1039
public func exists( _ path: AbsolutePath , followSymlink: Bool ) -> Bool {
1038
- return underlyingFileSystem. exists ( formUnderlyingPath ( path) , followSymlink: followSymlink)
1040
+ guard let underlying = try ? formUnderlyingPath ( path) else {
1041
+ return false
1042
+ }
1043
+ return underlyingFileSystem. exists ( underlying, followSymlink: followSymlink)
1039
1044
}
1040
1045
1041
1046
public func isDirectory( _ path: AbsolutePath ) -> Bool {
1042
- return underlyingFileSystem. isDirectory ( formUnderlyingPath ( path) )
1047
+ guard let underlying = try ? formUnderlyingPath ( path) else {
1048
+ return false
1049
+ }
1050
+ return underlyingFileSystem. isDirectory ( underlying)
1043
1051
}
1044
1052
1045
1053
public func isFile( _ path: AbsolutePath ) -> Bool {
1046
- return underlyingFileSystem. isFile ( formUnderlyingPath ( path) )
1054
+ guard let underlying = try ? formUnderlyingPath ( path) else {
1055
+ return false
1056
+ }
1057
+ return underlyingFileSystem. isFile ( underlying)
1047
1058
}
1048
1059
1049
1060
public func isSymlink( _ path: AbsolutePath ) -> Bool {
1050
- return underlyingFileSystem. isSymlink ( formUnderlyingPath ( path) )
1061
+ guard let underlying = try ? formUnderlyingPath ( path) else {
1062
+ return false
1063
+ }
1064
+ return underlyingFileSystem. isSymlink ( underlying)
1051
1065
}
1052
1066
1053
1067
public func isReadable( _ path: AbsolutePath ) -> Bool {
1054
- return underlyingFileSystem. isReadable ( formUnderlyingPath ( path) )
1068
+ guard let underlying = try ? formUnderlyingPath ( path) else {
1069
+ return false
1070
+ }
1071
+ return underlyingFileSystem. isReadable ( underlying)
1055
1072
}
1056
1073
1057
1074
public func isWritable( _ path: AbsolutePath ) -> Bool {
1058
- return underlyingFileSystem. isWritable ( formUnderlyingPath ( path) )
1075
+ guard let underlying = try ? formUnderlyingPath ( path) else {
1076
+ return false
1077
+ }
1078
+ return underlyingFileSystem. isWritable ( underlying)
1059
1079
}
1060
1080
1061
1081
public func isExecutableFile( _ path: AbsolutePath ) -> Bool {
1062
- return underlyingFileSystem. isExecutableFile ( formUnderlyingPath ( path) )
1082
+ guard let underlying = try ? formUnderlyingPath ( path) else {
1083
+ return false
1084
+ }
1085
+ return underlyingFileSystem. isExecutableFile ( underlying)
1063
1086
}
1064
1087
1065
1088
/// Virtualized current working directory.
1066
1089
public var currentWorkingDirectory : AbsolutePath ? {
1067
- return AbsolutePath ( " / " )
1090
+ return try ? AbsolutePath ( " / " )
1068
1091
}
1069
1092
1070
1093
public func changeCurrentWorkingDirectory( to path: AbsolutePath ) throws {
1071
1094
throw FileSystemError ( . unsupported, path)
1072
1095
}
1073
1096
1074
- public var homeDirectory : AbsolutePath {
1097
+ public func homeDirectory( ) throws -> AbsolutePath {
1075
1098
fatalError ( " homeDirectory on RerootedFileSystemView is not supported. " )
1076
1099
}
1077
1100
1078
1101
public var cachesDirectory : AbsolutePath ? {
1079
1102
fatalError ( " cachesDirectory on RerootedFileSystemView is not supported. " )
1080
1103
}
1081
1104
1082
- public var tempDirectory : AbsolutePath {
1105
+ public func tempDirectory( ) throws -> AbsolutePath {
1083
1106
fatalError ( " tempDirectory on RerootedFileSystemView is not supported. " )
1084
1107
}
1085
1108
@@ -1088,13 +1111,13 @@ public class RerootedFileSystemView: FileSystem {
1088
1111
}
1089
1112
1090
1113
public func createDirectory( _ path: AbsolutePath , recursive: Bool ) throws {
1091
- let path = formUnderlyingPath ( path)
1114
+ let path = try formUnderlyingPath ( path)
1092
1115
return try underlyingFileSystem. createDirectory ( path, recursive: recursive)
1093
1116
}
1094
1117
1095
1118
public func createSymbolicLink( _ path: AbsolutePath , pointingAt destination: AbsolutePath , relative: Bool ) throws {
1096
- let path = formUnderlyingPath ( path)
1097
- let destination = formUnderlyingPath ( destination)
1119
+ let path = try formUnderlyingPath ( path)
1120
+ let destination = try formUnderlyingPath ( destination)
1098
1121
return try underlyingFileSystem. createSymbolicLink ( path, pointingAt: destination, relative: relative)
1099
1122
}
1100
1123
@@ -1103,7 +1126,7 @@ public class RerootedFileSystemView: FileSystem {
1103
1126
}
1104
1127
1105
1128
public func writeFileContents( _ path: AbsolutePath , bytes: ByteString ) throws {
1106
- let path = formUnderlyingPath ( path)
1129
+ let path = try formUnderlyingPath ( path)
1107
1130
return try underlyingFileSystem. writeFileContents ( path, bytes: bytes)
1108
1131
}
1109
1132
0 commit comments