tesses-vfs-extras/OverlayTest/Program.cs

133 lines
3.3 KiB
C#
Raw Permalink Normal View History

2023-09-10 20:01:54 +00:00
using Tesses.VirtualFilesystem;
using Tesses.VirtualFilesystem.Extensions;
using Tesses.VirtualFilesystem.Filesystems;
using System.Text;
LocalFileSystem fs = new LocalFileSystem();
OverlayFilesystem overlay = new OverlayFilesystem(fs.GetSubdirFilesystem(Special.CurDir / "lower"),fs.GetSubdirFilesystem(Special.CurDir / "upper"));
UnixPath path = Special.Root;
int hasGone=0;
Func<string?> Prompt = ()=>{
Console.Write(path.ToString());
Console.Write(": ");
return Console.ReadLine();
};
Func<string,string[]> ParseArgs = (argStr)=>{
List<string> args = new List<string>();
StringBuilder b= new StringBuilder();
bool isString=false;
for(int i = 0;i<argStr.Length;i++)
{
if(argStr[i] == '\\')
{
i++;
b.Append(argStr[i]);
}
{
if(isString)
{
if(argStr[0] == '\"')
{
isString=false;
}
else
{
b.Append(argStr[i]);
}
}
else
{
if(argStr[i] == '\"')
{
isString=true;
}else
if(argStr[i] == ' ')
{
if(b.Length >0)
{
args.Add(b.ToString());
b.Clear();
}
}else
{
b.Append(argStr[i]);
}
}
}
}
if(b.Length >0)
{
args.Add(b.ToString());
b.Clear();
}
return args.ToArray();
};
var read = Prompt();
while(read != null)
{
string[] _args = ParseArgs(read);
switch(_args[0])
{
case "exit":
Environment.Exit(0);
break;
case "ls":
await foreach(var files in overlay.EnumerateFileSystemEntriesAsync(_args.Length > 1 ? _args[1] : path))
{
Console.WriteLine(files.Name);
}
break;
case "cat":
if(_args.Length > 1)
{
Console.Write(await overlay.ReadAllTextAsync(_args[1]));
}
break;
case "cd":
if(_args.Length > 1)
path = _args[1];
break;
case "mkdir":
if(_args.Length > 1)
{
await overlay.CreateDirectoryAsync(path / _args[1]);
}
break;
case "touch":
if(_args.Length > 1)
{
UnixPath p = path / _args[1];
if(!await overlay.FileExistsAsync(p) && !await overlay.DirectoryExistsAsync(p) && !await overlay.SymlinkExistsAsync(p))
{
await overlay.WriteAllTextAsync(p,"");
}
await overlay.SetLastWriteTimeAsync(p,DateTime.Now);
}
break;
case "append":
{
UnixPath p = path / _args[1];
using(var srcF = await overlay.OpenAsync(p,FileMode.Append,FileAccess.Write,FileShare.Read))
{
using(var sw = new StreamWriter(srcF))
{
sw.WriteLine(_args[2]);
}
}
}
break;
}
read = Prompt();
}