stc
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | List of all members
stc::FileLock Class Reference

#include <FileLock.hpp>

Public Types

enum class  Errors { OPEN_ERROR = 42 , LOCK_ERROR = 43 }
 

Public Member Functions

 FileLock (const std::filesystem::path &lockPath, bool lockNonblocking=true)
 
 ~FileLock ()
 
 FileLock (const FileLock &)=delete
 
FileLockoperator= (const FileLock &)=delete
 
bool hasLock ()
 
void unlock ()
 

Static Public Member Functions

static std::shared_ptr< FileLockdynamicAcquireLock (const std::filesystem::path &path, std::function< bool()> control, unsigned int sleepSeconds=1)
 

Detailed Description

Single-use file lock. It should NOT be used on the file meant to be protected, but a second file exclusively meant to indicate whether some other file or resource is currently locked. This class deletes the lockfile after use as well, meaning if you use it on a file you want to use normally, it will be deleted.

This won't actually prevent access to the relevant resources, but this strategy allows for programs to respect each other.

This class only does locking; any type of error handling has to be done by the programmer using this class.

There's two ways to lock a file; either, the constructor can be used, or a helper method within the class can be used. The helper method exists to make a minor framework around a repetitive check with some function callback; see its documentation for the exact callback details. The callback determines whether or not to continue, as well as allowing the programmer to specify a wait handler. Examples include updating a popup, printing a timer in the terminal, printing or rendering a progress bar, etc.

Member Enumeration Documentation

◆ Errors

enum class stc::FileLock::Errors
strong
Enumerator
OPEN_ERROR 
LOCK_ERROR 

Constructor & Destructor Documentation

◆ FileLock() [1/2]

stc::FileLock::FileLock ( const std::filesystem::path &  lockPath,
bool  lockNonblocking = true 
)
inline

This construtor creates the file lock.

Throws a value from Errors that can be used for error checking.

Parameters
lockPathThe path to the lockfile
lockNonblockingWhether or not to use LOCK_NB for Linux, or an equivalent for other operating systems if one exists. Default true. No effect on Windows due to implementation mechanics

◆ ~FileLock()

stc::FileLock::~FileLock ( )
inline

◆ FileLock() [2/2]

stc::FileLock::FileLock ( const FileLock )
delete

Member Function Documentation

◆ dynamicAcquireLock()

static std::shared_ptr< FileLock > stc::FileLock::dynamicAcquireLock ( const std::filesystem::path &  path,
std::function< bool()>  control,
unsigned int  sleepSeconds = 1 
)
inlinestatic

Utility method for lock acquiring; instead of an immediate fast fail or blocking while waiting for an unlock, this method uses failure to call a control method that allows for more responsive user interfaces.

The control method can also completely abort the locking. For instance, if you only have so much time to spend on waiting for the lock, your control function can check whether some amount of time has passed before returning false.

If there's a UI-centric application, a variable can be used to connect a "cancel" button to the control function, aborting the process if the user has had enough.

Automated systems can attempt to use a timer to determine whether to blatantly ignore the lock or not as well. For instance, if it's guaranteed that certain lock applications won't exceed a certain amount of time locked.

Parameters
pathThe path to the lockfile
controlThe control function; decides whether or not to continue, and it's highly recommended it's used to update the user interface.
sleepSecondsHow long to sleep between attempts; the default is one second. Note that due to fundamental inaccuracies in computers, any time inserted here isn't guaranteed to be accurate down to the max double resolution. Control invokations are not good for an exact indication of elapsed time.
Returns
nullptr if the lock wasn't acquired after control returns false, or if an OPEN_ERROR is met.
lock if the lock was acquired

◆ hasLock()

bool stc::FileLock::hasLock ( )
inline

Returns whether or not this process has a lock on the file. This does NOT indicate whether or not the file is locked by someone. If the constructor throws because it fails to create the lock

◆ operator=()

FileLock & stc::FileLock::operator= ( const FileLock )
delete

◆ unlock()

void stc::FileLock::unlock ( )
inline

Clears the lock.

Note that while this can be called an unlimited number of times, it's only useful once. The lock cannot be reacquired when it has been unlocked without creating a new lock for reasons that currently are future Windows compatibility because constness.


The documentation for this class was generated from the following file: