From 3cc6d2c92a661d65789c695bcd9f8043597747d4 Mon Sep 17 00:00:00 2001 From: Leonmmcoset Date: Wed, 4 Feb 2026 20:13:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8B=86=E5=88=86=E4=BB=A3=E7=A0=814?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- shell/Commands/FileSystem/CatCommand.cs | 19 + shell/Commands/FileSystem/CdCommand.cs | 10 + shell/Commands/FileSystem/CopyCommand.cs | 45 ++ shell/Commands/FileSystem/DiskInfoCommand.cs | 32 ++ shell/Commands/FileSystem/FindCommand.cs | 41 ++ shell/Commands/FileSystem/HeadCommand.cs | 53 ++ shell/Commands/FileSystem/LsCommand.cs | 10 + shell/Commands/FileSystem/MkdirCommand.cs | 21 + shell/Commands/FileSystem/MoveCommand.cs | 47 ++ shell/Commands/FileSystem/PwdCommand.cs | 12 + shell/Commands/FileSystem/RenameCommand.cs | 56 +++ shell/Commands/FileSystem/RmCommand.cs | 46 ++ shell/Commands/FileSystem/RmdirCommand.cs | 29 ++ shell/Commands/FileSystem/TailCommand.cs | 54 ++ shell/Commands/FileSystem/TouchCommand.cs | 27 + shell/Commands/FileSystem/TreeCommand.cs | 77 +++ shell/Commands/FileSystem/WordCountCommand.cs | 50 ++ shell/Commands/Power/RestartCommand.cs | 14 + shell/Commands/Power/ShutdownCommand.cs | 14 + shell/Shell.cs | 475 +----------------- 20 files changed, 676 insertions(+), 456 deletions(-) create mode 100644 shell/Commands/FileSystem/CatCommand.cs create mode 100644 shell/Commands/FileSystem/CdCommand.cs create mode 100644 shell/Commands/FileSystem/CopyCommand.cs create mode 100644 shell/Commands/FileSystem/DiskInfoCommand.cs create mode 100644 shell/Commands/FileSystem/FindCommand.cs create mode 100644 shell/Commands/FileSystem/HeadCommand.cs create mode 100644 shell/Commands/FileSystem/LsCommand.cs create mode 100644 shell/Commands/FileSystem/MkdirCommand.cs create mode 100644 shell/Commands/FileSystem/MoveCommand.cs create mode 100644 shell/Commands/FileSystem/PwdCommand.cs create mode 100644 shell/Commands/FileSystem/RenameCommand.cs create mode 100644 shell/Commands/FileSystem/RmCommand.cs create mode 100644 shell/Commands/FileSystem/RmdirCommand.cs create mode 100644 shell/Commands/FileSystem/TailCommand.cs create mode 100644 shell/Commands/FileSystem/TouchCommand.cs create mode 100644 shell/Commands/FileSystem/TreeCommand.cs create mode 100644 shell/Commands/FileSystem/WordCountCommand.cs create mode 100644 shell/Commands/Power/RestartCommand.cs create mode 100644 shell/Commands/Power/ShutdownCommand.cs diff --git a/shell/Commands/FileSystem/CatCommand.cs b/shell/Commands/FileSystem/CatCommand.cs new file mode 100644 index 0000000..0af8f64 --- /dev/null +++ b/shell/Commands/FileSystem/CatCommand.cs @@ -0,0 +1,19 @@ +using System; + +namespace CMLeonOS.Commands.FileSystem +{ + public static class CatCommand + { + public static void ProcessCat(CMLeonOS.FileSystem fileSystem, string args, Action showError) + { + if (string.IsNullOrEmpty(args)) + { + showError("Please specify a file name"); + } + else + { + Console.WriteLine(fileSystem.ReadFile(args)); + } + } + } +} diff --git a/shell/Commands/FileSystem/CdCommand.cs b/shell/Commands/FileSystem/CdCommand.cs new file mode 100644 index 0000000..59fd929 --- /dev/null +++ b/shell/Commands/FileSystem/CdCommand.cs @@ -0,0 +1,10 @@ +namespace CMLeonOS.Commands.FileSystem +{ + public static class CdCommand + { + public static void ProcessCd(CMLeonOS.FileSystem fileSystem, string args) + { + fileSystem.ChangeDirectory(args); + } + } +} diff --git a/shell/Commands/FileSystem/CopyCommand.cs b/shell/Commands/FileSystem/CopyCommand.cs new file mode 100644 index 0000000..5315fa4 --- /dev/null +++ b/shell/Commands/FileSystem/CopyCommand.cs @@ -0,0 +1,45 @@ +using System; + +namespace CMLeonOS.Commands.FileSystem +{ + public static class CopyCommand + { + public static void CopyFile(CMLeonOS.FileSystem fileSystem, string args, Action showError, Action showSuccess) + { + if (string.IsNullOrEmpty(args)) + { + showError("Please specify source and destination files"); + showError("cp "); + return; + } + + try + { + string[] parts = args.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); + if (parts.Length < 2) + { + showError("Please specify both source and destination"); + showError("cp "); + return; + } + + string sourceFile = parts[0]; + string destFile = parts[1]; + + string content = fileSystem.ReadFile(sourceFile); + if (content == null) + { + showError($"Source file '{sourceFile}' does not exist"); + return; + } + + fileSystem.WriteFile(destFile, content); + showSuccess($"File copied successfully from '{sourceFile}' to '{destFile}'"); + } + catch (Exception ex) + { + showError($"Error copying file: {ex.Message}"); + } + } + } +} diff --git a/shell/Commands/FileSystem/DiskInfoCommand.cs b/shell/Commands/FileSystem/DiskInfoCommand.cs new file mode 100644 index 0000000..d4dc1be --- /dev/null +++ b/shell/Commands/FileSystem/DiskInfoCommand.cs @@ -0,0 +1,32 @@ +using System; +using Sys = Cosmos.System; + +namespace CMLeonOS.Commands.FileSystem +{ + public static class DiskInfoCommand + { + public static void GetDiskInfo(Action showError) + { + Console.WriteLine("===================================="); + Console.WriteLine(" Disk Information"); + Console.WriteLine("===================================="); + + try + { + var disks = Sys.FileSystem.VFS.VFSManager.GetDisks(); + + if (disks == null || disks.Count == 0) + { + Console.WriteLine("No disks found."); + return; + } + + Console.WriteLine($"Total Disks: {disks.Count}"); + } + catch (Exception ex) + { + showError($"Error getting disk info: {ex.Message}"); + } + } + } +} diff --git a/shell/Commands/FileSystem/FindCommand.cs b/shell/Commands/FileSystem/FindCommand.cs new file mode 100644 index 0000000..ffcd432 --- /dev/null +++ b/shell/Commands/FileSystem/FindCommand.cs @@ -0,0 +1,41 @@ +using System; + +namespace CMLeonOS.Commands.FileSystem +{ + public static class FindCommand + { + public static void FindFile(CMLeonOS.FileSystem fileSystem, string args, Action showError) + { + if (string.IsNullOrEmpty(args)) + { + showError("Please specify a file name to search"); + showError("find "); + return; + } + + try + { + var files = fileSystem.GetFileList("."); + bool found = false; + + foreach (var file in files) + { + if (file.ToLower().Contains(args.ToLower())) + { + Console.WriteLine($"Found: {file}"); + found = true; + } + } + + if (!found) + { + Console.WriteLine($"No files found matching '{args}'"); + } + } + catch (Exception ex) + { + showError($"Error finding file: {ex.Message}"); + } + } + } +} diff --git a/shell/Commands/FileSystem/HeadCommand.cs b/shell/Commands/FileSystem/HeadCommand.cs new file mode 100644 index 0000000..bcbc408 --- /dev/null +++ b/shell/Commands/FileSystem/HeadCommand.cs @@ -0,0 +1,53 @@ +using System; + +namespace CMLeonOS.Commands.FileSystem +{ + public static class HeadCommand + { + public static void HeadFile(CMLeonOS.FileSystem fileSystem, string args, Action showError) + { + if (string.IsNullOrEmpty(args)) + { + showError("Please specify a file name"); + return; + } + + try + { + string[] parts = args.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); + string fileName = parts[0]; + int lineCount = 10; + + if (parts.Length > 1) + { + if (int.TryParse(parts[1], out int count)) + { + lineCount = count; + } + } + + string content = fileSystem.ReadFile(fileName); + if (string.IsNullOrEmpty(content)) + { + Console.WriteLine("File is empty"); + return; + } + + string[] lines = content.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None); + int displayLines = Math.Min(lineCount, lines.Length); + + Console.WriteLine($"First {displayLines} lines of {fileName}:"); + Console.WriteLine("--------------------------------"); + + for (int i = 0; i < displayLines; i++) + { + Console.WriteLine($"{i + 1}: {lines[i]}"); + } + } + catch (Exception ex) + { + showError($"Error reading file: {ex.Message}"); + } + } + } +} diff --git a/shell/Commands/FileSystem/LsCommand.cs b/shell/Commands/FileSystem/LsCommand.cs new file mode 100644 index 0000000..f0046d4 --- /dev/null +++ b/shell/Commands/FileSystem/LsCommand.cs @@ -0,0 +1,10 @@ +namespace CMLeonOS.Commands.FileSystem +{ + public static class LsCommand + { + public static void ProcessLs(CMLeonOS.FileSystem fileSystem, string args) + { + fileSystem.ListFiles(args); + } + } +} diff --git a/shell/Commands/FileSystem/MkdirCommand.cs b/shell/Commands/FileSystem/MkdirCommand.cs new file mode 100644 index 0000000..e2a69b3 --- /dev/null +++ b/shell/Commands/FileSystem/MkdirCommand.cs @@ -0,0 +1,21 @@ +using System; + +namespace CMLeonOS.Commands.FileSystem +{ + public static class MkdirCommand + { + public static void ProcessMkdir(CMLeonOS.FileSystem fileSystem, string args) + { + if (string.IsNullOrEmpty(args)) + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine("Please specify a directory name"); + Console.ResetColor(); + } + else + { + fileSystem.MakeDirectory(args); + } + } + } +} diff --git a/shell/Commands/FileSystem/MoveCommand.cs b/shell/Commands/FileSystem/MoveCommand.cs new file mode 100644 index 0000000..e4cd412 --- /dev/null +++ b/shell/Commands/FileSystem/MoveCommand.cs @@ -0,0 +1,47 @@ +using System; + +namespace CMLeonOS.Commands.FileSystem +{ + public static class MoveCommand + { + public static void MoveFile(CMLeonOS.FileSystem fileSystem, string args, Action showError, Action showSuccess) + { + if (string.IsNullOrEmpty(args)) + { + showError("Please specify source and destination files"); + showError("mv "); + return; + } + + try + { + string[] parts = args.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); + if (parts.Length < 2) + { + showError("Please specify both source and destination"); + showError("mv "); + return; + } + + string sourceFile = parts[0]; + string destFile = parts[1]; + + string content = fileSystem.ReadFile(sourceFile); + if (content == null) + { + showError($"Source file '{sourceFile}' does not exist"); + return; + } + + fileSystem.WriteFile(destFile, content); + fileSystem.DeleteFile(sourceFile); + + showSuccess($"File moved/renamed successfully from '{sourceFile}' to '{destFile}'"); + } + catch (Exception ex) + { + showError($"Error moving file: {ex.Message}"); + } + } + } +} diff --git a/shell/Commands/FileSystem/PwdCommand.cs b/shell/Commands/FileSystem/PwdCommand.cs new file mode 100644 index 0000000..dddcf49 --- /dev/null +++ b/shell/Commands/FileSystem/PwdCommand.cs @@ -0,0 +1,12 @@ +using System; + +namespace CMLeonOS.Commands.FileSystem +{ + public static class PwdCommand + { + public static void ProcessPwd(CMLeonOS.FileSystem fileSystem) + { + Console.WriteLine(fileSystem.CurrentDirectory); + } + } +} diff --git a/shell/Commands/FileSystem/RenameCommand.cs b/shell/Commands/FileSystem/RenameCommand.cs new file mode 100644 index 0000000..5c1c999 --- /dev/null +++ b/shell/Commands/FileSystem/RenameCommand.cs @@ -0,0 +1,56 @@ +using System; + +namespace CMLeonOS.Commands.FileSystem +{ + public static class RenameCommand + { + public static void RenameFile(CMLeonOS.FileSystem fileSystem, string args, Action showError, Action showSuccess) + { + if (string.IsNullOrEmpty(args)) + { + showError("Please specify source and new name"); + showError("rename "); + return; + } + + try + { + string[] parts = args.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); + if (parts.Length < 2) + { + showError("Please specify both source and new name"); + showError("rename "); + return; + } + + string sourceFile = parts[0]; + string newName = parts[1]; + + string sourcePath = fileSystem.GetFullPath(sourceFile); + string destPath = fileSystem.GetFullPath(newName); + + if (!System.IO.File.Exists(sourcePath)) + { + showError($"Source file '{sourceFile}' does not exist"); + return; + } + + if (System.IO.File.Exists(destPath)) + { + showError($"Destination '{newName}' already exists"); + return; + } + + string content = fileSystem.ReadFile(sourcePath); + System.IO.File.WriteAllText(destPath, content); + fileSystem.DeleteFile(sourcePath); + + showSuccess($"File renamed successfully from '{sourceFile}' to '{newName}'"); + } + catch (Exception ex) + { + showError($"Error renaming file: {ex.Message}"); + } + } + } +} diff --git a/shell/Commands/FileSystem/RmCommand.cs b/shell/Commands/FileSystem/RmCommand.cs new file mode 100644 index 0000000..771ce27 --- /dev/null +++ b/shell/Commands/FileSystem/RmCommand.cs @@ -0,0 +1,46 @@ +using System; + +namespace CMLeonOS.Commands.FileSystem +{ + public static class RmCommand + { + public static void ProcessRm(CMLeonOS.FileSystem fileSystem, string args, bool fixMode, Action showError) + { + if (string.IsNullOrEmpty(args)) + { + showError("Please specify a file name"); + } + else + { + bool isInSysFolder = (args.Contains(@"\system\") || args.Contains(@"/sys/")) && !fixMode; + + if (isInSysFolder) + { + string[] parts = args.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); + bool hasNorisk = false; + string filePath = args; + + if (parts.Length > 1) + { + hasNorisk = Array.IndexOf(parts, "-norisk") >= 0; + filePath = parts[0]; + } + + if (!hasNorisk) + { + showError("Cannot delete files in sys folder without -norisk parameter"); + showError("Usage: rm -norisk"); + } + else + { + fileSystem.DeleteFile(filePath); + } + } + else + { + fileSystem.DeleteFile(args); + } + } + } + } +} diff --git a/shell/Commands/FileSystem/RmdirCommand.cs b/shell/Commands/FileSystem/RmdirCommand.cs new file mode 100644 index 0000000..39fed40 --- /dev/null +++ b/shell/Commands/FileSystem/RmdirCommand.cs @@ -0,0 +1,29 @@ +using System; + +namespace CMLeonOS.Commands.FileSystem +{ + public static class RmdirCommand + { + public static void ProcessRmdir(CMLeonOS.FileSystem fileSystem, string args, bool fixMode, Action showError) + { + if (string.IsNullOrEmpty(args)) + { + showError("Please specify a directory name"); + } + else + { + bool isInSysFolder = (args.Contains(@"\system\") || args.Contains(@"/sys/")) && !fixMode; + + if (isInSysFolder) + { + showError("Cannot delete directories in sys folder"); + showError("Use fix mode to bypass this restriction"); + } + else + { + fileSystem.DeleteDirectory(args); + } + } + } + } +} diff --git a/shell/Commands/FileSystem/TailCommand.cs b/shell/Commands/FileSystem/TailCommand.cs new file mode 100644 index 0000000..cde6ac2 --- /dev/null +++ b/shell/Commands/FileSystem/TailCommand.cs @@ -0,0 +1,54 @@ +using System; + +namespace CMLeonOS.Commands.FileSystem +{ + public static class TailCommand + { + public static void TailFile(CMLeonOS.FileSystem fileSystem, string args, Action showError) + { + if (string.IsNullOrEmpty(args)) + { + showError("Please specify a file name"); + return; + } + + try + { + string[] parts = args.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); + string fileName = parts[0]; + int lineCount = 10; + + if (parts.Length > 1) + { + if (int.TryParse(parts[1], out int count)) + { + lineCount = count; + } + } + + string content = fileSystem.ReadFile(fileName); + if (string.IsNullOrEmpty(content)) + { + Console.WriteLine("File is empty"); + return; + } + + string[] lines = content.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None); + int displayLines = Math.Min(lineCount, lines.Length); + int startLine = Math.Max(0, lines.Length - displayLines); + + Console.WriteLine($"Last {displayLines} lines of {fileName}:"); + Console.WriteLine("--------------------------------"); + + for (int i = startLine; i < lines.Length; i++) + { + Console.WriteLine($"{i + 1}: {lines[i]}"); + } + } + catch (Exception ex) + { + showError($"Error reading file: {ex.Message}"); + } + } + } +} diff --git a/shell/Commands/FileSystem/TouchCommand.cs b/shell/Commands/FileSystem/TouchCommand.cs new file mode 100644 index 0000000..dc5ce17 --- /dev/null +++ b/shell/Commands/FileSystem/TouchCommand.cs @@ -0,0 +1,27 @@ +using System; + +namespace CMLeonOS.Commands.FileSystem +{ + public static class TouchCommand + { + public static void CreateEmptyFile(CMLeonOS.FileSystem fileSystem, string args, Action showError, Action showSuccess) + { + if (string.IsNullOrEmpty(args)) + { + showError("Please specify a file name"); + showError("touch "); + return; + } + + try + { + fileSystem.WriteFile(args, ""); + showSuccess($"Empty file '{args}' created successfully"); + } + catch (Exception ex) + { + showError($"Error creating file: {ex.Message}"); + } + } + } +} diff --git a/shell/Commands/FileSystem/TreeCommand.cs b/shell/Commands/FileSystem/TreeCommand.cs new file mode 100644 index 0000000..95fce8f --- /dev/null +++ b/shell/Commands/FileSystem/TreeCommand.cs @@ -0,0 +1,77 @@ +using System; + +namespace CMLeonOS.Commands.FileSystem +{ + public static class TreeCommand + { + public static void ShowTree(CMLeonOS.FileSystem fileSystem, string args, Action showError) + { + string startPath = string.IsNullOrEmpty(args) ? "." : args; + string fullPath = fileSystem.GetFullPath(startPath); + + if (!System.IO.Directory.Exists(fullPath)) + { + showError($"Directory not found: {startPath}"); + return; + } + + try + { + Console.WriteLine(fullPath); + PrintDirectoryTree(fileSystem, fullPath, "", true, showError); + } + catch (Exception ex) + { + showError($"Error displaying tree: {ex.Message}"); + } + } + + private static void PrintDirectoryTree(CMLeonOS.FileSystem fileSystem, string path, string prefix, bool isLast, Action showError) + { + try + { + var dirs = fileSystem.GetFullPathDirectoryList(path); + var files = fileSystem.GetFullPathFileList(path); + + int totalItems = dirs.Count + files.Count; + int current = 0; + + foreach (var dir in dirs) + { + current++; + bool isLastItem = current == totalItems; + string connector = isLastItem ? "+-- " : "|-- "; + string newPrefix = prefix + (isLastItem ? " " : "| "); + + string dirName = System.IO.Path.GetFileName(dir); + Console.WriteLine($"{prefix}{connector}{dirName}/"); + + PrintDirectoryTree(fileSystem, dir, newPrefix, isLastItem, showError); + } + + foreach (var file in files) + { + current++; + bool isLastItem = current == totalItems; + string connector = isLastItem ? "+-- " : "|-- "; + + string fileName = System.IO.Path.GetFileName(file); + Console.WriteLine($"{prefix}{connector}{fileName}"); + } + } + catch (System.IO.DirectoryNotFoundException) + { + showError($"Directory not found: {path}"); + } + catch (Exception ex) + { + string errorMsg = ex.Message; + if (string.IsNullOrEmpty(errorMsg)) + { + errorMsg = $"Error type: {ex.GetType().Name}"; + } + showError($"Error reading directory {path}: {errorMsg}"); + } + } + } +} diff --git a/shell/Commands/FileSystem/WordCountCommand.cs b/shell/Commands/FileSystem/WordCountCommand.cs new file mode 100644 index 0000000..77de6bd --- /dev/null +++ b/shell/Commands/FileSystem/WordCountCommand.cs @@ -0,0 +1,50 @@ +using System; + +namespace CMLeonOS.Commands.FileSystem +{ + public static class WordCountCommand + { + public static void WordCount(CMLeonOS.FileSystem fileSystem, string args, Action showError) + { + if (string.IsNullOrEmpty(args)) + { + showError("Please specify a file name"); + return; + } + + try + { + string content = fileSystem.ReadFile(args); + if (string.IsNullOrEmpty(content)) + { + Console.WriteLine("File is empty"); + return; + } + + string[] lines = content.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None); + int lineCount = lines.Length; + int wordCount = 0; + int charCount = content.Length; + + foreach (string line in lines) + { + if (!string.IsNullOrWhiteSpace(line)) + { + string[] words = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); + wordCount += words.Length; + } + } + + Console.WriteLine($"Word count for {args}:"); + Console.WriteLine("--------------------------------"); + Console.WriteLine($"Lines: {lineCount}"); + Console.WriteLine($"Words: {wordCount}"); + Console.WriteLine($"Characters: {charCount}"); + } + catch (Exception ex) + { + showError($"Error reading file: {ex.Message}"); + } + } + } +} diff --git a/shell/Commands/Power/RestartCommand.cs b/shell/Commands/Power/RestartCommand.cs new file mode 100644 index 0000000..e16039e --- /dev/null +++ b/shell/Commands/Power/RestartCommand.cs @@ -0,0 +1,14 @@ +using System; +using Sys = Cosmos.System; + +namespace CMLeonOS.Commands.Power +{ + public static class RestartCommand + { + public static void ProcessRestart() + { + Console.WriteLine("Restarting system..."); + Sys.Power.Reboot(); + } + } +} diff --git a/shell/Commands/Power/ShutdownCommand.cs b/shell/Commands/Power/ShutdownCommand.cs new file mode 100644 index 0000000..210d7b5 --- /dev/null +++ b/shell/Commands/Power/ShutdownCommand.cs @@ -0,0 +1,14 @@ +using System; +using Sys = Cosmos.System; + +namespace CMLeonOS.Commands.Power +{ + public static class ShutdownCommand + { + public static void ProcessShutdown() + { + Console.WriteLine("Shutting down system..."); + Sys.Power.Shutdown(); + } + } +} diff --git a/shell/Shell.cs b/shell/Shell.cs index c8ef8c7..b14096c 100644 --- a/shell/Shell.cs +++ b/shell/Shell.cs @@ -126,14 +126,12 @@ namespace CMLeonOS public void ProcessRestart() { - Console.WriteLine("Restarting system..."); - Sys.Power.Reboot(); + Commands.Power.RestartCommand.ProcessRestart(); } public void ProcessShutdown() { - Console.WriteLine("Shutting down system..."); - Sys.Power.Shutdown(); + Commands.Power.ShutdownCommand.ProcessShutdown(); } public void ProcessHelp(string args) @@ -153,102 +151,37 @@ namespace CMLeonOS public void ProcessLs(string args) { - fileSystem.ListFiles(args); + Commands.FileSystem.LsCommand.ProcessLs(fileSystem, args); } public void ProcessCd(string args) { - fileSystem.ChangeDirectory(args); + Commands.FileSystem.CdCommand.ProcessCd(fileSystem, args); } public void ProcessPwd() { - Console.WriteLine(fileSystem.CurrentDirectory); + Commands.FileSystem.PwdCommand.ProcessPwd(fileSystem); } public void ProcessMkdir(string args) { - if (string.IsNullOrEmpty(args)) - { - ShowError("Please specify a directory name"); - } - else - { - fileSystem.MakeDirectory(args); - } + Commands.FileSystem.MkdirCommand.ProcessMkdir(fileSystem, args); } public void ProcessRm(string args) { - if (string.IsNullOrEmpty(args)) - { - ShowError("Please specify a file name"); - } - else - { - bool isInSysFolder = (args.Contains(@"\system\") || args.Contains(@"/sys/")) && !fixMode; - - if (isInSysFolder) - { - string[] parts = args.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); - bool hasNorisk = false; - string filePath = args; - - if (parts.Length > 1) - { - hasNorisk = Array.IndexOf(parts, "-norisk") >= 0; - filePath = parts[0]; - } - - if (!hasNorisk) - { - ShowError("Cannot delete files in sys folder without -norisk parameter"); - ShowError("Usage: rm -norisk"); - } - else - { - fileSystem.DeleteFile(filePath); - } - } - else - { - fileSystem.DeleteFile(args); - } - } + Commands.FileSystem.RmCommand.ProcessRm(fileSystem, args, fixMode, ShowError); } public void ProcessRmdir(string args) { - if (string.IsNullOrEmpty(args)) - { - ShowError("Please specify a directory name"); - } - else - { - bool isInSysFolder = (args.Contains(@"\system\") || args.Contains(@"/sys/")) && !fixMode; - - if (isInSysFolder) - { - ShowError("Cannot delete directories in sys folder"); - ShowError("Use fix mode to bypass this restriction"); - } - else - { - fileSystem.DeleteDirectory(args); - } - } + Commands.FileSystem.RmdirCommand.ProcessRmdir(fileSystem, args, fixMode, ShowError); } public void ProcessCat(string args) { - if (string.IsNullOrEmpty(args)) - { - ShowError("Please specify a file name"); - } - else - { - Console.WriteLine(fileSystem.ReadFile(args)); - } + Commands.FileSystem.CatCommand.ProcessCat(fileSystem, args, ShowError); } public void ProcessVersion() @@ -755,422 +688,52 @@ namespace CMLeonOS public void HeadFile(string args) { - if (string.IsNullOrEmpty(args)) - { - ShowError("Please specify a file name"); - return; - } - - try - { - string[] parts = args.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); - string fileName = parts[0]; - int lineCount = 10; // 默认显示10行 - - if (parts.Length > 1) - { - if (int.TryParse(parts[1], out int count)) - { - lineCount = count; - } - } - - string content = fileSystem.ReadFile(fileName); - if (string.IsNullOrEmpty(content)) - { - Console.WriteLine("File is empty"); - return; - } - - string[] lines = content.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None); - int displayLines = Math.Min(lineCount, lines.Length); - - Console.WriteLine($"First {displayLines} lines of {fileName}:"); - Console.WriteLine("--------------------------------"); - - for (int i = 0; i < displayLines; i++) - { - Console.WriteLine($"{i + 1}: {lines[i]}"); - } - } - catch (Exception ex) - { - ShowError($"Error reading file: {ex.Message}"); - } + Commands.FileSystem.HeadCommand.HeadFile(fileSystem, args, ShowError); } public void TailFile(string args) { - if (string.IsNullOrEmpty(args)) - { - ShowError("Please specify a file name"); - return; - } - - try - { - string[] parts = args.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); - string fileName = parts[0]; - int lineCount = 10; - - if (parts.Length > 1) - { - if (int.TryParse(parts[1], out int count)) - { - lineCount = count; - } - } - - string content = fileSystem.ReadFile(fileName); - if (string.IsNullOrEmpty(content)) - { - Console.WriteLine("File is empty"); - return; - } - - string[] lines = content.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None); - int displayLines = Math.Min(lineCount, lines.Length); - int startLine = Math.Max(0, lines.Length - displayLines); - - Console.WriteLine($"Last {displayLines} lines of {fileName}:"); - Console.WriteLine("--------------------------------"); - - for (int i = startLine; i < lines.Length; i++) - { - Console.WriteLine($"{i + 1}: {lines[i]}"); - } - } - catch (Exception ex) - { - ShowError($"Error reading file: {ex.Message}"); - } + Commands.FileSystem.TailCommand.TailFile(fileSystem, args, ShowError); } public void WordCount(string args) { - if (string.IsNullOrEmpty(args)) - { - ShowError("Please specify a file name"); - return; - } - - try - { - string content = fileSystem.ReadFile(args); - if (string.IsNullOrEmpty(content)) - { - Console.WriteLine("File is empty"); - return; - } - - string[] lines = content.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None); - int lineCount = lines.Length; - int wordCount = 0; - int charCount = content.Length; - - foreach (string line in lines) - { - if (!string.IsNullOrWhiteSpace(line)) - { - string[] words = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); - wordCount += words.Length; - } - } - - Console.WriteLine($"Word count for {args}:"); - Console.WriteLine("--------------------------------"); - Console.WriteLine($"Lines: {lineCount}"); - Console.WriteLine($"Words: {wordCount}"); - Console.WriteLine($"Characters: {charCount}"); - } - catch (Exception ex) - { - ShowError($"Error reading file: {ex.Message}"); - } + Commands.FileSystem.WordCountCommand.WordCount(fileSystem, args, ShowError); } public void CopyFile(string args) { - if (string.IsNullOrEmpty(args)) - { - ShowError("Please specify source and destination files"); - ShowError("cp "); - return; - } - - try - { - string[] parts = args.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); - if (parts.Length < 2) - { - ShowError("Please specify both source and destination"); - ShowError("cp "); - return; - } - - string sourceFile = parts[0]; - string destFile = parts[1]; - - string content = fileSystem.ReadFile(sourceFile); - if (content == null) - { - ShowError($"Source file '{sourceFile}' does not exist"); - return; - } - - fileSystem.WriteFile(destFile, content); - ShowSuccess($"File copied successfully from '{sourceFile}' to '{destFile}'"); - } - catch (Exception ex) - { - ShowError($"Error copying file: {ex.Message}"); - } + Commands.FileSystem.CopyCommand.CopyFile(fileSystem, args, ShowError, ShowSuccess); } public void MoveFile(string args) { - if (string.IsNullOrEmpty(args)) - { - ShowError("Please specify source and destination files"); - ShowError("mv "); - return; - } - - try - { - string[] parts = args.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); - if (parts.Length < 2) - { - ShowError("Please specify both source and destination"); - ShowError("mv "); - return; - } - - string sourceFile = parts[0]; - string destFile = parts[1]; - - // 使用FileSystem读取源文件内容 - string content = fileSystem.ReadFile(sourceFile); - if (content == null) - { - ShowError($"Source file '{sourceFile}' does not exist"); - return; - } - - // 使用FileSystem写入目标文件 - fileSystem.WriteFile(destFile, content); - - // 删除源文件 - fileSystem.DeleteFile(sourceFile); - - ShowSuccess($"File moved/renamed successfully from '{sourceFile}' to '{destFile}'"); - } - catch (Exception ex) - { - ShowError($"Error moving file: {ex.Message}"); - } + Commands.FileSystem.MoveCommand.MoveFile(fileSystem, args, ShowError, ShowSuccess); } public void RenameFile(string args) { - if (string.IsNullOrEmpty(args)) - { - ShowError("Please specify source and new name"); - ShowError("rename "); - return; - } - - try - { - string[] parts = args.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); - if (parts.Length < 2) - { - ShowError("Please specify both source and new name"); - ShowError("rename "); - return; - } - - string sourceFile = parts[0]; - string newName = parts[1]; - - string sourcePath = fileSystem.GetFullPath(sourceFile); - string destPath = fileSystem.GetFullPath(newName); - - if (!System.IO.File.Exists(sourcePath)) - { - ShowError($"Source file '{sourceFile}' does not exist"); - return; - } - - if (System.IO.File.Exists(destPath)) - { - ShowError($"Destination '{newName}' already exists"); - return; - } - - string content = fileSystem.ReadFile(sourcePath); - System.IO.File.WriteAllText(destPath, content); - fileSystem.DeleteFile(sourcePath); - - ShowSuccess($"File renamed successfully from '{sourceFile}' to '{newName}'"); - } - catch (Exception ex) - { - ShowError($"Error renaming file: {ex.Message}"); - } + Commands.FileSystem.RenameCommand.RenameFile(fileSystem, args, ShowError, ShowSuccess); } public void CreateEmptyFile(string args) { - if (string.IsNullOrEmpty(args)) - { - ShowError("Please specify a file name"); - ShowError("touch "); - return; - } - - try - { - // 使用FileSystem创建空文件 - fileSystem.WriteFile(args, ""); - ShowSuccess($"Empty file '{args}' created successfully"); - } - catch (Exception ex) - { - ShowError($"Error creating file: {ex.Message}"); - } + Commands.FileSystem.TouchCommand.CreateEmptyFile(fileSystem, args, ShowError, ShowSuccess); } public void FindFile(string args) { - if (string.IsNullOrEmpty(args)) - { - ShowError("Please specify a file name to search"); - ShowError("find "); - return; - } - - try - { - var files = fileSystem.GetFileList("."); - bool found = false; - - foreach (var file in files) - { - if (file.ToLower().Contains(args.ToLower())) - { - Console.WriteLine($"Found: {file}"); - found = true; - } - } - - if (!found) - { - Console.WriteLine($"No files found matching '{args}'"); - } - } - catch (Exception ex) - { - ShowError($"Error finding file: {ex.Message}"); - } + Commands.FileSystem.FindCommand.FindFile(fileSystem, args, ShowError); } public void ShowTree(string args) { - string startPath = string.IsNullOrEmpty(args) ? "." : args; - string fullPath = fileSystem.GetFullPath(startPath); - - if (!System.IO.Directory.Exists(fullPath)) - { - ShowError($"Directory not found: {startPath}"); - return; - } - - try - { - Console.WriteLine(fullPath); - PrintDirectoryTree(fullPath, "", true); - } - catch (Exception ex) - { - ShowError($"Error displaying tree: {ex.Message}"); - } - } - - private void PrintDirectoryTree(string path, string prefix, bool isLast) - { - try - { - var dirs = fileSystem.GetFullPathDirectoryList(path); - var files = fileSystem.GetFullPathFileList(path); - - int totalItems = dirs.Count + files.Count; - int current = 0; - - foreach (var dir in dirs) - { - current++; - bool isLastItem = current == totalItems; - string connector = isLastItem ? "+-- " : "|-- "; - string newPrefix = prefix + (isLastItem ? " " : "| "); - - string dirName = System.IO.Path.GetFileName(dir); - Console.WriteLine($"{prefix}{connector}{dirName}/"); - - PrintDirectoryTree(dir, newPrefix, isLastItem); - } - - foreach (var file in files) - { - current++; - bool isLastItem = current == totalItems; - string connector = isLastItem ? "+-- " : "|-- "; - - string fileName = System.IO.Path.GetFileName(file); - Console.WriteLine($"{prefix}{connector}{fileName}"); - } - } - catch (System.IO.DirectoryNotFoundException) - { - ShowError($"Directory not found: {path}"); - } - // catch (System.IO.UnauthorizedAccessException) - // { - // ShowError($"Access denied to directory: {path}"); - // } - catch (Exception ex) - { - string errorMsg = ex.Message; - if (string.IsNullOrEmpty(errorMsg)) - { - errorMsg = $"Error type: {ex.GetType().Name}"; - } - ShowError($"Error reading directory {path}: {errorMsg}"); - } + Commands.FileSystem.TreeCommand.ShowTree(fileSystem, args, ShowError); } public void GetDiskInfo() { - Console.WriteLine("===================================="); - Console.WriteLine(" Disk Information"); - Console.WriteLine("===================================="); - - try - { - var disks = Sys.FileSystem.VFS.VFSManager.GetDisks(); - - if (disks == null || disks.Count == 0) - { - Console.WriteLine("No disks found."); - return; - } - - Console.WriteLine($"Total Disks: {disks.Count}"); - } - catch (Exception ex) - { - ShowError($"Error getting disk info: {ex.Message}"); - } + Commands.FileSystem.DiskInfoCommand.GetDiskInfo(ShowError); } private string FormatBytes(long bytes)