Skip to content

Commit

Permalink
fix #804: use xfun::file_rename() because file.rename() would fail to…
Browse files Browse the repository at this point in the history
… rename files across different disk volumes (#1476)
  • Loading branch information
yihui authored Oct 2, 2024
1 parent 5dcce03 commit 2770a24
Show file tree
Hide file tree
Showing 12 changed files with 16 additions and 49 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: bookdown
Type: Package
Title: Authoring Books and Technical Documents with R Markdown
Version: 0.40.1
Version: 0.40.2
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre"), email = "[email protected]", comment = c(ORCID = "0000-0003-0645-5666")),
person("Christophe", "Dervieux", , "[email protected]", role = c("ctb"),
Expand Down Expand Up @@ -65,7 +65,7 @@ Imports:
knitr (>= 1.38),
rmarkdown (>= 2.14),
jquerylib,
xfun (>= 0.39),
xfun (>= 0.47.12),
tinytex (>= 0.12),
yaml (>= 2.1.19)
Suggests:
Expand Down Expand Up @@ -94,3 +94,4 @@ Config/Needs/book: remotes, webshot, svglite
Config/Needs/website: pkgdown, tidyverse/tidytemplate, rstudio/quillt
Config/testthat/edition: 3
VignetteBuilder: knitr
Remotes: yihui/xfun
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import(utils)
importFrom(xfun,dir_create)
importFrom(xfun,dir_exists)
importFrom(xfun,existing_files)
importFrom(xfun,file_rename)
importFrom(xfun,in_dir)
importFrom(xfun,read_utf8)
importFrom(xfun,same_path)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
- If set to `TeX-AMS-MML_HTMLorMML` renders equations in HTML + CSS (which may look nicer for some equations).
- If set to `TeX-MML-AM_SVG` renders equations in SVG.

- Fixed the bug that `render_book()` fails due to `file.rename()` being unable to rename files across different disk volumes (thanks, @Giqles @katrinabrock, #804).

# CHANGES IN bookdown VERSION 0.40

- Footnotes are not rendered correctly when `katex` is used to render LaTeX math expressions (thanks, @pbreheny, #1470).
Expand Down
2 changes: 1 addition & 1 deletion R/bookdown-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"_PACKAGE"

## usethis namespace: start
#' @importFrom xfun dir_create dir_exists
#' @importFrom xfun dir_create dir_exists file_rename
## usethis namespace: end
NULL
2 changes: 1 addition & 1 deletion R/ebook.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ epub_book = function(
move_output = function(output) {
if (is.null(opts$get('output_dir'))) return(output)
output2 = output_path(output)
file.rename(output, output2)
file_rename(output, output2)
output2
}

Expand Down
2 changes: 1 addition & 1 deletion R/html.R
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ clean_html_tags = function(x) {
move_to_output_dir = function(files) {
files2 = output_path(files)
i = file.exists(files) & (files != files2)
file.rename(files[i], files2[i])
file_rename(files[i], files2[i])
files2
}

Expand Down
4 changes: 2 additions & 2 deletions R/latex.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ pdf_book = function(
if (is.null(o)) return(output)

output2 = file.path(o, output)
file.rename(output, output2)
if (keep_tex) file.rename(f, file.path(o, f))
file_rename(output, output2)
if (keep_tex) file_rename(f, file.path(o, f))
output2
}
# always enable tables (use packages booktabs, longtable, ...)
Expand Down
6 changes: 3 additions & 3 deletions R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ render_book = function(
aux_diro = '_bookdown_files'
# move _files and _cache from _bookdown_files to ./, then from ./ to _bookdown_files
aux_dirs = files_cache_dirs(aux_diro)
move_dirs(aux_dirs, basename(aux_dirs))
file_rename(aux_dirs, basename(aux_dirs))
on.exit({
aux_dirs = files_cache_dirs('.')
if (length(aux_dirs)) {
dir_create(aux_diro)
move_dirs(aux_dirs, file.path(aux_diro, basename(aux_dirs)))
file_rename(aux_dirs, file.path(aux_diro, basename(aux_dirs)))
}
}, add = TRUE)

Expand Down Expand Up @@ -215,7 +215,7 @@ render_new_session = function(files, main, config, output_format, clean, envir,

meta = clean_meta(render_meta, files)
move = !(unlist(meta) %in% files) # do not move input files to output dir
on.exit(file.rename(unlist(meta)[move], files_md[move]), add = TRUE)
on.exit(file_rename(unlist(meta)[move], files_md[move]), add = TRUE)

merge_chapters(unlist(meta), main, orig = files)

Expand Down
2 changes: 1 addition & 1 deletion R/skeleton.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ bookdown_skeleton = function(path, output_format = skeleton_formats()) {
skeleton_build_index(path, output_format)
skeleton_build_output_yml(path, output_format)
skeleton_build_bookdown_yml(path, output_format)
move_dir(file.path(path, output_format), path) # move left format files
file_rename(file.path(path, output_format), path) # move left format files
skeleton_remove_blocks(path, output_format)

# Get missing assets
Expand Down
13 changes: 0 additions & 13 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -457,19 +457,6 @@ files_cache_dirs = function(dir = '.') {
out
}

# file.rename() does not work if target directory is not empty, so we just copy
# everything from `from` to `to`, and delete `from`
move_dir = function(from, to) {
if (!dir_exists(to)) return(file.rename(from, to))
to_copy = list.files(from, full.names = TRUE)
if (length(to_copy) == 0 ||
any(file.copy(list.files(from, full.names = TRUE), to, recursive = TRUE))
) unlink(from, recursive = TRUE)
invisible(TRUE)
}

move_dirs = function(from, to) mapply(move_dir, from, to)

#' @importFrom xfun existing_files
existing_r = function(base) {
x = apply(expand.grid(base, c('R', 'r')), 1, paste, collapse = '.')
Expand Down
2 changes: 1 addition & 1 deletion inst/scripts/render_one.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ local({

out_expected = xfun::with_ext(args[1], '.md')
if (out != out_expected) {
file.rename(out, out_expected)
file_rename(out, out_expected)
attributes(out_expected) = attributes(out)
out = out_expected
}
Expand Down
24 changes: 0 additions & 24 deletions tests/testit/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -165,27 +165,3 @@ assert('fence_theorems() converts the knitr engine syntax to fenced Divs', {
res = fence_theorems(text = old)
(unclass(res) %==% old)
})

assert("move_dir works", {
# work in temp dir
dir.create(tmp_dir <- tempfile())
owd = setwd(tmp_dir)

# empty dir is not moved but deleted
dir.create("dest")
dir.create("empty")
move_dir("empty", "dest")
(dir.exists("empty") %==% FALSE)

# files are moved correctly
dir.create("filled")
dummy_files = c("dummy1", "dummy2")
file.create(file.path("filled", dummy_files))
move_dir("filled", "dest")
(dir.exists("filled") %==% FALSE)
(list.files("dest") %==% dummy_files)

# remove temp dir
setwd(owd)
unlink(tmp_dir, recursive = TRUE)
})

0 comments on commit 2770a24

Please sign in to comment.