REFACTORING

This commit is contained in:
2021-10-04 23:25:42 +03:00
parent c71d2b8356
commit 2defc3ae5e
25 changed files with 583 additions and 539 deletions

View File

@@ -10,10 +10,10 @@ namespace DTLib.Filesystem
// создает папку, если её не существует
public static void Create(string dir)
{
if (!Directory.Exists(dir))
if(!Directory.Exists(dir))
{
// проверяет существование папки, в которой нужно создать dir
if (dir.Contains("\\") && !Directory.Exists(dir.Remove(dir.LastIndexOf('\\'))))
if(dir.Contains("\\")&&!Directory.Exists(dir.Remove(dir.LastIndexOf('\\'))))
Create(dir.Remove(dir.LastIndexOf('\\')));
System.IO.Directory.CreateDirectory(dir);
}
@@ -22,13 +22,13 @@ namespace DTLib.Filesystem
public static void Copy(string source_dir, string new_dir, bool owerwrite = false)
{
Create(new_dir);
List<string> subdirs = new List<string>();
var subdirs = new List<string>();
List<string> files = GetAllFiles(source_dir, ref subdirs);
for (int i = 0; i < subdirs.Count; i++)
for(int i = 0; i<subdirs.Count; i++)
{
Create(subdirs[i].Replace(source_dir, new_dir));
}
for (int i = 0; i < files.Count; i++)
for(int i = 0; i<files.Count; i++)
{
string f = files[i].Replace(source_dir, new_dir);
File.Copy(files[i], f, owerwrite);
@@ -39,18 +39,19 @@ namespace DTLib.Filesystem
// копирует все файлы и папки и выдаёт список конфликтующих файлов
public static void Copy(string source_dir, string new_dir, out List<string> conflicts, bool owerwrite = false)
{
conflicts = new List<string>();
conflicts=new List<string>();
var subdirs = new List<string>();
var files = GetAllFiles(source_dir, ref subdirs);
List<string> files = GetAllFiles(source_dir, ref subdirs);
Create(new_dir);
for (int i = 0; i < subdirs.Count; i++)
for(int i = 0; i<subdirs.Count; i++)
{
Create(subdirs[i].Replace(source_dir, new_dir));
}
for (int i = 0; i < files.Count; i++)
for(int i = 0; i<files.Count; i++)
{
string newfile = files[i].Replace(source_dir, new_dir);
if (File.Exists(newfile)) conflicts.Add(newfile);
if(File.Exists(newfile))
conflicts.Add(newfile);
File.Copy(files[i], newfile, owerwrite);
//PublicLog.Log(new string[] {"g", $"file <", "c", files[i], "b", "> have copied to <", "c", newfile, "b", ">\n'" });
}
@@ -60,16 +61,18 @@ namespace DTLib.Filesystem
public static void Delete(string dir)
{
var subdirs = new List<string>();
var files = GetAllFiles(dir, ref subdirs);
for (int i = 0; i < files.Count; i++)
List<string> 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--)
for(int i = subdirs.Count-1; i>=0; i--)
{
PublicLog.Log($"deleting {subdirs[i]}\n");
if (Directory.Exists(subdirs[i])) System.IO.Directory.Delete(subdirs[i], true);
if(Directory.Exists(subdirs[i]))
System.IO.Directory.Delete(subdirs[i], true);
}
PublicLog.Log($"deleting {dir}\n");
if (Directory.Exists(dir)) System.IO.Directory.Delete(dir, true);
if(Directory.Exists(dir))
System.IO.Directory.Delete(dir, true);
}
public static string[] GetFiles(string dir) => System.IO.Directory.GetFiles(dir);
@@ -79,15 +82,15 @@ namespace DTLib.Filesystem
// выдает список всех файлов
public static List<string> GetAllFiles(string dir)
{
List<string> all_files = new List<string>();
var all_files = new List<string>();
string[] cur_files = Directory.GetFiles(dir);
for (int i = 0; i < cur_files.Length; i++)
for(int i = 0; i<cur_files.Length; i++)
{
all_files.Add(cur_files[i]);
//PublicLog.Log(new string[] { "b", "file found: <", "c", cur_files[i], "b", ">\n" });
}
string[] cur_subdirs = Directory.GetDirectories(dir);
for (int i = 0; i < cur_subdirs.Length; i++)
for(int i = 0; i<cur_subdirs.Length; i++)
{
//PublicLog.Log(new string[] { "b", "subdir found: <", "c", cur_subdirs[i], "b", ">\n" });
all_files.AddRange(GetAllFiles(cur_subdirs[i]));
@@ -98,15 +101,15 @@ namespace DTLib.Filesystem
// выдает список всех файлов и подпапок в папке
public static List<string> GetAllFiles(string dir, ref List<string> all_subdirs)
{
List<string> all_files = new List<string>();
var all_files = new List<string>();
string[] cur_files = Directory.GetFiles(dir);
for (int i = 0; i < cur_files.Length; i++)
for(int i = 0; i<cur_files.Length; i++)
{
all_files.Add(cur_files[i]);
//PublicLog.Log(new string[] { "b", "file found: <", "c", cur_files[i], "b", ">\n" });
}
string[] cur_subdirs = Directory.GetDirectories(dir);
for (int i = 0; i < cur_subdirs.Length; i++)
for(int i = 0; i<cur_subdirs.Length; i++)
{
all_subdirs.Add(cur_subdirs[i]);
//PublicLog.Log(new string[] { "b", "subdir found: <", "c", cur_subdirs[i], "b", ">\n" });
@@ -119,13 +122,13 @@ namespace DTLib.Filesystem
public static void GrantAccess(string fullPath)
{
System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(fullPath);
var dirInfo = new System.IO.DirectoryInfo(fullPath);
System.Security.AccessControl.DirectorySecurity dirSecurity = dirInfo.GetAccessControl();
dirSecurity.AddAccessRule(new System.Security.AccessControl.FileSystemAccessRule(
new System.Security.Principal.SecurityIdentifier(
System.Security.Principal.WellKnownSidType.WorldSid, null),
System.Security.AccessControl.FileSystemRights.FullControl,
System.Security.AccessControl.InheritanceFlags.ObjectInherit |
System.Security.AccessControl.InheritanceFlags.ObjectInherit|
System.Security.AccessControl.InheritanceFlags.ContainerInherit,
System.Security.AccessControl.PropagationFlags.NoPropagateInherit,
System.Security.AccessControl.AccessControlType.Allow));

View File

@@ -11,18 +11,21 @@ namespace DTLib.Filesystem
// если файл не существует, создаёт файл, создаёт папки из его пути
public static void Create(string file, bool delete_old = false)
{
if (delete_old && File.Exists(file)) File.Delete(file);
if (!File.Exists(file))
if(delete_old&&File.Exists(file))
File.Delete(file);
if(!File.Exists(file))
{
if (file.Contains("\\")) Directory.Create(file.Remove(file.LastIndexOf('\\')));
using var stream = System.IO.File.Create(file);
if(file.Contains("\\"))
Directory.Create(file.Remove(file.LastIndexOf('\\')));
using System.IO.FileStream stream = System.IO.File.Create(file);
stream.Close();
}
}
public static void Copy(string srcPath, string newPath, bool replace = false)
{
if (!replace && Exists(newPath)) throw new Exception($"file <{newPath}> alredy exists");
if(!replace&&Exists(newPath))
throw new Exception($"file <{newPath}> alredy exists");
Create(newPath);
WriteAllBytes(newPath, ReadAllBytes(srcPath));
}
@@ -31,7 +34,7 @@ namespace DTLib.Filesystem
public static byte[] ReadAllBytes(string file)
{
using var stream = File.OpenRead(file);
using System.IO.FileStream stream = File.OpenRead(file);
int size = GetSize(file);
byte[] output = new byte[size];
stream.Read(output, 0, size);
@@ -43,7 +46,7 @@ namespace DTLib.Filesystem
public static void WriteAllBytes(string file, byte[] content)
{
using var stream = File.OpenWrite(file);
using System.IO.FileStream stream = File.OpenWrite(file);
stream.Write(content, 0, content.Length);
stream.Close();
}
@@ -52,7 +55,7 @@ namespace DTLib.Filesystem
public static void AppendAllBytes(string file, byte[] content)
{
using var stream = File.OpenAppend(file);
using System.IO.FileStream stream = File.OpenAppend(file);
stream.Write(content, 0, content.Length);
stream.Close();
}
@@ -61,7 +64,8 @@ namespace DTLib.Filesystem
public static System.IO.FileStream OpenRead(string file)
{
if (!Exists(file)) throw new Exception($"file not found: <{file}>");
if(!Exists(file))
throw new Exception($"file not found: <{file}>");
return System.IO.File.OpenRead(file);
}
public static System.IO.FileStream OpenWrite(string file)

View File

@@ -10,7 +10,7 @@ namespace DTLib.Filesystem
// записывает текст в файл и закрывает файл
public static void LogToFile(string logfile, string msg)
{
lock (new object())
lock(new object())
{
File.AppendAllText(logfile, msg);
}
@@ -19,39 +19,41 @@ namespace DTLib.Filesystem
// чтение параметров из конфига
public static string ReadFromConfig(string configfile, string key)
{
lock (new object())
lock(new object())
{
key += ": ";
key+=": ";
using var reader = new System.IO.StreamReader(configfile);
while (!reader.EndOfStream)
while(!reader.EndOfStream)
{
string st = reader.ReadLine();
if (st.StartsWith(key))
if(st.StartsWith(key))
{
string value = "";
for (int i = key.Length; i < st.Length; i++)
for(int i = key.Length; i<st.Length; i++)
{
if (st[i] == '#') return value;
if (st[i] == '%')
if(st[i]=='#')
return value;
if(st[i]=='%')
{
bool stop = false;
string placeholder = "";
i++;
while (!stop)
while(!stop)
{
if (st[i] == '%')
if(st[i]=='%')
{
stop = true;
value += ReadFromConfig(configfile, placeholder);
stop=true;
value+=ReadFromConfig(configfile, placeholder);
}
else
{
placeholder += st[i];
placeholder+=st[i];
i++;
}
}
}
else value += st[i];
else
value+=st[i];
}
reader.Close();
//if (value == "") throw new System.Exception($"ReadFromConfig({configfile}, {key}) error: key not found");