zio icon indicating copy to clipboard operation
zio copied to clipboard

Symlinks?

Open agocke opened this issue 2 years ago • 3 comments

I'm trying to adopt a VFS for testing and my app uses symlinks. I didn't see support in the IFileSystem API. Curious whether that's desired.

In theory it could be part of a new ISymlinkFileSystem interface instead. That would presumably prevent things like ZipFileSystem from having to implement support (or throw, more likely).

agocke avatar Mar 08 '23 05:03 agocke

I didn't see support in the IFileSystem API. Curious whether that's desired.

Why not. Originally, the IFileSystem API was replicating what is accessible from System.IO.FileInfo and directory. I dunno if we have anything related to symlinks in more recent .NET?

I would probably avoid a new interface and add it to IFileSystem directly. The base FileSystem could throw for most of the implementations.

xoofx avatar Mar 08 '23 05:03 xoofx

I found some stuff in my extensions that deals with this, using a mount file system.

        public static void MountSymlinkedDirectories(this MountFileSystem fs)
        {
            var dirs = fs.EnumerateDirectoryEntries("/", "*", SearchOption.AllDirectories);
            var symlinkDirs = dirs.Where(d => d.Attributes.HasFlag(FileAttributes.ReparsePoint)).ToList();

            foreach (var sd in symlinkDirs)
            {
                var systemPath = ResolveSymlinkedSystemPath(fs, sd.FullName);
                var slfs = CreatePhysicalSubFileSystem(systemPath);
                fs.Mount(sd.FullName, slfs);
            }
        }

        public static string ResolveSymlinkedSystemPath(this IFileSystem fs, UPath path)
        {
            var systemPath = fs.ConvertPathToInternal(path);
            var di = new DirectoryInfo(systemPath);
            return di.ResolveLinkTarget(true).FullName;
        }
        
        public static IFileSystem CreatePhysicalSubFileSystem(string systemPath)
        {
            var p = new PhysicalFileSystem();
            var subFilePath = p.ConvertPathFromInternal(systemPath);
            return new SubFileSystem(p, subFilePath);
        }
        

Ellested avatar Feb 27 '24 19:02 Ellested