2020-09-01 11:54:16 +01:00
|
|
|
using System;
|
2020-09-03 00:14:11 +01:00
|
|
|
using System.Linq;
|
2020-09-03 00:39:05 +01:00
|
|
|
using System.Runtime.InteropServices;
|
2020-09-01 11:54:16 +01:00
|
|
|
using System.Threading.Tasks;
|
2020-09-03 00:14:11 +01:00
|
|
|
using RomRepoMgr.Database;
|
2020-09-01 11:54:16 +01:00
|
|
|
|
2020-08-30 14:22:41 +01:00
|
|
|
namespace RomRepoMgr.Core.Filesystem
|
|
|
|
|
{
|
2020-09-01 11:54:16 +01:00
|
|
|
public class Vfs : IDisposable
|
2020-08-30 14:22:41 +01:00
|
|
|
{
|
2020-09-02 16:53:21 +01:00
|
|
|
Fuse _fuse;
|
|
|
|
|
Winfsp _winfsp;
|
2020-09-01 11:54:16 +01:00
|
|
|
|
2020-08-30 14:22:41 +01:00
|
|
|
public static bool IsAvailable => Winfsp.IsAvailable || Fuse.IsAvailable;
|
2020-09-01 11:54:16 +01:00
|
|
|
|
|
|
|
|
public void Dispose() => _fuse?.Dispose();
|
|
|
|
|
|
|
|
|
|
public event EventHandler<System.EventArgs> Umounted;
|
|
|
|
|
|
2020-09-02 16:53:21 +01:00
|
|
|
public void MountTo(string mountPoint)
|
2020-09-01 11:54:16 +01:00
|
|
|
{
|
|
|
|
|
if(Fuse.IsAvailable)
|
|
|
|
|
{
|
2020-09-03 00:14:11 +01:00
|
|
|
_fuse = new Fuse(this)
|
2020-09-01 11:54:16 +01:00
|
|
|
{
|
2020-09-02 16:53:21 +01:00
|
|
|
MountPoint = mountPoint
|
2020-09-01 11:54:16 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
_fuse.Start();
|
|
|
|
|
|
|
|
|
|
Umounted?.Invoke(this, System.EventArgs.Empty);
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-09-02 16:53:21 +01:00
|
|
|
else if(Winfsp.IsAvailable)
|
|
|
|
|
{
|
2020-09-03 00:14:11 +01:00
|
|
|
_winfsp = new Winfsp(this);
|
2020-09-02 16:53:21 +01:00
|
|
|
bool ret = _winfsp.Mount(mountPoint);
|
|
|
|
|
|
|
|
|
|
if(ret)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
_winfsp = null;
|
|
|
|
|
Umounted?.Invoke(this, System.EventArgs.Empty);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
Umounted?.Invoke(this, System.EventArgs.Empty);
|
2020-09-01 11:54:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Umount()
|
|
|
|
|
{
|
|
|
|
|
_fuse?.Umount();
|
|
|
|
|
_fuse = null;
|
2020-09-02 16:53:21 +01:00
|
|
|
_winfsp?.Umount();
|
|
|
|
|
_winfsp = null;
|
|
|
|
|
|
|
|
|
|
Umounted?.Invoke(this, System.EventArgs.Empty);
|
2020-09-01 11:54:16 +01:00
|
|
|
}
|
2020-09-03 00:14:11 +01:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2020-09-03 00:39:05 +01:00
|
|
|
|
|
|
|
|
internal string[] SplitPath(string path) =>
|
|
|
|
|
path.Split(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "\\" : "/",
|
|
|
|
|
StringSplitOptions.RemoveEmptyEntries);
|
2020-08-30 14:22:41 +01:00
|
|
|
}
|
|
|
|
|
}
|