-
Notifications
You must be signed in to change notification settings - Fork 175
added truncate file option to context menu - .net 8 #334
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RandallFlagg
wants to merge
8
commits into
LogExperts:Development
Choose a base branch
from
RandallFlagg:truncate-file-menu-option
base: Development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e662b6d
added truncate file option to context menu
jsuppe c4078bb
Check if lock is held on the file
jsuppe 5945151
Removed unused delegates
978c814
Changed: Removed the thread that updates the status line and replaced…
ccd0374
Updated the code to .net 8
89b3f7f
Fixed cross thread exception
5601fd0
Merge branch 'LogExperts:Development' into truncate-file-menu-option
RandallFlagg 72b8685
Merge branch 'Development' of github.com:RandallFlagg/LogExpert into …
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<RootNamespace>FileLockFinder</RootNamespace> | ||
<AssemblyName>FileLockFinder</AssemblyName> | ||
<Title>FileLockFinder</Title> | ||
<OutputType>Library</OutputType> | ||
<SignAssembly>true</SignAssembly> | ||
<AssemblyOriginatorKeyFile>..\Solution Items\Key.snk</AssemblyOriginatorKeyFile> | ||
<OutputPath>$(SolutionDir)..\bin\$(Configuration)\plugins</OutputPath> | ||
<DefineConstants>$(DefineConstants)</DefineConstants> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> | ||
<Optimize>False</Optimize> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'"> | ||
<Optimize>True</Optimize> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Include="..\Solution Items\Key.snk"> | ||
<Link>Key.snk</Link> | ||
</None> | ||
</ItemGroup> | ||
</Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Runtime.InteropServices; | ||
|
||
// Expanded with some helpers from: https://code.msdn.microsoft.com/windowsapps/How-to-know-the-process-704839f4/ | ||
// Uses Windows Restart Manager. | ||
// A more involved and cross platform solution to this problem is here: https://github.com/cklutz/LockCheck | ||
|
||
|
||
namespace FileLockFinder | ||
{ | ||
public class LockFinder | ||
{ | ||
|
||
/// <summary> | ||
/// Method <c>FindLockedProcessName</c> Retrieve the first process name | ||
/// that is locking the file at the specified path | ||
/// </summary> | ||
/// <param name="path">The path of a file with a write lock held by a | ||
/// process</param> | ||
/// <resturns>The name of the first process found with a lock</resturns> | ||
/// <exception cref="Exception"> | ||
/// Thrown when the file path is not locked | ||
/// </exception> | ||
static public string FindLockedProcessName(string path) | ||
{ | ||
var list = FindLockProcesses(path); | ||
if (list.Count == 0) | ||
{ | ||
throw new Exception( | ||
"No processes are locking the path specified"); | ||
} | ||
return list[0].ProcessName; | ||
} | ||
|
||
/// <summary> | ||
/// Method <c>CheckIfFileIsLocked</c> Check if the file specified has a | ||
/// write lock held by a process | ||
/// </summary> | ||
/// <param name="path">The path of a file being checked if a write lock | ||
/// held by a process</param> | ||
/// <returns>true when one or more processes with lock</returns> | ||
static public bool CheckIfFileIsLocked(string path) | ||
{ | ||
var list = FindLockProcesses(path); | ||
if (list.Count > 0) { return true; } | ||
return false; | ||
} | ||
|
||
/// <summary> | ||
/// Used to find processes holding a lock on the file. This would cause | ||
/// other usage, such as file truncation or write opretions to throw | ||
/// IOException if an exclusive lock is attempted. | ||
/// </summary> | ||
/// <param name="path">Path being checked</param> | ||
/// <returns>List of processes holding file lock to path</returns> | ||
/// <exception cref="Exception"></exception> | ||
static public List<Process> FindLockProcesses(string path) | ||
{ | ||
var key = Guid.NewGuid().ToString(); | ||
var processes = new List<Process>(); | ||
|
||
int res = RmStartSession(out uint handle, 0, key); | ||
if (res != 0) | ||
{ | ||
throw new Exception("Could not begin restart session. " + | ||
"Unable to determine file locker."); | ||
} | ||
|
||
try | ||
{ | ||
uint pnProcInfo = 0; | ||
uint lpdwRebootReasons = RmRebootReasonNone; | ||
string[] resources = [path]; | ||
|
||
res = RmRegisterResources(handle, (uint)resources.Length, | ||
resources, 0, null, 0, null); | ||
if (res != 0) | ||
{ | ||
throw new Exception("Could not register resource."); | ||
} | ||
res = RmGetList(handle, out uint pnProcInfoNeeded, ref pnProcInfo, null, | ||
ref lpdwRebootReasons); | ||
const int ERROR_MORE_DATA = 234; | ||
if (res == ERROR_MORE_DATA) | ||
{ | ||
RM_PROCESS_INFO[] processInfo = | ||
new RM_PROCESS_INFO[pnProcInfoNeeded]; | ||
pnProcInfo = pnProcInfoNeeded; | ||
// Get the list. | ||
res = RmGetList(handle, out pnProcInfoNeeded, ref pnProcInfo, processInfo, ref lpdwRebootReasons); | ||
if (res == 0) | ||
{ | ||
processes = new List<Process>((int)pnProcInfo); | ||
for (int i = 0; i < pnProcInfo; i++) | ||
{ | ||
try | ||
{ | ||
processes.Add(Process.GetProcessById(processInfo[i]. | ||
Process.dwProcessId)); | ||
} | ||
catch (ArgumentException) { } | ||
} | ||
} | ||
else | ||
{ | ||
throw new Exception("Could not list processes locking resource"); | ||
} | ||
} | ||
else if (res != 0) | ||
{ | ||
throw new Exception("Could not list processes locking resource." + | ||
"Failed to get size of result."); | ||
} | ||
} | ||
catch (Exception exception) | ||
{ | ||
Trace.WriteLine(exception.Message); | ||
} | ||
finally | ||
{ | ||
Trace.WriteLine($"RmEndSession: {RmEndSession(handle)}"); | ||
} | ||
|
||
return processes; | ||
} | ||
private const int RmRebootReasonNone = 0; | ||
private const int CCH_RM_MAX_APP_NAME = 255; | ||
private const int CCH_RM_MAX_SVC_NAME = 63; | ||
|
||
[StructLayout(LayoutKind.Sequential)] | ||
struct RM_UNIQUE_PROCESS | ||
{ | ||
public int dwProcessId; | ||
public System.Runtime.InteropServices. | ||
ComTypes.FILETIME ProcessStartTime; | ||
} | ||
[DllImport("rstrtmgr.dll", | ||
CharSet = CharSet.Auto, SetLastError = true)] | ||
static extern int RmGetList(uint dwSessionHandle, | ||
out uint pnProcInfoNeeded, | ||
ref uint pnProcInfo, | ||
[In, Out] RM_PROCESS_INFO[] rgAffectedApps, | ||
ref uint lpdwRebootReasons); | ||
[StructLayout(LayoutKind.Sequential, | ||
CharSet = CharSet.Auto)] | ||
struct RM_PROCESS_INFO | ||
{ | ||
public RM_UNIQUE_PROCESS Process; | ||
[MarshalAs(UnmanagedType.ByValTStr, | ||
SizeConst = CCH_RM_MAX_APP_NAME + 1)] | ||
public string strAppName; | ||
[MarshalAs(UnmanagedType.ByValTStr, | ||
SizeConst = CCH_RM_MAX_SVC_NAME + 1)] | ||
public string strServiceShortName; | ||
public RM_APP_TYPE ApplicationType; | ||
public uint AppStatus; | ||
public uint TSSessionId; | ||
[MarshalAs(UnmanagedType.Bool)] | ||
public bool bRestartable; | ||
} | ||
|
||
enum RM_APP_TYPE | ||
{ | ||
RmUnknownApp = 0, | ||
RmMainWindow = 1, | ||
RmOtherWindow = 2, | ||
RmService = 3, | ||
RmExplorer = 4, | ||
RmConsole = 5, | ||
RmCritical = 1000 | ||
} | ||
|
||
[DllImport("rstrtmgr.dll", CharSet = CharSet.Auto, SetLastError = true)] | ||
static extern int RmRegisterResources( | ||
uint pSessionHandle, | ||
UInt32 nFiles, | ||
string[] rgsFilenames, | ||
UInt32 nApplications, | ||
[In] RM_UNIQUE_PROCESS[] rgApplications, | ||
UInt32 nServices, string[] rgsServiceNames); | ||
|
||
[DllImport("rstrtmgr.dll", CharSet = CharSet.Auto, SetLastError = true)] | ||
static extern int RmStartSession( | ||
out uint pSessionHandle, | ||
int dwSessionFlags, | ||
string strSessionKey); | ||
|
||
[DllImport("rstrtmgr.dll", CharSet = CharSet.Auto, SetLastError = true)] | ||
static extern int RmEndSession(uint pSessionHandle); | ||
} | ||
} |
48 changes: 29 additions & 19 deletions
48
src/LogExpert/Controls/LogTabWindow/LogTabWindow.designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't re-throw exception, add the exception to the logger