Skip to content

Commit

Permalink
Backups now retain their original timestamp (last modified) where pos…
Browse files Browse the repository at this point in the history
…sible (even when zipped)
  • Loading branch information
Stefan Fleck committed Nov 23, 2020
1 parent eb2411f commit 55a18e7
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: rotor
Title: Log Rotation and Conditional Backups
Version: 0.3.2
Version: 0.3.2.9000
Authors@R:
person(given = "Stefan",
family = "Fleck",
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# rotor (dev)

* Backups now retain their original timestamp (created, last modified) where
possible (even when zipped)


# rotor 0.3.2

* fixes time zone related issue in `Cache$prune()`.
Expand Down
1 change: 1 addition & 0 deletions R/copy_or_compress.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,6 @@ copy_or_compress <- function(
stop("should not be possible to arrive here")
}

Sys.setFileTime(outname, file.mtime(file))
outname
}
2 changes: 1 addition & 1 deletion R/utils-fs.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ file_copy <- function(
return(DRY_RUN$create(to))
}

file.copy(from, to, ...)
file.copy(from, to, ..., copy.date = TRUE)
}


Expand Down
33 changes: 33 additions & 0 deletions tests/testthat/test_copy_or_compress.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ context("copy_or_compress")

dr <- tempdir()
td <- file.path(dr, "rotor")
timestamp_tolerance <- 10 # seconds

dir.create(td, recursive = TRUE)
teardown({
unlink(td, recursive = TRUE)
Expand All @@ -10,6 +12,17 @@ teardown({



create_testfile <- function(){
tf <- file.path(td, "compresstest.log")
saveRDS(iris, file = tf, compress = FALSE)
fake_time <- as.POSIXct("1990-01-01 02:03:04")
Sys.setFileTime(tf, fake_time)
expect_true(equalish(file.info(tf)$mtime, fake_time, timestamp_tolerance), "cannot fake timestamp")
tf
}




test_that("copy_or_compress works with default zip command", {
if (!is_zipcmd_available())
Expand Down Expand Up @@ -58,3 +71,23 @@ test_that("copy_or_compress works with zip::zipr", {
expect_identical(zip::zip_list(r)[1, ]$filename, "compresstest.log")
})




test_that("copy_or_compress preserves timestamp", {
tf <- create_testfile()
on.exit(unlink(tf))

copy <- copy_or_compress(tf, paste0(tf, ".copy"))
on.exit(unlink(copy), add = TRUE)
expect_true(equalish(file.mtime(tf), file.mtime(copy), tolerance = timestamp_tolerance))

zip <- copy_or_compress(tf, tf, compression = "utils::zip")
on.exit(unlink(zip), add = TRUE)
expect_true(equalish(file.mtime(zip), file.mtime(tf), timestamp_tolerance))
unlink(zip)

zip <- copy_or_compress(tf, tf, compression = "zip::zipr")
expect_true(equalish(file.mtime(zip), file.mtime(tf), timestamp_tolerance))
})

0 comments on commit 55a18e7

Please sign in to comment.