Skip to content

Commit

Permalink
Enhance robustness and introduce new features
Browse files Browse the repository at this point in the history
- Implemented initial support for `/sort` argument in Namespace TTLAgent within Program.cs, indicating future sorting functionality.
- Introduced `SafeEnumerateFiles` method in Program.cs to replace `Directory.EnumerateFiles` for improved exception handling, specifically targeting `UnauthorizedAccessException`. This method also supports enumerating files in subdirectories.
- Updated exception handling in Program.cs by removing empty catch blocks and adding silent handling or logging, enhancing program stability.
- Updated AssemblyInfo.cs version from `3.4.0.*` to `3.5.0.*`, preparing for a new release with added features and improvements.
- Added `/compress` command-line parameter in Strings.resx, indicating a new feature for file compression using NTFS.
- Modified TTLAgent.csproj by adding a `<SubType>` tag to `Strings.resx` resource, likely affecting IDE experience or resource handling.
  • Loading branch information
dmki committed Jul 9, 2024
1 parent 9ede7f4 commit cd97f34
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 6 deletions.
76 changes: 72 additions & 4 deletions TTLAgent/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ static void Main(string[] args)
_km = KillMethod.BFG;
_bytez = Encoding.ASCII.GetBytes(new string((char)0, 32768));
}


bool daysSet = false;
foreach (var arg in args)
Expand Down Expand Up @@ -151,6 +150,12 @@ static void Main(string[] args)
}

CheckLongPathsEnabled();
// Sorting related parameters
var sort = args.Contains("/sort");
if (sort)
{
PrintConsole("This function is not implemented yet.");
}
//Process the file or directory
string targetName = args[0];
var isDir = IsDirectory(targetName);
Expand Down Expand Up @@ -313,7 +318,7 @@ private static void ProcessDirectory(string path)
{
if (_keepalive == 0)
{
files.AddRange(Directory.EnumerateFiles(path, mask, so));
files.AddRange(SafeEnumerateFiles(path, mask, so));
continue;
}
var di = new DirectoryInfo(path);
Expand All @@ -334,7 +339,7 @@ private static void ProcessDirectory(string path)
}
catch (Exception ex)
{

}
}
//Process files
Expand All @@ -353,7 +358,7 @@ private static void ProcessDirectory(string path)
{
try
{
if (Directory.EnumerateFiles(dir, "*", SearchOption.AllDirectories).Any()) continue;
if (SafeEnumerateFiles(dir, "*", SearchOption.AllDirectories).Any()) continue;
//the directory is empty, kill it!
PrintConsole("Deleting empty directory " + dir);
if (_testMode)
Expand Down Expand Up @@ -579,6 +584,69 @@ public static string GenRandomString(Int32 length)
}
return result;
}
/// <summary>
/// Enumerates files in a directory, handling UnauthorizedAccessExceptions and other exceptions.
/// </summary>
/// <param name="rootPath">Root directory from which to begin search</param>
/// <param name="searchPattern">File name mask, e.g. *.txt</param>
/// <param name="searchOption"></param>
/// <returns></returns>
static IEnumerable<string> SafeEnumerateFiles(string rootPath, string searchPattern, SearchOption searchOption)
{
Queue<string> folders = new Queue<string>();
folders.Enqueue(rootPath);

while (folders.Count > 0)
{
string currentFolder = folders.Dequeue();
IEnumerable<string> files = null;
IEnumerable<string> subDirs = null;

try
{
files = Directory.EnumerateFiles(currentFolder, searchPattern);
}
catch (UnauthorizedAccessException)
{
// Skip this folder
PrintConsole($"Access to {rootPath} denied. Try to run this application with administrator privileges.", MessageType.Warning, true);
continue;
}
catch (Exception ex)
{
Console.WriteLine($"Exception accessing files in folder {currentFolder}: {ex.Message}");
continue;
}

foreach (string file in files)
{
yield return file;
}

if (searchOption == SearchOption.AllDirectories)
{
try
{
subDirs = Directory.EnumerateDirectories(currentFolder);
}
catch (UnauthorizedAccessException)
{
// Skip this folder
continue;
}
catch (Exception ex)
{
Console.WriteLine($"Exception accessing subdirectories in folder {currentFolder}: {ex.Message}");
continue;
}

foreach (string subDir in subDirs)
{
folders.Enqueue(subDir);
}
}
}
}
}

public enum MessageType
Expand Down
4 changes: 2 additions & 2 deletions TTLAgent/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("3.4.0.*")]
[assembly: AssemblyFileVersion("3.4.0")]
[assembly: AssemblyVersion("3.5.0.*")]
[assembly: AssemblyFileVersion("3.5.0")]
1 change: 1 addition & 0 deletions TTLAgent/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ Parameters:
/mask:[file mask] Only process files with this mask. E.g. *.txt means text files. You can separate multiple masks by comma.
/noempty Delete empty sub-directories.
/recycle Delete to recycle bin.
/compress Compress files using NTFS compression instead of deleting
/rmdir Delete directory itself, with all contents. Otherwise only files will be deleted.
/secure Securely delete files - slow but secure.
/subs Delete files in sub-directories.
Expand Down
1 change: 1 addition & 0 deletions TTLAgent/TTLAgent.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
<EmbeddedResource Include="Strings.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Strings1.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
Expand Down

0 comments on commit cd97f34

Please sign in to comment.