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 .github/workflows/benchmark.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-dotnet@v4
with:
dotnet-version: '9.0.x'
dotnet-version: '10.0.x'
- name: Run benchmark
run: cd ForceOps.Benchmarks && dotnet run -c release --exporters json --filter '*'
- name: Store benchmark result
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Install Dotnet
uses: actions/setup-dotnet@v4
with:
dotnet-version: "9.0.x"
dotnet-version: "10.0.x"

- name: Dotnet Installation Info
run: dotnet --info
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
- name: Install Dotnet
uses: actions/setup-dotnet@v4
with:
dotnet-version: "9.0.x"
dotnet-version: "10.0.x"

- name: Directory structure
run: |
Expand Down Expand Up @@ -61,7 +61,7 @@ jobs:
with:
draft: true
name: "${{ steps.get_version.outputs.version }}"
files: ForceOps/bin_aot/Release/net9.0/win-x64/publish/ForceOps.exe
files: ForceOps/bin_aot/Release/net10.0/win-x64/publish/ForceOps.exe
tag_name: "${{ steps.get_version.outputs.version }}"

- name: Publish NuGet
Expand Down
4 changes: 2 additions & 2 deletions 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 All @@ -11,7 +11,7 @@
<PackageReleaseNotes>https://github.com/domsleee/forceops/blob/main/CHANGELOG.md</PackageReleaseNotes>
<IsPackable>false</IsPackable>

<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<RollForward>LatestMajor</RollForward>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
Expand Down
46 changes: 44 additions & 2 deletions ForceOps.Lib/src/DirectoryUtils.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
namespace ForceOps.Lib;
using 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,29 @@ 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);
}

/// <summary>
/// Attempt to delete a file with a Windows reserved device name (NUL, CON, PRN, etc.)
/// using the \\?\ extended-length path prefix to bypass reserved name checks.
/// </summary>
/// <returns>true if the file was a reserved name and was deleted (or didn't exist).</returns>
public static bool TryDeleteReservedDeviceNameFile(string absolutePath)
{
if (!IsReservedDeviceName(absolutePath))
return false;

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

return true;
}
}
7 changes: 7 additions & 0 deletions 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 @@ -46,6 +50,9 @@ public void DeleteFileOrDirectory(string fileOrDirectory, bool force)
const int ERROR_ACCESS_DENIED = 5;
internal void DeleteFile(FileInfo file)
{
if (TryDeleteReservedDeviceNameFile(file.FullName))
return;

for (var attempt = 1; attempt <= forceOpsContext.maxRetries + 1; attempt++)
{
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
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "9.0.200",
"version": "10.0.100",
"rollForward": "latestFeature"
}
}
Loading