Skip to content

Commit

Permalink
Added check in WriteTableInfo to prevent null reference when trash in…
Browse files Browse the repository at this point in the history
…fo is inconsistent with actual trash contents
  • Loading branch information
jorystewart committed Sep 16, 2024
1 parent c9c24c2 commit 09d766e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
13 changes: 11 additions & 2 deletions Trashman/src/HelperFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,17 @@ private static void WriteTableInfo(List<FileDetails> list, Dictionary<string, in

if (result)
{
int rightPadding = keyValue - property.GetValue(item).ToString().Length - 1;
builder.Append(' ', rightPadding);
if (property.GetValue(item) != null)
{
int rightPadding = keyValue - property.GetValue(item).ToString().Length - 1;
builder.Append(' ', rightPadding);
}
else
{
int rightPadding = keyValue - 4;
builder.Append("0 B");
builder.Append(' ', rightPadding);
}
}
}
}
Expand Down
51 changes: 49 additions & 2 deletions Trashman/src/Trash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ namespace Trashman;

public class Trash
{
private static string _trashLocation = (Environment.GetEnvironmentVariable("XDG_DATA_HOME") == (String.Empty) || Environment.GetEnvironmentVariable("XDG_DATA_HOME") == null)
private static string _trashLocation =
(Environment.GetEnvironmentVariable("XDG_DATA_HOME") == (String.Empty) ||
Environment.GetEnvironmentVariable("XDG_DATA_HOME") == null)
? Environment.GetEnvironmentVariable("HOME") + "/.local/share/Trash"
: Environment.GetEnvironmentVariable("XDG_DATA_HOME") + "/Trash";

Expand Down Expand Up @@ -77,7 +79,40 @@ private static void TestTrashDirectories()
}
}

public static void SendToTrash(FileSystemInfo file)
private static void ValidateTrashConsistency()
{

DirectoryInfo trashInfoDir;
DirectoryInfo trashFilesDir;

try
{
trashInfoDir = new DirectoryInfo(_trashLocation + "/info");
trashFilesDir = new DirectoryInfo(_trashLocation + "/files");
}
catch (Exception e) when (e is ArgumentException or ArgumentNullException)
{
Console.Error.WriteLine("Unable to obtain handle of trash directories - path is null or invalid");
return;
}
catch (Exception e) when (e is SecurityException)
{
Console.Error.WriteLine("Unable to obtain handle of trash directories - insufficient permissions");
return;
}
catch (Exception e) when (e is PathTooLongException)
{
Console.Error.WriteLine("Unable to obtain handle of trash directories - path exceeds system maximum path length");
return;
}



}



public static void SendToTrash(FileSystemInfo file)
{
if (_protectedPaths.Contains(file.FullName))
{
Expand Down Expand Up @@ -1017,6 +1052,18 @@ where starReplace.IsMatch(item.Item2.Name)
continue;
}
}

else
{
try
{
File.Delete(item.Item1.FullName);
}
catch
{
Console.WriteLine("File " + item.Item1.Name + " does not correspond to a file in trash. Failed to delete orphan .info");
}
}
}
}
}
Expand Down

0 comments on commit 09d766e

Please sign in to comment.