From be9f61329ec1a8ce72bc6e9d13854f27920f5ae2 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Fri, 11 Nov 2022 19:43:38 +0600 Subject: [PATCH 1/4] Filesystem.New --- DTLib/Extensions/StringConverter.cs | 41 +++++++--- DTLib/Filesystem/File.cs | 7 +- DTLib/Filesystem/New/Directory.cs | 120 ++++++++++++++++++++++++++++ DTLib/Filesystem/New/File.cs | 62 ++++++++++++++ DTLib/Filesystem/New/Path.cs | 39 +++++++++ 5 files changed, 254 insertions(+), 15 deletions(-) create mode 100644 DTLib/Filesystem/New/Directory.cs create mode 100644 DTLib/Filesystem/New/File.cs create mode 100644 DTLib/Filesystem/New/Path.cs diff --git a/DTLib/Extensions/StringConverter.cs b/DTLib/Extensions/StringConverter.cs index a3ce624..80d94b1 100644 --- a/DTLib/Extensions/StringConverter.cs +++ b/DTLib/Extensions/StringConverter.cs @@ -44,30 +44,47 @@ public static class StringConverter public static bool StartsWith(this string s, char c) => s[0] == c; public static bool EndsWith(this string s, char c) => s[s.Length - 1] == c; - public static string MergeToString(params object[] parts) + public static string MergeToString(params TVal[] parts) { + if (parts.Length == 0) + return ""; StringBuilder builder = new(); for (int i = 0; i < parts.Length; i++) builder.Append(parts[i]); return builder.ToString(); } - public static string MergeToString(this IEnumerable collection, string separator) + public static string MergeToString(TSep separator, params TVal[] parts) { - StringBuilder builder = new(); - foreach (T elem in collection) - { - builder.Append(elem); - builder.Append(separator); - } - if (builder.Length == 0) + if (parts.Length == 0) return ""; - builder.Remove(builder.Length - separator.Length, separator.Length); + StringBuilder builder = new(); + builder.Append(parts[0]); + for (int i = 1; i < parts.Length; i++) + { + builder.Append(separator); + builder.Append(parts[i]); + } return builder.ToString(); } - public static string MergeToString(this IEnumerable collection) + + public static string MergeToString(this IEnumerable collection, TSep separator) { StringBuilder builder = new(); - foreach (T elem in collection) + using var e = collection.GetEnumerator(); + if(e.MoveNext()) + builder.Append(e.Current); + while (e.MoveNext()) + { + builder.Append(separator); + builder.Append(e.Current); + } + return builder.ToString(); + } + + public static string MergeToString(this IEnumerable collection) + { + StringBuilder builder = new(); + foreach (var elem in collection) builder.Append(elem); return builder.ToString(); } diff --git a/DTLib/Filesystem/File.cs b/DTLib/Filesystem/File.cs index 8ed8912..85b58ee 100644 --- a/DTLib/Filesystem/File.cs +++ b/DTLib/Filesystem/File.cs @@ -3,7 +3,7 @@ namespace DTLib.Filesystem; public static class File { - public static int GetSize(string file) => new System.IO.FileInfo(file).Length.ToInt(); + public static long GetSize(string file) => new System.IO.FileInfo(file).Length; public static bool Exists(string file) => System.IO.File.Exists(file); @@ -32,9 +32,10 @@ public static class File public static byte[] ReadAllBytes(string file) { using System.IO.FileStream stream = OpenRead(file); - int size = GetSize(file); + int size = GetSize(file).ToInt(); byte[] output = new byte[size]; - stream.Read(output, 0, size); + if (stream.Read(output, 0, size) < size) + throw new Exception("can't read all bytes"); stream.Close(); return output; } diff --git a/DTLib/Filesystem/New/Directory.cs b/DTLib/Filesystem/New/Directory.cs new file mode 100644 index 0000000..bb2a280 --- /dev/null +++ b/DTLib/Filesystem/New/Directory.cs @@ -0,0 +1,120 @@ +namespace DTLib.Filesystem.New; + +public static class Directory +{ + public static bool Exists(string dir) => System.IO.Directory.Exists(dir); + + // создает папку, если её не существует + public static void Create(string dir) + { + if (!Exists(dir)) + { + // проверяет существование папки, в которой нужно создать dir + if (dir.Contains(Путь.Разд) && !Exists(dir.Remove(dir.LastIndexOf(Путь.Разд)))) + Create(dir.Remove(dir.LastIndexOf(Путь.Разд))); + System.IO.Directory.CreateDirectory(dir); + } + } + // копирует все файлы и папки + public static void Copy(string source_dir, string new_dir, bool owerwrite = false) + { + Create(new_dir); + var subdirs = new List(); + List files = GetAllFiles(source_dir, ref subdirs); + for (int i = 0; i < subdirs.Count; i++) + Create(subdirs[i].Replace(source_dir, new_dir)); + for (int i = 0; i < files.Count; i++) + File.Copy(files[i], files[i].Replace(source_dir, new_dir), owerwrite); + } + + // копирует все файлы и папки и выдаёт список конфликтующих файлов + public static void Copy(string source_dir, string new_dir, out List conflicts, bool owerwrite = false) + { + conflicts = new List(); + var subdirs = new List(); + List files = GetAllFiles(source_dir, ref subdirs); + Create(new_dir); + for (int i = 0; i < subdirs.Count; i++) + Create(subdirs[i].Replace(source_dir, new_dir)); + for (int i = 0; i < files.Count; i++) + { + string newfile = files[i].Replace(source_dir, new_dir); + if (File.Exists(newfile)) + conflicts.Add(newfile); + File.Copy(files[i], newfile, owerwrite); + } + } + + // удаляет папку со всеми подпапками и файлами + public static void Delete(string dir) + { + var subdirs = new List(); + List files = GetAllFiles(dir, ref subdirs); + for (int i = 0; i < files.Count; i++) + File.Delete(files[i]); + for (int i = subdirs.Count - 1; i >= 0; i--) + { + Log($"deleting {subdirs[i]}"); + if (Exists(subdirs[i])) + System.IO.Directory.Delete(subdirs[i], true); + } + Log($"deleting {dir}"); + if (Exists(dir)) + System.IO.Directory.Delete(dir, true); + } + + public static string[] GetFiles(string dir) => System.IO.Directory.GetFiles(dir); + public static string[] GetFiles(string dir, string searchPattern) => System.IO.Directory.GetFiles(dir, searchPattern); + public static string[] GetDirectories(string dir) => System.IO.Directory.GetDirectories(dir); + + // выдает список всех файлов + public static List GetAllFiles(string dir) + { + var all_files = new List(); + string[] cur_files = GetFiles(dir); + for (int i = 0; i < cur_files.Length; i++) + all_files.Add(cur_files[i]); + string[] cur_subdirs = GetDirectories(dir); + for (int i = 0; i < cur_subdirs.Length; i++) + all_files.AddRange(GetAllFiles(cur_subdirs[i])); + return all_files; + } + + // выдает список всех файлов и подпапок в папке + public static List GetAllFiles(string dir, ref List all_subdirs) + { + var all_files = new List(); + string[] cur_files = GetFiles(dir); + for (int i = 0; i < cur_files.Length; i++) + all_files.Add(cur_files[i]); + string[] cur_subdirs = GetDirectories(dir); + for (int i = 0; i < cur_subdirs.Length; i++) + { + all_subdirs.Add(cur_subdirs[i]); + all_files.AddRange(GetAllFiles(cur_subdirs[i], ref all_subdirs)); + } + return all_files; + } + + public static string GetCurrent() => System.IO.Directory.GetCurrentDirectory(); + + public static void CreateSymlink(string sourceName, string symlinkName) + { + if (symlinkName.Contains(Путь.Разд)) + Create(symlinkName.Remove(symlinkName.LastIndexOf(Путь.Разд))); + if (!Symlink.CreateSymbolicLink(symlinkName, sourceName, Symlink.SymlinkTarget.Directory)) + throw new InvalidOperationException($"some error occured while creating symlink\nDirectory.CreateSymlink({symlinkName}, {sourceName})"); + } + + // copies directory with symlinks instead of files + public static int SymCopy(string srcdir, string newdir) + { + List files = GetAllFiles(srcdir); + if (!srcdir.EndsWith(Путь.Разд)) srcdir += Путь.Разд; + if (!newdir.EndsWith(Путь.Разд)) newdir += Путь.Разд; + int i = 0; + for (; i < files.Count; i++) + File.CreateSymlink(files[i], files[i].Replace(srcdir, newdir)); + return i; + } +} diff --git a/DTLib/Filesystem/New/File.cs b/DTLib/Filesystem/New/File.cs new file mode 100644 index 0000000..cd8274a --- /dev/null +++ b/DTLib/Filesystem/New/File.cs @@ -0,0 +1,62 @@ +using System.IO; + +namespace DTLib.Filesystem.New; + +public class File +{ + public enum FileOpenMode + { + // open a file for reading + Read=1, + // (re)create a file for writing + Write=2, + // opens file for writing additional data to the end / creates new file + Append=4, + // (re)creates file for reading/writing + ReadWrite=Read|Write, + // opens file for readng/writing additional data to the end / creates new file + ReadAppend=Read|Append + } + + public readonly FileOpenMode Mode; + public readonly FileStream Stream; + public string Name; + + public File(string path, FileOpenMode mode) + { + if(!Exists(path)) + { + if (mode == FileOpenMode.Read) + throw new Exception($"file <{path}> is not found"); + } + Mode = mode; + Stream = mode switch + { + FileOpenMode.Read => System.IO.File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), + FileOpenMode.Write => System.IO.File.Open(path, FileMode.Open, FileAccess.Write, FileShare.ReadWrite), + FileOpenMode.Append => System.IO.File.Open(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite), + FileOpenMode.ReadWrite => System.IO.File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite), + FileOpenMode.ReadAppend => System.IO.File.Open(path, FileMode.Append, FileAccess.ReadWrite, FileShare.ReadWrite), + _ => throw new Exception($"unknown file mode: {mode}") + }; + } + + public static bool Exists(string path) => System.IO.File.Exists(path); + + public static void Create(string path) + { + if (Exists(path)) + throw new Exception($"file <{path} already exists"); + int sepIndex = path.LastIndexOf(Path.Sep); + if (sepIndex>-1) + Directory.Create(path.Remove(sepIndex)); + System.IO.File.Create(path).Close(); + } + + public static void Delete(string path) => System.IO.File.Delete(path); + + public void Delete(string path) + { + + } +} \ No newline at end of file diff --git a/DTLib/Filesystem/New/Path.cs b/DTLib/Filesystem/New/Path.cs new file mode 100644 index 0000000..149892d --- /dev/null +++ b/DTLib/Filesystem/New/Path.cs @@ -0,0 +1,39 @@ +using System.Runtime.CompilerServices; + +namespace DTLib.Filesystem.New; + +public static class Path +{ + + public static readonly char Sep = Environment.OSVersion.Platform == PlatformID.Win32NT ? '\\' : '/'; + private static readonly char NotSep = Environment.OSVersion.Platform == PlatformID.Win32NT ? '/' : '\\' ; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static string Concat(string path, string addition) => $"{path}{Sep}{addition}"; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static string Concat(params string[] parts) => StringConverter.MergeToString(Sep, parts); + + public static string FixSeparators(string path) + { + var chars = path.ToCharArray(); + int length = path.Length; + for(int i=0; i FixSeparators(Concat(parts)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static string Resolve(string path, string addition) => FixSeparators(Concat(path, addition)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ThrowIfEscapes(string path) + { + if (path.Contains("..")) + throw new Exception($"path <{path}> uses <..>, that's not allowed"); + } +} \ No newline at end of file From 5c2638e2216d74fdb334163cd69528095f0a23bf Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Mon, 5 Dec 2022 21:17:27 +0600 Subject: [PATCH 2/4] Path.CorrectString --- DTLib/Filesystem/New/Directory.cs | 4 +-- DTLib/Filesystem/New/File.cs | 6 ++-- DTLib/Filesystem/New/Path.cs | 51 +++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/DTLib/Filesystem/New/Directory.cs b/DTLib/Filesystem/New/Directory.cs index bb2a280..9e92358 100644 --- a/DTLib/Filesystem/New/Directory.cs +++ b/DTLib/Filesystem/New/Directory.cs @@ -107,7 +107,7 @@ public static class Directory } // copies directory with symlinks instead of files - public static int SymCopy(string srcdir, string newdir) + /*public static int SymCopy(string srcdir, string newdir) { List files = GetAllFiles(srcdir); if (!srcdir.EndsWith(Путь.Разд)) srcdir += Путь.Разд; @@ -116,5 +116,5 @@ public static class Directory for (; i < files.Count; i++) File.CreateSymlink(files[i], files[i].Replace(srcdir, newdir)); return i; - } + }*/ } diff --git a/DTLib/Filesystem/New/File.cs b/DTLib/Filesystem/New/File.cs index cd8274a..5aa4933 100644 --- a/DTLib/Filesystem/New/File.cs +++ b/DTLib/Filesystem/New/File.cs @@ -55,8 +55,10 @@ public class File public static void Delete(string path) => System.IO.File.Delete(path); - public void Delete(string path) + public static void Copy(string srcPath, string newPath, bool replace = false) { - + System.IO.File.Copy(srcPath, newPath, replace); } + + // public void Delete(string path) } \ No newline at end of file diff --git a/DTLib/Filesystem/New/Path.cs b/DTLib/Filesystem/New/Path.cs index 149892d..607e594 100644 --- a/DTLib/Filesystem/New/Path.cs +++ b/DTLib/Filesystem/New/Path.cs @@ -36,4 +36,55 @@ public static class Path if (path.Contains("..")) throw new Exception($"path <{path}> uses <..>, that's not allowed"); } + + /// Replaces restricted characters in string + public static string CorrectString(string str) + { +#if NETSTANDARD2_1 || NET6_0 || NET7_0 || NET8_0 + var a = str.AsSpan(); +#else + var a = str.ToArray(); +#endif + char[] r = new char[a.Length]; + for (int i = 0; i < a.Length; i++) + { + switch (a[i]) + { + case '/': + case '\\': + case ':': + case ';': + r[i] = '-'; + break; + case '\n': + case '\r': + case ' ': + case '#': + case '%': + case '&': + case '{': + case '}': + case '<': + case '>': + case '*': + case '?': + case '$': + case '!': + case '\'': + case '"': + case '@': + case '+': + case '`': + case '|': + case '=': + r[i] = '_'; + break; + default: + r[i] = a[i]; + break; + } + } + + return new string(r); + } } \ No newline at end of file From 4e5190b91d9e54a636d1869c252cca2bfa8c7cb2 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Mon, 5 Dec 2022 21:32:25 +0600 Subject: [PATCH 3/4] StingConverter merged --- DTLib/Extensions/StringConverter.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/DTLib/Extensions/StringConverter.cs b/DTLib/Extensions/StringConverter.cs index 80d94b1..40f5db0 100644 --- a/DTLib/Extensions/StringConverter.cs +++ b/DTLib/Extensions/StringConverter.cs @@ -2,11 +2,10 @@ public static class StringConverter { - public static ASCIIEncoding ASCII = new ASCIIEncoding(); public static Encoding UTF8 = new UTF8Encoding(false); public static Encoding UTF8BOM = new UTF8Encoding(true); - public static byte[] ToBytes(this string str) => UTF8.GetBytes(str); - public static string BytesToString(this byte[] bytes) => UTF8.GetString(bytes); + public static byte[] ToBytes(this string str, Encoding encoding) => encoding.GetBytes(str); + public static string BytesToString(this byte[] bytes, Encoding encoding) => encoding.GetString(bytes); // хеш в виде массива байт в строку (хеш изначально не в кодировке UTF8, так что метод выше не работает с ним) public static string HashToString(this byte[] hash) @@ -44,6 +43,10 @@ public static class StringConverter public static bool StartsWith(this string s, char c) => s[0] == c; public static bool EndsWith(this string s, char c) => s[s.Length - 1] == c; + // String.Join(sep,string...) does some low-level memory manipulations, that are faster than StringBuilder + public static string MergeToString(params string[] parts) + =>string.Join(null, parts); + public static string MergeToString(params TVal[] parts) { if (parts.Length == 0) From 92997c9325869fd29c68703ce54a494acd7cba5e Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Mon, 5 Dec 2022 22:08:48 +0600 Subject: [PATCH 4/4] Path --- DTLib.Logging/Loggers/FileLogger.cs | 2 +- DTLib.Network/FSP.cs | 22 ++-- DTLib.Tests/Dtsod/TestDtsodV23.cs | 12 +- DTLib.Tests/Dtsod/TestDtsodV24.cs | 12 +- DTLib/DependencyResolver.cs | 6 +- .../File.cs => Experimental/FileInstance.cs} | 31 +---- DTLib/Filesystem/Directory.cs | 12 +- DTLib/Filesystem/File.cs | 8 +- DTLib/Filesystem/New/Directory.cs | 120 ------------------ DTLib/Filesystem/{New => }/Path.cs | 36 ++---- DTLib/Filesystem/Путь.cs | 31 ----- DTLib/Logging/FileLogger.cs | 2 +- 12 files changed, 54 insertions(+), 240 deletions(-) rename DTLib/{Filesystem/New/File.cs => Experimental/FileInstance.cs} (64%) delete mode 100644 DTLib/Filesystem/New/Directory.cs rename DTLib/Filesystem/{New => }/Path.cs (72%) delete mode 100644 DTLib/Filesystem/Путь.cs diff --git a/DTLib.Logging/Loggers/FileLogger.cs b/DTLib.Logging/Loggers/FileLogger.cs index 0736c21..dc8f1d6 100644 --- a/DTLib.Logging/Loggers/FileLogger.cs +++ b/DTLib.Logging/Loggers/FileLogger.cs @@ -27,7 +27,7 @@ public class FileLogger : ILogger {} public FileLogger(string dir, string programName, ILogFormat format) - : this($"{dir}{Путь.Разд}{programName}_{DateTime.Now.ToString(MyTimeFormat.ForFileNames)}.log", format) + : this($"{dir}{Path.Sep}{programName}_{DateTime.Now.ToString(MyTimeFormat.ForFileNames)}.log", format) {} public FileLogger(string dir, string programName) : this(dir, programName, new DefaultLogFormat()) diff --git a/DTLib.Network/FSP.cs b/DTLib.Network/FSP.cs index d418497..b424b00 100644 --- a/DTLib.Network/FSP.cs +++ b/DTLib.Network/FSP.cs @@ -18,8 +18,8 @@ public class FSP // скачивает файл с помощью FSP протокола public void DownloadFile(string filePath_server, string filePath_client) { - Путь.Предупредить(filePath_server); - Путь.Предупредить(filePath_client); + Path.ThrowIfEscapes(filePath_server); + Path.ThrowIfEscapes(filePath_client); lock (MainSocket) { Debug("b", $"requesting file download: {filePath_server}"); @@ -31,7 +31,7 @@ public class FSP public void DownloadFile(string filePath_client) { - Путь.Предупредить(filePath_client); + Path.ThrowIfEscapes(filePath_client); using System.IO.Stream fileStream = File.OpenWrite(filePath_client); Download_SharedCode(fileStream, true); fileStream.Close(); @@ -40,7 +40,7 @@ public class FSP public byte[] DownloadFileToMemory(string filePath_server) { - Путь.Предупредить(filePath_server); + Path.ThrowIfEscapes(filePath_server); lock (MainSocket) { Debug("b", $"requesting file download: {filePath_server}"); @@ -102,7 +102,7 @@ public class FSP // отдаёт файл с помощью FSP протокола public void UploadFile(string filePath) { - Путь.Предупредить(filePath); + Path.ThrowIfEscapes(filePath); BytesUploaded = 0; Debug("b", $"uploading file {filePath}"); using System.IO.FileStream fileStream = File.OpenRead(filePath); @@ -137,10 +137,10 @@ public class FSP public void DownloadByManifest(string dirOnServer, string dirOnClient, bool overwrite = false, bool delete_excess = false) { - if (!dirOnClient.EndsWith(Путь.Разд)) - dirOnClient += Путь.Разд; - if (!dirOnServer.EndsWith(Путь.Разд)) - dirOnServer += Путь.Разд; + if (!dirOnClient.EndsWith(Path.Sep)) + dirOnClient += Path.Sep; + if (!dirOnServer.EndsWith(Path.Sep)) + dirOnServer += Path.Sep; Debug("b", "downloading manifest <", "c", dirOnServer + "manifest.dtsod", "b", ">"); var manifest = new DtsodV23(DownloadFileToMemory(dirOnServer + "manifest.dtsod").BytesToString(StringConverter.UTF8)); Debug("g", $"found {manifest.Values.Count} files in manifest"); @@ -183,8 +183,8 @@ public class FSP Log("y", $"can't create manifest, dir <{dir}> doesn't exist"); return; } - if (!dir.EndsWith(Путь.Разд)) - dir += Путь.Разд; + if (!dir.EndsWith(Path.Sep)) + dir += Path.Sep; Log($"b", $"creating manifest of {dir}"); StringBuilder manifestBuilder = new(); Hasher hasher = new(); diff --git a/DTLib.Tests/Dtsod/TestDtsodV23.cs b/DTLib.Tests/Dtsod/TestDtsodV23.cs index 5f7ac9a..82c5a33 100644 --- a/DTLib.Tests/Dtsod/TestDtsodV23.cs +++ b/DTLib.Tests/Dtsod/TestDtsodV23.cs @@ -17,7 +17,7 @@ public static class TestDtsodV23 public static void TestBaseTypes() { OldLogger.Log("c", "-----[TestDtsodV23/TestBaseTypes]-----"); - DtsodV23 dtsod = new(File.ReadAllText($"Dtsod{Путь.Разд}TestResources{Путь.Разд}DtsodV23{Путь.Разд}base_types.dtsod")); + DtsodV23 dtsod = new(File.ReadAllText($"Dtsod{Path.Sep}TestResources{Path.Sep}DtsodV23{Path.Sep}base_types.dtsod")); foreach (var pair in dtsod) OldLogger.Log("b", pair.Value.GetType().Name + ' ', "w", pair.Key + ' ', "c", pair.Value.ToString()); OldLogger.Log("g", "test completed"); @@ -25,7 +25,7 @@ public static class TestDtsodV23 public static void TestLists() { OldLogger.Log("c", "-------[TestDtsodV23/TestLists]-------"); - DtsodV23 dtsod = new(File.ReadAllText($"Dtsod{Путь.Разд}TestResources{Путь.Разд}DtsodV23{Путь.Разд}lists.dtsod")); + DtsodV23 dtsod = new(File.ReadAllText($"Dtsod{Path.Sep}TestResources{Path.Sep}DtsodV23{Path.Sep}lists.dtsod")); foreach (var pair in dtsod) { OldLogger.Log("b", pair.Value.GetType().Name + ' ', "w", pair.Key, "c", @@ -39,7 +39,7 @@ public static class TestDtsodV23 public static void TestComplexes() { OldLogger.Log("c", "-----[TestDtsodV23/TestComplexes]-----"); - DtsodV23 dtsod = new(File.ReadAllText($"Dtsod{Путь.Разд}TestResources{Путь.Разд}DtsodV23{Путь.Разд}complexes.dtsod")); + DtsodV23 dtsod = new(File.ReadAllText($"Dtsod{Path.Sep}TestResources{Path.Sep}DtsodV23{Path.Sep}complexes.dtsod")); foreach (var complex in dtsod) { OldLogger.Log("b", complex.Value.GetType().Name + ' ', "w", complex.Key, @@ -55,7 +55,7 @@ public static class TestDtsodV23 { OldLogger.Log("c", "--[TestDtsodV23/TestReSerialization]--"); var dtsod = new DtsodV23(new DtsodV23(new DtsodV23( - new DtsodV23(File.ReadAllText($"Dtsod{Путь.Разд}TestResources{Путь.Разд}DtsodV23{Путь.Разд}complexes.dtsod")).ToString()).ToString()).ToString()); + new DtsodV23(File.ReadAllText($"Dtsod{Path.Sep}TestResources{Path.Sep}DtsodV23{Path.Sep}complexes.dtsod")).ToString()).ToString()).ToString()); OldLogger.Log("y", dtsod.ToString()); OldLogger.Log("g", "test completed"); } @@ -64,7 +64,7 @@ public static class TestDtsodV23 { OldLogger.Log("c", "-------[TestDtsodV23/TestSpeed]-------"); IDtsod dtsod=null; - string text = File.ReadAllText($"Dtsod{Путь.Разд}TestResources{Путь.Разд}DtsodV23{Путь.Разд}messages.dtsod"); + string text = File.ReadAllText($"Dtsod{Path.Sep}TestResources{Path.Sep}DtsodV23{Path.Sep}messages.dtsod"); LogOperationTime("V21 deserialization",64,()=>dtsod=new DtsodV21(text)); LogOperationTime("V21 serialization", 64, () => _=dtsod.ToString()); LogOperationTime("V23 deserialization", 64, () => dtsod = new DtsodV23(text)); @@ -75,7 +75,7 @@ public static class TestDtsodV23 public static void TestMemoryConsumption() { OldLogger.Log("c", "----[TestDtsodV23/TestMemConsumpt]----"); - string text = File.ReadAllText($"Dtsod{Путь.Разд}TestResources{Путь.Разд}DtsodV23{Путь.Разд}messages.dtsod"); + string text = File.ReadAllText($"Dtsod{Path.Sep}TestResources{Path.Sep}DtsodV23{Path.Sep}messages.dtsod"); var a = GC.GetTotalMemory(true); var dtsods = new DtsodV23[64]; for (int i = 0; i < dtsods.Length; i++) diff --git a/DTLib.Tests/Dtsod/TestDtsodV24.cs b/DTLib.Tests/Dtsod/TestDtsodV24.cs index 5e840bb..3861c36 100644 --- a/DTLib.Tests/Dtsod/TestDtsodV24.cs +++ b/DTLib.Tests/Dtsod/TestDtsodV24.cs @@ -18,7 +18,7 @@ public static class TestDtsodV24 public static void TestBaseTypes() { OldLogger.Log("c", "-----[TestDtsodV24/TestBaseTypes]-----"); - DtsodV24 dtsod = new(File.ReadAllText($"Dtsod{Путь.Разд}TestResources{Путь.Разд}DtsodV24{Путь.Разд}base_types.dtsod")); + DtsodV24 dtsod = new(File.ReadAllText($"Dtsod{Path.Sep}TestResources{Path.Sep}DtsodV24{Path.Sep}base_types.dtsod")); foreach (var pair in dtsod) OldLogger.Log("b", pair.ToString()); OldLogger.Log("g", "test completed"); @@ -27,7 +27,7 @@ public static class TestDtsodV24 public static void TestComplexes() { OldLogger.Log("c", "-----[TestDtsodV24/TestComplexes]-----"); - DtsodV24 dtsod = new(File.ReadAllText($"Dtsod{Путь.Разд}TestResources{Путь.Разд}DtsodV24{Путь.Разд}complexes.dtsod")); + DtsodV24 dtsod = new(File.ReadAllText($"Dtsod{Path.Sep}TestResources{Path.Sep}DtsodV24{Path.Sep}complexes.dtsod")); OldLogger.Log("h", dtsod.ToString()); OldLogger.Log("g", "test completed"); } @@ -35,7 +35,7 @@ public static class TestDtsodV24 public static void TestLists() { OldLogger.Log("c", "-------[TestDtsodV24/TestLists]-------"); - DtsodV24 dtsod = new(File.ReadAllText($"Dtsod{Путь.Разд}TestResources{Путь.Разд}DtsodV24{Путь.Разд}lists.dtsod")); + DtsodV24 dtsod = new(File.ReadAllText($"Dtsod{Path.Sep}TestResources{Path.Sep}DtsodV24{Path.Sep}lists.dtsod")); foreach (KVPair pair in dtsod) { var list = new Autoarr(pair.value.VoidPtr, false); @@ -60,7 +60,7 @@ public static class TestDtsodV24 { OldLogger.Log("c", "--[TestDtsodV24/TestReSerialization]--"); var dtsod = new DtsodV24(new DtsodV24(new DtsodV24( - new DtsodV24(File.ReadAllText($"Dtsod{Путь.Разд}TestResources{Путь.Разд}DtsodV24{Путь.Разд}complexes.dtsod")).ToString()).ToString()).ToString()); + new DtsodV24(File.ReadAllText($"Dtsod{Path.Sep}TestResources{Path.Sep}DtsodV24{Path.Sep}complexes.dtsod")).ToString()).ToString()).ToString()); OldLogger.Log("h", dtsod.ToString()); OldLogger.Log("g", "test completed"); } @@ -69,12 +69,12 @@ public static class TestDtsodV24 { OldLogger.Log("c", "-------[TestDtsodV24/TestSpeed]-------"); IDtsod dtsod=null; - string _text = File.ReadAllText($"Dtsod{Путь.Разд}TestResources{Путь.Разд}DtsodV23{Путь.Разд}messages.dtsod"); + string _text = File.ReadAllText($"Dtsod{Path.Sep}TestResources{Path.Sep}DtsodV23{Path.Sep}messages.dtsod"); string text = ""; LogOperationTime( "V23 to V24 conversion", 32, ()=> text = DtsodConverter.ConvertVersion(new DtsodV23(_text), DtsodVersion.V24).ToString() ); - File.WriteAllText($"Dtsod{Путь.Разд}TestResources{Путь.Разд}DtsodV24{Путь.Разд}messages.dtsod",text); + File.WriteAllText($"Dtsod{Path.Sep}TestResources{Path.Sep}DtsodV24{Path.Sep}messages.dtsod",text); LogOperationTime("V24 deserialization", 64, () => dtsod = new DtsodV24(text)); LogOperationTime("V24 serialization", 64, () => text = dtsod.ToString()); OldLogger.Log("g", "test completed"); diff --git a/DTLib/DependencyResolver.cs b/DTLib/DependencyResolver.cs index fc43bb2..b9f5f63 100644 --- a/DTLib/DependencyResolver.cs +++ b/DTLib/DependencyResolver.cs @@ -9,14 +9,14 @@ public static class DependencyResolver public static void CopyLibs() { if(DepsCopied) return; - string depsdir = $"Dependencies{Путь.Разд}"; + string depsdir = $"Dependencies{Path.Sep}"; depsdir += Environment.OSVersion.Platform switch { PlatformID.Unix => "linux", PlatformID.Win32NT => "windows", _=> throw new Exception($"unsupported os {Environment.OSVersion.Platform}") }; - depsdir += Путь.Разд; + depsdir += Path.Sep; depsdir += RuntimeInformation.ProcessArchitecture switch { Architecture.X64 => "x64", @@ -27,7 +27,7 @@ public static class DependencyResolver }; foreach (var file in Directory.GetAllFiles(depsdir)) { - var extracted = file.Substring(file.LastIndexOf(Путь.Разд) + 1); + var extracted = file.Substring(file.LastIndexOf(Path.Sep) + 1); File.Copy(file,extracted, true); Log("g",$"{extracted} copied"); } diff --git a/DTLib/Filesystem/New/File.cs b/DTLib/Experimental/FileInstance.cs similarity index 64% rename from DTLib/Filesystem/New/File.cs rename to DTLib/Experimental/FileInstance.cs index 5aa4933..a457fbd 100644 --- a/DTLib/Filesystem/New/File.cs +++ b/DTLib/Experimental/FileInstance.cs @@ -1,8 +1,8 @@ using System.IO; -namespace DTLib.Filesystem.New; +namespace DTLib.Experimental; -public class File +public class FileInstance { public enum FileOpenMode { @@ -22,9 +22,11 @@ public class File public readonly FileStream Stream; public string Name; - public File(string path, FileOpenMode mode) + public FileInstance(string path, FileOpenMode mode) { - if(!Exists(path)) + if (path.IsNullOrEmpty()) + throw new NullReferenceException("path is null"); + if(!System.IO.File.Exists(path)) { if (mode == FileOpenMode.Read) throw new Exception($"file <{path}> is not found"); @@ -40,25 +42,4 @@ public class File _ => throw new Exception($"unknown file mode: {mode}") }; } - - public static bool Exists(string path) => System.IO.File.Exists(path); - - public static void Create(string path) - { - if (Exists(path)) - throw new Exception($"file <{path} already exists"); - int sepIndex = path.LastIndexOf(Path.Sep); - if (sepIndex>-1) - Directory.Create(path.Remove(sepIndex)); - System.IO.File.Create(path).Close(); - } - - public static void Delete(string path) => System.IO.File.Delete(path); - - public static void Copy(string srcPath, string newPath, bool replace = false) - { - System.IO.File.Copy(srcPath, newPath, replace); - } - - // public void Delete(string path) } \ No newline at end of file diff --git a/DTLib/Filesystem/Directory.cs b/DTLib/Filesystem/Directory.cs index 8ed3de6..bf7158a 100644 --- a/DTLib/Filesystem/Directory.cs +++ b/DTLib/Filesystem/Directory.cs @@ -10,8 +10,8 @@ public static class Directory if (!Exists(dir)) { // проверяет существование папки, в которой нужно создать dir - if (dir.Contains(Путь.Разд) && !Exists(dir.Remove(dir.LastIndexOf(Путь.Разд)))) - Create(dir.Remove(dir.LastIndexOf(Путь.Разд))); + if (dir.Contains(Path.Sep) && !Exists(dir.Remove(dir.LastIndexOf(Path.Sep)))) + Create(dir.Remove(dir.LastIndexOf(Path.Sep))); System.IO.Directory.CreateDirectory(dir); } } @@ -100,8 +100,8 @@ public static class Directory public static void CreateSymlink(string sourceName, string symlinkName) { - if (symlinkName.Contains(Путь.Разд)) - Create(symlinkName.Remove(symlinkName.LastIndexOf(Путь.Разд))); + if (symlinkName.Contains(Path.Sep)) + Create(symlinkName.Remove(symlinkName.LastIndexOf(Path.Sep))); if (!Symlink.CreateSymbolicLink(symlinkName, sourceName, Symlink.SymlinkTarget.Directory)) throw new InvalidOperationException($"some error occured while creating symlink\nDirectory.CreateSymlink({symlinkName}, {sourceName})"); } @@ -110,8 +110,8 @@ public static class Directory public static int SymCopy(string srcdir, string newdir) { List files = GetAllFiles(srcdir); - if (!srcdir.EndsWith(Путь.Разд)) srcdir += Путь.Разд; - if (!newdir.EndsWith(Путь.Разд)) newdir += Путь.Разд; + if (!srcdir.EndsWith(Path.Sep)) srcdir += Path.Sep; + if (!newdir.EndsWith(Path.Sep)) newdir += Path.Sep; int i = 0; for (; i < files.Count; i++) File.CreateSymlink(files[i], files[i].Replace(srcdir, newdir)); diff --git a/DTLib/Filesystem/File.cs b/DTLib/Filesystem/File.cs index 76a0b84..1be3f16 100644 --- a/DTLib/Filesystem/File.cs +++ b/DTLib/Filesystem/File.cs @@ -12,8 +12,8 @@ public static class File { if (!Exists(file)) { - if (file.Contains(Путь.Разд)) - Directory.Create(file.Remove(file.LastIndexOf(Путь.Разд))); + if (file.Contains(Path.Sep)) + Directory.Create(file.Remove(file.LastIndexOf(Path.Sep))); using System.IO.FileStream stream = System.IO.File.Create(file); stream.Close(); } @@ -79,8 +79,8 @@ public static class File public static void CreateSymlink(string sourceName, string symlinkName) { - if (symlinkName.Contains(Путь.Разд)) - Directory.Create(symlinkName.Remove(symlinkName.LastIndexOf(Путь.Разд))); + if (symlinkName.Contains(Path.Sep)) + Directory.Create(symlinkName.Remove(symlinkName.LastIndexOf(Path.Sep))); if (!Symlink.CreateSymbolicLink(symlinkName, sourceName, Symlink.SymlinkTarget.File)) throw new InvalidOperationException($"some error occured while creating symlink\nFile.CreateSymlink({symlinkName}, {sourceName})"); } diff --git a/DTLib/Filesystem/New/Directory.cs b/DTLib/Filesystem/New/Directory.cs deleted file mode 100644 index 9e92358..0000000 --- a/DTLib/Filesystem/New/Directory.cs +++ /dev/null @@ -1,120 +0,0 @@ -namespace DTLib.Filesystem.New; - -public static class Directory -{ - public static bool Exists(string dir) => System.IO.Directory.Exists(dir); - - // создает папку, если её не существует - public static void Create(string dir) - { - if (!Exists(dir)) - { - // проверяет существование папки, в которой нужно создать dir - if (dir.Contains(Путь.Разд) && !Exists(dir.Remove(dir.LastIndexOf(Путь.Разд)))) - Create(dir.Remove(dir.LastIndexOf(Путь.Разд))); - System.IO.Directory.CreateDirectory(dir); - } - } - // копирует все файлы и папки - public static void Copy(string source_dir, string new_dir, bool owerwrite = false) - { - Create(new_dir); - var subdirs = new List(); - List files = GetAllFiles(source_dir, ref subdirs); - for (int i = 0; i < subdirs.Count; i++) - Create(subdirs[i].Replace(source_dir, new_dir)); - for (int i = 0; i < files.Count; i++) - File.Copy(files[i], files[i].Replace(source_dir, new_dir), owerwrite); - } - - // копирует все файлы и папки и выдаёт список конфликтующих файлов - public static void Copy(string source_dir, string new_dir, out List conflicts, bool owerwrite = false) - { - conflicts = new List(); - var subdirs = new List(); - List files = GetAllFiles(source_dir, ref subdirs); - Create(new_dir); - for (int i = 0; i < subdirs.Count; i++) - Create(subdirs[i].Replace(source_dir, new_dir)); - for (int i = 0; i < files.Count; i++) - { - string newfile = files[i].Replace(source_dir, new_dir); - if (File.Exists(newfile)) - conflicts.Add(newfile); - File.Copy(files[i], newfile, owerwrite); - } - } - - // удаляет папку со всеми подпапками и файлами - public static void Delete(string dir) - { - var subdirs = new List(); - List files = GetAllFiles(dir, ref subdirs); - for (int i = 0; i < files.Count; i++) - File.Delete(files[i]); - for (int i = subdirs.Count - 1; i >= 0; i--) - { - Log($"deleting {subdirs[i]}"); - if (Exists(subdirs[i])) - System.IO.Directory.Delete(subdirs[i], true); - } - Log($"deleting {dir}"); - if (Exists(dir)) - System.IO.Directory.Delete(dir, true); - } - - public static string[] GetFiles(string dir) => System.IO.Directory.GetFiles(dir); - public static string[] GetFiles(string dir, string searchPattern) => System.IO.Directory.GetFiles(dir, searchPattern); - public static string[] GetDirectories(string dir) => System.IO.Directory.GetDirectories(dir); - - // выдает список всех файлов - public static List GetAllFiles(string dir) - { - var all_files = new List(); - string[] cur_files = GetFiles(dir); - for (int i = 0; i < cur_files.Length; i++) - all_files.Add(cur_files[i]); - string[] cur_subdirs = GetDirectories(dir); - for (int i = 0; i < cur_subdirs.Length; i++) - all_files.AddRange(GetAllFiles(cur_subdirs[i])); - return all_files; - } - - // выдает список всех файлов и подпапок в папке - public static List GetAllFiles(string dir, ref List all_subdirs) - { - var all_files = new List(); - string[] cur_files = GetFiles(dir); - for (int i = 0; i < cur_files.Length; i++) - all_files.Add(cur_files[i]); - string[] cur_subdirs = GetDirectories(dir); - for (int i = 0; i < cur_subdirs.Length; i++) - { - all_subdirs.Add(cur_subdirs[i]); - all_files.AddRange(GetAllFiles(cur_subdirs[i], ref all_subdirs)); - } - return all_files; - } - - public static string GetCurrent() => System.IO.Directory.GetCurrentDirectory(); - - public static void CreateSymlink(string sourceName, string symlinkName) - { - if (symlinkName.Contains(Путь.Разд)) - Create(symlinkName.Remove(symlinkName.LastIndexOf(Путь.Разд))); - if (!Symlink.CreateSymbolicLink(symlinkName, sourceName, Symlink.SymlinkTarget.Directory)) - throw new InvalidOperationException($"some error occured while creating symlink\nDirectory.CreateSymlink({symlinkName}, {sourceName})"); - } - - // copies directory with symlinks instead of files - /*public static int SymCopy(string srcdir, string newdir) - { - List files = GetAllFiles(srcdir); - if (!srcdir.EndsWith(Путь.Разд)) srcdir += Путь.Разд; - if (!newdir.EndsWith(Путь.Разд)) newdir += Путь.Разд; - int i = 0; - for (; i < files.Count; i++) - File.CreateSymlink(files[i], files[i].Replace(srcdir, newdir)); - return i; - }*/ -} diff --git a/DTLib/Filesystem/New/Path.cs b/DTLib/Filesystem/Path.cs similarity index 72% rename from DTLib/Filesystem/New/Path.cs rename to DTLib/Filesystem/Path.cs index 607e594..864fd4e 100644 --- a/DTLib/Filesystem/New/Path.cs +++ b/DTLib/Filesystem/Path.cs @@ -1,6 +1,6 @@ using System.Runtime.CompilerServices; -namespace DTLib.Filesystem.New; +namespace DTLib.Filesystem; public static class Path { @@ -50,33 +50,17 @@ public static class Path { switch (a[i]) { - case '/': - case '\\': - case ':': - case ';': + case '/': case '\\': + case ':': case ';': r[i] = '-'; break; - case '\n': - case '\r': - case ' ': - case '#': - case '%': - case '&': - case '{': - case '}': - case '<': - case '>': - case '*': - case '?': - case '$': - case '!': - case '\'': - case '"': - case '@': - case '+': - case '`': - case '|': - case '=': + case '\n': case '\r': case ' ': + case '#': case '%': case '&': + case '{': case '}': case '<': + case '>': case '*': case '?': + case '$': case '!': case '\'': + case '"': case '@': case '+': + case '`': case '|': case '=': r[i] = '_'; break; default: diff --git a/DTLib/Filesystem/Путь.cs b/DTLib/Filesystem/Путь.cs deleted file mode 100644 index 7e8baf1..0000000 --- a/DTLib/Filesystem/Путь.cs +++ /dev/null @@ -1,31 +0,0 @@ -namespace DTLib.Filesystem; - -public static class Путь -{ - public static readonly char Разд = Environment.OSVersion.Platform == PlatformID.Win32NT ? '\\' : '/'; - - public static string ИсправитьРазд(this string путь) - { - if (Разд == '\\') - { - if (путь.Contains('/')) - путь = путь.Replace('/', '\\'); - } - else - { - if (путь.Contains('\\')) - путь = путь.Replace('\\', '/'); - } - return путь; - } - - // replaces wrong characters to use string as путь - public static string НормализоватьДляПути(this string путь) => - путь.Replace(':', '-').Replace(' ', '_'); - - public static void Предупредить(string может_быть_с_точками) - { - if(может_быть_с_точками.Contains("..")) - throw new Exception($"лее точки двойные убери: {может_быть_с_точками}"); - } -} \ No newline at end of file diff --git a/DTLib/Logging/FileLogger.cs b/DTLib/Logging/FileLogger.cs index eb674aa..b0c5ade 100644 --- a/DTLib/Logging/FileLogger.cs +++ b/DTLib/Logging/FileLogger.cs @@ -9,7 +9,7 @@ public class FileLogger : IDisposable } public FileLogger(string dir, string programName) - : this($"{dir}{Путь.Разд}{programName}_{DateTime.Now.ToString(MyTimeFormat.ForFileNames)}.log") { } + : this($"{dir}{Path.Sep}{programName}_{DateTime.Now.ToString(MyTimeFormat.ForFileNames)}.log") { } public string LogfileName { get; protected set; } public System.IO.FileStream LogfileStream { get; protected set; }