Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<Description>By hook or by crook, perform operations on files and directories. If they are
in use by a process, kill the process.</Description>
<Version>1.5.1</Version>
<Version>1.6.0</Version>
<PackageProjectUrl>https://github.com/domsleee/forceops</PackageProjectUrl>
<RepositoryUrl>https://github.com/domsleee/forceops</RepositoryUrl>
<RepositoryType>git</RepositoryType>
Expand Down
41 changes: 39 additions & 2 deletions ForceOps.Lib/src/DirectoryUtils.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
锘縩amespace ForceOps.Lib;
锘縰sing System.Text.RegularExpressions;

namespace ForceOps.Lib;

public static class DirectoryUtils
{
static readonly Regex ReservedDeviceNamePattern = new(
@"^(CON|PRN|AUX|NUL|COM[0-9]|LPT[0-9])(\..+)?$",
RegexOptions.IgnoreCase | RegexOptions.Compiled);

public static string CombineWithCWDAndGetAbsolutePath(string path)
{
string currentDirectory = Directory.GetCurrentDirectory();
return Path.GetFullPath(Path.Combine(currentDirectory, path));
string combined = Path.Combine(currentDirectory, path);

// Path.GetFullPath resolves reserved device names (NUL, CON, etc.) to \\.\NUL.
// Resolve the parent directory instead and re-append the filename.
string fileName = Path.GetFileName(combined);
if (IsReservedDeviceName(fileName))
{
string parentDir = Path.GetFullPath(Path.GetDirectoryName(combined)!);
return Path.Combine(parentDir, fileName);
}

return Path.GetFullPath(combined);
}

public static bool IsSymLink(string folder) => IsSymLink(new DirectoryInfo(folder));
Expand All @@ -22,4 +39,24 @@ public static void MarkAsNotReadOnly(FileSystemInfo fileSystemInfo)
fileSystemInfo.Attributes &= ~FileAttributes.ReadOnly;
}
}

public static bool IsReservedDeviceName(string path)
{
var fileName = Path.GetFileName(path);
return ReservedDeviceNamePattern.IsMatch(fileName);
}

public static bool TryDeleteReservedDeviceNameFile(string absolutePath)
{
if (!IsReservedDeviceName(absolutePath))
return false;

var extendedPath = @"\\?\" + absolutePath;
if (File.Exists(extendedPath))
{
File.Delete(extendedPath);
}

return true;
}
}
9 changes: 8 additions & 1 deletion ForceOps.Lib/src/FileAndDirectoryDeleter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public void DeleteFileOrDirectory(string fileOrDirectory, bool force)
DeleteDirectory(new DirectoryInfo(fileOrDirectory));
return;
}
if (TryDeleteReservedDeviceNameFile(fileOrDirectory))
{
return;
}

if (!force)
{
Expand All @@ -54,9 +58,12 @@ internal void DeleteFile(FileInfo file)
file.Delete();
break;
}
catch when (!file.Exists) { }
catch when (!file.Exists && !IsReservedDeviceName(file.FullName)) { }
catch (Exception ex) when (ex is IOException || ex is System.UnauthorizedAccessException)
{
if (TryDeleteReservedDeviceNameFile(file.FullName))
return;

var getProcessesLockingFileFunc = () =>
{
try
Expand Down
28 changes: 28 additions & 0 deletions ForceOps.Test/src/FileAndDirectoryDeleterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,34 @@ public void DeletingReadonlyFileOpenByPowershell()
Could not delete file .*. Beginning retry 1/10 in 50ms. ForceOps process is not elevated. Found 1 process to try to kill: \[\d+ \- powershell.exe\]", testContext.fakeLoggerFactory.GetAllLogsString());
}

[Fact]
public void DeletingFileWithReservedDeviceName()
{
var nulFilePath = Path.Combine(tempFolderPath, "nul");
var extendedPath = @"\\?\" + nulFilePath;

File.Create(extendedPath).Dispose();
Assert.True(File.Exists(extendedPath));

fileAndDirectoryDeleter.DeleteFileOrDirectory(nulFilePath, false);

Assert.False(File.Exists(extendedPath));
}

[Fact]
public void DeletingDirectoryContainingFileWithReservedDeviceName()
{
var nulFilePath = Path.Combine(tempFolderPath, "nul");
var extendedPath = @"\\?\" + nulFilePath;

File.Create(extendedPath).Dispose();
Assert.True(File.Exists(extendedPath));

fileAndDirectoryDeleter.DeleteDirectory(new DirectoryInfo(tempFolderPath));

Assert.False(Directory.Exists(tempFolderPath));
}

public ForceOpsMethodsTest()
{
tempFolderPath = GetTemporaryFileName();
Expand Down
Loading