@@ -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
+ var homeDirectory : AbsolutePath { get throws }
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
+ var tempDirectory : AbsolutePath { get throws }
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
}
@@ -353,7 +355,7 @@ private class LocalFileSystem: FileSystem {
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
@@ -368,19 +370,23 @@ private class LocalFileSystem: FileSystem {
368
370
}
369
371
370
372
var homeDirectory : AbsolutePath {
371
- return AbsolutePath ( NSHomeDirectory ( ) )
373
+ get throws {
374
+ return try AbsolutePath ( validating: NSHomeDirectory ( ) )
375
+ }
372
376
}
373
377
374
378
var cachesDirectory : AbsolutePath ? {
375
- return FileManager . default. urls ( for: . cachesDirectory, in: . userDomainMask) . first. flatMap { AbsolutePath ( $0. path) }
379
+ return FileManager . default. urls ( for: . cachesDirectory, in: . userDomainMask) . first. flatMap { try ? AbsolutePath ( validating : $0. path) }
376
380
}
377
381
378
382
var tempDirectory : AbsolutePath {
379
- let override = ProcessEnv . vars [ " TMPDIR " ] ?? ProcessEnv . vars [ " TEMP " ] ?? ProcessEnv . vars [ " TMP " ]
380
- if let path = override. flatMap ( { try ? AbsolutePath ( validating: $0) } ) {
381
- return path
383
+ get throws {
384
+ let override = ProcessEnv . vars [ " TMPDIR " ] ?? ProcessEnv . vars [ " TEMP " ] ?? ProcessEnv . vars [ " TMP " ]
385
+ if let path = override. flatMap ( { try ? AbsolutePath ( validating: $0) } ) {
386
+ return path
387
+ }
388
+ return try AbsolutePath ( validating: NSTemporaryDirectory ( ) )
382
389
}
383
- return AbsolutePath ( NSTemporaryDirectory ( ) )
384
390
}
385
391
386
392
func getDirectoryContents( _ path: AbsolutePath ) throws -> [ String ] {
@@ -664,7 +670,7 @@ public class InMemoryFileSystem: FileSystem {
664
670
case . directory, . file:
665
671
return node
666
672
case . symlink( let destination) :
667
- let destination = AbsolutePath ( destination, relativeTo: path. parentDirectory)
673
+ let destination = try AbsolutePath ( validating : destination, relativeTo: path. parentDirectory)
668
674
return followSymlink ? try getNodeInternal ( destination) : node
669
675
case . none:
670
676
return nil
@@ -745,24 +751,28 @@ public class InMemoryFileSystem: FileSystem {
745
751
746
752
/// Virtualized current working directory.
747
753
public var currentWorkingDirectory : AbsolutePath ? {
748
- return AbsolutePath ( " / " )
754
+ return try ? AbsolutePath ( validating : " / " )
749
755
}
750
756
751
757
public func changeCurrentWorkingDirectory( to path: AbsolutePath ) throws {
752
758
throw FileSystemError ( . unsupported, path)
753
759
}
754
760
755
761
public var homeDirectory : AbsolutePath {
756
- // FIXME: Maybe we should allow setting this when creating the fs.
757
- return AbsolutePath ( " /home/user " )
762
+ get throws {
763
+ // FIXME: Maybe we should allow setting this when creating the fs.
764
+ return try AbsolutePath ( validating: " /home/user " )
765
+ }
758
766
}
759
767
760
768
public var cachesDirectory : AbsolutePath ? {
761
- return self . homeDirectory. appending ( component: " caches " )
769
+ return try ? self . homeDirectory. appending ( component: " caches " )
762
770
}
763
771
764
772
public var tempDirectory : AbsolutePath {
765
- return AbsolutePath ( " /tmp " )
773
+ get throws {
774
+ return try AbsolutePath ( validating: " /tmp " )
775
+ }
766
776
}
767
777
768
778
public func getDirectoryContents( _ path: AbsolutePath ) throws -> [ String ] {
@@ -978,7 +988,7 @@ public class InMemoryFileSystem: FileSystem {
978
988
public func withLock< T> ( on path: AbsolutePath , type: FileLock . LockType = . exclusive, _ body: ( ) throws -> T ) throws -> T {
979
989
let resolvedPath : AbsolutePath = try lock. withLock {
980
990
if case let . symlink( destination) = try getNode ( path) ? . contents {
981
- return AbsolutePath ( destination, relativeTo: path. parentDirectory)
991
+ return try AbsolutePath ( validating : destination, relativeTo: path. parentDirectory)
982
992
} else {
983
993
return path
984
994
}
@@ -1023,48 +1033,69 @@ public class RerootedFileSystemView: FileSystem {
1023
1033
}
1024
1034
1025
1035
/// Adjust the input path for the underlying file system.
1026
- private func formUnderlyingPath( _ path: AbsolutePath ) -> AbsolutePath {
1036
+ private func formUnderlyingPath( _ path: AbsolutePath ) throws -> AbsolutePath {
1027
1037
if path == AbsolutePath . root {
1028
1038
return root
1029
1039
} else {
1030
1040
// FIXME: Optimize?
1031
- return AbsolutePath ( String ( path. pathString. dropFirst ( 1 ) ) , relativeTo: root)
1041
+ return try AbsolutePath ( validating : String ( path. pathString. dropFirst ( 1 ) ) , relativeTo: root)
1032
1042
}
1033
1043
}
1034
1044
1035
1045
// MARK: FileSystem Implementation
1036
1046
1037
1047
public func exists( _ path: AbsolutePath , followSymlink: Bool ) -> Bool {
1038
- return underlyingFileSystem. exists ( formUnderlyingPath ( path) , followSymlink: followSymlink)
1048
+ guard let underlying = try ? formUnderlyingPath ( path) else {
1049
+ return false
1050
+ }
1051
+ return underlyingFileSystem. exists ( underlying, followSymlink: followSymlink)
1039
1052
}
1040
1053
1041
1054
public func isDirectory( _ path: AbsolutePath ) -> Bool {
1042
- return underlyingFileSystem. isDirectory ( formUnderlyingPath ( path) )
1055
+ guard let underlying = try ? formUnderlyingPath ( path) else {
1056
+ return false
1057
+ }
1058
+ return underlyingFileSystem. isDirectory ( underlying)
1043
1059
}
1044
1060
1045
1061
public func isFile( _ path: AbsolutePath ) -> Bool {
1046
- return underlyingFileSystem. isFile ( formUnderlyingPath ( path) )
1062
+ guard let underlying = try ? formUnderlyingPath ( path) else {
1063
+ return false
1064
+ }
1065
+ return underlyingFileSystem. isFile ( underlying)
1047
1066
}
1048
1067
1049
1068
public func isSymlink( _ path: AbsolutePath ) -> Bool {
1050
- return underlyingFileSystem. isSymlink ( formUnderlyingPath ( path) )
1069
+ guard let underlying = try ? formUnderlyingPath ( path) else {
1070
+ return false
1071
+ }
1072
+ return underlyingFileSystem. isSymlink ( underlying)
1051
1073
}
1052
1074
1053
1075
public func isReadable( _ path: AbsolutePath ) -> Bool {
1054
- return underlyingFileSystem. isReadable ( formUnderlyingPath ( path) )
1076
+ guard let underlying = try ? formUnderlyingPath ( path) else {
1077
+ return false
1078
+ }
1079
+ return underlyingFileSystem. isReadable ( underlying)
1055
1080
}
1056
1081
1057
1082
public func isWritable( _ path: AbsolutePath ) -> Bool {
1058
- return underlyingFileSystem. isWritable ( formUnderlyingPath ( path) )
1083
+ guard let underlying = try ? formUnderlyingPath ( path) else {
1084
+ return false
1085
+ }
1086
+ return underlyingFileSystem. isWritable ( underlying)
1059
1087
}
1060
1088
1061
1089
public func isExecutableFile( _ path: AbsolutePath ) -> Bool {
1062
- return underlyingFileSystem. isExecutableFile ( formUnderlyingPath ( path) )
1090
+ guard let underlying = try ? formUnderlyingPath ( path) else {
1091
+ return false
1092
+ }
1093
+ return underlyingFileSystem. isExecutableFile ( underlying)
1063
1094
}
1064
1095
1065
1096
/// Virtualized current working directory.
1066
1097
public var currentWorkingDirectory : AbsolutePath ? {
1067
- return AbsolutePath ( " / " )
1098
+ return try ? AbsolutePath ( validating : " / " )
1068
1099
}
1069
1100
1070
1101
public func changeCurrentWorkingDirectory( to path: AbsolutePath ) throws {
@@ -1088,13 +1119,13 @@ public class RerootedFileSystemView: FileSystem {
1088
1119
}
1089
1120
1090
1121
public func createDirectory( _ path: AbsolutePath , recursive: Bool ) throws {
1091
- let path = formUnderlyingPath ( path)
1122
+ let path = try formUnderlyingPath ( path)
1092
1123
return try underlyingFileSystem. createDirectory ( path, recursive: recursive)
1093
1124
}
1094
1125
1095
1126
public func createSymbolicLink( _ path: AbsolutePath , pointingAt destination: AbsolutePath , relative: Bool ) throws {
1096
- let path = formUnderlyingPath ( path)
1097
- let destination = formUnderlyingPath ( destination)
1127
+ let path = try formUnderlyingPath ( path)
1128
+ let destination = try formUnderlyingPath ( destination)
1098
1129
return try underlyingFileSystem. createSymbolicLink ( path, pointingAt: destination, relative: relative)
1099
1130
}
1100
1131
@@ -1103,7 +1134,7 @@ public class RerootedFileSystemView: FileSystem {
1103
1134
}
1104
1135
1105
1136
public func writeFileContents( _ path: AbsolutePath , bytes: ByteString ) throws {
1106
- let path = formUnderlyingPath ( path)
1137
+ let path = try formUnderlyingPath ( path)
1107
1138
return try underlyingFileSystem. writeFileContents ( path, bytes: bytes)
1108
1139
}
1109
1140
0 commit comments