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)); }
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 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)); }
private static void DeleteLogAndChilds(string dirPath) { string logFilePath = GetLogFilePath(dirPath); if (File.Exists(logFilePath) == false) { return; } List <string> list = ReadLogFile(logFilePath); foreach (string s in list) { if (GetFileType(s) == FileType.Directory) { DeleteLogAndChilds(dirPath + separator + GetFileName(s)); } } File.Delete(logFilePath); DigdaSysLog.RemoveLogContent(DigdaSysLog.DigChangeLogPath, Path.GetFileName(logFilePath)); }
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)); }
public static int Main(string[] args) { DigdaSysLog.LastShow = DateTime.Now; DirectoryInfo current = new DirectoryInfo(Directory.GetCurrentDirectory()); char[] options = null; if (Directory.Exists(DigdaLog.LogSaveDirPath) == false) { Directory.CreateDirectory(DigdaLog.LogSaveDirPath); } if (Directory.Exists(DigdaSysLog.SysLogSaveDirPath) == false) { Directory.CreateDirectory(DigdaSysLog.SysLogSaveDirPath); } if (Directory.Exists(DigdaSysLog.FileChangesDirPath) == false) { Directory.CreateDirectory(DigdaSysLog.FileChangesDirPath); } if (args.Length > 0) { options = args[0].Trim(' ', '-').ToCharArray(); if (Array.Exists(options, c => c == 'h' || c == 'H')) //-h 는 help를 보여줍니다. 이 옵션은 프로그램을 종료시킵니다. { PrintHelp(); return(0); } if (Array.Exists(options, c => c == 'r' || c == 'R')) //-r은 현재 디렉토리에 상관 없이 루트 디렉토리를 시작 디렉토리로 정합니다. { current = current.Root; } if (Array.Exists(options, c => c == 'a' || c == 'A')) //-a는 현재 디렉토리부터 하위 디렉토리/폴더들을 모두 탐색해 로그를 갱신합니다. { Console.WriteLine($"[Calculated Size] : ({GetDirectorySize(current, 0)}byte(s)) {current.FullName}"); } } if (File.Exists(DigdaLog.GetLogFilePath(current.FullName)) == false) { Console.WriteLine("Current Directory's log file does not exist"); Console.WriteLine("Creating log files..."); Console.WriteLine($"[Calculated Size] : ({GetDirectorySize(current, 0)}byte(s)) {current.FullName}"); } FileSystemWatcher watcher = new FileSystemWatcher(current.FullName, "*.*") { NotifyFilter = NotifyFilters.Size | NotifyFilters.FileName | NotifyFilters.DirectoryName, IncludeSubdirectories = true }; watcher.Changed += new FileSystemEventHandler(OnChanged); watcher.Created += new FileSystemEventHandler(OnChanged); watcher.Deleted += new FileSystemEventHandler(OnChanged); watcher.Renamed += new RenamedEventHandler(OnRenamed); watcher.EnableRaisingEvents = true; do { string command = Console.ReadLine().ToLower().Trim(); if (command.Equals("q") || command.Equals("quit")) { Console.WriteLine("Write changes before closing..."); DigdaSysLog.WriteChanges(); Console.WriteLine($"[System] ChangeLog writed at {DigdaSysLog.FileChangesDirPath}"); break; } else if (command.Equals("r") || command.Equals("refresh")) { } else if (command.Equals("c") || command.Equals("cd")) { } else if (command.Equals("s") || command.Equals("show")) { DigdaSysLog.WriteChanges(); Console.WriteLine($"[System] ChangeLog written at {DigdaSysLog.FileChangesDirPath}"); } else if (command.Equals("h") || command.Equals("help")) { PrintHelp(); } else if (command.Equals("")) { //pass } else { Console.WriteLine("[Error] Invalid Command"); PrintHelp(); } }while (true); return(0); }