Skip to content

Commit

Permalink
Remove files with mask: rm /lib/lib*
Browse files Browse the repository at this point in the history
  • Loading branch information
teplofizik committed Jan 28, 2024
1 parent b366b05 commit 1bbb36c
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 8 deletions.
59 changes: 56 additions & 3 deletions NyaFs/ImageFormat/Elements/Fs/LinuxFilesystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
using NyaFs.Filesystem.Universal.Items;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;

namespace NyaFs.ImageFormat.Elements.Fs
{

public class LinuxFilesystem
{
public NyaFs.Filesystem.Universal.Filesystem Fs = new NyaFs.Filesystem.Universal.Filesystem();
public Filesystem.Universal.Filesystem Fs = new Filesystem.Universal.Filesystem();

/// <summary>
/// Filesystem type
Expand Down Expand Up @@ -53,12 +55,63 @@ public class LinuxFilesystem
/// <returns>Total amount of data in filesystem</returns>
public long GetContentSize() => GetDirSize(Fs.Root);

/// <summary>
/// Search filesystem items in dir by path with mask
/// </summary>
/// <param name="Dir">Directory for search</param>
/// <param name="Mask">Search mask</param>
/// <returns></returns>
public string[] Search(Dir Dir, string Mask)
{
string pattern = "^" + Regex.Escape(Mask).Replace("\\*", ".*");

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

/// <summary>
/// Search filesystem items by path with mask
/// </summary>
/// <param name="Mask"></param>
/// <returns></returns>
public string[] Search(string Mask) => Search(Fs.Root, Mask);

/// <summary>
/// Search elements by path
/// </summary>
/// <param name="Dir">Directory for search</param>
/// <param name="Condition"></param>
/// <returns></returns>
public string[] Search(Dir Dir, Func<FilesystemItem,bool> Condition)
{
var Res = new List<string>();

foreach (var I in Dir.Items)
{
switch (I.ItemType)
{
case Filesystem.Universal.Types.FilesystemItemType.File:
case Filesystem.Universal.Types.FilesystemItemType.SymLink:
if (Condition(I))
Res.Add(I.Filename);
break;
case Filesystem.Universal.Types.FilesystemItemType.Directory:
if (Condition(I))
Res.Add(I.Filename);
else
Res.AddRange(Search(I as Dir, Condition));
break;
}
}

return Res.ToArray();
}

/// <summary>
/// Calculate total amount of data over all files in directory (only content of regular files + text of symlinks)
/// </summary>
/// <param name="Dir">Directory to calc sum of files size</param>
/// <returns>Total amount of data in deirectory</returns>
private long GetDirSize(Filesystem.Universal.Items.Dir Dir)
private long GetDirSize(Dir Dir)
{
long Res = 0;

Expand All @@ -71,7 +124,7 @@ private long GetDirSize(Filesystem.Universal.Items.Dir Dir)
Res += I.Size;
break;
case Filesystem.Universal.Types.FilesystemItemType.Directory:
Res += GetDirSize(I as Filesystem.Universal.Items.Dir);
Res += GetDirSize(I as Dir);
break;
}
}
Expand Down
50 changes: 45 additions & 5 deletions NyaFs/Processor/Scripting/Commands/Fs/Rm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,55 @@ public override ScriptStepResult Exec(ImageProcessor Processor)
if (Fs == null)
return new ScriptStepResult(ScriptStepStatus.Error, "Filesystem is not loaded");

if (Fs.Exists(Path))
if(Path.Contains('*'))
{
// Есть старый файл в файловой системе. Удалим.
Fs.Delete(Path);
if (Path.StartsWith("/"))
{
// Это путь с маской
var Items = Fs.Search(Path.Substring(1));

return new ScriptStepResult(ScriptStepStatus.Ok, $"{Path} deleted!");
if(Items.Length > 0)
{
foreach(var I in Items)
Fs.Delete(I);

return new ScriptStepResult(ScriptStepStatus.Ok, $"{Path} deleted {Items.Length} items!");
}
else
return new ScriptStepResult(ScriptStepStatus.Warning, $"{Path} not found!");
}
else
{
var Dir = Fs.GetDirectory(Processor.ActivePath);
if(Dir != null)
{
var Items = Fs.Search(Dir, Path);
if (Items.Length > 0)
{
foreach (var I in Items)
Fs.Delete(I);

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

return new ScriptStepResult(ScriptStepStatus.Ok, $"{Path} deleted!");
}
else
return new ScriptStepResult(ScriptStepStatus.Warning, $"{Path} not found!");
}
}
}
}
Expand Down

0 comments on commit 1bbb36c

Please sign in to comment.