@@ -187,13 +187,13 @@ public protocol FileSystem: AnyObject {
187187 func changeCurrentWorkingDirectory( to path: AbsolutePath ) throws
188188
189189 /// Get the home directory of current user
190- var homeDirectory : AbsolutePath { get }
190+ var homeDirectory : AbsolutePath { get throws }
191191
192192 /// Get the caches directory of current user
193193 var cachesDirectory : AbsolutePath ? { get }
194194
195195 /// Get the temp directory
196- var tempDirectory : AbsolutePath { get }
196+ var tempDirectory : AbsolutePath { get throws }
197197
198198 /// Create the given directory.
199199 func createDirectory( _ path: AbsolutePath ) throws
@@ -317,7 +317,9 @@ private class LocalFileSystem: FileSystem {
317317 }
318318
319319 func isFile( _ path: AbsolutePath ) -> Bool {
320- let path = resolveSymlinks ( path)
320+ guard let path = try ? resolveSymlinks ( path) else {
321+ return false
322+ }
321323 let attrs = try ? FileManager . default. attributesOfItem ( atPath: path. pathString)
322324 return attrs ? [ . type] as? FileAttributeType == . typeRegular
323325 }
@@ -353,7 +355,7 @@ private class LocalFileSystem: FileSystem {
353355 let fsr : UnsafePointer < Int8 > = cwdStr. fileSystemRepresentation
354356 defer { fsr. deallocate ( ) }
355357
356- return try ? AbsolutePath ( validating : String ( cString: fsr) )
358+ return try ? AbsolutePath ( String ( cString: fsr) )
357359#endif
358360 }
359361
@@ -368,19 +370,23 @@ private class LocalFileSystem: FileSystem {
368370 }
369371
370372 var homeDirectory : AbsolutePath {
371- return AbsolutePath ( NSHomeDirectory ( ) )
373+ get throws {
374+ return try AbsolutePath ( validating: NSHomeDirectory ( ) )
375+ }
372376 }
373377
374378 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) }
376380 }
377381
378382 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 ( ) )
382389 }
383- return AbsolutePath ( NSTemporaryDirectory ( ) )
384390 }
385391
386392 func getDirectoryContents( _ path: AbsolutePath ) throws -> [ String ] {
@@ -664,7 +670,7 @@ public class InMemoryFileSystem: FileSystem {
664670 case . directory, . file:
665671 return node
666672 case . symlink( let destination) :
667- let destination = AbsolutePath ( destination, relativeTo: path. parentDirectory)
673+ let destination = try AbsolutePath ( validating : destination, relativeTo: path. parentDirectory)
668674 return followSymlink ? try getNodeInternal ( destination) : node
669675 case . none:
670676 return nil
@@ -745,24 +751,28 @@ public class InMemoryFileSystem: FileSystem {
745751
746752 /// Virtualized current working directory.
747753 public var currentWorkingDirectory : AbsolutePath ? {
748- return AbsolutePath ( " / " )
754+ return try ? AbsolutePath ( validating : " / " )
749755 }
750756
751757 public func changeCurrentWorkingDirectory( to path: AbsolutePath ) throws {
752758 throw FileSystemError ( . unsupported, path)
753759 }
754760
755761 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+ }
758766 }
759767
760768 public var cachesDirectory : AbsolutePath ? {
761- return self . homeDirectory. appending ( component: " caches " )
769+ return try ? self . homeDirectory. appending ( component: " caches " )
762770 }
763771
764772 public var tempDirectory : AbsolutePath {
765- return AbsolutePath ( " /tmp " )
773+ get throws {
774+ return try AbsolutePath ( validating: " /tmp " )
775+ }
766776 }
767777
768778 public func getDirectoryContents( _ path: AbsolutePath ) throws -> [ String ] {
@@ -978,7 +988,7 @@ public class InMemoryFileSystem: FileSystem {
978988 public func withLock< T> ( on path: AbsolutePath , type: FileLock . LockType = . exclusive, _ body: ( ) throws -> T ) throws -> T {
979989 let resolvedPath : AbsolutePath = try lock. withLock {
980990 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)
982992 } else {
983993 return path
984994 }
@@ -1023,48 +1033,69 @@ public class RerootedFileSystemView: FileSystem {
10231033 }
10241034
10251035 /// Adjust the input path for the underlying file system.
1026- private func formUnderlyingPath( _ path: AbsolutePath ) -> AbsolutePath {
1036+ private func formUnderlyingPath( _ path: AbsolutePath ) throws -> AbsolutePath {
10271037 if path == AbsolutePath . root {
10281038 return root
10291039 } else {
10301040 // FIXME: Optimize?
1031- return AbsolutePath ( String ( path. pathString. dropFirst ( 1 ) ) , relativeTo: root)
1041+ return try AbsolutePath ( validating : String ( path. pathString. dropFirst ( 1 ) ) , relativeTo: root)
10321042 }
10331043 }
10341044
10351045 // MARK: FileSystem Implementation
10361046
10371047 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)
10391052 }
10401053
10411054 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)
10431059 }
10441060
10451061 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)
10471066 }
10481067
10491068 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)
10511073 }
10521074
10531075 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)
10551080 }
10561081
10571082 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)
10591087 }
10601088
10611089 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)
10631094 }
10641095
10651096 /// Virtualized current working directory.
10661097 public var currentWorkingDirectory : AbsolutePath ? {
1067- return AbsolutePath ( " / " )
1098+ return try ? AbsolutePath ( validating : " / " )
10681099 }
10691100
10701101 public func changeCurrentWorkingDirectory( to path: AbsolutePath ) throws {
@@ -1088,13 +1119,13 @@ public class RerootedFileSystemView: FileSystem {
10881119 }
10891120
10901121 public func createDirectory( _ path: AbsolutePath , recursive: Bool ) throws {
1091- let path = formUnderlyingPath ( path)
1122+ let path = try formUnderlyingPath ( path)
10921123 return try underlyingFileSystem. createDirectory ( path, recursive: recursive)
10931124 }
10941125
10951126 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)
10981129 return try underlyingFileSystem. createSymbolicLink ( path, pointingAt: destination, relative: relative)
10991130 }
11001131
@@ -1103,7 +1134,7 @@ public class RerootedFileSystemView: FileSystem {
11031134 }
11041135
11051136 public func writeFileContents( _ path: AbsolutePath , bytes: ByteString ) throws {
1106- let path = formUnderlyingPath ( path)
1137+ let path = try formUnderlyingPath ( path)
11071138 return try underlyingFileSystem. writeFileContents ( path, bytes: bytes)
11081139 }
11091140
0 commit comments