r-lib / zip

Platform independent zip compression via miniz
https://r-lib.github.io/zip/
Other
83 stars 19 forks source link

Crash when attempting to overwrite open file #93

Closed sda030 closed 1 year ago

sda030 commented 1 year ago

Downstream packages that use zip rely on zip to fail safely when overwriting a zip-file (or e.g. a docx-compressed folder). Currently, zip::zip does not fail like utils::zip(). Apology for the not-strictly minimal reprex as a Word-file is excellent for showing this bug as Word keeps the file open. However, the officer-package is innocent in this regard.

Crashes:

library(officer)
x <- read_docx()  
print(x, target="test.docx")
# Manually open test.docx-file in Word
cat("test", file="testfile.txt") # make arbitrary file
zip::zipr(zipfile = "test.docx", files = "testfile.txt") # Crashes

## Only results in pretty error (not as pretty as cli though):
library(officer)
x <- read_docx()  
print(x, target="test.docx")
# Manually open test.docx-file in Word
cat("test", file="testfile.txt")
utils::zip(zipfile = "test.docx", files = "testfile.txt")
## zip I/O error: Device or resource busy
## zip error: Could not create output file (test.docx)

Created on 2022-11-03 with reprex v2.0.2

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.2.1 (2022-06-23 ucrt) #> os Windows 10 x64 (build 22621) #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate nb.utf8 #> ctype nb.utf8 #> tz Europe/Berlin #> date 2022-11-03 #> pandoc 2.19.2 @ C:/Program Files/RStudio/bin/quarto/bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> cli 3.4.1 2022-09-23 [1] CRAN (R 4.2.1) #> digest 0.6.30 2022-10-18 [1] CRAN (R 4.2.1) #> evaluate 0.17 2022-10-07 [1] CRAN (R 4.2.1) #> fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.0.3) #> fs 1.5.2 2021-12-08 [1] CRAN (R 4.1.2) #> glue 1.6.2 2022-02-24 [1] CRAN (R 4.1.2) #> highr 0.9 2021-04-16 [1] CRAN (R 4.0.5) #> htmltools 0.5.3 2022-07-18 [1] CRAN (R 4.2.1) #> knitr 1.40 2022-08-24 [1] CRAN (R 4.2.1) #> lifecycle 1.0.3 2022-10-07 [1] CRAN (R 4.2.1) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.1.3) #> officer * 0.4.4 2022-09-09 [1] CRAN (R 4.2.1) #> purrr 0.3.5 2022-10-06 [1] CRAN (R 4.2.1) #> R.cache 0.16.0 2022-07-21 [1] CRAN (R 4.2.1) #> R.methodsS3 1.8.2 2022-06-13 [1] CRAN (R 4.2.0) #> R.oo 1.25.0 2022-06-12 [1] CRAN (R 4.2.0) #> R.utils 2.12.1 2022-10-30 [1] CRAN (R 4.2.1) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.1.1) #> reprex 2.0.2 2022-08-17 [1] CRAN (R 4.2.1) #> rlang 1.0.6 2022-09-24 [1] CRAN (R 4.2.1) #> rmarkdown 2.17 2022-10-07 [1] CRAN (R 4.2.1) #> rstudioapi 0.14 2022-08-22 [1] CRAN (R 4.2.1) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.1.2) #> stringi 1.7.8 2022-07-11 [1] CRAN (R 4.2.1) #> stringr 1.4.1 2022-08-20 [1] CRAN (R 4.2.1) #> styler 1.8.0 2022-10-22 [1] CRAN (R 4.2.1) #> uuid 1.1-0 2022-04-19 [1] CRAN (R 4.2.0) #> withr 2.5.0 2022-03-03 [1] CRAN (R 4.1.2) #> xfun 0.34 2022-10-18 [1] CRAN (R 4.2.1) #> xml2 1.3.3 2021-11-30 [1] CRAN (R 4.1.2) #> yaml 2.3.6 2022-10-18 [1] CRAN (R 4.2.1) #> zip 2.2.2 2022-10-26 [1] CRAN (R 4.2.1) #> #> [1] C:/Users/py128/OneDrive - NIFU/R #> [2] C:/Users/py128/AppData/Local/Programs/R/R-4.2.1/library #> #> ────────────────────────────────────────────────────────────────────────────── ```
gaborcsardi commented 1 year ago

This appears to be fixed in the main branch now:

> zip::zipr(zipfile = "zip.dll", files = "testfile.txt") # Crashes
Error in zip_internal(zipfile, files, recurse, compression_level, append = FALSE,  :
  zip error: Cannot open zip file `zip.dll` for writing in file zip.c:309