portage.locks module

portage.locks._close_fds()

This is intended to be called after a fork, in order to close file descriptors for locks held by the parent process. This can be called safely after a fork without exec, unlike the _setup_pipes close_fds behavior.

Parameters

fd (Integer) – an open file descriptor

Return type

Integer

Returns

the current number of hardlinks to the file

portage.locks._get_lock_fn()

Returns fcntl.lockf if proven to work, and otherwise returns fcntl.flock. On some platforms fcntl.lockf is known to be broken.

class portage.locks._lock_manager(fd, fstat_result, path)

Bases: object

close()
fd
inode_key
portage.locks._lockfile_iteration(mypath, wantnewlockfile=False, unlinkfile=False, waiting_msg=None, flags=0)

Acquire a lock on mypath, without retry. Return None if the lockfile was removed by previous lock holder (caller must retry).

Parameters
  • mypath (str) – lock file path

  • wantnewlockfile (bool) – use a separate new lock file

  • unlinkfile (bool) – remove lock file prior to unlock

  • waiting_msg (str) – message to show before blocking

  • flags (int) – lock flags (only supports os.O_NONBLOCK)

Return type

bool

Returns

unlockfile tuple on success, None if retry is needed

portage.locks._lockfile_was_removed(lock_fd, lock_path)

Check if lock_fd still refers to a file located at lock_path, since the file may have been removed by a concurrent process that held the lock earlier. This implementation includes support for NFS, where stat is not reliable for removed files due to the default file attribute cache behavior (‘ac’ mount option).

Parameters
  • lock_fd (int) – an open file descriptor for a lock file

  • lock_path (str) – path of lock file

Return type

bool

Returns

a tuple of (removed, fstat_result), where removed is True if lock_path does not correspond to lock_fd, and False otherwise

portage.locks._test_lock_fn(lock_fn: Callable[[str, int, int], Callable[[], None]]) bool

Does the NFS, hardlink shuffle to ensure locking on the disk. We create a PRIVATE hardlink to the real lockfile, that is just a placeholder on the disk. If our file can 2 references, then we have the lock. :) Otherwise we lather, rise, and repeat.

portage.locks.hardlock_cleanup(path, remove_all_locks=False)
portage.locks.hardlock_name(path)
portage.locks.lockdir(mydir, flags=0)
portage.locks.lockfile(mypath, wantnewlockfile=0, unlinkfile=0, waiting_msg=None, flags=0)

If wantnewlockfile is True then this creates a lockfile in the parent directory as the file: ‘.’ + basename + ‘.portage_lockfile’.

portage.locks.unlockdir(mylock)
portage.locks.unlockfile(mytuple)