clearDirectories: Clear expired directories

View source: R/clearDirectories.R

clearDirectoriesR Documentation

Clear expired directories

Description

Remove versioned directories that have passed on expiration limit.

Usage

clearDirectories(dir, reference = NULL, limit = NULL, force = FALSE)

Arguments

dir

String containing the path to a package cache containing any number of versioned directories.

reference

A package_version specifying a reference version to be protected from deletion.

limit

Integer scalar specifying the maximum number of days to have passed before a versioned directory expires.

force

Logical scalar indicating whether to forcibly re-examine dir for expired versioned directories.

Details

This function checks the last access date in the *_dir.expiry files in dir. If the last access date is too old, the corresponding subdirectory in path is treated as expired and is deleted. The age threshold depends on limit, which defaults to the value of the environment variable BIOC_DIR_EXPIRY_LIMIT. If this is not specified, it is set to 30 days.

If reference is specified, any directory of that name is protected from deletion. In addition, directories with version numbers greater than (or equal to) reference are not deleted, even if their last access date was older than the specified limit. This aims to favor the retention of newer versions, which is generally a sensible outcome when the aim is to stay up-to-date.

This function will acquire exclusive locks on the package cache directory and on each versioned directory before attempting to delete the latter. Applications can achieve thread safety by calling lockDirectory prior to any operations on the versioned directory. This ensures that clearDirectories will not delete a directory in use by another process, especially if the latter might update the last access time.

By default, this function will remember the values of dir that were passed in previous calls, and will avoid re-examining those same dirs for expired directories on the same day. This avoids unnecessary file system queries and locks when this function is repeatedly called. Advanced users can force a re-examination by setting force=TRUE.

Value

Expired directories are deleted and NULL is invisibly returned.

Author(s)

Aaron Lun

See Also

touchDirectory, which calls this function automatically when clear=TRUE.

Examples

# Creating the package cache.
cache.dir <- tempfile(pattern="expired_demo")

# Creating an older versioned directory.
version <- package_version("1.11.0")
version.dir <- file.path(cache.dir, version)

lck <- lockDirectory(version.dir)
dir.create(version.dir)
touchDirectory(version.dir, date=Sys.Date() - 100)
unlockDirectory(lck, clear=FALSE) # manually clear below.

list.files(cache.dir)

# Clearing them out.
clearDirectories(cache.dir)
list.files(cache.dir)


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