Merge pull request #2 from Timerix22/new_filesystem

Path class
This commit is contained in:
Timerix22 2022-12-05 22:10:44 +06:00 committed by GitHub
commit e98534a586
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 194 additions and 89 deletions

View File

@ -27,7 +27,7 @@ public class FileLogger : ILogger
{} {}
public FileLogger(string dir, string programName, ILogFormat format) 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()) public FileLogger(string dir, string programName) : this(dir, programName, new DefaultLogFormat())

View File

@ -18,8 +18,8 @@ public class FSP
// скачивает файл с помощью FSP протокола // скачивает файл с помощью FSP протокола
public void DownloadFile(string filePath_server, string filePath_client) public void DownloadFile(string filePath_server, string filePath_client)
{ {
Путь.Предупредить(filePath_server); Path.ThrowIfEscapes(filePath_server);
Путь.Предупредить(filePath_client); Path.ThrowIfEscapes(filePath_client);
lock (MainSocket) lock (MainSocket)
{ {
Debug("b", $"requesting file download: {filePath_server}"); Debug("b", $"requesting file download: {filePath_server}");
@ -31,7 +31,7 @@ public class FSP
public void DownloadFile(string filePath_client) public void DownloadFile(string filePath_client)
{ {
Путь.Предупредить(filePath_client); Path.ThrowIfEscapes(filePath_client);
using System.IO.Stream fileStream = File.OpenWrite(filePath_client); using System.IO.Stream fileStream = File.OpenWrite(filePath_client);
Download_SharedCode(fileStream, true); Download_SharedCode(fileStream, true);
fileStream.Close(); fileStream.Close();
@ -40,7 +40,7 @@ public class FSP
public byte[] DownloadFileToMemory(string filePath_server) public byte[] DownloadFileToMemory(string filePath_server)
{ {
Путь.Предупредить(filePath_server); Path.ThrowIfEscapes(filePath_server);
lock (MainSocket) lock (MainSocket)
{ {
Debug("b", $"requesting file download: {filePath_server}"); Debug("b", $"requesting file download: {filePath_server}");
@ -102,7 +102,7 @@ public class FSP
// отдаёт файл с помощью FSP протокола // отдаёт файл с помощью FSP протокола
public void UploadFile(string filePath) public void UploadFile(string filePath)
{ {
Путь.Предупредить(filePath); Path.ThrowIfEscapes(filePath);
BytesUploaded = 0; BytesUploaded = 0;
Debug("b", $"uploading file {filePath}"); Debug("b", $"uploading file {filePath}");
using System.IO.FileStream fileStream = File.OpenRead(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) public void DownloadByManifest(string dirOnServer, string dirOnClient, bool overwrite = false, bool delete_excess = false)
{ {
if (!dirOnClient.EndsWith(Путь.Разд)) if (!dirOnClient.EndsWith(Path.Sep))
dirOnClient += Путь.Разд; dirOnClient += Path.Sep;
if (!dirOnServer.EndsWith(Путь.Разд)) if (!dirOnServer.EndsWith(Path.Sep))
dirOnServer += Путь.Разд; dirOnServer += Path.Sep;
Debug("b", "downloading manifest <", "c", dirOnServer + "manifest.dtsod", "b", ">"); Debug("b", "downloading manifest <", "c", dirOnServer + "manifest.dtsod", "b", ">");
var manifest = new DtsodV23(DownloadFileToMemory(dirOnServer + "manifest.dtsod").BytesToString(StringConverter.UTF8)); var manifest = new DtsodV23(DownloadFileToMemory(dirOnServer + "manifest.dtsod").BytesToString(StringConverter.UTF8));
Debug("g", $"found {manifest.Values.Count} files in manifest"); 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"); Log("y", $"can't create manifest, dir <{dir}> doesn't exist");
return; return;
} }
if (!dir.EndsWith(Путь.Разд)) if (!dir.EndsWith(Path.Sep))
dir += Путь.Разд; dir += Path.Sep;
Log($"b", $"creating manifest of {dir}"); Log($"b", $"creating manifest of {dir}");
StringBuilder manifestBuilder = new(); StringBuilder manifestBuilder = new();
Hasher hasher = new(); Hasher hasher = new();

View File

@ -17,7 +17,7 @@ public static class TestDtsodV23
public static void TestBaseTypes() public static void TestBaseTypes()
{ {
OldLogger.Log("c", "-----[TestDtsodV23/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) foreach (var pair in dtsod)
OldLogger.Log("b", pair.Value.GetType().Name + ' ', "w", pair.Key + ' ', "c", pair.Value.ToString()); OldLogger.Log("b", pair.Value.GetType().Name + ' ', "w", pair.Key + ' ', "c", pair.Value.ToString());
OldLogger.Log("g", "test completed"); OldLogger.Log("g", "test completed");
@ -25,7 +25,7 @@ public static class TestDtsodV23
public static void TestLists() public static void TestLists()
{ {
OldLogger.Log("c", "-------[TestDtsodV23/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) foreach (var pair in dtsod)
{ {
OldLogger.Log("b", pair.Value.GetType().Name + ' ', "w", pair.Key, "c", OldLogger.Log("b", pair.Value.GetType().Name + ' ', "w", pair.Key, "c",
@ -39,7 +39,7 @@ public static class TestDtsodV23
public static void TestComplexes() public static void TestComplexes()
{ {
OldLogger.Log("c", "-----[TestDtsodV23/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) foreach (var complex in dtsod)
{ {
OldLogger.Log("b", complex.Value.GetType().Name + ' ', "w", complex.Key, OldLogger.Log("b", complex.Value.GetType().Name + ' ', "w", complex.Key,
@ -55,7 +55,7 @@ public static class TestDtsodV23
{ {
OldLogger.Log("c", "--[TestDtsodV23/TestReSerialization]--"); OldLogger.Log("c", "--[TestDtsodV23/TestReSerialization]--");
var dtsod = new DtsodV23(new DtsodV23(new DtsodV23( 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("y", dtsod.ToString());
OldLogger.Log("g", "test completed"); OldLogger.Log("g", "test completed");
} }
@ -64,7 +64,7 @@ public static class TestDtsodV23
{ {
OldLogger.Log("c", "-------[TestDtsodV23/TestSpeed]-------"); OldLogger.Log("c", "-------[TestDtsodV23/TestSpeed]-------");
IDtsod dtsod=null; 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 deserialization",64,()=>dtsod=new DtsodV21(text));
LogOperationTime("V21 serialization", 64, () => _=dtsod.ToString()); LogOperationTime("V21 serialization", 64, () => _=dtsod.ToString());
LogOperationTime("V23 deserialization", 64, () => dtsod = new DtsodV23(text)); LogOperationTime("V23 deserialization", 64, () => dtsod = new DtsodV23(text));
@ -75,7 +75,7 @@ public static class TestDtsodV23
public static void TestMemoryConsumption() public static void TestMemoryConsumption()
{ {
OldLogger.Log("c", "----[TestDtsodV23/TestMemConsumpt]----"); 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 a = GC.GetTotalMemory(true);
var dtsods = new DtsodV23[64]; var dtsods = new DtsodV23[64];
for (int i = 0; i < dtsods.Length; i++) for (int i = 0; i < dtsods.Length; i++)

View File

@ -18,7 +18,7 @@ public static class TestDtsodV24
public static void TestBaseTypes() public static void TestBaseTypes()
{ {
OldLogger.Log("c", "-----[TestDtsodV24/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) foreach (var pair in dtsod)
OldLogger.Log("b", pair.ToString()); OldLogger.Log("b", pair.ToString());
OldLogger.Log("g", "test completed"); OldLogger.Log("g", "test completed");
@ -27,7 +27,7 @@ public static class TestDtsodV24
public static void TestComplexes() public static void TestComplexes()
{ {
OldLogger.Log("c", "-----[TestDtsodV24/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("h", dtsod.ToString());
OldLogger.Log("g", "test completed"); OldLogger.Log("g", "test completed");
} }
@ -35,7 +35,7 @@ public static class TestDtsodV24
public static void TestLists() public static void TestLists()
{ {
OldLogger.Log("c", "-------[TestDtsodV24/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) foreach (KVPair pair in dtsod)
{ {
var list = new Autoarr<Unitype>(pair.value.VoidPtr, false); var list = new Autoarr<Unitype>(pair.value.VoidPtr, false);
@ -60,7 +60,7 @@ public static class TestDtsodV24
{ {
OldLogger.Log("c", "--[TestDtsodV24/TestReSerialization]--"); OldLogger.Log("c", "--[TestDtsodV24/TestReSerialization]--");
var dtsod = new DtsodV24(new DtsodV24(new DtsodV24( 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("h", dtsod.ToString());
OldLogger.Log("g", "test completed"); OldLogger.Log("g", "test completed");
} }
@ -69,12 +69,12 @@ public static class TestDtsodV24
{ {
OldLogger.Log("c", "-------[TestDtsodV24/TestSpeed]-------"); OldLogger.Log("c", "-------[TestDtsodV24/TestSpeed]-------");
IDtsod dtsod=null; 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 = ""; string text = "";
LogOperationTime( "V23 to V24 conversion", 32, ()=> LogOperationTime( "V23 to V24 conversion", 32, ()=>
text = DtsodConverter.ConvertVersion(new DtsodV23(_text), DtsodVersion.V24).ToString() 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 deserialization", 64, () => dtsod = new DtsodV24(text));
LogOperationTime("V24 serialization", 64, () => text = dtsod.ToString()); LogOperationTime("V24 serialization", 64, () => text = dtsod.ToString());
OldLogger.Log("g", "test completed"); OldLogger.Log("g", "test completed");

View File

@ -9,14 +9,14 @@ public static class DependencyResolver
public static void CopyLibs() public static void CopyLibs()
{ {
if(DepsCopied) return; if(DepsCopied) return;
string depsdir = $"Dependencies{Путь.Разд}"; string depsdir = $"Dependencies{Path.Sep}";
depsdir += Environment.OSVersion.Platform switch depsdir += Environment.OSVersion.Platform switch
{ {
PlatformID.Unix => "linux", PlatformID.Unix => "linux",
PlatformID.Win32NT => "windows", PlatformID.Win32NT => "windows",
_=> throw new Exception($"unsupported os {Environment.OSVersion.Platform}") _=> throw new Exception($"unsupported os {Environment.OSVersion.Platform}")
}; };
depsdir += Путь.Разд; depsdir += Path.Sep;
depsdir += RuntimeInformation.ProcessArchitecture switch depsdir += RuntimeInformation.ProcessArchitecture switch
{ {
Architecture.X64 => "x64", Architecture.X64 => "x64",
@ -27,7 +27,7 @@ public static class DependencyResolver
}; };
foreach (var file in Directory.GetAllFiles(depsdir)) 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); File.Copy(file,extracted, true);
Log("g",$"{extracted} copied"); Log("g",$"{extracted} copied");
} }

View File

@ -0,0 +1,45 @@
using System.IO;
namespace DTLib.Experimental;
public class FileInstance
{
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 FileInstance(string path, FileOpenMode mode)
{
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");
}
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}")
};
}
}

View File

@ -43,35 +43,51 @@ public static class StringConverter
public static bool StartsWith(this string s, char c) => s[0] == c; 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 bool EndsWith(this string s, char c) => s[s.Length - 1] == c;
public static string MergeToString(params object[] parts) // 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<TVal>(params TVal[] parts)
{ {
if (parts.Length == 0)
return "";
StringBuilder builder = new(); StringBuilder builder = new();
for (int i = 0; i < parts.Length; i++) for (int i = 0; i < parts.Length; i++)
builder.Append(parts[i]); builder.Append(parts[i]);
return builder.ToString(); return builder.ToString();
} }
public static string MergeToString<TVal, TSep>(TSep separator, params TVal[] parts)
// 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<T>(this IEnumerable<T> collection, string separator)
{ {
StringBuilder builder = new(); if (parts.Length == 0)
foreach (T elem in collection)
{
builder.Append(elem);
builder.Append(separator);
}
if (builder.Length == 0)
return ""; 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(); return builder.ToString();
} }
public static string MergeToString<T>(this IEnumerable<T> collection)
public static string MergeToString<TVal, TSep>(this IEnumerable<TVal> collection, TSep separator)
{ {
StringBuilder builder = new(); 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<TVal>(this IEnumerable<TVal> collection)
{
StringBuilder builder = new();
foreach (var elem in collection)
builder.Append(elem); builder.Append(elem);
return builder.ToString(); return builder.ToString();
} }

View File

@ -10,8 +10,8 @@ public static class Directory
if (!Exists(dir)) if (!Exists(dir))
{ {
// проверяет существование папки, в которой нужно создать dir // проверяет существование папки, в которой нужно создать dir
if (dir.Contains(Путь.Разд) && !Exists(dir.Remove(dir.LastIndexOf(Путь.Разд)))) if (dir.Contains(Path.Sep) && !Exists(dir.Remove(dir.LastIndexOf(Path.Sep))))
Create(dir.Remove(dir.LastIndexOf(Путь.Разд))); Create(dir.Remove(dir.LastIndexOf(Path.Sep)));
System.IO.Directory.CreateDirectory(dir); System.IO.Directory.CreateDirectory(dir);
} }
} }
@ -100,8 +100,8 @@ public static class Directory
public static void CreateSymlink(string sourceName, string symlinkName) public static void CreateSymlink(string sourceName, string symlinkName)
{ {
if (symlinkName.Contains(Путь.Разд)) if (symlinkName.Contains(Path.Sep))
Create(symlinkName.Remove(symlinkName.LastIndexOf(Путь.Разд))); Create(symlinkName.Remove(symlinkName.LastIndexOf(Path.Sep)));
if (!Symlink.CreateSymbolicLink(symlinkName, sourceName, Symlink.SymlinkTarget.Directory)) if (!Symlink.CreateSymbolicLink(symlinkName, sourceName, Symlink.SymlinkTarget.Directory))
throw new InvalidOperationException($"some error occured while creating symlink\nDirectory.CreateSymlink({symlinkName}, {sourceName})"); 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) public static int SymCopy(string srcdir, string newdir)
{ {
List<string> files = GetAllFiles(srcdir); List<string> files = GetAllFiles(srcdir);
if (!srcdir.EndsWith(Путь.Разд)) srcdir += Путь.Разд; if (!srcdir.EndsWith(Path.Sep)) srcdir += Path.Sep;
if (!newdir.EndsWith(Путь.Разд)) newdir += Путь.Разд; if (!newdir.EndsWith(Path.Sep)) newdir += Path.Sep;
int i = 0; int i = 0;
for (; i < files.Count; i++) for (; i < files.Count; i++)
File.CreateSymlink(files[i], files[i].Replace(srcdir, newdir)); File.CreateSymlink(files[i], files[i].Replace(srcdir, newdir));

View File

@ -3,7 +3,7 @@ namespace DTLib.Filesystem;
public static class File 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); public static bool Exists(string file) => System.IO.File.Exists(file);
@ -12,8 +12,8 @@ public static class File
{ {
if (!Exists(file)) if (!Exists(file))
{ {
if (file.Contains(Путь.Разд)) if (file.Contains(Path.Sep))
Directory.Create(file.Remove(file.LastIndexOf(Путь.Разд))); Directory.Create(file.Remove(file.LastIndexOf(Path.Sep)));
using System.IO.FileStream stream = System.IO.File.Create(file); using System.IO.FileStream stream = System.IO.File.Create(file);
stream.Close(); stream.Close();
} }
@ -32,9 +32,10 @@ public static class File
public static byte[] ReadAllBytes(string file) public static byte[] ReadAllBytes(string file)
{ {
using System.IO.FileStream stream = OpenRead(file); using System.IO.FileStream stream = OpenRead(file);
int size = GetSize(file); int size = GetSize(file).ToInt();
byte[] output = new byte[size]; 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(); stream.Close();
return output; return output;
} }
@ -78,8 +79,8 @@ public static class File
public static void CreateSymlink(string sourceName, string symlinkName) public static void CreateSymlink(string sourceName, string symlinkName)
{ {
if (symlinkName.Contains(Путь.Разд)) if (symlinkName.Contains(Path.Sep))
Directory.Create(symlinkName.Remove(symlinkName.LastIndexOf(Путь.Разд))); Directory.Create(symlinkName.Remove(symlinkName.LastIndexOf(Path.Sep)));
if (!Symlink.CreateSymbolicLink(symlinkName, sourceName, Symlink.SymlinkTarget.File)) if (!Symlink.CreateSymbolicLink(symlinkName, sourceName, Symlink.SymlinkTarget.File))
throw new InvalidOperationException($"some error occured while creating symlink\nFile.CreateSymlink({symlinkName}, {sourceName})"); throw new InvalidOperationException($"some error occured while creating symlink\nFile.CreateSymlink({symlinkName}, {sourceName})");
} }

74
DTLib/Filesystem/Path.cs Normal file
View File

@ -0,0 +1,74 @@
using System.Runtime.CompilerServices;
namespace DTLib.Filesystem;
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<length; i++)
if (chars[i] == NotSep)
chars[i] = Sep;
return chars.ToString();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string Resolve(params string[] parts) => 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");
}
/// 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);
}
}

View File

@ -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($"лее точки двойные убери: {может_быть_сочками}");
}
}

View File

@ -9,7 +9,7 @@ public class FileLogger : IDisposable
} }
public FileLogger(string dir, string programName) 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 string LogfileName { get; protected set; }
public System.IO.FileStream LogfileStream { get; protected set; } public System.IO.FileStream LogfileStream { get; protected set; }