From 88a5b2eb9c3c6bef406a66e554af2112cad6b760 Mon Sep 17 00:00:00 2001 From: gyk4j <147011991+gyk4j@users.noreply.github.com> Date: Wed, 28 Feb 2024 14:11:49 +0800 Subject: [PATCH] Add ToRealPath() --- JShim/Sun/NIO/FS/WindowsPath.cs | 45 ++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/JShim/Sun/NIO/FS/WindowsPath.cs b/JShim/Sun/NIO/FS/WindowsPath.cs index ffc5f08..c26e0ae 100644 --- a/JShim/Sun/NIO/FS/WindowsPath.cs +++ b/JShim/Sun/NIO/FS/WindowsPath.cs @@ -320,7 +320,50 @@ public System.IO.FileSystemInfo ToFile() /// public Path ToRealPath(params LinkOption[] options) { - throw new NotImplementedException(); + bool noFollowLinks = false; + + foreach(LinkOption o in options) + { + if(o == LinkOption.NoFollowLinks) + { + noFollowLinks = true; + break; + } + } + + string fp = System.IO.Path.GetFullPath(this.path); + System.IO.FileSystemInfo fsi; + + if(System.IO.File.Exists(fp)) + fsi = new System.IO.FileInfo(fp); + else if(System.IO.Directory.Exists(fp)) + fsi = new System.IO.DirectoryInfo(fp); + else + throw new ApplicationException("Unknown file system object type: " + fp); + + if(noFollowLinks && Microsoft.NET.FSUtils.IsReparsePoint(fsi)) + return null; + + WindowsPathType type; + string root, path; + + int volumePos = fp.IndexOf(System.IO.Path.VolumeSeparatorChar); + if(volumePos < 0) + { + // Has no root/volume (path with no drive) + type = WindowsPathType.Relative; + root = null; + path = fp; + } + else + { + // Has root/volume + type = WindowsPathType.Absolute; + root = fp.Substring(0, volumePos+1); + path = fp.Substring(volumePos+1); + } + + return new WindowsPath(fs, type, root, path); } public Uri ToURI()