Skip to content

Commit

Permalink
Update to use .net8, and change defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
domsleee committed May 18, 2024
1 parent 7eb6137 commit d187f11
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/benchmark.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
fetch-depth: 0
- uses: actions/setup-dotnet@v3
with:
dotnet-version: '7.0.x'
dotnet-version: '8.0.x'
- name: Build Deps
run: |
cd subrepos/LockCheck;
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- name: Install Dotnet
uses: actions/setup-dotnet@v3
with:
dotnet-version: "7.0.x"
dotnet-version: "8.0.x"

- name: Dotnet Installation Info
run: dotnet --info
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
- name: Install Dotnet
uses: actions/setup-dotnet@v3
with:
dotnet-version: "7.0.x"
dotnet-version: "8.0.x"

- name: Directory structure
run: |
Expand Down
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
<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.3.1</Version>
<Version>1.4.0</Version>
<PackageProjectUrl>https://github.com/domsleee/forceops</PackageProjectUrl>
<RepositoryUrl>https://github.com/domsleee/forceops</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<Authors>Dom Slee</Authors>
<PackageTags>lock file directory force delete</PackageTags>
<PackageReleaseNotes>https://github.com/domsleee/forceops/blob/main/CHANGELOG.md</PackageReleaseNotes>

<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<RollForward>LatestMajor</RollForward>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
Expand Down
4 changes: 2 additions & 2 deletions ForceOps.Lib/src/ForceOpsContext/ForceOpsContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ public class ForceOpsContext
/// The number of retries when performing an operation.
/// For example, five retries equals six total attempts.
/// </summary>
public int maxRetries = 5;
public int maxRetries = 10;

/// <summary>
/// The time to wait between before retrying the operation.
/// </summary>
public TimeSpan retryDelay = TimeSpan.FromMilliseconds(500);
public TimeSpan retryDelay = TimeSpan.FromMilliseconds(50);

public IProcessKiller processKiller;
public IElevateUtils elevateUtils;
Expand Down
16 changes: 8 additions & 8 deletions ForceOps.Test/src/FileAndDirectoryDeleterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ public void DeletingDirectoryOpenInPowershellWorkingDirectory()
Assert.IsType<IOException>(exceptionWithNoRetries);
Assert.StartsWith("The process cannot access the file", exceptionWithNoRetries.Message);

forceOpsContext.maxRetries = 5;
forceOpsContext.maxRetries = 10;
var exceptionWithDirectoryStrategy = Record.Exception(() => fileAndDirectoryDeleter.DeleteDirectory(new DirectoryInfo(tempFolderPath)));
Assert.True(null == exceptionWithDirectoryStrategy, testContext.fakeLoggerFactory.GetAllLogsString());

Assert.Matches(@"Exceeded retry count of 0. Failed. ForceOps process is not elevated.
Could not delete directory .*. Beginning retry 1/5 in 500ms. ForceOps process is not elevated. Found 1 process to try to kill: \[\d+ \- powershell.exe\]", testContext.fakeLoggerFactory.GetAllLogsString());
Could not delete directory .*. 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]
Expand All @@ -40,12 +40,12 @@ public void DeletingReadonlyDirectoryOpenInPowershellWorkingDirectory()
Assert.IsType<IOException>(exceptionWithNoRetries);
Assert.StartsWith("The process cannot access the file", exceptionWithNoRetries.Message);

forceOpsContext.maxRetries = 5;
forceOpsContext.maxRetries = 10;
var exceptionWithDirectoryStrategy = Record.Exception(() => fileAndDirectoryDeleter.DeleteDirectory(new DirectoryInfo(tempFolderPath)));
Assert.True(null == exceptionWithDirectoryStrategy, testContext.fakeLoggerFactory.GetAllLogsString());

Assert.Matches(@"Exceeded retry count of 0. Failed. ForceOps process is not elevated.
Could not delete directory .*. Beginning retry 1/5 in 500ms. ForceOps process is not elevated. Found 1 process to try to kill: \[\d+ \- powershell.exe\]", testContext.fakeLoggerFactory.GetAllLogsString());
Could not delete directory .*. 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]
Expand All @@ -59,12 +59,12 @@ public void DeletingFileOpenByPowershell()
Assert.IsType<IOException>(exceptionWithNoRetries);
Assert.StartsWith("The process cannot access the file", exceptionWithNoRetries.Message);

forceOpsContext.maxRetries = 5;
forceOpsContext.maxRetries = 10;
var exceptionWithDirectoryStrategy = Record.Exception(() => fileAndDirectoryDeleter.DeleteFile(new FileInfo(tempFilePath)));
Assert.True(null == exceptionWithDirectoryStrategy, testContext.fakeLoggerFactory.GetAllLogsString());

Assert.Matches($@"Exceeded retry count of 0. Failed. ForceOps process is not elevated.
Could not delete file .*. Beginning retry 1/5 in 500ms. ForceOps process is not elevated. Found 1 process to try to kill: \[\d+ \- powershell.exe\]", testContext.fakeLoggerFactory.GetAllLogsString());
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]
Expand All @@ -79,12 +79,12 @@ public void DeletingReadonlyFileOpenByPowershell()
Assert.IsType<IOException>(exceptionWithNoRetries);
Assert.StartsWith("The process cannot access the file", exceptionWithNoRetries.Message);

forceOpsContext.maxRetries = 5;
forceOpsContext.maxRetries = 10;
var exceptionWithDirectoryStrategy = Record.Exception(() => fileAndDirectoryDeleter.DeleteFile(new FileInfo(tempFilePath)));
Assert.True(null == exceptionWithDirectoryStrategy, testContext.fakeLoggerFactory.GetAllLogsString());

Assert.Matches($@"Exceeded retry count of 0. Failed. ForceOps process is not elevated.
Could not delete file .*. Beginning retry 1/5 in 500ms. ForceOps process is not elevated. Found 1 process to try to kill: \[\d+ \- powershell.exe\]", testContext.fakeLoggerFactory.GetAllLogsString());
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());
}

public ForceOpsMethodsTest()
Expand Down
4 changes: 2 additions & 2 deletions ForceOps/src/ForceOps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ Command CreateDeleteCommand()

var forceOption = new Option<bool>(new[] { "-f", "--force" }, "Ignore nonexistent files and arguments.");
var disableElevate = new Option<bool>(new[] { "-e", "--disable-elevate" }, "Do not attempt to elevate if the file can't be deleted.");
var retryDelay = new Option<int>(new[] { "-d", "--retry-delay" }, () => 500, "Delay when retrying to delete a file, after deleting processes holding a lock.");
var maxRetries = new Option<int>(new[] { "-n", "--max-retries" }, () => 5, "Number of retries when deleting a locked file.");
var retryDelay = new Option<int>(new[] { "-d", "--retry-delay" }, () => 50, "Delay when retrying to delete a file, after deleting processes holding a lock.");
var maxRetries = new Option<int>(new[] { "-n", "--max-retries" }, () => 10, "Number of retries when deleting a locked file.");

var deleteCommand = new Command("delete", "Delete files or a directories recursively.")
{
Expand Down
34 changes: 22 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,33 @@ Refer also to `10.4 Example: file deletion in Windows` from "A Philosophy of Sof
### Deleting when a process owned by the current user is using it
```shell
❯ forceops rm .\bin\
[14:55:33 INF] Could not delete file "C:\Users\user\myprogram\bin\Debug\net6\myprogram.dll". Beginning retry 1/5 in 500ms. ForceOps process is not elevated. Found 1 process to try to kill: [100724 - myprogram.exe].
[14:55:34 INF] Could not delete file "C:\Users\user\myprogram\bin\Debug\net6\myprogram.dll". Beginning retry 2/5 in 500ms. ForceOps process is not elevated. Found 0 processes to try to kill: [].
[14:55:33 INF] Could not delete file "C:\Users\user\myprogram\bin\Debug\net6\myprogram.dll". Beginning retry 1/10 in 50ms. ForceOps process is not elevated. Found 1 process to try to kill: [100724 - myprogram.exe].
[14:55:34 INF] Could not delete file "C:\Users\user\myprogram\bin\Debug\net6\myprogram.dll". Beginning retry 2/10 in 50ms. ForceOps process is not elevated. Found 0 processes to try to kill: [].
```
### Deleting when a process owned by another user is using it (e.g. a windows service)
```shell
❯ forceops rm .\bin\Debug\net6\myprogram.exe
[15:07:34 INF] Could not delete file "C:\Users\user\myprogram\bin\Debug\net6\myprogram.exe". Beginning retry 1/5 in 500ms. ForceOps process is not elevated. Found 1 process to try to kill: [115744 - ].
[15:07:34 WRN] Failed to kill process 115744: Access is denied.
[15:07:35 INF] Could not delete file "C:\Users\user\myprogram\bin\Debug\net6\myprogram.exe". Beginning retry 2/5 in 500ms. ForceOps process is not elevated. Found 1 process to try to kill: [115744 - ].
[15:07:35 WRN] Failed to kill process 115744: Access is denied.
[15:07:36 INF] Could not delete file "C:\Users\user\myprogram\bin\Debug\net6\myprogram.exe". Beginning retry 3/5 in 500ms. ForceOps process is not elevated. Found 1 process to try to kill: [115744 - ].
[15:07:37 WRN] Failed to kill process 115744: Access is denied.
[15:07:37 INF] Could not delete file "C:\Users\user\myprogram\bin\Debug\net6\myprogram.exe". Beginning retry 4/5 in 500ms. ForceOps process is not elevated. Found 1 process to try to kill: [115744 - ].
[15:07:38 WRN] Failed to kill process 115744: Access is denied.
[15:07:38 INF] Could not delete file "C:\Users\user\myprogram\bin\Debug\net6\myprogram.exe". Beginning retry 5/5 in 500ms. ForceOps process is not elevated. Found 1 process to try to kill: [115744 - ].
[15:07:39 INF] Could not delete file "C:\Users\user\myprogram\bin\Debug\net6\myprogram.exe". Beginning retry 1/10 in 50ms. ForceOps process is not elevated. Found 1 process to try to kill: [115744 - ].
[15:07:39 WRN] Failed to kill process 115744: Access is denied.
[15:07:39 INF] Exceeded retry count of 5. Failed. ForceOps process is not elevated.
[15:07:39 INF] Could not delete file "C:\Users\user\myprogram\bin\Debug\net6\myprogram.exe". Beginning retry 2/10 in 50ms. ForceOps process is not elevated. Found 1 process to try to kill: [115744 - ].
[15:07:39 WRN] Failed to kill process 115744: Access is denied.
[15:07:39 INF] Could not delete file "C:\Users\user\myprogram\bin\Debug\net6\myprogram.exe". Beginning retry 3/10 in 50ms. ForceOps process is not elevated. Found 1 process to try to kill: [115744 - ].
[15:07:39 WRN] Failed to kill process 115744: Access is denied.
[15:07:39 INF] Could not delete file "C:\Users\user\myprogram\bin\Debug\net6\myprogram.exe". Beginning retry 4/10 in 50ms. ForceOps process is not elevated. Found 1 process to try to kill: [115744 - ].
[15:07:39 WRN] Failed to kill process 115744: Access is denied.
[15:07:39 INF] Could not delete file "C:\Users\user\myprogram\bin\Debug\net6\myprogram.exe". Beginning retry 5/10 in 50ms. ForceOps process is not elevated. Found 1 process to try to kill: [115744 - ].
[15:07:39 WRN] Failed to kill process 115744: Access is denied.
[15:07:39 INF] Could not delete file "C:\Users\user\myprogram\bin\Debug\net6\myprogram.exe". Beginning retry 6/10 in 50ms. ForceOps process is not elevated. Found 1 process to try to kill: [115744 - ].
[15:07:39 WRN] Failed to kill process 115744: Access is denied.
[15:07:39 INF] Could not delete file "C:\Users\user\myprogram\bin\Debug\net6\myprogram.exe". Beginning retry 7/10 in 50ms. ForceOps process is not elevated. Found 1 process to try to kill: [115744 - ].
[15:07:39 WRN] Failed to kill process 115744: Access is denied.
[15:07:39 INF] Could not delete file "C:\Users\user\myprogram\bin\Debug\net6\myprogram.exe". Beginning retry 8/10 in 50ms. ForceOps process is not elevated. Found 1 process to try to kill: [115744 - ].
[15:07:39 WRN] Failed to kill process 115744: Access is denied.
[15:07:39 INF] Could not delete file "C:\Users\user\myprogram\bin\Debug\net6\myprogram.exe". Beginning retry 9/10 in 50ms. ForceOps process is not elevated. Found 1 process to try to kill: [115744 - ].
[15:07:39 WRN] Failed to kill process 115744: Access is denied.
[15:07:39 INF] Could not delete file "C:\Users\user\myprogram\bin\Debug\net6\myprogram.exe". Beginning retry 10/10 in 50ms. ForceOps process is not elevated. Found 1 process to try to kill: [115744 - ].
[15:07:39 WRN] Failed to kill process 115744: Access is denied.
[15:07:39 INF] Exceeded retry count of 10. Failed. ForceOps process is not elevated.
[15:07:39 INF] Unable to perform operation as an unelevated process. Retrying as elevated and logging to "C:\Users\user\AppData\Local\Temp\tmp9C14.tmp".
[15:07:42 INF] Successfully deleted as admin
```
Expand Down

0 comments on commit d187f11

Please sign in to comment.