2323 invisible ()
2424}
2525
26+ # ' Delete a cache or temp directory
27+ # '
28+ # ' For safety, `path` is only deleted if it is a sub-directory of a temporary
29+ # ' directory or user cache. Since this function relies on `tools::R_user_dir()`,
30+ # ' it early returns `FALSE` on `R < 4.0.0`.
31+ # ' @param path Absolute path to a directory to delete.
32+ # ' @returns `TRUE` if anything was deleted, `FALSE` otherwise.
33+ # ' @keywords internal
34+ delete_if_cache_directory <- function (path ) {
35+ path <- normalizePath(path )
36+ if (getRversion() < package_version(" 4.0.0" )) {
37+ return (FALSE )
38+ }
39+ designated_cache_path <- normalizePath(tools :: R_user_dir(" R.cache" , which = " cache" ))
40+ is_in_tools_cache <- startsWith(path , designated_cache_path )
41+ temp_dir <- normalizePath(Sys.getenv(" TMPDIR" , Sys.getenv(" TMP" )))
42+ is_in_generic_cache <- startsWith(path , temp_dir )
43+ if (is_in_tools_cache || is_in_generic_cache ) {
44+ all_files <- list.files(path ,
45+ full.names = TRUE ,
46+ recursive = TRUE ,
47+ all.files = FALSE
48+ )
49+ if (length(all_files ) < 1L ) {
50+ unlink(path , recursive = TRUE )
51+ return (TRUE )
52+ }
53+ }
54+ FALSE
55+ }
56+
2657
2758ask_to_switch_to_non_default_cache_root <- function (ask = interactive()) {
2859 if (ask && stats :: runif(1L ) > 0.9 && is.null(getOption(" styler.cache_root" ))) {
@@ -40,14 +71,24 @@ ask_to_switch_to_non_default_cache_root_impl <- function() {
4071}
4172
4273remove_old_cache_files <- function () {
74+ path_version_specific <- R.cache :: getCachePath(c(" styler" , styler_version ))
4375 all_cached <- list.files(
44- R.cache :: getCachePath(c( " styler " , styler_version )) ,
76+ path_version_specific ,
4577 full.names = TRUE , recursive = TRUE
4678 )
4779 date_boundary <- Sys.time() - 60L * 60L * 24L * 6L
4880 file.remove(
4981 all_cached [file.info(all_cached )$ mtime < date_boundary ]
5082 )
83+ path_styler_specific <- dirname(path_version_specific )
84+ path_r_cache_specific <- dirname(path_styler_specific )
85+ paths <- normalizePath(
86+ c(path_version_specific , path_styler_specific , path_r_cache_specific )
87+ )
88+ purrr :: walk(
89+ paths ,
90+ delete_if_cache_directory
91+ )
5192}
5293
5394
0 commit comments