-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test app to test file locking usecases
- Loading branch information
Showing
3 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,20 @@ | ||
cmake_minimum_required(VERSION 3.10) | ||
|
||
ADD_DEFINITIONS(-DUNICODE) | ||
ADD_DEFINITIONS(-D_UNICODE) | ||
|
||
file(GLOB SOURCES "*.cpp") | ||
|
||
project(file_blocker_tester) | ||
add_executable(file_blocker_tester ${SOURCES} ) | ||
|
||
if(WIN32) | ||
set_target_properties(file_blocker_tester PROPERTIES LINK_FLAGS_DEBUG "/SUBSYSTEM:CONSOLE") | ||
set_target_properties(file_blocker_tester PROPERTIES COMPILE_DEFINITIONS_DEBUG "_CONSOLE") | ||
set_target_properties(file_blocker_tester PROPERTIES LINK_FLAGS_RELWITHDEBINFO "/SUBSYSTEM:CONSOLE") | ||
set_target_properties(file_blocker_tester PROPERTIES COMPILE_DEFINITIONS_RELWITHDEBINFO "_CONSOLE") | ||
set_target_properties(file_blocker_tester PROPERTIES LINK_FLAGS_RELEASE "/SUBSYSTEM:CONSOLE") | ||
set_target_properties(file_blocker_tester PROPERTIES LINK_FLAGS_MINSIZEREL "/SUBSYSTEM:CONSOLE") | ||
endif(WIN32) | ||
|
||
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT file_blocker_tester) |
This file contains 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,19 @@ | ||
# file-blocker | ||
Simple console app that can be used to test use case when file access blocked by other process. | ||
|
||
|
||
## Build | ||
``` | ||
mkdir build | ||
cd build | ||
cmake -G "Visual Studio 15 2017 Win64" ../ | ||
cmake --build . --target ALL_BUILD --config Release -- /p:CharacterSet=Unicode | ||
``` | ||
|
||
## Running | ||
|
||
This app block itself. | ||
|
||
When run without params it just runs and that block file from being rewriten or deleted. | ||
|
||
When run with first param `-l` it will try to get exclusive access to itself with `LockFileEx` so no other app can even read that file. |
This file contains 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,63 @@ | ||
// file_blocker.cpp : This file contains the 'main' function. Program execution begins and ends there. | ||
// | ||
|
||
#include <iostream> | ||
#include <string> | ||
#include <Windows.h> | ||
|
||
bool use_extended_locking = false; | ||
|
||
int main(int argc, char * argv[]) | ||
{ | ||
HANDLE h_file = 0; | ||
BOOL lock_success = false; | ||
std::cout << "Hello there! Gonna lock this file\n"; | ||
|
||
if (argc >= 2) | ||
{ | ||
std::string first_param(argv[1]); | ||
if (first_param.compare("-l") == 0) | ||
{ | ||
use_extended_locking = true; | ||
} | ||
} | ||
|
||
if (use_extended_locking) | ||
{ | ||
wchar_t app_path_buffer[MAX_PATH + 1]; | ||
if (GetModuleFileName(NULL, app_path_buffer, MAX_PATH) == 0) | ||
{ | ||
std::cout << "Error: Failed get file path\n"; | ||
return; | ||
} | ||
|
||
std::wstring app_path_wstring = app_path_buffer; | ||
std::cout << "Run file: \"" << std::string(app_path_wstring.begin(), app_path_wstring.end()) << "\" (only block writing and deleting )\n"; | ||
|
||
OVERLAPPED overlapvar = { 0 }; | ||
|
||
std::cout << "Lock file: \"" << std::string(app_path_wstring.begin(), app_path_wstring.end()) << "\" (block reading too)\n"; | ||
|
||
h_file = CreateFile(app_path_buffer, GENERIC_READ, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); | ||
if (h_file) | ||
{ | ||
lock_success = LockFileEx(h_file, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0, MAXDWORD, MAXDWORD, &overlapvar); | ||
if (!lock_success) | ||
{ | ||
std::cout << "Error: Failed to lock file\n"; | ||
} | ||
} | ||
else { | ||
std::cout << "Error: Failed to lock file\n"; | ||
} | ||
} | ||
|
||
while (1) | ||
{ | ||
Sleep(500); | ||
std::cout << "Still blocking file: \"" << argv[0] << "\"\n"; | ||
} | ||
|
||
CloseHandle(h_file); | ||
} | ||
|