Skip to content

Commit

Permalink
test app to test file locking usecases
Browse files Browse the repository at this point in the history
  • Loading branch information
summeroff committed Apr 24, 2019
1 parent 684535b commit 19f3375
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
20 changes: 20 additions & 0 deletions CMakeLists.txt
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)
19 changes: 19 additions & 0 deletions README.md
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.
63 changes: 63 additions & 0 deletions file_blocker.cpp
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);
}

0 comments on commit 19f3375

Please sign in to comment.