This class can be used to implement file-based directory locks.
Many classes of this project open some directories (workspaces, projects, library elements, ...). But it's very dangerous if a directory is opened multiple times simultaneously (by the same or another instance of the application, maybe even on different computers if the directories are located on a network drive). To avoid such problems, this class provides a mechanism to create directory locks.
How such a directory lock works:
Let's say that you want to open the directory "/foo/bar/". Then a lock file with the filepath "/foo/bar/.lock" will be created. After closing the directory, the lock file will be removed. So, while the directory (e.g. a project) is open, there will be a lock file in the same directory. If the same or another instance of the application now wants to open the same directory at the same time, the lock file is detected and opening the directory will be denied.
The lock file is a simple UTF-8 encoded text file with 5 lines with following values:
- The full name (first name + last name) of the user which holds the lock
- The username (logon name) of the user which holds the lock
- The hostname of the user's computer which holds the lock
- The process id (PID) of the application instance which holds the lock
- The process name of the application instance which holds the lock
- The datetime when the lock file was created/updated (UTC and ISO format!)
Example:
Homer Simpson
homer
homer-workstation
1234
2013-04-13T12:43:52Z
Definition: occmodel.cpp:77
The lock file (and especially its content) is also used to detect application crashes. If the application crashes while a directory was locked, the lock file will still exist after the application crashed. Now, if the user tries to open the locked directory again, the content of the lock file will be parsed. If the username and the hostname in the lock file is equal to the current user which tries to get the lock, it's clear that the lock file does NOT exist because the locked directory is already open, but that the application crashed while the directory was locked. If there exists a backup of the locked directory (e.g. project auto-save), this allows to ask the user whether the backup should be restored or not.
How to use this class:
First, you need to create an instance of this class for the directory you want to protect with a lock. There are two different constructors for this purpose. If you use the default constructor, you need to call setDirToLock() afterwards. Now you can read the lock status of the specified directory with getStatus(). With lock() you can create the lock file, and with unlock() you can remove the lock file. There are also the two convenience methods tryLock() and unlockIfLocked(), just read their documentation for more information.
- Note
- The destructor will automatically call unlockIfLocked(). This allows a reliable implementation of a directory lock, because you can add a DirectoryLock instance to the attributes of your class which access a directory which should be locked. This will ensure that the lock will be released when your object gets destroyed (RAII). See the code example below.
Code Example:
class MyDirectoryOpeningClass {
public:
MyDirectoryOpeningClass()
mLock.setDirToLock(
FilePath(
"C:/myDirectory"));
switch (mLock.getStatus()) {
mLock.lock();
break;
break;
default:
break;
}
}
~MyDirectoryOpeningClass() {
}
private:
};
@ LockedByThisApp
The directory is locked by this application instance.
@ LockedByOtherUser
The directory is locked by another user or machine.
@ LockedByUnknownApp
The directory is locked by an unknown application (may be stale).
@ Unlocked
The directory is not locked (lock file does not exist).
@ StaleLock
The directory is locked by a crashed application instance.
@ LockedByOtherApp
The directory is locked by another application instance on this machine.
DirectoryLock() noexcept
The default constructor.
Definition: directorylock.cpp:41
The Exception class.
Definition: exceptions.h:84
This class represents absolute, well-formatted paths to files or directories.
Definition: filepath.h:129