Skip to content

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
wants to merge 8 commits into
base: Development
Choose a base branch
from
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
26 changes: 26 additions & 0 deletions src/FileLockFinder/FileLockFinder.csproj
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>
193 changes: 193 additions & 0 deletions src/FileLockFinder/LockFinder.cs
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 src/LogExpert/Controls/LogTabWindow/LogTabWindow.designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,11 @@ private void OnFindInExplorerToolStripMenuItemClick(object sender, EventArgs e)
explorer.Start();
}

private void truncateFileToolStripMenuItem_Click(object sender, EventArgs e)
{
CurrentLogWindow?.TryToTruncate();
}

private void OnExportBookmarksToolStripMenuItemClick(object sender, EventArgs e)
{
CurrentLogWindow?.ExportBookmarkList();
Expand Down Expand Up @@ -1018,4 +1023,4 @@ private void OnTabRenameToolStripMenuItemClick(object sender, EventArgs e)

#endregion
}
}
}
23 changes: 22 additions & 1 deletion src/LogExpert/Controls/LogWindow/LogWindowsPublic.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using LogExpert.Classes;
using FileLockFinder;

Check failure on line 1 in src/LogExpert/Controls/LogWindow/LogWindowsPublic.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'FileLockFinder' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 1 in src/LogExpert/Controls/LogWindow/LogWindowsPublic.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'FileLockFinder' could not be found (are you missing a using directive or an assembly reference?)
using LogExpert.Classes;
using LogExpert.Classes.Bookmark;
using LogExpert.Classes.Columnizer;
using LogExpert.Classes.Filter;
Expand Down Expand Up @@ -579,6 +580,26 @@
}
}

public void TryToTruncate()
{
try
{
if (LockFinder.CheckIfFileIsLocked(Title))
{
var name = LockFinder.FindLockedProcessName(Title);
var status = string.Format("Truncate failed: file is locked by {0}", name);
StatusLineText(status);
return;
}

File.WriteAllText(Title, "");
}catch(Exception E)
{
StatusLineText("Unexpected issue truncating file");
throw E;
Copy link
Collaborator

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

}
}

public void StartSearch()
{
_guiStateArgs.MenuEnabled = false;
Expand Down
Loading