Skip to content

Commit

Permalink
Merge pull request #10 from b3b00/feature/chroot
Browse files Browse the repository at this point in the history
chroot and many fixes on memory and resources FS
  • Loading branch information
b3b00 authored May 16, 2023
2 parents ddf54ff + c0b3a1b commit 0ba25f8
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 24 deletions.
29 changes: 29 additions & 0 deletions SharpCoreFileSystem/FileSystemPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,5 +268,34 @@ public override int GetHashCode()
{
return !(pathA == pathB);
}

public bool IsRootedBy(FileSystemPath path)
{
if (path.IsRoot)
{
return true;
}
var rootSegments = path.GetDirectorySegments();
var segments = GetDirectorySegments();
if (rootSegments.Length <= segments.Length)
{
int i = 0;
while (i < rootSegments.Length)
{
var rootSegment = rootSegments[i];
var segment = segments[i];
if (rootSegment != segment)
{
return false;
}
i++;
}

return true;

}

return false;
}
}
}
37 changes: 27 additions & 10 deletions SharpCoreFileSystem/FileSystems/AbstractFileSystem.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand All @@ -15,23 +16,39 @@ public abstract class AbstractFileSystem : IFileSystem
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 virtual void ChRoot(FileSystemPath newRoot)
{
Root = newRoot;
}

public ICollection<FileSystemPath> GetDirectories(FileSystemPath path)
public virtual ICollection<FileSystemPath> GetFiles(FileSystemPath path)
{
if (!path.IsDirectory)
{
throw new InvalidOperationException("Path must be a directory.");
}
var entities = GetEntities(path);
var files = entities.Where(x => x.IsFile && x.ParentPath == path.Path).ToList();
return files;
}


public virtual ICollection<FileSystemPath> GetDirectories(FileSystemPath path)
{
var directories = GetEntities(path).Where(x => x.IsDirectory).ToList();
return directories;
var directories = new List<FileSystemPath>();
var entities = GetEntities(path);
foreach (var entity in entities)
{

var parents = entity.ParentPath.GetDirectorySegments();
if (parents.Any() || path != FileSystemPath.DirectorySeparator.ToString())
{
directories.Add(FileSystemPath.DirectorySeparator+parents.First());
}
}

return directories.Distinct().ToList();
}

public abstract bool IsReadOnly { get; }
Expand Down
22 changes: 13 additions & 9 deletions SharpCoreFileSystem/FileSystems/EmbeddedResourceFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ public EmbeddedResourceFileSystem(Assembly assembly)

public override ICollection<FileSystemPath> GetEntities(FileSystemPath path)
{
if (!path.IsRoot)
throw new DirectoryNotFoundException();
var entities = new List<FileSystemPath>();
var resources = Assembly.GetManifestResourceNames();

Expand All @@ -47,19 +45,25 @@ public override ICollection<FileSystemPath> GetEntities(FileSystemPath path)
string entityPath = "";
if (resourcePath.Any())
{
entityPath = "/" + string.Join("/", resourcePath) + "/" + filename;
entityPath = FileSystemPath.DirectorySeparator + string.Join(FileSystemPath.DirectorySeparator.ToString(), resourcePath) + FileSystemPath.DirectorySeparator + filename;
}
else
{
entityPath = "/" + filename;
entityPath = FileSystemPath.DirectorySeparator + filename;
}

if (!Root.IsRoot && entityPath.StartsWith(Root))

var rootedRoot = path.IsRoot ? Root : Root.AppendPath(path);
if (!rootedRoot.Path.EndsWith(FileSystemPath.DirectorySeparator.ToString()))
{
rootedRoot += FileSystemPath.DirectorySeparator;
}
if (!Root.IsRoot && entityPath.StartsWith(rootedRoot))
{
entityPath = entityPath.Replace(Root.Path, "");
if (!entityPath.StartsWith("/"))
if (!entityPath.StartsWith(FileSystemPath.DirectorySeparator.ToString()))
{
entityPath = "/" + entityPath;
entityPath = FileSystemPath.DirectorySeparator + entityPath;
}
entities.Add(entityPath);
}
Expand All @@ -75,12 +79,12 @@ public override ICollection<FileSystemPath> GetEntities(FileSystemPath path)

private string GetResourceName(FileSystemPath path)
{
var root = Root.IsRoot ? "" : Root.PathWithoutLeadingSlash.Replace("/", ".") ?? "";
var root = Root.IsRoot ? "" : Root.PathWithoutLeadingSlash.Replace(FileSystemPath.DirectorySeparator.ToString(), ".") ?? "";
if (!string.IsNullOrEmpty(root) && !root.EndsWith("."))
{
root += ".";
}
return $"{AssemblyName}.{root}{path.Path.Substring(1).Replace("/",".")}";
return $"{AssemblyName}.{root}{path.Path.Substring(1).Replace(FileSystemPath.DirectorySeparator.ToString(),".")}";
}

public override bool Exists(FileSystemPath path)
Expand Down
9 changes: 5 additions & 4 deletions SharpCoreFileSystem/FileSystems/MemoryFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ public override ICollection<FileSystemPath> GetEntities(FileSystemPath path)
{
if (!path.IsDirectory)
throw new ArgumentException("The specified path is no directory.", "path");
ISet<FileSystemPath> subentities;
if (!_directories.TryGetValue(path, out subentities))
throw new DirectoryNotFoundException();
return subentities;
ISet<MemoryFile> subentities;

var files = _files.Where(x => x.Key.IsRootedBy(path)).Select(x => x.Key).ToList();

return files;
}

public override bool Exists(FileSystemPath path)
Expand Down
13 changes: 13 additions & 0 deletions SharpCoreFileSystem/FileSystems/MergedFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,18 @@ public override void ChRoot(FileSystemPath root)
fileSystem.ChRoot(root);
}
}

public virtual ICollection<FileSystemPath> GetFiles(FileSystemPath path)
{
var files = FileSystems.SelectMany(x => x.GetFiles(path)).ToList();
return files.Distinct().ToList();
}


public virtual ICollection<FileSystemPath> GetDirectories(FileSystemPath path)
{
var directories = FileSystems.SelectMany(x => x.GetDirectories(path)).ToList();
return directories.Distinct().ToList();
}
}
}
13 changes: 13 additions & 0 deletions SharpFileSystem.Tests/FileSystemPathTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,5 +314,18 @@ public void AppendDirectoryTest()
EAssert.Throws<InvalidOperationException>(() => fileA.AppendDirectory("dir"));
EAssert.Throws<ArgumentException>(() => root.AppendDirectory("dir/dir"));
}

[Theory]
[InlineData("/root/subRoot/file.txt","/root/subRoot/",true)]
[InlineData("/root/subRoot/subsubroot/file.txt","/root/subRoot/",true)]
[InlineData("/root/subRoot2/file.txt","/root/subRoot/",false)]
[InlineData("/root/subRoot/file.txt","/root/subRoot/",true)]
[InlineData("/root/subRoot/file.txt","/root/subroot/subsubroot/",false)]
[InlineData("/root/subRoot/","/racine/",false)]

public void testIsRooted(string path, string root, bool isRooted)
{
Assert.Equal(isRooted,((FileSystemPath)path).IsRootedBy(root));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ void chrootedeResFSTest()
Assert.Equal(3,entities.Count);

Assert.True(eRscFS.Exists("/deep/deep.txt"));

var directories = eRscFS.GetDirectories(FileSystemPath.Root).ToList();
Assert.Equal(1,directories.Count);
Assert.Equal("deep",directories[0].EntityName);

var files = eRscFS.GetFiles(FileSystemPath.Root).ToList();
Assert.Equal(1,files.Count);
Assert.Equal("deepFile.txt",files[0].EntityName);

files = eRscFS.GetFiles("/deep/").ToList();
Assert.Equal(1,files.Count);
Assert.Equal("deep.txt",files[0].EntityName);
}

[Fact]
Expand Down
29 changes: 28 additions & 1 deletion SharpFileSystem.Tests/FileSystems/MergeFSTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NFluent;
using NUnit.Framework;
using SharpFileSystem.FileSystems;
using Xunit;
using Assert = Xunit.Assert;

namespace SharpFileSystem.Tests.FileSystems
{
Expand Down Expand Up @@ -43,7 +48,29 @@ public void TestMerge()
}
Assert.True(merge.Exists("/resDir/memoryFileThatMatchAnEmbeddedPath.txt"));
content = merge.ReadAllText("/resDir/memoryFileThatMatchAnEmbeddedPath.txt");
Assert.Equal("hello embed !",content);
Check.That(content).IsEqualTo("hello embed !");


var files = merge.GetFiles("/resDir/");
var expectedFiles = new List<FileSystemPath>()
{ "/resDir/deepFile.txt", "/resDir/memoryFileThatMatchAnEmbeddedPath.txt" };
Check.That(files).IsEquivalentTo(expectedFiles);


var directories = merge.GetDirectories("/");
var expectedDirectories = new List<FileSystemPath>() { "/memory", "/resDir" };
Check.That(directories).IsEquivalentTo(expectedDirectories);


embedFS.ChRoot("/resDir");

files = merge.GetFiles("/");
expectedFiles = new List<FileSystemPath>() { "/deepFile.txt" };
Check.That(files).IsEquivalentTo(expectedFiles);

directories = merge.GetDirectories("/");
expectedDirectories = new List<FileSystemPath>() { "/deep","/memory","/resDir" };
Check.That(directories).IsEquivalentTo(expectedDirectories);
}
}
}
1 change: 1 addition & 0 deletions SharpFileSystem.Tests/SharpFileSystem.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
</PackageReference>
<PackageReference Include="AssertNet.Xunit" Version="1.3.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="NFluent" Version="3.0.0.351" />
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="System.Memory" Version="4.5.3" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
Expand Down

0 comments on commit 0ba25f8

Please sign in to comment.