checkFileSum: Validate a file against a checksum file

View source: R/fileUtils.R

checkFileSumR Documentation

Validate a file against a checksum file

Description

Validates a file using a specified checksum algorithm against a file that contains just the expected checksum result. By default looks for a file in parallel to the file tested, except with an additional extension matching the checksum algorithm. Can specify what to do after testing match through use of the ⁠as=⁠ argument. Supports returning TRUE/FALSE, a string message, or signaling a message via stop(), warning(), or message(). To support testing without throwing errors, any errors such as missing file are reported the same as an error triggered by a mismatched checksum, except it causes NA to be returned with as=test.

Usage

checkFileSum(
  file,
  algo = c("md5", "sha1", "crc32", "sha256", "sha512"),
  checkFile = NULL,
  as = c("test", "check", "error", "warning", "message", "stop")
)

Arguments

file

The file to validate

algo

The checksum algorithm to use (see digest::digest()).

checkFile

The name of the file holding the expected checksum value. If NULL, will attempt to guess; assumes named the same as ⁠file=⁠, except with an additional extension matching ⁠algo=⁠. If not found, looks for a hidden checkFile (with a leading "."). e.g. an md5 checkFile for file.txt would be guessed as file.txt.md5 or .file.txt.md5.

as

How the results of the checksum test are reported. as="test" returns TRUE if the checksums match, FALSE if they do not, and NA if there is an error looking for the requisite files. All others rely on a generated string - either the empty string "" when the checksums match, an error with the file names and checksums when they fail to match, or an error if something prevents them from being calculated (e.g. missing files). as="check" returns this string, as="error" and as="stop" are aliases; they either return the generated empty string (silently), or trigger an error exit if the string is not empty. as="warning" and as="message" return the generated string silently, but also signal the appropriate message if the string is not empty.

Value

Depends on ⁠as=⁠. With matching checksums, will either be TRUE or the empty string ⁠"⁠ (possibly silently). If checksums don't match, it will either be FALSE, a string giving the files and their mismatched checksums (possibly silently), or an error will be triggered. If there is a problem calculating the checksums, e.g. a file does not exits, the result will either be "NA", a string describing the problem, or an error will be triggered.


jefferys/JefferysRUtils documentation built on June 18, 2024, 4:39 a.m.