using System; using System.Linq; using System.Runtime.InteropServices; using System.Threading.Tasks; using RomRepoMgr.Database; namespace RomRepoMgr.Core.Filesystem { public class Vfs : IDisposable { Fuse _fuse; Winfsp _winfsp; public static bool IsAvailable => Winfsp.IsAvailable || Fuse.IsAvailable; public void Dispose() => _fuse?.Dispose(); public event EventHandler Umounted; public void MountTo(string mountPoint) { if(Fuse.IsAvailable) { _fuse = new Fuse(this) { MountPoint = mountPoint }; Task.Run(() => { _fuse.Start(); Umounted?.Invoke(this, System.EventArgs.Empty); }); } else if(Winfsp.IsAvailable) { _winfsp = new Winfsp(this); bool ret = _winfsp.Mount(mountPoint); if(ret) return; _winfsp = null; Umounted?.Invoke(this, System.EventArgs.Empty); } else Umounted?.Invoke(this, System.EventArgs.Empty); } public void Umount() { _fuse?.Umount(); _fuse = null; _winfsp?.Umount(); _winfsp = null; Umounted?.Invoke(this, System.EventArgs.Empty); } internal void GetInfo(out ulong files, out ulong totalSize) { using var ctx = Context.Create(Settings.Settings.Current.DatabasePath); totalSize = (ulong)ctx.Files.Where(f => f.IsInRepo).Sum(f => (double)f.Size); files = (ulong)ctx.Files.Count(f => f.IsInRepo); } internal string[] SplitPath(string path) => path.Split(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "\\" : "/", StringSplitOptions.RemoveEmptyEntries); } }