Skip to content

Commit

Permalink
Recursive directory loading
Browse files Browse the repository at this point in the history
  • Loading branch information
teplofizik committed Jan 29, 2024
1 parent 1bbb36c commit d898d42
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 7 deletions.
9 changes: 8 additions & 1 deletion NyaFs/Processor/Scripting/Commands/Fs/Rm.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace NyaFs.Processor.Scripting.Commands.Fs
Expand Down Expand Up @@ -44,8 +45,11 @@ public override ScriptStepResult Exec(ImageProcessor Processor)

if(Items.Length > 0)
{
foreach(var I in Items)
foreach (var I in Items)
{
Log.Write(2, $"Deleted {I}!");
Fs.Delete(I);
}

return new ScriptStepResult(ScriptStepStatus.Ok, $"{Path} deleted {Items.Length} items!");
}
Expand All @@ -61,7 +65,10 @@ public override ScriptStepResult Exec(ImageProcessor Processor)
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!");
}
Expand Down
18 changes: 15 additions & 3 deletions NyaFs/Processor/Scripting/Helper/FsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,21 @@

namespace NyaFs.Processor.Scripting.Helper
{
internal static class FsHelper
public static class FsHelper
{
internal static string DetectFilePath(this ScriptStep S, string Path)
public static string DetectDirPath(this ScriptStep S, string Path)
{
if (System.IO.File.Exists(Path))
return Path;

var RelPath = System.IO.Path.Combine(S.ScriptPath, Path);
if (System.IO.Directory.Exists(RelPath))
return RelPath;

return null;
}

public static string DetectFilePath(this ScriptStep S, string Path)
{
if (System.IO.File.Exists(Path))
return Path;
Expand Down Expand Up @@ -46,7 +58,7 @@ internal static Filesystem.Universal.FilesystemItem GetItem(ImageFormat.Elements
return GetItem(Fs, CombinePath(Base, Path));
}

internal static string CombinePath(string Base, string Name)
public static string CombinePath(string Base, string Name)
{
if ((Base == "/") || (Base == ".")) return Name;

Expand Down
2 changes: 1 addition & 1 deletion NyaFs/Processor/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace NyaFs.Processor
{
static class Utils
public static class Utils
{
public static bool CheckMode(string Text)
{
Expand Down
213 changes: 213 additions & 0 deletions Plugins/NyaFsFiles/Commands/LoadDir.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
using System;
using System.Collections.Generic;
using System.Text;
using NyaFs;
using NyaFs.Filesystem.Universal;
using NyaFs.Filesystem.Universal.Items;
using NyaFs.Filesystem.Universal.Types;
using NyaFs.FlattenedDeviceTree.Types;
using NyaFs.Processor;
using NyaFs.Processor.Scripting;
using NyaFs.Processor.Scripting.Helper;

namespace NyaFsFiles.Commands
{
public class LoadDir : ScriptStepGenerator
{
public LoadDir() : base("loaddir")
{
AddConfig(new ScriptArgsConfig(1, new ScriptArgsParam[] {
new NyaFs.Processor.Scripting.Params.FsPathScriptArgsParam(),
new NyaFs.Processor.Scripting.Params.LocalPathScriptArgsParam(),
new NyaFs.Processor.Scripting.Params.ModeScriptArgsParam(),
new NyaFs.Processor.Scripting.Params.ModeScriptArgsParam(),
new NyaFs.Processor.Scripting.Params.NumberScriptArgsParam("user"),
new NyaFs.Processor.Scripting.Params.NumberScriptArgsParam("group")
}));
}

public override ScriptStep Get(ScriptArgs Args)
{
var A = Args.RawArgs;

return new DirScriptStep(A[0], A[1], Utils.ConvertMode(A[2]), Utils.ConvertMode(A[3]), Convert.ToUInt32(A[4]), Convert.ToUInt32(A[5]));

}

public class DirScriptStep : ScriptStep
{
string Path = null;
string LocalPath = null;
uint User = uint.MaxValue;
uint Group = uint.MaxValue;
uint DirMode = uint.MaxValue;
uint FileMode = uint.MaxValue;

public DirScriptStep(string Path, string LocalPath, uint DirMode, uint FileMode, uint User, uint Group) : base("loaddir")
{
this.Path = Path;
this.LocalPath = LocalPath;
this.User = User;
this.Group = Group;
this.DirMode = DirMode;
this.FileMode = FileMode;
}

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

var DetectedDir = this.DetectDirPath(LocalPath);
if (DetectedDir == null)
return new ScriptStepResult(ScriptStepStatus.Error, $"Local directory {LocalPath} is not found");

if (Fs.Exists(Path))
{
var Item = Fs.GetElement(Path);
if (Item.ItemType == FilesystemItemType.Directory)
{
var Dir = Item as Dir;

Dir.Mode = DirMode;
Dir.User = User;
Dir.Group = Group;

Dir.Modified = DateTime.Now;

if(LoadDirectory(Dir, DetectedDir))
return new ScriptStepResult(ScriptStepStatus.Ok, $"Dir {Path} updated!");
else
return new ScriptStepResult(ScriptStepStatus.Error, $"Cannot load items to {Path}!");
}
else
return new ScriptStepResult(ScriptStepStatus.Error, $"{Path} is not dir!");
}
else
{
var Parent = Fs.GetParentDirectory(Path);
if (Parent != null)
{
var Dir = new Dir(Path, User, Group, DirMode);

Parent.Items.Add(Dir);

if (LoadDirectory(Dir, DetectedDir))
return new ScriptStepResult(ScriptStepStatus.Ok, $"Dir {Path} added!");
else
return new ScriptStepResult(ScriptStepStatus.Error, $"Cannot load items to {Path}!");
}
else
return new ScriptStepResult(ScriptStepStatus.Error, $"Parent dir for {Path} is not found!");
}
}

/// <summary>
/// Is item exists in filesystem
/// </summary>
/// <param name="dir"></param>
/// <param name="path"></param>
/// <returns></returns>
private FilesystemItem GetElement(Dir dir, string path)
{
foreach(var I in dir.Items)
{
if(I.Filename == path) return I;
}

return null;
}

/// <summary>
/// Load file to directory
/// </summary>
/// <param name="Dir"></param>
/// <param name="Path"></param>
/// <returns></returns>
private void LoadFile(Dir dir, string path)
{
var DetectedFilename = this.DetectFilePath(path);
if (DetectedFilename != null)
{
var FsPath = FsHelper.CombinePath(dir.Filename, System.IO.Path.GetFileName(DetectedFilename));
var El = GetElement(dir, FsPath);

if(El != null)
{
if (El.ItemType == FilesystemItemType.File)
{
var File = El as File;

File.Mode = FileMode;
File.User = User;
File.Group = Group;

File.Modified = DateTime.Now;
File.Content = System.IO.File.ReadAllBytes(DetectedFilename);
Log.Write(0, $"Updated {FsPath}!");
}
else
Log.Warning(0, $"Cannot update {FsPath}: not file!");
}
else
{
var Content = System.IO.File.ReadAllBytes(DetectedFilename);
var File = new File(Path, User, Group, FileMode, Content);

dir.Items.Add(File);
Log.Write(0, $"Added {FsPath}!");
}
}
}

/// <summary>
/// Load local directory to filesystem
/// </summary>
/// <param name="dir">Directory in filesystem</param>
/// <param name="path">Local path</param>
private bool LoadDirectory(Dir dir, string path)
{
var Dirs = System.IO.Directory.GetDirectories(path);
var Files = System.IO.Directory.GetFiles(path);

foreach(var Dir in Dirs)
{
var FsPath = FsHelper.CombinePath(dir.Filename, System.IO.Path.GetFileName(Dir));
var El = GetElement(dir, FsPath);
if (El != null)
{
if (El.ItemType == FilesystemItemType.Directory)
{
var NDir = El as Dir;

NDir.Mode = DirMode;
NDir.User = User;
NDir.Group = Group;

NDir.Modified = DateTime.Now;
Log.Write(2, $"Updated {FsPath}!");
}
else
Log.Warning(0, $"Cannot update {FsPath}: not directory!");
}
else
{
var NDir = new Dir(FsPath, User, Group, DirMode);

dir.Items.Add(NDir);
Log.Write(2, $"Added dir {FsPath}!");

LoadDirectory(NDir, Dir);
}
}

foreach (var File in Files)
LoadFile(dir, File);

return true;
}
}
}
}
3 changes: 2 additions & 1 deletion Plugins/NyaFsFiles/FilesPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public override ScriptStepGenerator[] GetGenerators()
return new ScriptStepGenerator[] {
new Commands.Copy(),
new Commands.Download(),
new Commands.Remove()
new Commands.Remove(),
new Commands.LoadDir()
};
}
}
Expand Down
8 changes: 7 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ Add fifo:
fifo <path> <mode> <uid> <gid>
```

Remove file or dir or etc:
Remove file or dir or etc (allowed * for mask):
```
rm <path>
```
Expand Down Expand Up @@ -518,6 +518,12 @@ Remove file from local filesystem:
remove <filename>
```

Load local directory to filesystem:
```loaddir <path> <localdir> <dirmode> <filemode> <user> <group>```
Example:
```loaddir /tmp/xxx include/xxx rwxr-xr-x rw-r--r-- 0 0```


## Linux-specific operations (NyaFsLinux.dll)
List of all users (from /etc/passwd):
```
Expand Down

0 comments on commit d898d42

Please sign in to comment.