private static void RenameLogAndChilds(string oldDirPath, string newDirPath) { string oldLogFilePath = GetLogFilePath(oldDirPath); string newLogFilePath = GetLogFilePath(newDirPath); if (File.Exists(oldLogFilePath) == false) { return; } List <string> list = ReadLogFile(oldLogFilePath); StreamWriter writer = Digda.WaitAndGetWriter(oldLogFilePath, FileMode.Create); foreach (string s in list) { string content = s; FileType type = GetFileType(s); if (type == FileType.Directory) { string dir = GetFileName(s); RenameLogAndChilds(oldDirPath + separator + dir, newDirPath + separator + dir); } else if (type == FileType.This) { content = MakeThisDirInfos(newDirPath, GetSize(s), GetAddSize(s)); } writer.WriteLine(content); } writer.Close(); File.Move(oldLogFilePath, newLogFilePath); DigdaSysLog.ChangeLogContent(DigdaSysLog.DigChangeLogPath, Path.GetFileName(oldLogFilePath), Path.GetFileName(newLogFilePath)); }
private static void WriteEmptyFolderLog(string dirPath, string filePath) { StreamWriter writer = Digda.WaitAndGetWriter(filePath, FileMode.Create); writer.WriteLine(MakeThisDirInfos(dirPath, 0, 0)); writer.Close(); }
private static List <string> ReadLogFile(string path) { StreamReader reader = Digda.WaitAndGetReader(path, FileMode.OpenOrCreate); List <string> r = ReadLogFile(reader); reader.Close(); return(r); }
public static void RenameLogContent(string oldFile, string newFile) { string newContent = null; if (File.Exists(newFile)) { FileInfo file = new FileInfo(newFile); newContent = MakeFileInfos(file, file.Length); } else if (Directory.Exists(newFile)) { List <string> tmpList = ReadLogFile(GetLogFilePath(Path.GetFileName(oldFile))); if (tmpList.Count > 0) { string thisInfo = tmpList[tmpList.Count - 1]; long size = GetSize(thisInfo); long addSize = GetAddSize(thisInfo); newContent = MakeDirectoryInfos(newFile, size, addSize); } else { newContent = MakeDirectoryInfos(newFile, 0, 0); } RenameLogAndChilds(oldFile, newFile); } else { return; } string path = GetLogFilePath(Path.GetDirectoryName(oldFile)); List <string> list = ReadLogFile(path); StreamWriter writer = Digda.WaitAndGetWriter(path, FileMode.Create); bool isRemoved = false; bool isWritten = false; foreach (string s in list) { if (isRemoved == false && GetFileName(s).Equals(Path.GetFileName(oldFile))) { isRemoved = true; continue; } if (isWritten == false && FileInfoCompare(newContent, s) < 0) { writer.WriteLine(newContent); isWritten = true; } writer.WriteLine(s); } writer.Close(); }
public static void DeleteLogContent(string fileFullPath) { string path = GetLogFilePath(Path.GetDirectoryName(fileFullPath)); string fileName = Path.GetFileName(fileFullPath); List <string> list = ReadLogFile(path); int last = list.Count - 1; if (last < 0) { WriteEmptyFolderLog(Path.GetDirectoryName(fileFullPath), path); list = ReadLogFile(path); last = list.Count - 1; } StreamWriter writer = Digda.WaitAndGetWriter(path, FileMode.Create); long removeSize = 0; for (int i = 0; i < list.Count - 1; i++) { if (GetFileName(list[i]).Equals(fileName)) { removeSize = -1 * GetSize(list[i]); list[last] = AddSizeToBoth(list[last], removeSize); long sizeDiff = GetSize(list[i]) - GetAddSize(list[i]); if (sizeDiff > 0) { DigdaSysLog.InsertLogContent(DigdaSysLog.DeletedFilesLogPath, fileFullPath + '|' + sizeDiff); } continue; } writer.WriteLine(list[i]); } writer.WriteLine(list[last]); writer.Close(); if (File.Exists(GetLogFilePath(fileFullPath))) { DeleteLogAndChilds(fileFullPath); } if (removeSize != 0) { UpdateParentLog(Path.GetDirectoryName(fileFullPath), GetSize(list[last])); } DigdaSysLog.InsertLogContent(DigdaSysLog.DigChangeLogPath, Path.GetFileName(path)); }
public static void RemoveLogContent(string logFile, string removeContent) { List <string> list = ReadLog(logFile); StreamWriter writer = Digda.WaitAndGetWriter(logFile, FileMode.Create); foreach (string s in list) { if (s.Equals(removeContent)) { continue; } writer.WriteLine(s); } writer.Close(); }
private static List <string> ReadLog(string path) { StreamReader reader = Digda.WaitAndGetReader(path, FileMode.OpenOrCreate); List <string> list = new List <string>(); while (true) { string s = reader.ReadLine(); if (s == null || s.Equals("")) { break; } list.Add(s); } reader.Close(); return(list); }
public static void ChangeLogContent(string fullPath, long size) { string path = GetLogFilePath(Path.GetDirectoryName(fullPath)); List <string> list = ReadLogFile(path); int last = list.Count - 1; if (last < 0) { WriteEmptyFolderLog(Path.GetDirectoryName(fullPath), path); list = ReadLogFile(path); last = list.Count - 1; } StreamWriter writer = Digda.WaitAndGetWriter(path, FileMode.Create); long diff = 0; for (int i = 0; i < list.Count - 1; i++) { if (GetFileName(list[i]).Equals(Path.GetFileName(fullPath))) { diff = size - GetSize(list[i]); string content = File.Exists(fullPath) ? MakeFileInfos(fullPath, size, diff) : MakeDirectoryInfos(fullPath, size, diff); content = AddSizeToAddSize(content, GetAddSize(list[i])); writer.WriteLine(content); list[last] = AddSizeToBoth(list[last], diff); continue; } writer.WriteLine(list[i]); } writer.WriteLine(list[last]); writer.Close(); if (diff != 0) { UpdateParentLog(Path.GetDirectoryName(fullPath), GetSize(list[last])); } DigdaSysLog.InsertLogContent(DigdaSysLog.DigChangeLogPath, Path.GetFileName(path)); }
public static void WriteChanges() { changesHolder.Clear(); int firstDepth = -1; while (true) { List <string> list = ReadLog(DigChangeLogPath); if (list.Count <= 0) { break; } if (firstDepth < 0) { firstDepth = list[0].Split('@').Length - 1; } Console.WriteLine($"[Debug] {list[0]}"); WriteChanges(DigdaLog.LogSaveDirPath + separator + list[0], (list[0].Split('@').Length - 1) - firstDepth); } DateTime current = DateTime.Now; StreamWriter writer = Digda.WaitAndGetWriter(FileChangesDirPath + separator + string.Format("{0:yyyy-MM-dd HH,mm,ss}.log", current), FileMode.Create); writer.WriteLine("{0:yyyy-MM-dd HH:mm:ss} => {1:yyyy-MM-dd HH:mm:ss}", LastShow, current); LastShow = current; foreach (string s in changesHolder) { writer.WriteLine(s); } writer.Close(); File.Delete(DigChangeLogPath); File.Delete(DeletedFilesLogPath); changesHolder.Clear(); }
public static void ChangeLogContent(string logFile, string oldContent, string newContent) { List <string> list = ReadLog(logFile); List <string> final = new List <string>(list.Count); StreamWriter writer = Digda.WaitAndGetWriter(logFile, FileMode.Create); bool isExists = false; foreach (string s in list) { if (s.Equals(oldContent)) { isExists = true; continue; } final.Add(s); } foreach (string s in final) { if (isExists && string.Compare(newContent, s, true) < 0) { writer.WriteLine(newContent); isExists = false; } writer.WriteLine(s); } if (isExists) { writer.WriteLine(newContent); } writer.Close(); }
public static void InsertLogContent(string logFile, string content) { List <string> list = ReadLog(logFile); StreamWriter writer = Digda.WaitAndGetWriter(logFile, FileMode.Create); bool isWritten = false; if (list.Count == 0) { writer.WriteLine(content); } else { foreach (string s in list) { if (isWritten == false && string.Compare(content, s, true) < 0) { writer.WriteLine(content); isWritten = true; } else if (isWritten == false && content.Equals(s)) { isWritten = true; } writer.WriteLine(s); } if (isWritten == false) { writer.WriteLine(content); } } writer.Close(); }
private static void WriteChanges(string logPath, int depth) { RemoveLogContent(DigChangeLogPath, Path.GetFileName(logPath)); Console.WriteLine($"[Debug] {logPath}"); List <string> log = ReadLog(logPath); List <string> deleted = ReadLog(DeletedFilesLogPath); int last = log.Count - 1; if (last < 0) { Console.WriteLine("log is empty, writing failed..."); return; } if (DigdaLog.GetAddSize(log[last]) == 0) { return; } else if (DigdaLog.GetAddSize(log[last]) == DigdaLog.GetSize(log[last])) { changesHolder.Add(GetSpaces(depth) + "[Created] " + MakeChangesContent(log[last])); } else { changesHolder.Add(GetSpaces(depth) + "[Changed] " + MakeChangesContent(log[last])); } foreach (string s in deleted) { string[] split = s.Split('|'); string tmpLogFilePath = DigdaLog.GetLogFilePath(Path.GetDirectoryName(split[0])); long size = long.Parse(split[1]); if (tmpLogFilePath.Equals(logPath)) { changesHolder.Add(GetSpaces(depth + 1) + "[Deleted] " + string.Format("({0:+#;-#;0}byte(s)) ", size * -1) + Path.GetFileName(split[0])); RemoveLogContent(DeletedFilesLogPath, s); } } StreamWriter writer = Digda.WaitAndGetWriter(logPath, FileMode.Create); foreach (string s in log) { string tmp = s; if (DigdaLog.GetAddSize(s) != 0) { string status = null; if (DigdaLog.GetSize(s) == DigdaLog.GetAddSize(s)) { status = "[Created] "; } else { status = "[Changed] "; } switch (DigdaLog.GetFileType(s)) { case FileType.File: changesHolder.Add(GetSpaces(depth + 1) + status + MakeChangesContent(s)); break; case FileType.Directory: string tmpLogPath = logPath.Remove(logPath.Length - 4) + '@' + DigdaLog.GetFileName(s) + ".dig"; WriteChanges(tmpLogPath, depth + 1); break; case FileType.This: break; } tmp = DigdaLog.SetAddSize(tmp, 0); } writer.WriteLine(tmp); } writer.Close(); }
public static void AddLogContent(string fileFullPath) { string path = GetLogFilePath(Path.GetDirectoryName(fileFullPath)); if (File.Exists(path) == false) { WriteEmptyFolderLog(Path.GetDirectoryName(fileFullPath), path); } string content = null; try { if (File.Exists(fileFullPath)) { FileInfo file = new FileInfo(fileFullPath); content = MakeFileInfos(file, file.Length); } else if (Directory.Exists(fileFullPath)) { DirectoryInfo dir = new DirectoryInfo(fileFullPath); long size = Digda.GetDirectorySize(dir, 0); content = MakeDirectoryInfos(dir, size, size); } else { return; } } catch (System.UnauthorizedAccessException) { return; } catch (FileNotFoundException) { return; } List <string> list = ReadLogFile(path); int last = list.Count - 1; long diff = GetSize(content); if (diff != 0) { list[last] = AddSizeToBoth(list[last], diff); } StreamWriter writer = Digda.WaitAndGetWriter(path, FileMode.Create); bool isWritten = false; for (int i = 0; i < list.Count; i++) { if (isWritten == false && FileInfoCompare(content, list[i]) < 0) { writer.WriteLine(content); isWritten = true; } writer.WriteLine(list[i]); } writer.Close(); if (diff != 0) { UpdateParentLog(Path.GetDirectoryName(fileFullPath), GetSize(list[last])); } DigdaSysLog.InsertLogContent(DigdaSysLog.DigChangeLogPath, Path.GetFileName(path)); }