tesses-vfs/Tesses.VirtualFilesystem.Zio/Class1.cs

178 lines
7.0 KiB
C#

/*
Tesses.VirtualFilesystem a library for virtual filesystems in .NET
Copyright (C) 2023 Mike Nolan
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using Zio;
using Zio.FileSystems;
using Tesses.VirtualFilesystem;
using System.Collections.Generic;
using System;
using System.IO;
namespace Tesses.VirtualFilesystem.Filesystems
{
public class ZioFileSystem : SyncFileSystem
{
IFileSystem fs;
public ZioFileSystem(IFileSystem fs)
{
this.fs = fs;
}
public UPath ConvertUPathFromUnixPath(UnixPath path)
{
return path.Path;
}
public UnixPath ConvertUPathToUnixPath(UPath path)
{
return new UnixPath(path.ToString());
}
public override void CreateDirectory(UnixPath directory)
{
fs.CreateDirectory(ConvertUPathFromUnixPath(directory));
}
public override void DeleteDirectory(UnixPath path)
{
fs.DeleteDirectory(ConvertUPathFromUnixPath(path),false);
}
public override void DeleteFile(UnixPath path)
{
fs.DeleteFile(ConvertUPathFromUnixPath(path));
}
public override bool DirectoryExists(UnixPath path)
{
return fs.DirectoryExists(ConvertUPathFromUnixPath(path));
}
public override bool FileExists(UnixPath path)
{
return fs.FileExists(ConvertUPathFromUnixPath(path));
}
public override DateTime GetCreationTime(UnixPath path)
{
return fs.GetCreationTime(ConvertUPathFromUnixPath(path));
}
public override DateTime GetLastAccessTime(UnixPath path)
{
return fs.GetLastAccessTime(ConvertUPathFromUnixPath(path));
}
public override DateTime GetLastWriteTime(UnixPath path)
{
return fs.GetLastWriteTime(ConvertUPathFromUnixPath(path));
}
public override void MoveDirectory(UnixPath src, UnixPath dest)
{
fs.MoveDirectory(ConvertUPathFromUnixPath(src),ConvertUPathFromUnixPath(dest));
}
public override void MoveFile(UnixPath src, UnixPath dest)
{
fs.MoveFile(ConvertUPathFromUnixPath(src),ConvertUPathFromUnixPath(dest));
}
public override Stream Open(UnixPath path, FileMode mode, FileAccess access, FileShare share)
{
return fs.OpenFile(ConvertUPathFromUnixPath(path),mode,access,share);
}
public override void SetCreationTime(UnixPath path, DateTime time)
{
fs.SetCreationTime(ConvertUPathFromUnixPath(path),time);
}
public override void SetLastAccessTime(UnixPath path, DateTime time)
{
fs.SetLastAccessTime(ConvertUPathFromUnixPath(path),time);
}
public override void SetLastWriteTime(UnixPath path, DateTime time)
{
fs.SetLastWriteTime(ConvertUPathFromUnixPath(path),time);
}
public override void SetAttributes(UnixPath path, FileAttributes attributes)
{
fs.SetAttributes(ConvertUPathFromUnixPath(path),attributes);
}
public override FileAttributes GetAttributes(UnixPath path)
{
return fs.GetAttributes(ConvertUPathFromUnixPath(path));
}
public override bool CanWatch(UnixPath path)
{
return fs.CanWatch(ConvertUPathFromUnixPath(path));
}
public override IVirtualWatcher WatchDirectory(UnixPath dir)
{
return new ZioWatcher(this,fs,dir);
}
public override IEnumerable<UnixPath> EnumerateFileSystemEntries(UnixPath path)
{
foreach(var item in fs.EnumeratePaths(ConvertUPathFromUnixPath(path)))
{
yield return ConvertUPathToUnixPath(item);
}
}
private class ZioWatcher : IVirtualWatcher
{
Zio.IFileSystemWatcher watcher;
ZioFileSystem _fs;
public ZioWatcher(ZioFileSystem fs,IFileSystem fs2,UnixPath path)
{
watcher = fs2.Watch(fs.ConvertUPathFromUnixPath(path));
watcher.Changed += (sender,e)=>{
Changed?.Invoke(this,new VirtualWatcherChangedArgs(fs,(System.IO.WatcherChangeTypes)(int)e.ChangeType,fs.ConvertUPathToUnixPath(e.FullPath)));
};
watcher.Created += (sender,e)=>{
Created?.Invoke(this,new VirtualWatcherChangedArgs(fs,(System.IO.WatcherChangeTypes)(int)e.ChangeType,fs.ConvertUPathToUnixPath(e.FullPath)));
};
watcher.Deleted += (sender,e)=>{
Deleted?.Invoke(this,new VirtualWatcherChangedArgs(fs,(System.IO.WatcherChangeTypes)(int)e.ChangeType,fs.ConvertUPathToUnixPath(e.FullPath)));
};
watcher.Error += (sender,e)=>{
Error?.Invoke(this,new ErrorEventArgs(e.Exception));
};
watcher.Renamed += (sender,e)=>{
Renamed?.Invoke(this,new VirtualWatcherRenamedEventArgs(fs,(System.IO.WatcherChangeTypes)(int)e.ChangeType,fs.ConvertUPathToUnixPath(e.FullPath),fs.ConvertUPathToUnixPath(e.OldFullPath)));
};
_fs=fs;
}
public int InternalBufferSize { get => watcher.InternalBufferSize; set => watcher.InternalBufferSize=value;}
public System.IO.NotifyFilters NotifyFilter { get => (System.IO.NotifyFilters)watcher.NotifyFilter; set => watcher.NotifyFilter=(Zio.NotifyFilters)value; }
public bool EnableRaisingEvents { get => watcher.EnableRaisingEvents; set => watcher.EnableRaisingEvents=value; }
public string Filter { get => watcher.Filter; set => watcher.Filter=value; }
public bool IncludeSubdirectories { get => watcher.IncludeSubdirectories; set => watcher.IncludeSubdirectories=value;}
public event EventHandler<VirtualWatcherChangedArgs> Changed;
public event EventHandler<VirtualWatcherChangedArgs> Created;
public event EventHandler<VirtualWatcherChangedArgs> Deleted;
public event EventHandler<VirtualWatcherRenamedEventArgs> Renamed;
public event EventHandler<ErrorEventArgs> Error;
}
}
}