Skip to content

Commit

Permalink
Fix trash
Browse files Browse the repository at this point in the history
Folders that had files in them/were more than 1 directory deep were unable to be deleted. This has been fixed
If a file/folder's parent is being removed, the folder is not printed to help with clutter.
  • Loading branch information
bfourk committed Feb 24, 2024
1 parent 97ae050 commit e35d778
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 30 deletions.
14 changes: 6 additions & 8 deletions FileDiff/Crawler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ public static void Crawl(string Root, string NewDir, CrawlInfo Output)
string NewPath = Path.Join(Root,NewDir);
string[] Files = Directory.GetFiles(NewPath);
string[] Directories = Directory.GetDirectories(NewPath);
foreach (string CrawledFile in Files)
{
// Add file to the list
foreach (string CrawledFile in Files) // Add file to list
Output.Files.Add(CrawledFile.Substring(CrawledFile.IndexOf("/./")+3));
}
foreach (string Dir in Directories)
{
// Crawl subdirectory
Expand All @@ -40,11 +37,12 @@ public static void FindFolderChanges(List<string> DirectoryListMain, List<string

foreach (string FolderLocation in DirectoryListSync)
if (!DirectoryListMain.Contains(FolderLocation) && !FolderLocation.Contains(".DiffTrash"))
{
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine("- [{0}]",FolderLocation);
Deletions.Add(FolderLocation);
}
// Remove unnecessary folders (parent folders removed)
Util.RecursiveRemove(Deletions, Deletions);
Console.ForegroundColor = ConsoleColor.DarkRed;
foreach (string del in Deletions)
Console.WriteLine("- [{0}]",del);
}
// Goes through all files in a list, checks if they were added, checks if different from sync folder.
public static void FindFileChanges(string[] Main, CrawlInfo Sync, string MainDir, string SyncDir, ref List<string> Additions, ref List<string> Deletions, ref List<string> Changes)
Expand Down
63 changes: 41 additions & 22 deletions FileDiff/FileDiff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,33 @@ namespace FileDiff;

public class FDiff
{
private const int Threads = 6; // Can change to whatever you want
private const int Threads = 16; // Can change to whatever you want
private static void RecreateDirectoryTree(string GarbagePath, string? path)
{
string? DirName = Path.GetDirectoryName(path);
if (DirName == null)
return;
string[] WithoutFile = DirName.Split("/");
string CurrentPath = "";
foreach (string str in WithoutFile)
{
if (str == "")
continue;
CurrentPath += string.Format("{0}/",str);
if (!CurrentPath.Contains(".DiffTrash"))
continue;
int index = CurrentPath.IndexOf(".DiffTrash");
Console.WriteLine(CurrentPath.Substring(index + 11));
Directory.CreateDirectory(Path.Join(GarbagePath,CurrentPath.Substring(index + 11)));
}
}
public static void Main(string[] args)
{
Stopwatch sw = new Stopwatch(); // For calculating the total time
Console.Write("Input Directory 1: ");
string? MainDirectory = Console.ReadLine();
string? MainDirectory = "/tmp/ramdisk/tmp1";//Console.ReadLine();
Console.Write("\nInput Directory 2: ");
string? SyncDirectory = Console.ReadLine();
string? SyncDirectory = "/tmp/ramdisk/tmp2";//Console.ReadLine();
if (MainDirectory == null || SyncDirectory == null)
{
Environment.Exit(1);
Expand Down Expand Up @@ -86,11 +105,11 @@ public static void Main(string[] args)
if (Remainder != 0)
for (int i = 0; i < Remainder; i++)
{
string[] RemainderAppended = new string[FilesPerList+Remainder];
string[] RemainderAppended = new string[FilesPerList + Remainder];
for (int f = 0; f < FilesPerList; f++)
RemainderAppended[f] = FileLists[0][f];
for (int f = 0; f < Remainder; f++)
RemainderAppended[FilesPerList+f] = MainDirectoryList.Files[FilesPerList*Threads+f];
RemainderAppended[FilesPerList+f] = MainDirectoryList.Files[FilesPerList * Threads + f];
FileLists[0] = RemainderAppended;
}

Expand Down Expand Up @@ -118,12 +137,12 @@ public static void Main(string[] args)
// Search for deletions
foreach(string FileLocation in SyncDirectoryList.Files)
if (!MainDirectoryList.Files.Contains(FileLocation) && !FileLocation.Contains(".DiffTrash"))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("- {0}",FileLocation);
fDel.Add(FileLocation);
}

// Remove unnecessary files (parent folders removed)
Util.RecursiveRemove(dDel, fDel);
Console.ForegroundColor = ConsoleColor.Red;
foreach (string del in fDel)
Console.WriteLine("- {0}",del);
Console.ForegroundColor = ConsoleColor.White;
int Total = fAdd.Count() + fDel.Count() + fChanges.Count() + dAdd.Count() + dDel.Count();

Expand Down Expand Up @@ -220,51 +239,51 @@ public static void Main(string[] args)
string GarbagePath = Path.Join(SyncDirectory,".DiffTrash");
if (!Directory.Exists(GarbagePath))
Directory.CreateDirectory(GarbagePath);

// Files
foreach (string del in fDel)
{
inc++;
string Path1 = Path.Join(SyncDirectory, del);

// The file could have been deleted at this point, double-check
if (!File.Exists(Path1))
continue;
Console.WriteLine("- {0}", del);
try
{
string NewPath = Path.Join(GarbagePath,del);
string NewPath = Path.Join(GarbagePath, del);

// Re-create directory path in trash folder
RecreateDirectoryTree(GarbagePath, NewPath);
if (File.Exists(NewPath))
{
Console.WriteLine("Warn: File with similar name already exists in trash, adding number to beginning");
File.Move(Path1,Path.Join(GarbagePath,inc.ToString()+del));
File.Move(Path1, Path.Join(GarbagePath,string.Format("{0}-{1}", inc.ToString(), del)));
}
else
File.Move(Path1,NewPath);
File.Move(Path1, NewPath);
}
catch (Exception ex)
{
Console.WriteLine("Failed to delete file {0}\nReason: {1}", del, ex.ToString());
continue;
}
}
// Folders
foreach (string del in dDel)
{
try
{
string DirPath = Path.Join(SyncDirectory, del);
Console.WriteLine(DirPath);
// The directory could have been deleted at this point, double-check.
if (!Directory.Exists(DirPath))
continue;

string NewPath = Path.Join(GarbagePath, del);

RecreateDirectoryTree(GarbagePath, NewPath);
if (Directory.Exists(NewPath))
{
Console.WriteLine("Warn: File with similar name already exists in trash, adding number to beginning");
Directory.Move(DirPath,Path.Join(GarbagePath,inc.ToString()+del));
Directory.Move(DirPath, Path.Join(GarbagePath,string.Format("{0}-{1}", inc.ToString(), del)));
}
else
Directory.Move(DirPath,NewPath);
Directory.Move(DirPath, NewPath);
}
catch (Exception ex)
{
Expand Down
22 changes: 22 additions & 0 deletions FileDiff/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,26 @@ public static bool RequestYN(string arg)
return true;
}
}

public static void RecursiveRemove(List<string> List1, List<string> List2)
{
while (true)
{
bool changed = false;
foreach (string l1Str in List1)
{
foreach (string l2Str in List2)
if (l1Str != l2Str && l2Str.StartsWith(l1Str))
{
List2.Remove(l2Str);
changed = true;
break;
}
if (changed)
break;
}
if (!changed)
break;
}
}
}

0 comments on commit e35d778

Please sign in to comment.