Skip to content

Commit

Permalink
1.6.0: Major improvements with async scan and Dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyrix25633 committed Nov 22, 2022
1 parent 70c90d7 commit 77df594
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 146 deletions.
68 changes: 21 additions & 47 deletions Files.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ public class DirectoryEntry {
/// <summary>
/// Initializer
/// </summary>
public DirectoryEntry(string path, string from) {
public DirectoryEntry(string path, string relativePath) {
fileInfo = new FileInfo(path);
relativePath = path.Substring(from.Length + 1);
this.relativePath = relativePath;
}
public string relativePath;
public FileInfo fileInfo;
public Reason reason;
/// <summary>
/// Function to search in the destination folder for the same file and compare them
/// (<paramref name="destinationList"/>, <paramref name="allExtensions"/>, <paramref name="extensions"/>)
/// (<paramref name="destinationDictionary"/>, <paramref name="allExtensions"/>, <paramref name="extensions"/>)
/// </summary>
/// <param name="destinationList">The list of all files in the destination folder</param>
/// <param name="destinationDictionary">The dictionaty of all files in the destination folder</param>
/// <param name="allExtensions">If all extensions have to be checked for content differencies</param>
/// <param name="extensions">The list of the extensions to check for content differencies</param>
/// <returns>Returns true if the file has to be copied</returns>
public bool ToCopy(ref DirectoryEntry[] destinationList, bool allExtensions, string[] extensions) {
Int32 pos;
bool found = IsInList(destinationList, out pos);
if(!found) {
public bool ToCopy(ref Dictionary<string, DirectoryEntry> destinationDictionary, bool allExtensions, string[] extensions) {
DirectoryEntry e;
try {e = destinationDictionary[relativePath];}
catch(Exception) {
// Does not exist
reason = Reason.CopyNotThere;
return true;
}
DirectoryEntry e = destinationList[pos];
destinationList = RemoveAt(destinationList, pos);
// Skip if directory
if((e.fileInfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory) return false;
// Check size
if(fileInfo.Length != e.fileInfo.Length) {
Expand Down Expand Up @@ -57,46 +57,20 @@ public bool ToCopy(ref DirectoryEntry[] destinationList, bool allExtensions, str
}
/// <summary>
/// Function to know of a file has to be removed
/// (<paramref name="sourceList"/>)
/// (<paramref name="sourceDictionary"/>)
/// </summary>
/// <param name="sourceList">The list of files in the source folder</param>
/// <param name="sourceDictionary">The dictionary of files in the source folder</param>
/// <returns>Returns true if the file has to be removed</returns>
public bool ToRemove(DirectoryEntry[] sourceList) {
bool toRemove = !IsInList(sourceList, out _);
if(toRemove) reason = Reason.Remove;
return toRemove;
}
/// <summary>
/// Function to search a file in a list of files
/// (<paramref name="list"/>, <paramref name="pos"/>)
/// </summary>
/// <param name="list">The list of files</param>
/// <param name="pos">The returned position</param>
/// <returns>Returns true if the file is in the list</returns>
private bool IsInList(DirectoryEntry[] list, out Int32 pos) {
Int32 length = list.Length;
for(pos = 0; pos < length; pos++) {
if(relativePath == list[pos].relativePath) return true;
public bool ToRemove(ref Dictionary<string, DirectoryEntry> sourceDictionary) {
DirectoryEntry e;
try {
e = sourceDictionary[relativePath];
return false;
}
catch(Exception) {
reason = Reason.Remove;
return true;
}
return false;
}
/// <summary>
/// Function to search remove an item from a DirectoryEntry array
/// (<paramref name="source"/>, <paramref name="index"/>)
/// </summary>
/// <param name="source">The source DirectoryEntry array</param>
/// <param name="index">The index of the item to remove</param>
/// <returns>Returns the source array without the item to remove</returns>
public static DirectoryEntry[] RemoveAt(DirectoryEntry[] source, Int32 index) {
Int32 lenght = source.Length;
DirectoryEntry[] dest = new DirectoryEntry[lenght - 1];
if( index > 0 )
Array.Copy(source, 0, dest, 0, index);

if( index < lenght - 1 )
Array.Copy(source, index + 1, dest, index, lenght - index - 1);

return dest;
}
}

Expand Down
95 changes: 57 additions & 38 deletions Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ public class Logger {
private static string barFull = "█", barEmpty = " ";
private static string? logfilename;
private static StreamWriter? logstream;
private static Task? logTask;
private static List<Task> logTasks = new List<Task>();
private static int ticket = 0;
/// <summary>
/// Function to initialize the file logging
/// (<paramref name="log"/>)
Expand All @@ -30,71 +31,89 @@ public static void ReinitializeLogging() {
/// </summary>
public static async void TerminateLogging() {
if(logstream != null) {
if(logTask != null) await logTask;
if(logTasks.Count != 0) await logTasks[logTasks.Count - 1];
logstream.Close();
logTasks = new List<Task>();
}
}
/// <summary>
/// Function to output a success message
/// (<paramref name="message"/>)
/// </summary>
/// <param name="message">The message to output</param>
public static void Success(string message) {
public static async void Success(string message) {
int t = ticket++;
string time = TimeString();
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write(time);
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("(Success) ");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(message);
Console.ResetColor();
if(logstream != null) logTask = logstream.WriteLineAsync(time + "(Success) " + message);
await logTasks.ElementAt(t);
logTasks.Append(Task.Run(() => {
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write(time);
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("(Success) ");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(message);
Console.ResetColor();
if(logstream != null) logstream.WriteLineAsync(time + "(Success) " + message);
}));
}
/// <summary>
/// Function to output an info message
/// (<paramref name="message"/>)
/// </summary>
/// <param name="message">The message to output</param>
public static void Info(string message) {
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write(TimeString());
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write("(Info) ");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(message);
Console.ResetColor();
public static async void Info(string message) {
int t = ticket++;
string time = TimeString();
await logTasks.ElementAt(t);
logTasks.Append(Task.Run(() => {
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write(time);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write("(Info) ");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(message);
Console.ResetColor();
}));
}
/// <summary>
/// Function to output a warning message
/// (<paramref name="message"/>)
/// </summary>
/// <param name="message">The message to output</param>
public static void Warning(string message) {
string time = TimeString();
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write(time);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("(Warning) ");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(message);
Console.ResetColor();
if(logstream != null) logTask = logstream.WriteLineAsync(time + "(Warning) " + message);
public static async void Warning(string message) {
int t = ticket++;
string time = TimeString();
await logTasks.ElementAt(t);
logTasks.Append(Task.Run(() => {
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write(time);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("(Warning) ");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(message);
Console.ResetColor();
if(logstream != null) logstream.WriteLineAsync(time + "(Warning) " + message);
}));
}
/// <summary>
/// Function to output an error message
/// (<paramref name="message"/>)
/// </summary>
/// <param name="message">The message to output</param>
public static void Error(string message) {
public static async void Error(string message) {
int t = ticket++;
string time = TimeString();
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write(time);
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("(Error) ");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(message);
Console.ResetColor();
if(logstream != null) logTask = logstream.WriteLineAsync(time + "(Error) " + message);
await logTasks.ElementAt(t);
logTasks.Append(Task.Run(() => {
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write(time);
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("(Error) ");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(message);
Console.ResetColor();
if(logstream != null) logstream.WriteLineAsync(time + "(Error) " + message);
}));
}
/// <summary>
/// Function to clear the last console line
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ run-dotnet-debug:
> dotnet ./transfer/docker/debug/backup-utility.dll -- -s test/source -d test/destination -r test/removed -t 100 -e extensions.txt -l -b

run-dotnet-release:
> ./transfer/docker/release/linux-x64/backup-utility
> dotnet publish -c debug
> ./transfer/docker/release/linux-x64/backup-utility -s test/source -d test/destination -r test/removed -l
Loading

0 comments on commit 77df594

Please sign in to comment.