Add virtual filesystem for FUSE supported platforms.

This commit is contained in:
2020-08-26 01:46:02 +01:00
parent 09ca223f02
commit ac74986cbc
6 changed files with 1376 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,205 @@
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : ForcedSeekStream.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Filters.
//
// --[ Description ] ----------------------------------------------------------
//
// Provides a seekable stream from a forward-readable stream.
//
// --[ License ] --------------------------------------------------------------
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2020 Natalia Portillo
// ****************************************************************************/
using System;
using System.IO;
namespace RomRepoMgr.Core
{
/// <inheritdoc />
/// <summary>
/// ForcedSeekStream allows to seek a forward-readable stream (like System.IO.Compression streams) by doing the
/// slow and known trick of rewinding and forward reading until arriving the desired position.
/// </summary>
internal sealed class ForcedSeekStream<T> : Stream where T : Stream
{
const int BUFFER_LEN = 1048576;
readonly string _backFile;
readonly FileStream _backStream;
readonly T _baseStream;
long _streamLength;
/// <inheritdoc />
/// <summary>Initializes a new instance of the <see cref="T:RomRepoMgr.Core.ForcedSeekStream`1" /> class.</summary>
/// <param name="length">The real (uncompressed) length of the stream.</param>
/// <param name="args">Parameters that are used to create the base stream.</param>
public ForcedSeekStream(long length, params object[] args)
{
_streamLength = length;
_baseStream = (T)Activator.CreateInstance(typeof(T), args);
_backFile = Path.GetTempFileName();
_backStream = new FileStream(_backFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
if(length == 0)
CalculateLength();
}
public override bool CanRead => _baseStream.CanRead;
public override bool CanSeek => true;
public override bool CanWrite => false;
public override long Length => _streamLength;
public override long Position
{
get => _backStream.Position;
set => SetPosition(value);
}
/// <summary>
/// Calculates the real (uncompressed) length of the stream. It basically reads (decompresses) the whole stream to
/// memory discarding its contents, so it should be used as a last resort.
/// </summary>
/// <returns>The length.</returns>
void CalculateLength()
{
int read;
do
{
byte[] buffer = new byte[BUFFER_LEN];
read = _baseStream.Read(buffer, 0, BUFFER_LEN);
_backStream.Write(buffer, 0, read);
} while(read == BUFFER_LEN);
_streamLength = _backStream.Length;
_backStream.Position = 0;
}
void SetPosition(long position)
{
if(position == _backStream.Position)
return;
if(position < _backStream.Length)
{
_backStream.Position = position;
return;
}
_backStream.Position = _backStream.Length;
long toPosition = position - _backStream.Position;
int fullBufferReads = (int)(toPosition / BUFFER_LEN);
int restToRead = (int)(toPosition % BUFFER_LEN);
byte[] buffer;
for(int i = 0; i < fullBufferReads; i++)
{
buffer = new byte[BUFFER_LEN];
_baseStream.Read(buffer, 0, BUFFER_LEN);
_backStream.Write(buffer, 0, BUFFER_LEN);
}
buffer = new byte[restToRead];
_baseStream.Read(buffer, 0, restToRead);
_backStream.Write(buffer, 0, restToRead);
}
public override void Flush()
{
_baseStream.Flush();
_backStream.Flush();
}
public override int Read(byte[] buffer, int offset, int count)
{
if(_backStream.Position + count <= _backStream.Length)
return _backStream.Read(buffer, offset, count);
SetPosition(_backStream.Position + count);
SetPosition(_backStream.Position - count);
return _backStream.Read(buffer, offset, count);
}
public override int ReadByte()
{
if(_backStream.Position + 1 <= _backStream.Length)
return _backStream.ReadByte();
SetPosition(_backStream.Position + 1);
SetPosition(_backStream.Position - 1);
return _backStream.ReadByte();
}
public override long Seek(long offset, SeekOrigin origin)
{
switch(origin)
{
case SeekOrigin.Begin:
if(offset < 0)
throw new IOException("Cannot seek before stream start.");
SetPosition(offset);
break;
case SeekOrigin.End:
if(offset > 0)
throw new IOException("Cannot seek after stream end.");
if(_streamLength == 0)
CalculateLength();
SetPosition(_streamLength + offset);
break;
default:
SetPosition(_backStream.Position + offset);
break;
}
return _backStream.Position;
}
public override void SetLength(long value) => throw new NotSupportedException();
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
public override void Close()
{
_backStream?.Close();
File.Delete(_backFile);
}
~ForcedSeekStream()
{
_backStream?.Close();
File.Delete(_backFile);
}
}
}

View File

@@ -6,6 +6,7 @@
<ItemGroup>
<PackageReference Include="DotNetZip" Version="1.13.8" />
<PackageReference Include="Mono.Fuse.NETStandard" Version="1.0.0" />
<PackageReference Include="SharpCompress" Version="0.26.0" />
<PackageReference Include="System.Security.Principal.Windows" Version="4.7.0" />
</ItemGroup>

View File

@@ -0,0 +1,4 @@
<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/=romrepombgrfs/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=xattr/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=xattrs/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

View File

@@ -35,6 +35,7 @@ using MessageBox.Avalonia;
using MessageBox.Avalonia.Enums;
using ReactiveUI;
using RomRepoMgr.Core.EventArgs;
using RomRepoMgr.Core.Filesystem;
using RomRepoMgr.Core.Models;
using RomRepoMgr.Views;
@@ -59,6 +60,7 @@ namespace RomRepoMgr.ViewModels
EditRomSetCommand = ReactiveCommand.Create(ExecuteEditRomSetCommand);
ExportDatCommand = ReactiveCommand.Create(ExecuteExportDatCommand);
ExportRomsCommand = ReactiveCommand.Create(ExecuteExportRomsCommand);
MountCommand = ReactiveCommand.Create(ExecuteMountCommand);
RomSets = new ObservableCollection<RomSetModel>(romSets);
}
@@ -91,6 +93,7 @@ namespace RomRepoMgr.ViewModels
public ReactiveCommand<Unit, Unit> EditRomSetCommand { get; }
public ReactiveCommand<Unit, Unit> ExportDatCommand { get; }
public ReactiveCommand<Unit, Unit> ExportRomsCommand { get; }
public ReactiveCommand<Unit, Unit> MountCommand { get; }
public RomSetModel SelectedRomSet
{
@@ -282,5 +285,28 @@ namespace RomRepoMgr.ViewModels
dialog.DataContext = viewModel;
await dialog.ShowDialog(_view);
}
async void ExecuteMountCommand()
{
// TODO: Detect if Windows or *NIX
// TODO: Detect if libraries are available
var dlgOpen = new OpenFolderDialog
{
Title = "Select mount point..."
};
string result = await dlgOpen.ShowAsync(_view);
if(result == null)
return;
var fs = new Fuse
{
MountPoint = result
};
fs.Start();
}
}
}

View File

@@ -16,6 +16,9 @@
<Separator />
<MenuItem Header="E_xit" IsVisible="{Binding !NativeMenuSupported}" Command="{Binding ExitCommand}" />
</MenuItem>
<MenuItem Header="File_system">
<MenuItem Header="_Mount" Command="{Binding MountCommand}" /> <Separator />
</MenuItem>
<MenuItem Header="_ROMs">
<MenuItem Header="_Import folder" Command="{Binding ImportRomFolderCommand}" /> <Separator />
</MenuItem>