Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

consider 'with_backup' helper? #51

Open
kevinushey opened this issue Aug 30, 2017 · 3 comments
Open

consider 'with_backup' helper? #51

kevinushey opened this issue Aug 30, 2017 · 3 comments
Labels
feature a feature request or enhancement

Comments

@kevinushey
Copy link

I'm imaging a case where you're writing a function that attempts to write a file at some location, but restores the old copy (filesystem state) if something goes wrong during update. E.g.

with_backup <- function(file, expr) {
  
  backup <- tempfile(
    pattern = sprintf(".backup-%s-", tools::file_path_sans_ext(basename(file))),
    tmpdir = dirname(file),
    fileext = paste0(".", tools::file_ext(file))
  )
  
  file.rename(file, backup)
  on.exit({
    if (file.exists(file))
      unlink(backup)
    else
      file.rename(backup, file)
  })
  
  force(expr)
}
@krlmlr
Copy link
Member

krlmlr commented Aug 31, 2017

i'd always write to a temporary location and then substitute on success (via file rename). Even with a with_backup() function the in-place update is unsafe if the R session terminates (e.g., when debugging).

@jimhester
Copy link
Member

Yeah that seems reasonable, we can use a similar approach to with_tempfile(), which hasn't yet made it to master...

@DarwinAwardWinner
Copy link

I think a good name for the behavior suggested by @krlmlr would be with_atomic_file.

@jimhester jimhester added the feature a feature request or enhancement label May 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants