### Enable SeDebugPrivilege for Full System Scan Source: https://context7.com/velopack/filelocksmith-rs/llms.txt Call `set_debug_privilege` to grant the current process `SeDebugPrivilege`. This is required for `NtQuerySystemInformation` to enumerate handles from all processes, including those owned by other users or the SYSTEM account. The function returns `true` if successful and requires the process to be already running as administrator. ```rust use filelocksmith::{is_process_elevated, set_debug_privilege, find_processes_locking_path}; fn prepare_for_full_scan() -> bool { if !is_process_elevated() { eprintln!("Must be run as administrator to enable SeDebugPrivilege."); return false; } if set_debug_privilege() { println!("SeDebugPrivilege enabled — full system scan available."); true } else { eprintln!("Failed to enable SeDebugPrivilege."); false } } fn main() { let ready = prepare_for_full_scan(); let target = r"C:\\Windows\\Temp\\shared-resource.tmp"; let pids = find_processes_locking_path(target); if ready { // Results include SYSTEM and other-user processes println!("Full scan found {} locking process(es): {{pids:?}}", pids.len()); } else { // Results limited to processes accessible without SeDebugPrivilege println!("Partial scan found {} locking process(es): {{pids:?}}", pids.len()); } } ``` -------------------------------- ### Add filelocksmith to Cargo.toml Source: https://github.com/velopack/filelocksmith-rs/blob/master/README.md Include filelocksmith as a dependency in your project's Cargo.toml file to use its functionality. ```toml [dependencies] filelocksmith = "0.1" ``` -------------------------------- ### pid_to_process_path Source: https://context7.com/velopack/filelocksmith-rs/llms.txt Resolves a given process ID (PID) to its corresponding executable file path. This function queries the process information and uses Windows APIs to retrieve the module name. It returns `None` if the process is inaccessible or has exited. ```APIDOC ## pid_to_process_path ### Description Opens the process with specific rights and calls `GetModuleFileNameExW` to obtain the full Win32 path of the process's main module. Returns `None` if the process no longer exists, has already exited, or the caller lacks sufficient privileges. ### Function Signature `pub fn pid_to_process_path(pid: usize) -> Option` ### Parameters #### Path Parameter - **pid** (usize) - Required - The process ID to resolve. ### Request Example ```rust use filelocksmith::pid_to_process_path; let pid = 1234usize; match pid_to_process_path(pid) { Some(path) => println!("PID {pid} -> {path}"), None => println!("PID {pid} -> "), } ``` ### Response #### Success Response - **Option** - An `Option` containing the full path to the process's executable if successful, otherwise `None`. ``` -------------------------------- ### Find and Quit Processes Locking a File in Rust Source: https://github.com/velopack/filelocksmith-rs/blob/master/README.md Use filelocksmith to find processes locking a specified file path and then quit those processes. Ensure the path is correctly formatted for Windows. ```rust use filelocksmith::{find_processes_locking_path, quit_processes, pid_to_process_path}; let path = "C:\\path\\to\\file.txt"; let pids = find_processes_locking_path(path); // print paths of processes locking the file for pid in &pids { println!("[{}] {:?}", pid, pid_to_process_path(*pid)); } // quit the processes locking the file if quit_processes(pids) { println!("Processes quit successfully"); } ``` -------------------------------- ### Terminate Processes by PID List Source: https://context7.com/velopack/filelocksmith-rs/llms.txt Use `quit_processes` to terminate a list of processes by their PIDs. It returns `true` if all processes were successfully terminated or if the list was empty. Returns `false` if any process could not be opened or terminated, which might happen if a process has already exited or due to insufficient privileges. ```rust use filelocksmith::{find_processes_locking_path, quit_processes, pid_to_process_path}; fn unlock_path(path: &str) -> bool { let pids = find_processes_locking_path(path); if pids.is_empty() { println!("Path is already unlocked."); return true; } println!("Terminating {} locking process(es):", pids.len()); for pid in &pids { let exe = pid_to_process_path(*pid).unwrap_or_default(); println!(" [{{pid}}] {{exe}}"); } let success = quit_processes(pids); if success { println!("All locking processes terminated."); } else { println!("WARNING: Some processes could not be terminated."); } success } fn main() { let target = r"C:\\staging\\update-package.zip"; let unlocked = unlock_path(target); if unlocked { // Safe to delete / move the file now std::fs::remove_file(target).expect("failed to remove file"); println!("File removed successfully."); } } ``` -------------------------------- ### Find Processes Locking a File or Directory in Rust Source: https://context7.com/velopack/filelocksmith-rs/llms.txt Use this function to find all process IDs (PIDs) that have a lock on a given file or directory. It requires administrator privileges to detect all locking processes. Call `set_debug_privilege` beforehand if full system visibility is needed. ```rust use filelocksmith::{find_processes_locking_path, is_process_elevated, set_debug_privilege, pid_to_process_path}; fn main() { // Optionally escalate to see all processes (requires admin) if is_process_elevated() { let granted = set_debug_privilege(); println!("SeDebugPrivilege granted: {granted}"); } // Works with both files and directories let file_path = r"C:\Users\user\Downloads\setup.exe"; let pids = find_processes_locking_path(file_path); if pids.is_empty() { println!("No processes are locking {file_path}"); } else { println!("{} process(es) locking the file:", pids.len()); for pid in &pids { let name = pid_to_process_path(*pid) .unwrap_or_else(|| "".to_string()); println!(" PID {pid}: {name}"); } } // Also works on directories — detects any process with an open handle // inside the directory tree let dir_path = r"C:\Program Files\MyApp"; let dir_pids = find_processes_locking_path(dir_path); println!("Processes locking directory: {dir_pids:?}"); } ``` -------------------------------- ### is_process_elevated Source: https://context7.com/velopack/filelocksmith-rs/llms.txt Checks if the current process is running with administrator privileges. This is done by querying the process token for elevation status. It's recommended to call this before performing operations that require elevated rights or to warn users about potential detection limitations. ```APIDOC ## is_process_elevated ### Description Queries the current process token with `GetTokenInformation(TokenElevation)` and returns `true` if the token's `TokenIsElevated` field is non-zero. Use this before calling `set_debug_privilege` or before warning users that detection results may be incomplete. ### Usage Example ```rust use filelocksmith::is_process_elevated; let elevated = is_process_elevated(); println!("Running as administrator: {elevated}"); if !elevated { eprintln!("WARNING: Not running as administrator. Processes owned by other users or elevated processes may not be detected."); } ``` ``` -------------------------------- ### quit_processes Source: https://context7.com/velopack/filelocksmith-rs/llms.txt Terminates a list of processes specified by their Process IDs (PIDs). It attempts to open each process with termination rights, calls `TerminateProcess`, and closes the handle. Returns true if all processes were successfully terminated or if the list was empty; returns false if any process could not be opened or terminated. ```APIDOC ## quit_processes ### Description Iterates the supplied PIDs, opens each with `PROCESS_TERMINATE` rights, calls `TerminateProcess`, and closes the handle. Returns `true` if every process was successfully opened and terminated (or the list was empty), and `false` if any process could not be opened (e.g., the process already exited between detection and termination, or privileges were insufficient). ### Usage Example ```rust use filelocksmith::quit_processes; let pids_to_terminate = vec![1234, 5678]; let success = quit_processes(pids_to_terminate); if success { println!("All specified processes terminated successfully."); } else { println!("Warning: Some processes could not be terminated."); } ``` ``` -------------------------------- ### set_debug_privilege Source: https://context7.com/velopack/filelocksmith-rs/llms.txt Acquires the `SeDebugPrivilege` for the current process. This privilege is necessary to view and interact with handles owned by other processes, including elevated ones, providing complete system visibility for file locking detection. ```APIDOC ## set_debug_privilege ### Description Acquires the `SeDebugPrivilege` for the current process. This privilege is required to see handles owned by elevated processes, enabling complete system visibility for file locking detection. ### Function Signature `pub fn set_debug_privilege() -> bool` ### Parameters None ### Request Example ```rust use filelocksmith::set_debug_privilege; let granted = set_debug_privilege(); println!("SeDebugPrivilege granted: {granted}"); ``` ### Response #### Success Response - **bool** - Returns `true` if the privilege was successfully acquired, `false` otherwise. ``` -------------------------------- ### find_processes_locking_path Source: https://context7.com/velopack/filelocksmith-rs/llms.txt Finds all process IDs (PIDs) that are holding a lock on a specified file or directory. This function enumerates system handles and scans loaded modules to identify locking processes. Administrator privileges are recommended for complete results. ```APIDOC ## find_processes_locking_path ### Description Enumerates every open handle on the system and scans loaded modules to identify processes locking a given file or directory. Returns a `Vec` of matching process IDs. Requires administrator privileges for complete results; call `set_debug_privilege` first. ### Function Signature `pub fn find_processes_locking_path(path: &str) -> Vec` ### Parameters #### Path Parameter - **path** (string) - Required - The file or directory path to check for locks. ### Request Example ```rust use filelocksmith::find_processes_locking_path; let file_path = r"C:\Users\user\Downloads\setup.exe"; let pids = find_processes_locking_path(file_path); if pids.is_empty() { println!("No processes are locking {file_path}"); } else { println!("{} process(es) locking the file:", pids.len()); for pid in &pids { // ... further processing with pid ... } } ``` ### Response #### Success Response - **Vec** - A vector containing the PIDs of processes that have a lock on the specified path. ``` -------------------------------- ### Resolve Process ID to Executable Path in Rust Source: https://context7.com/velopack/filelocksmith-rs/llms.txt This function resolves a given process ID (PID) to its corresponding executable file path. It may return `None` if the process has exited, or if the caller lacks the necessary privileges to query it. ```rust use filelocksmith::pid_to_process_path; fn print_process_info(pids: &[usize]) { for &pid in pids { match pid_to_process_path(pid) { Some(path) => println!("PID {pid:6} -> {path}"), None => println!("PID {pid:6} -> "), } } } fn main() { // Inspect a known PID (e.g. the current process) let my_pid = std::process::id() as usize; if let Some(path) = pid_to_process_path(my_pid) { println!("Current process: {path}"); } // Typical usage after find_processes_locking_path let pids = vec![1234usize, 5678, 9012]; print_process_info(&pids); } ``` -------------------------------- ### set_debug_privilege Source: https://context7.com/velopack/filelocksmith-rs/llms.txt Grants the current process the `SeDebugPrivilege`. This privilege is necessary to allow the process to query handles of all processes on the system, including those owned by other users or the SYSTEM account. This function requires the process to be already running as an administrator and returns `false` if the privilege adjustment fails. ```APIDOC ## set_debug_privilege ### Description Calls `OpenProcessToken` + `AdjustTokenPrivileges` to enable `SE_DEBUG_NAME` on the current process token. This privilege allows `NtQuerySystemInformation` to enumerate handles belonging to every process on the system, including those owned by other users or the SYSTEM account. Requires that the current process is already running as administrator; returns `false` if the privilege adjustment fails. ### Usage Example ```rust use filelocksmith::{is_process_elevated, set_debug_privilege}; if !is_process_elevated() { eprintln!("Must be run as administrator to enable SeDebugPrivilege."); } else { if set_debug_privilege() { println!("SeDebugPrivilege enabled — full system scan available."); } else { eprintln!("Failed to enable SeDebugPrivilege."); } } ``` ``` -------------------------------- ### is_process_elevated Source: https://context7.com/velopack/filelocksmith-rs/llms.txt Checks if the current process is running with administrator privileges. This is useful to determine if `set_debug_privilege` is necessary or likely to succeed. ```APIDOC ## is_process_elevated ### Description Checks if the current process is running with administrator privileges. This function is useful for determining whether escalating privileges is necessary or feasible. ### Function Signature `pub fn is_process_elevated() -> bool` ### Parameters None ### Request Example ```rust use filelocksmith::is_process_elevated; if is_process_elevated() { println!("Process is elevated."); } else { println!("Process is not elevated."); } ``` ### Response #### Success Response - **bool** - Returns `true` if the process is elevated, `false` otherwise. ``` -------------------------------- ### Check if Current Process is Elevated Source: https://context7.com/velopack/filelocksmith-rs/llms.txt Use `is_process_elevated` to determine if the current process is running with administrator privileges. This is crucial for understanding the scope of process detection; non-elevated processes may not detect all locking processes. ```rust use filelocksmith::{is_process_elevated, find_processes_locking_path}; fn main() { let elevated = is_process_elevated(); println!("Running as administrator: {{elevated}}"); if !elevated { eprintln!( "WARNING: Not running as administrator. \n Processes owned by other users or elevated processes may not be detected." ); } let pids = find_processes_locking_path(r"C:\\important\\file.dat"); println!("Detected locking PIDs: {{pids:?}}"); // Non-elevated run may miss elevated processes; // elevated run with SeDebugPrivilege sees everything. } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.