Skip to content

Commit

Permalink
Improved removing: multiply pathes + exclude pathes
Browse files Browse the repository at this point in the history
  • Loading branch information
teplofizik committed Jan 29, 2024
1 parent d898d42 commit 08254d9
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 56 deletions.
4 changes: 2 additions & 2 deletions NyaFs/ImageFormat/Elements/Fs/LinuxFilesystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public class LinuxFilesystem
/// <returns></returns>
public string[] Search(Dir Dir, string Mask)
{
string pattern = "^" + Regex.Escape(Mask).Replace("\\*", ".*");
string pattern = "^" + Regex.Escape(Mask).Replace("\\*", ".*") + "$";

return Search(Dir, I => Regex.IsMatch(I.Filename, pattern));
}

Expand Down
163 changes: 124 additions & 39 deletions NyaFs/Processor/Scripting/Commands/Fs/Rm.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using NyaFs.Processor.Scripting.Configs;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;

Expand All @@ -13,85 +15,168 @@ public Rm() : base("rm")
{
new Params.FsPathScriptArgsParam()
}));
AddConfig(new AnyConfig(1));
}

public override ScriptStep Get(ScriptArgs Args)
{
return new RmScriptStep(Args.RawArgs[0]);
if(Args.ArgConfig == 0)
return new RmScriptStep(Args.RawArgs[0]);
else
return new RmScriptStep(Args.RawArgs);
}

public class RmScriptStep : ScriptStep
{
string Path;
List<string> Pathes = new List<string>();
List<string> Excluded = new List<string>();
List<string> ExcludeMask = new List<string>();
int Total = 0;

public RmScriptStep(string Path) : base("rm")
public RmScriptStep(string path) : base("rm")
{
this.Path = Path;
Pathes.Add(path);
}

public override ScriptStepResult Exec(ImageProcessor Processor)
public RmScriptStep(string[] args) : base("rm")
{
var Fs = Processor.GetFs();
// Проверим наличие загруженной файловой системы
if (Fs == null)
return new ScriptStepResult(ScriptStepStatus.Error, "Filesystem is not loaded");
foreach(var A in args)
{
if (A.StartsWith("-"))
{
// Добавляем в исключения
ExcludeMask.Add(A.Substring(1));
}
else
Pathes.Add(A);
}
}

private bool IsExcluded(string path) => Excluded.Contains(path);

if(Path.Contains('*'))
private void GenerateExcludedFileList(ImageProcessor processor, ImageFormat.Elements.Fs.LinuxFilesystem fs)
{
foreach (var path in ExcludeMask)
{
if (Path.StartsWith("/"))
if (path.Contains('*'))
{
// Это путь с маской
var Items = Fs.Search(Path.Substring(1));
if (path.StartsWith("/"))
{
var Items = fs.Search(path.Substring(1));

if(Items.Length > 0)
Excluded.AddRange(Items);
}
else
{
foreach (var I in Items)
var Dir = fs.GetDirectory(processor.ActivePath);
if (Dir != null)
{
Log.Write(2, $"Deleted {I}!");
Fs.Delete(I);
var Items = fs.Search(Dir, path);

Excluded.AddRange(Items);
}
}
}
else
{
if (path.StartsWith("/"))
{
if (fs.Exists(path))
Excluded.Add(path.Substring(1));
}
else
{
var Dir = fs.GetDirectory(processor.ActivePath);
if (Dir != null)
{
var Items = fs.Search(Dir, "*" + path);

return new ScriptStepResult(ScriptStepStatus.Ok, $"{Path} deleted {Items.Length} items!");
Excluded.AddRange(Items);
}
}
}
}
}

private void Delete(ImageFormat.Elements.Fs.LinuxFilesystem fs, string path)
{
if (fs.Exists(path))
{
if (!IsExcluded(path))
{
fs.Delete(path);
Log.Write(2, $"Deleted {path}");
Total++;
}
else
Log.Warning(2, $"Item is not removed: {path}");
}
}

private void ProcessPath(ImageProcessor processor, ImageFormat.Elements.Fs.LinuxFilesystem fs, string path)
{
if (path.Contains('*'))
{
if (path.StartsWith("/"))
{
// Это путь с маской
var Items = fs.Search(path.Substring(1));

if (Items.Length > 0)
{
foreach (var I in Items)
Delete(fs, I);
}
else
return new ScriptStepResult(ScriptStepStatus.Warning, $"{Path} not found!");
Log.Warning(0, $"{path} not found!");
}
else
{
var Dir = Fs.GetDirectory(Processor.ActivePath);
if(Dir != null)
var Dir = fs.GetDirectory(processor.ActivePath);
if (Dir != null)
{
var Items = Fs.Search(Dir, Path);
var Items = fs.Search(Dir, path);
if (Items.Length > 0)
{
foreach (var I in Items)
{
Log.Write(2, $"Deleted {I}!");
Fs.Delete(I);
}

return new ScriptStepResult(ScriptStepStatus.Ok, $"{Path} deleted {Items.Length} items!");
foreach (var I in Items)
Delete(fs, I);
}
else
return new ScriptStepResult(ScriptStepStatus.Warning, $"{Path} not found!");
Log.Warning(0, $"{path} not found!");
}
else
return new ScriptStepResult(ScriptStepStatus.Warning, $"{Processor.ActivePath} not found!");
Log.Warning(0, $"{processor.ActivePath} not found!");
}
}
else
{
if (Fs.Exists(Path))
{
if (fs.Exists(path))
{
// Есть старый файл в файловой системе. Удалим.
Fs.Delete(Path);

return new ScriptStepResult(ScriptStepStatus.Ok, $"{Path} deleted!");
Delete(fs, path);
}
else
return new ScriptStepResult(ScriptStepStatus.Warning, $"{Path} not found!");
Log.Warning(0, $"{path} not found!");
}
}

public override ScriptStepResult Exec(ImageProcessor Processor)
{
var Fs = Processor.GetFs();
// Проверим наличие загруженной файловой системы
if (Fs == null)
return new ScriptStepResult(ScriptStepStatus.Error, "Filesystem is not loaded");

if(ExcludeMask.Count > 0)
GenerateExcludedFileList(Processor, Fs);

foreach (var P in Pathes)
ProcessPath(Processor, Fs, P);

if(Total > 0)
return new ScriptStepResult(ScriptStepStatus.Ok, $"Deleted {Total} items!");
else
return new ScriptStepResult(ScriptStepStatus.Warning, $"Items to delete are not found!");
}
}
}
}
36 changes: 36 additions & 0 deletions NyaFs/Processor/Scripting/Configs/AnyConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace NyaFs.Processor.Scripting.Configs
{
class ErrorConfig : ScriptArgsConfig
{
private string Message = "";

public ErrorConfig() : base(-1, null)
{

}

public ErrorConfig(string Message) : base(-1, null)
{
this.Message = Message;
}

public override bool IsMyConfig(string[] Args) => true;

public override bool CheckArgs(string[] Args)
{
var M = Message + "";

for(int i = 1; i < Args.Length; i++)
{
M = M.Replace($"%{i}%", Args[i]);
}

Log.Error(0, M);
return false;
}
}
}
23 changes: 10 additions & 13 deletions NyaFs/Processor/Scripting/Configs/ErrorConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,30 @@

namespace NyaFs.Processor.Scripting.Configs
{
class ErrorConfig : ScriptArgsConfig
class AnyConfig : ScriptArgsConfig
{
private string Message = "";
Func<string, bool> ArgChecker;

public ErrorConfig() : base(-1, null)
public AnyConfig(int Index) : base(Index, null)
{

ArgChecker = (A => true);
}

public ErrorConfig(string Message) : base(-1, null)
public AnyConfig(int Index, Func<string,bool> argchecker) : base(Index, null)
{
this.Message = Message;
ArgChecker = argchecker;
}

public override bool IsMyConfig(string[] Args) => true;

public override bool CheckArgs(string[] Args)
{
var M = Message + "";

for(int i = 1; i < Args.Length; i++)
foreach(var A in Args)
{
M = M.Replace($"%{i}%", Args[i]);
if(!ArgChecker(A))
return false;
}

Log.Error(0, M);
return false;
return true;
}
}
}
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,9 @@ Add fifo:
fifo <path> <mode> <uid> <gid>
```

Remove file or dir or etc (allowed * for mask):
Remove file or dir or etc with excluded pathes (allowed * for mask):
```
rm <path>
rm <path1> [<path2>] [<path3>] [-<exclude1>] [-<exclude2>]
```

Change file mode:
Expand Down

0 comments on commit 08254d9

Please sign in to comment.