lockDirectory: Lock and unlock directories

View source: R/lockDirectory.R

lockDirectoryR Documentation

Lock and unlock directories

Description

Mark directories as locked or unlocked for thread-safe processing, using a standard naming scheme for the lock files.

Usage

lockDirectory(path, ...)

unlockDirectory(lock.info, clear = TRUE, ...)

Arguments

path

String containing the path to a versioned directory. The dirname should be the package cache while the basename should be a version number.

...

For lockDirectory, further arguments to pass to lock.

For unlockDirectory, further arguments to pass to clearDirectories.

lock.info

The list returned by lockDirectory.

clear

Logical scalar indicating whether to remove expired versions via clearDirectories.

Details

lockDirectory actually creates two locks:

  • The first lock is applied to the versioned directory (i.e., basename(path)) within the package cache (i.e., dirname(path)). This provides thread-safe read/write on its contents, protecting against other processes that want to write to the same versioned directory. Concurrent read operations are also permitted by setting exclusive=FALSE in ... to define a shared lock..

  • The second lock is applied to the package cache and is always a shared lock, regardless of the contents of .... This provides thread-safe access to the lock file used in the first lock, protecting it from deletion when the relevant directory expires in clearDirectories.

If dirname(path) does not exist, it will be created by lockDirectory.

clearDirectories is called in unlockDirectory as the former needs to hold an exclusive lock on the package cache. Thus, the clearing can only be performed after the shared lock created by lockDirectory is released.

Value

lockDirectory returns a list of locking information, including lock handles generated by the filelock package.

unlockDirectory unlocks the handles generated by lockDirectory. If clear=TRUE, versioned directories that have expired are removed by clearDirectories. It returns a NULL invisibly.

Author(s)

Aaron Lun

Examples

# Creating the relevant directories.
cache.dir <- tempfile(pattern="expired_demo")
version <- package_version("1.11.0")

handle <- lockDirectory(file.path(cache.dir, version))
handle
unlockDirectory(handle)

list.files(cache.dir)


LTLA/dir.expiry documentation built on Sept. 14, 2022, 4:12 p.m.