Skip to content

Commit

Permalink
1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
olduh29 committed May 15, 2023
1 parent a8c3fbe commit 98a35ee
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 64 deletions.
32 changes: 32 additions & 0 deletions SharpCoreFileSystem/FileSystems/AbstractFileSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace SharpFileSystem.FileSystems
{
public abstract class AbstractFileSystem : IFileSystem
{
public abstract ICollection<FileSystemPath> GetEntities(FileSystemPath path);
public abstract bool Exists(FileSystemPath path);
public abstract Stream CreateFile(FileSystemPath path);
public abstract Stream OpenFile(FileSystemPath path, FileAccess access);
public abstract void CreateDirectory(FileSystemPath path);
public abstract void Delete(FileSystemPath path);

public ICollection<FileSystemPath> GetFiles(FileSystemPath path)
{
var files = GetEntities(path).Where(x => x.IsFile).ToList();
return files;
}

public ICollection<FileSystemPath> GetDirectories(FileSystemPath path)
{
var directories = GetEntities(path).Where(x => x.IsDirectory).ToList();
return directories;
}

public abstract bool IsReadOnly { get; }

public abstract void Dispose();
}
}
21 changes: 12 additions & 9 deletions SharpCoreFileSystem/FileSystems/EmbeddedResourceFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,20 @@ public static string GetShortName(this Assembly assembly)
return assembly.FullName.Split(new[] {','}).First();
}
}
public class EmbeddedResourceFileSystem : IFileSystem
public class EmbeddedResourceFileSystem : AbstractFileSystem
{
public Assembly Assembly { get; private set; }

public bool IsReadOnly => true;

public override bool IsReadOnly => true;

private string AssemblyName => Assembly.GetShortName();
public EmbeddedResourceFileSystem(Assembly assembly)
{
Assembly = assembly;
}

public ICollection<FileSystemPath> GetEntities(FileSystemPath path)
public override ICollection<FileSystemPath> GetEntities(FileSystemPath path)
{
if (!path.IsRoot)
throw new DirectoryNotFoundException();
Expand All @@ -47,12 +48,12 @@ public ICollection<FileSystemPath> GetEntities(FileSystemPath path)
//return Assembly.GetManifestResourceNames().Select(name => FileSystemPath.Root.AppendFile(name.Replace(AssemblyName+".",""))).ToArray();
}

public bool Exists(FileSystemPath path)
public override bool Exists(FileSystemPath path)
{
return path.IsRoot || !path.IsDirectory && Assembly.GetManifestResourceNames().Contains($"{AssemblyName}.{path.Path.Substring(1).Replace("/",".")}");
}

public Stream OpenFile(FileSystemPath path, FileAccess access)
public override Stream OpenFile(FileSystemPath path, FileAccess access)
{
if (access == FileAccess.Write)
throw new NotSupportedException();
Expand All @@ -61,22 +62,24 @@ public Stream OpenFile(FileSystemPath path, FileAccess access)
return Assembly.GetManifestResourceStream($"{AssemblyName}.{path.Path.Substring(1).Replace("/",".")}");
}

public Stream CreateFile(FileSystemPath path)
public override Stream CreateFile(FileSystemPath path)
{
throw new NotSupportedException();
}

public void CreateDirectory(FileSystemPath path)
public override void CreateDirectory(FileSystemPath path)
{
throw new NotSupportedException();
}

public void Delete(FileSystemPath path)
public override void Delete(FileSystemPath path)
{
throw new NotSupportedException();
}

public void Dispose()


public override void Dispose()
{
}
}
Expand Down
18 changes: 9 additions & 9 deletions SharpCoreFileSystem/FileSystems/FileSystemMounter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ public MountPoint(FileSystemPath path, IFileSystem fs)
}
}

public class FileSystemMounter : IFileSystem
public class FileSystemMounter : AbstractFileSystem
{

public bool IsReadOnly => Mounts.All(x => x.Value.IsReadOnly);
public override bool IsReadOnly => Mounts.All(x => x.Value.IsReadOnly);

public ICollection<KeyValuePair<FileSystemPath, IFileSystem>> Mounts { get; private set; }

Expand Down Expand Up @@ -50,44 +50,44 @@ public FileSystemMounter(params KeyValuePair<FileSystemPath, IFileSystem>[] moun
return Mounts.First(pair => pair.Key == path || pair.Key.IsParentOf(path));
}

public void Dispose()
public override void Dispose()
{
foreach (var mount in Mounts.Select(p => p.Value))
mount.Dispose();
}

public ICollection<FileSystemPath> GetEntities(FileSystemPath path)
public override ICollection<FileSystemPath> GetEntities(FileSystemPath path)
{
var pair = Get(path);
var entities = pair.Value.GetEntities(path.IsRoot ? path : path.RemoveParent(pair.Key));
return new EnumerableCollection<FileSystemPath>(entities.Select(p => pair.Key.AppendPath(p)), entities.Count);
}

public bool Exists(FileSystemPath path)
public override bool Exists(FileSystemPath path)
{
var pair = Get(path);
return pair.Value.Exists(path.RemoveParent(pair.Key));
}

public Stream CreateFile(FileSystemPath path)
public override Stream CreateFile(FileSystemPath path)
{
var pair = Get(path);
return pair.Value.CreateFile(path.RemoveParent(pair.Key));
}

public Stream OpenFile(FileSystemPath path, FileAccess access)
public override Stream OpenFile(FileSystemPath path, FileAccess access)
{
var pair = Get(path);
return pair.Value.OpenFile(path.RemoveParent(pair.Key), access);
}

public void CreateDirectory(FileSystemPath path)
public override void CreateDirectory(FileSystemPath path)
{
var pair = Get(path);
pair.Value.CreateDirectory(path.RemoveParent(pair.Key));
}

public void Delete(FileSystemPath path)
public override void Delete(FileSystemPath path)
{
var pair = Get(path);
pair.Value.Delete(path.RemoveParent(pair.Key));
Expand Down
18 changes: 9 additions & 9 deletions SharpCoreFileSystem/FileSystems/MemoryFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

namespace SharpFileSystem.FileSystems
{
public class MemoryFileSystem : IFileSystem
public class MemoryFileSystem : AbstractFileSystem
{
public bool IsReadOnly => false;
public override bool IsReadOnly => false;

private IDictionary<FileSystemPath, ISet<FileSystemPath>> _directories =
new Dictionary<FileSystemPath, ISet<FileSystemPath>>();
Expand All @@ -19,7 +19,7 @@ public MemoryFileSystem()
_directories.Add(FileSystemPath.Root, new HashSet<FileSystemPath>());
}

public ICollection<FileSystemPath> GetEntities(FileSystemPath path)
public override ICollection<FileSystemPath> GetEntities(FileSystemPath path)
{
if (!path.IsDirectory)
throw new ArgumentException("The specified path is no directory.", "path");
Expand All @@ -29,12 +29,12 @@ public ICollection<FileSystemPath> GetEntities(FileSystemPath path)
return subentities;
}

public bool Exists(FileSystemPath path)
public override bool Exists(FileSystemPath path)
{
return path.IsDirectory ? _directories.ContainsKey(path) : _files.ContainsKey(path);
}

public Stream CreateFile(FileSystemPath path)
public override Stream CreateFile(FileSystemPath path)
{
if (!path.IsFile)
throw new ArgumentException("The specified path is no file.", "path");
Expand All @@ -44,7 +44,7 @@ public Stream CreateFile(FileSystemPath path)
return new MemoryFileStream(_files[path] = new MemoryFile());
}

public Stream OpenFile(FileSystemPath path, FileAccess access)
public override Stream OpenFile(FileSystemPath path, FileAccess access)
{
if (!path.IsFile)
throw new ArgumentException("The specified path is no file.", "path");
Expand All @@ -54,7 +54,7 @@ public Stream OpenFile(FileSystemPath path, FileAccess access)
return new MemoryFileStream(file);
}

public void CreateDirectory(FileSystemPath path)
public override void CreateDirectory(FileSystemPath path)
{
if (!path.IsDirectory)
throw new ArgumentException("The specified path is no directory.", "path");
Expand All @@ -67,7 +67,7 @@ public void CreateDirectory(FileSystemPath path)
_directories[path] = new HashSet<FileSystemPath>();
}

public void Delete(FileSystemPath path)
public override void Delete(FileSystemPath path)
{
if (path.IsRoot)
throw new ArgumentException("The root cannot be deleted.");
Expand All @@ -82,7 +82,7 @@ public void Delete(FileSystemPath path)
parent.Remove(path);
}

public void Dispose()
public override void Dispose()
{
}

Expand Down
18 changes: 9 additions & 9 deletions SharpCoreFileSystem/FileSystems/MergedFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

namespace SharpFileSystem.FileSystems
{
public class MergedFileSystem: IFileSystem
public class MergedFileSystem: AbstractFileSystem
{
public bool IsReadOnly => FileSystems.All(x => x.IsReadOnly);
public override bool IsReadOnly => FileSystems.All(x => x.IsReadOnly);

public IEnumerable<IFileSystem> FileSystems { get; private set; }
public MergedFileSystem(IEnumerable<IFileSystem> fileSystems)
Expand All @@ -21,13 +21,13 @@ public MergedFileSystem(params IFileSystem[] fileSystems)
FileSystems = fileSystems.ToArray();
}

public void Dispose()
public override void Dispose()
{
foreach(var fs in FileSystems)
fs.Dispose();
}

public ICollection<FileSystemPath> GetEntities(FileSystemPath path)
public override ICollection<FileSystemPath> GetEntities(FileSystemPath path)
{
var entities = new SortedList<FileSystemPath, FileSystemPath>();
foreach (var fs in FileSystems.Where(fs => fs.Exists(path)))
Expand All @@ -39,7 +39,7 @@ public ICollection<FileSystemPath> GetEntities(FileSystemPath path)
return entities.Values;
}

public bool Exists(FileSystemPath path)
public override bool Exists(FileSystemPath path)
{
return FileSystems.Any(fs => fs.Exists(path));
}
Expand All @@ -54,21 +54,21 @@ public IFileSystem GetFirstRW(FileSystemPath path)
return FileSystems.FirstOrDefault(fs => !fs.IsReadOnly && fs.Exists(path));
}

public Stream CreateFile(FileSystemPath path)
public override Stream CreateFile(FileSystemPath path)
{
var fs = GetFirstRW(path) ?? FileSystems.First();
return fs.CreateFile(path);
}

public Stream OpenFile(FileSystemPath path, FileAccess access)
public override Stream OpenFile(FileSystemPath path, FileAccess access)
{
var fs = GetFirst(path);
if (fs == null)
throw new FileNotFoundException();
return fs.OpenFile(path, access);
}

public void CreateDirectory(FileSystemPath path)
public override void CreateDirectory(FileSystemPath path)
{
if (Exists(path))
throw new ArgumentException("The specified directory already exists.");
Expand All @@ -78,7 +78,7 @@ public void CreateDirectory(FileSystemPath path)
fs.CreateDirectory(path);
}

public void Delete(FileSystemPath path)
public override void Delete(FileSystemPath path)
{
foreach(var fs in FileSystems.Where(fs => fs.Exists(path)))
fs.Delete(path);
Expand Down
18 changes: 9 additions & 9 deletions SharpCoreFileSystem/FileSystems/NetZipArchiveFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

namespace SharpFileSystem.FileSystems
{
public class NetZipArchiveFileSystem : IFileSystem
public class NetZipArchiveFileSystem : AbstractFileSystem
{
public ZipArchive ZipArchive { get; private set; }

public bool IsReadOnly => false;
public override bool IsReadOnly => false;

public static NetZipArchiveFileSystem Open(Stream s)
{
Expand All @@ -31,7 +31,7 @@ private NetZipArchiveFileSystem(ZipArchive archive)
{
ZipArchive = archive;
}
public void Dispose()
public override void Dispose()
{
ZipArchive.Dispose();
}
Expand All @@ -54,7 +54,7 @@ protected ZipArchiveEntry ToEntry(FileSystemPath path)
{
return ZipArchive.GetEntry(ToEntryPath(path));
}
public ICollection<FileSystemPath> GetEntities(FileSystemPath path)
public override ICollection<FileSystemPath> GetEntities(FileSystemPath path)
{
return GetZipEntries().Select(ToPath).Where(path.IsParentOf)
.Select(entryPath => entryPath.ParentPath == path
Expand All @@ -65,7 +65,7 @@ public ICollection<FileSystemPath> GetEntities(FileSystemPath path)
.ToList();
}

public bool Exists(FileSystemPath path)
public override bool Exists(FileSystemPath path)
{
if (path.IsFile)
return ToEntry(path) != null;
Expand All @@ -74,7 +74,7 @@ public bool Exists(FileSystemPath path)
.Any(entryPath => entryPath.IsChildOf(path) || entryPath.Equals(path));
}

public Stream CreateFile(FileSystemPath path)
public override Stream CreateFile(FileSystemPath path)
{
if (ZipArchive.Mode == ZipArchiveMode.Read)
throw new InvalidOperationException("This is a read-only filesystem.");
Expand All @@ -83,7 +83,7 @@ public Stream CreateFile(FileSystemPath path)
return zae.Open();
}

public Stream OpenFile(FileSystemPath path, FileAccess access)
public override Stream OpenFile(FileSystemPath path, FileAccess access)
{
if (ZipArchive.Mode == ZipArchiveMode.Read && access != FileAccess.Read)
throw new InvalidOperationException("This is a read-only filesystem.");
Expand All @@ -92,15 +92,15 @@ public Stream OpenFile(FileSystemPath path, FileAccess access)
return zae.Open();
}

public void CreateDirectory(FileSystemPath path)
public override void CreateDirectory(FileSystemPath path)
{
if (ZipArchive.Mode == ZipArchiveMode.Read)
throw new InvalidOperationException("This is a read-only filesystem.");

ZipArchive.CreateEntry(ToEntryPath(path));
}

public void Delete(FileSystemPath path)
public override void Delete(FileSystemPath path)
{
if (ZipArchive.Mode == ZipArchiveMode.Read)
throw new InvalidOperationException("This is a read-only filesystem.");
Expand Down
Loading

0 comments on commit 98a35ee

Please sign in to comment.