KiCad PCB EDA Suite
Loading...
Searching...
No Matches
KIPLATFORM::IO::FILE_LOCK Class Reference

An exclusive advisory lock on a file, held for the lifetime of this object. More...

#include <io.h>

Public Types

enum class  STATE { NONE , HELD , BUSY , UNSUPPORTED }
 

Public Member Functions

 FILE_LOCK ()=default
 
 ~FILE_LOCK ()
 
 FILE_LOCK (FILE_LOCK &&aOther) noexcept
 
FILE_LOCKoperator= (FILE_LOCK &&aOther) noexcept
 
 FILE_LOCK (const FILE_LOCK &)=delete
 
FILE_LOCKoperator= (const FILE_LOCK &)=delete
 
STATE Acquire (const wxString &aPath, bool &aCreated)
 Open aPath, creating it if it does not exist, and try to take the lock without ever blocking on it.
 
bool OpenForInspect (const wxString &aPath, bool &aHeldByAnother)
 Open an existing file and report whether another process holds its lock, creating nothing and keeping no lock of our own, so that a caller which only wants to know who holds a lock cannot disturb it.
 
bool IsOpen () const
 
bool ReadAll (std::string &aContents) const
 Read the whole file through the descriptor we hold.
 
bool Rewrite (const std::string &aContents)
 Replace the file contents through the descriptor we hold, keeping the same inode.
 
void Release ()
 Release the lock and close the file.
 

Private Attributes

int m_fd = -1
 
STATE m_state = STATE::NONE
 

Detailed Description

An exclusive advisory lock on a file, held for the lifetime of this object.

The operating system releases the lock when the holding process dies, however it dies, so a lock that can be taken proves that whoever wrote the file is gone. That answer costs nothing to obtain and needs no cooperation from the other process, unlike a recorded process id, which is meaningless outside the pid namespace that recorded it.

POSIX uses flock(2), which is held by the open file description; Windows locks a byte far past the end of the file so that readers can still see the contents. The descriptor is opened close-on-exec: a child that inherited it would keep the lock alive past the death of its owner.

Windows releases the locks of a process that died in its own time rather than at once, so shortly after a crash a lock can still read as held. That errs towards leaving a lock alone, which is the safe direction to err in.

Content is read and written through the locked descriptor rather than by path, so that a file replaced underneath us cannot be mistaken for the one we hold.

Definition at line 90 of file io.h.

Member Enumeration Documentation

◆ STATE

Enumerator
NONE 

No file is open.

HELD 

We hold the lock.

BUSY 

Another process holds the lock.

UNSUPPORTED 

The file is open but the filesystem cannot answer.

Definition at line 93 of file io.h.

Constructor & Destructor Documentation

◆ FILE_LOCK() [1/3]

KIPLATFORM::IO::FILE_LOCK::FILE_LOCK ( )
default

◆ ~FILE_LOCK()

KIPLATFORM::IO::FILE_LOCK::~FILE_LOCK ( )

Definition at line 481 of file common/io.cpp.

References Release().

◆ FILE_LOCK() [2/3]

KIPLATFORM::IO::FILE_LOCK::FILE_LOCK ( FILE_LOCK && aOther)
noexcept

Definition at line 487 of file common/io.cpp.

References FILE_LOCK().

◆ FILE_LOCK() [3/3]

KIPLATFORM::IO::FILE_LOCK::FILE_LOCK ( const FILE_LOCK & )
delete

References FILE_LOCK().

Member Function Documentation

◆ Acquire()

KIPLATFORM::IO::FILE_LOCK::STATE KIPLATFORM::IO::FILE_LOCK::Acquire ( const wxString & aPath,
bool & aCreated )

Open aPath, creating it if it does not exist, and try to take the lock without ever blocking on it.

The file stays open whatever the outcome, so the caller can read the contents of a lock another process holds.

Parameters
aPathfile to open and lock.
aCreatedset true if this call created the file, false if it existed.
Returns
the resulting lock state.

Definition at line 364 of file common/io.cpp.

References BUSY, HELD, m_fd, m_state, Release(), and UNSUPPORTED.

Referenced by BOOST_AUTO_TEST_CASE().

◆ IsOpen()

bool KIPLATFORM::IO::FILE_LOCK::IsOpen ( ) const

Definition at line 421 of file common/io.cpp.

References m_fd.

Referenced by ReadAll(), Release(), and Rewrite().

◆ OpenForInspect()

bool KIPLATFORM::IO::FILE_LOCK::OpenForInspect ( const wxString & aPath,
bool & aHeldByAnother )

Open an existing file and report whether another process holds its lock, creating nothing and keeping no lock of our own, so that a caller which only wants to know who holds a lock cannot disturb it.

Parameters
aPathfile to look at.
aHeldByAnotherset true only when another process provably holds the lock, so that an answer we cannot obtain never reads as in use.
Returns
true if the file was opened, and its contents can therefore be read.

Definition at line 400 of file common/io.cpp.

References m_fd, and Release().

Referenced by LOCKFILE::Inspect().

◆ operator=() [1/2]

FILE_LOCK & KIPLATFORM::IO::FILE_LOCK::operator= ( const FILE_LOCK & )
delete

References FILE_LOCK().

◆ operator=() [2/2]

KIPLATFORM::IO::FILE_LOCK & KIPLATFORM::IO::FILE_LOCK::operator= ( FILE_LOCK && aOther)
noexcept

Definition at line 493 of file common/io.cpp.

References FILE_LOCK(), m_fd, m_state, NONE, and Release().

◆ ReadAll()

bool KIPLATFORM::IO::FILE_LOCK::ReadAll ( std::string & aContents) const

Read the whole file through the descriptor we hold.

Definition at line 427 of file common/io.cpp.

References IsOpen(), and m_fd.

◆ Release()

void KIPLATFORM::IO::FILE_LOCK::Release ( )

Release the lock and close the file.

Definition at line 465 of file common/io.cpp.

References IsOpen(), m_fd, m_state, and NONE.

Referenced by Acquire(), OpenForInspect(), operator=(), and ~FILE_LOCK().

◆ Rewrite()

bool KIPLATFORM::IO::FILE_LOCK::Rewrite ( const std::string & aContents)

Replace the file contents through the descriptor we hold, keeping the same inode.

Definition at line 444 of file common/io.cpp.

References IsOpen(), and m_fd.

Member Data Documentation

◆ m_fd

int KIPLATFORM::IO::FILE_LOCK::m_fd = -1
private

Definition at line 156 of file io.h.

Referenced by Acquire(), IsOpen(), OpenForInspect(), operator=(), ReadAll(), Release(), and Rewrite().

◆ m_state

STATE KIPLATFORM::IO::FILE_LOCK::m_state = STATE::NONE
private

Definition at line 158 of file io.h.

Referenced by Acquire(), operator=(), and Release().


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