Allow to umount VFS.

This commit is contained in:
2020-09-01 11:54:16 +01:00
parent b1fcf40945
commit b1dd940d00
8 changed files with 723 additions and 939 deletions

View File

@@ -27,6 +27,7 @@ namespace RomRepoMgr.Core.Filesystem
readonly ConcurrentDictionary<long, Stream> _streamsCache;
long _lastHandle;
ConcurrentDictionary<string, long> _rootDirectoryCache;
string _umountToken;
public Fuse()
{
@@ -107,7 +108,27 @@ namespace RomRepoMgr.Core.Filesystem
FillRootDirectoryCache();
if(!_rootDirectoryCache.TryGetValue(pieces[0], out long romSetId))
{
if(pieces[0] == ".fuse_umount" &&
_umountToken != null)
{
stat = new Stat
{
st_mode = FilePermissions.S_IFREG | NativeConvert.FromOctalPermissionString("0444"),
st_nlink = 1,
st_ctime = NativeConvert.ToTimeT(DateTime.UtcNow),
st_mtime = NativeConvert.ToTimeT(DateTime.UtcNow),
st_blksize = 0,
st_blocks = 0,
st_ino = 0,
st_size = 0
};
return 0;
}
return Errno.ENOENT;
}
if(!_romSetsCache.TryGetValue(romSetId, out RomSet romSet))
{
@@ -472,8 +493,25 @@ namespace RomRepoMgr.Core.Filesystem
protected override Errno OnSynchronizeHandle(string file, OpenedPathInfo info, bool onlyUserData) =>
Errno.EOPNOTSUPP;
protected override Errno OnSetPathExtendedAttribute(string path, string name, byte[] value, XattrFlags flags) =>
Errno.EROFS;
protected override Errno OnSetPathExtendedAttribute(string path, string name, byte[] value, XattrFlags flags)
{
if(_umountToken == null)
return Errno.EROFS;
if(path != "/.fuse_umount")
return Errno.EROFS;
if(name != _umountToken)
return Errno.EROFS;
if(value?.Length != 0)
return Errno.EROFS;
_umountToken = null;
Stop();
return 0;
}
protected override Errno OnGetPathExtendedAttribute(string path, string name, byte[] value,
out int bytesWritten)
@@ -1155,6 +1193,18 @@ namespace RomRepoMgr.Core.Filesystem
return Errno.EOPNOTSUPP;
}
[DllImport("libc", SetLastError = true)]
static extern int setxattr(string path, string name, IntPtr value, long size, int flags);
public void Umount()
{
var rnd = new Random();
byte[] token = new byte[64];
rnd.NextBytes(token);
_umountToken = Base32.ToBase32String(token);
setxattr(Path.Combine(MountPoint, ".fuse_umount"), _umountToken, IntPtr.Zero, 0, 0);
}
sealed class CachedMachine
{
public ulong Id { get; set; }

View File

@@ -1,7 +1,40 @@
using System;
using System.Threading.Tasks;
namespace RomRepoMgr.Core.Filesystem
{
public class Vfs
public class Vfs : IDisposable
{
Fuse _fuse;
public static bool IsAvailable => Winfsp.IsAvailable || Fuse.IsAvailable;
public void Dispose() => _fuse?.Dispose();
public event EventHandler<System.EventArgs> Umounted;
public void MountTo(string result)
{
if(Fuse.IsAvailable)
{
_fuse = new Fuse
{
MountPoint = result
};
Task.Run(() =>
{
_fuse.Start();
Umounted?.Invoke(this, System.EventArgs.Empty);
});
}
}
public void Umount()
{
_fuse?.Umount();
_fuse = null;
}
}
}

View File

@@ -1,9 +1,13 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=Aaru/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=datfiles/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=dlclose/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=libdl/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=lsar/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=romrepo/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=romrepombgrfs/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=setxattr/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Umounted/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=unar/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Winfsp/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=xattr/@EntryIndexedValue">True</s:Boolean>

File diff suppressed because it is too large Load Diff

View File

@@ -340,4 +340,7 @@
<data name="UnArPathLabel" xml:space="preserve">
<value>Ruta a UnAr</value>
</data>
<data name="FilesystemMenuUmountText" xml:space="preserve">
<value>_Desmontar</value>
</data>
</root>

View File

@@ -346,4 +346,7 @@ Do you want to delete the file?</value>
<data name="ExitButtonText" xml:space="preserve">
<value>Exit</value>
</data>
<data name="FilesystemMenuUmountText" xml:space="preserve">
<value>_Umount</value>
</data>
</root>

View File

@@ -23,8 +23,10 @@
// Copyright © 2020 Natalia Portillo
*******************************************************************************/
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Reactive;
using Avalonia;
@@ -45,8 +47,8 @@ namespace RomRepoMgr.ViewModels
public class MainWindowViewModel : ViewModelBase
{
readonly MainWindow _view;
RomSetModel _selectedRomSet;
RomSetModel _selectedRomSet;
Vfs _vfs;
public MainWindowViewModel(MainWindow view, List<RomSetModel> romSets)
{
@@ -62,6 +64,7 @@ namespace RomRepoMgr.ViewModels
ExportDatCommand = ReactiveCommand.Create(ExecuteExportDatCommand);
ExportRomsCommand = ReactiveCommand.Create(ExecuteExportRomsCommand);
MountCommand = ReactiveCommand.Create(ExecuteMountCommand);
UmountCommand = ReactiveCommand.Create(ExecuteUmountCommand);
RomSets = new ObservableCollection<RomSetModel>(romSets);
}
@@ -96,6 +99,7 @@ namespace RomRepoMgr.ViewModels
public string RomSetsMenuDeleteText => Localization.RomSetsMenuDeleteText;
public string HelpMenuText => Localization.HelpMenuText;
public string HelpMenuAboutText => Localization.HelpMenuAboutText;
public string FilesystemMenuUmountText => Localization.FilesystemMenuUmountText;
public bool NativeMenuSupported =>
NativeMenu.GetIsNativeMenuExported((Application.Current.ApplicationLifetime as
@@ -112,6 +116,13 @@ namespace RomRepoMgr.ViewModels
public ReactiveCommand<Unit, Unit> ExportDatCommand { get; }
public ReactiveCommand<Unit, Unit> ExportRomsCommand { get; }
public ReactiveCommand<Unit, Unit> MountCommand { get; }
public ReactiveCommand<Unit, Unit> UmountCommand { get; }
public Vfs Vfs
{
get => _vfs;
set => this.RaiseAndSetIfChanged(ref _vfs, value);
}
public RomSetModel SelectedRomSet
{
@@ -304,6 +315,9 @@ namespace RomRepoMgr.ViewModels
async void ExecuteMountCommand()
{
if(Vfs != null)
return;
var dlgOpen = new OpenFolderDialog
{
Title = Localization.SelectMountPointDialogTitle
@@ -314,12 +328,23 @@ namespace RomRepoMgr.ViewModels
if(result == null)
return;
var fs = new Fuse
try
{
MountPoint = result
};
Vfs = new Vfs();
Vfs.Umounted += VfsOnUmounted;
Vfs.MountTo(result);
}
catch(Exception)
{
if(Debugger.IsAttached)
throw;
fs.Start();
Vfs = null;
}
}
void VfsOnUmounted(object sender, EventArgs e) => Vfs = null;
void ExecuteUmountCommand() => Vfs?.Umount();
}
}

View File

@@ -20,7 +20,12 @@
Command="{Binding ExitCommand}" />
</MenuItem>
<MenuItem Header="{Binding FilesystemMenuText}" IsEnabled="{Binding IsVfsAvailable}">
<MenuItem Header="{Binding FilesystemMenuMountText}" Command="{Binding MountCommand}" /> <Separator />
<MenuItem Header="{Binding FilesystemMenuMountText}" Command="{Binding MountCommand}"
IsEnabled="{Binding Vfs, Converter={x:Static ObjectConverters.IsNull}}" />
<Separator />
<MenuItem Header="{Binding FilesystemMenuUmountText}" Command="{Binding UmountCommand}"
IsEnabled="{Binding Vfs, Converter={x:Static ObjectConverters.IsNotNull}}" />
<Separator />
</MenuItem>
<MenuItem Header="{Binding RomsMenuText}">
<MenuItem Header="{Binding RomsMenuImportText}" Command="{Binding ImportRomFolderCommand}" />