Skip to content

Commit

Permalink
Enforce safeguards when moving files
Browse files Browse the repository at this point in the history
  • Loading branch information
Arni Magnusson committed Jun 28, 2018
1 parent ff4d679 commit b49a727
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
6 changes: 4 additions & 2 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ o Changed clean() so user can specify directories to remove.

o Changed write.taf() to search in the global workspace if 'x' is a string.

o Changed cp() to enforce safeguards when moving files.




Expand All @@ -29,7 +31,7 @@ o Changed read.taf() and write.taf() so they can read/write many tables in one

o Changed write.taf() so the name of the data frame is the default filename.

o Changed cp() so it preserves the timestamp when copying a file.
o Changed cp() to preserve the timestamp when copying a file.



Expand Down Expand Up @@ -61,7 +63,7 @@ o Added arguments 'include' and 'engine' to make().

o Added argument 'local' to sourceTAF(), replacing the 'rm' argument.

o Changed sourceAll() so it only runs TAF scripts: data.R, input.R, model.R,
o Changed sourceAll() to only run TAF scripts: data.R, input.R, model.R,
output.R, and report.R.


Expand Down
26 changes: 26 additions & 0 deletions R/cp.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,24 @@
#'
#' @return \code{TRUE} for success, \code{FALSE} for failure, invisibly.
#'
#' @note
#' To prevent accidental loss of files, two safeguards are enforced when
#' \code{move = TRUE}:
#' \enumerate{
#' \item When moving files, the \code{to} argument must either have a filename
#' extension or be an existing directory.
#' \item When moving many files to one destination, the \code{to} argument must
#' be an existing directory.
#' }
#' If these conditions do not hold, no files are changed and an error is
#' returned.
#'
#' @seealso
#' \code{\link{file.copy}} and \code{\link{unlink}} are the underlying functions
#' used to copy and (if \code{move = TRUE}) delete files.
#'
#' \code{\link{file.rename}} is the base function to rename files.
#'
#' \code{\link{icesTAF-package}} gives an overview of the package.
#'
#' @examples
Expand All @@ -27,6 +41,8 @@
#' cp("datasets/*", "everything")
#' }
#'
#' @importFrom tools file_ext
#'
#' @export

cp <- function(from, to, move=FALSE)
Expand All @@ -35,6 +51,16 @@ cp <- function(from, to, move=FALSE)
## in case some filenames without asterisk are not found
from <- sort(unique(c(Sys.glob(from), from[!grepl("\\*", from)])))

if(move)
{
## Safeguard 1: destination must have file_ext or exist as dir
if(any(file_ext(to)=="" & !dir.exists(to)))
stop("when moving, 'to' must have file extension or exist as directory")
## Safeguard 2: many-one, dir must exist
if(length(from)>1 && length(to)==1 && !dir.exists(to))
stop("when moving many -> one, 'to' must be an existing directory")
}

out <- mapply(file.copy, from, to, overwrite=TRUE,
recursive=dir.exists(from), copy.date=TRUE)
if(move)
Expand Down
14 changes: 14 additions & 0 deletions man/cp.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b49a727

Please sign in to comment.