[Mono.Posix] Add UnixFileSystemInfo.TryGetFileSystemEntry()
authorJonathan Pryor <jonpryor@vt.edu>
Tue, 6 Mar 2012 13:08:07 +0000 (08:08 -0500)
committerJonathan Pryor <jonpryor@vt.edu>
Tue, 6 Mar 2012 13:13:37 +0000 (08:13 -0500)
UnixFileSystemInfo.GetFileSystemEntry() throws a
DirectoryNotFoundException when provided an invalid path (e.g.
/path/to/filename.ext/extra), because lstat(2) returns ENOTDIR.

Add UnixFileSystemInfo.TryGetFileSystemEntry() as a non-throwing
variant which returns the UnixFileSystemInfo as an `out` parameter.
This allows avoiding the exception, if desired.

mcs/class/Mono.Posix/Mono.Unix/UnixFileSystemInfo.cs

index 13909c9794ae03b94abb9f017bdaae1e4039232a..c6a9b283b5f193b5a061d1c6108777522d75bd1c 100644 (file)
@@ -387,19 +387,40 @@ namespace Mono.Unix {
                }
 
                public static UnixFileSystemInfo GetFileSystemEntry (string path)
+               {
+                       UnixFileSystemInfo info;
+                       if (TryGetFileSystemEntry (path, out info))
+                               return info;
+
+                       UnixMarshal.ThrowExceptionForLastError ();
+
+                       // Throw DirectoryNotFoundException because lstat(2) probably failed
+                       // because of ENOTDIR (e.g. "/path/to/file/wtf"), so
+                       // DirectoryNotFoundException is what would have been thrown anyway.
+                       throw new DirectoryNotFoundException ("UnixMarshal.ThrowExceptionForLastError didn't throw?!");
+               }
+
+               public static bool TryGetFileSystemEntry (string path, out UnixFileSystemInfo entry)
                {
                        Native.Stat stat;
                        int r = Native.Syscall.lstat (path, out stat);
-                       if (r == -1 && Native.Stdlib.GetLastError() == Native.Errno.ENOENT) {
-                               return new UnixFileInfo (path);
+                       if (r == -1) {
+                               if (Native.Stdlib.GetLastError() == Native.Errno.ENOENT) {
+                                       entry = new UnixFileInfo (path);
+                                       return true;
+                               }
+                               entry = null;
+                               return false;
                        }
-                       UnixMarshal.ThrowExceptionForLastErrorIf (r);
 
                        if (IsFileType (stat.st_mode, Native.FilePermissions.S_IFDIR))
-                               return new UnixDirectoryInfo (path, stat);
+                               entry = new UnixDirectoryInfo (path, stat);
                        else if (IsFileType (stat.st_mode, Native.FilePermissions.S_IFLNK))
-                               return new UnixSymbolicLinkInfo (path, stat);
-                       return new UnixFileInfo (path, stat);
+                               entry = new UnixSymbolicLinkInfo (path, stat);
+                       else
+                               entry = new UnixFileInfo (path, stat);
+
+                       return true;
                }
        }
 }