--- title: "User-defined symlink types" # For package builds output: rmarkdown::html_vignette Encoding: UTF-8 vignette: > %\VignetteIndexEntry{User-defined symlink types} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r setup, include = FALSE} knitr::opts_chunk$set(collapse = TRUE, comment = "#>") .opt_width <- options(width = 120) flag_eval <- vmTools::dir_supports_links(tempdir()) # Chunks with `mask_user = TRUE` print "USER" in place of the name of whoever # builds the vignette (the registry file records it in its `user` column). # "USER" is padded to the name's width so printed columns stay aligned. knitr::knit_hooks$set(output = local({ hook_output <- knitr::knit_hooks$get("output") user_name <- Sys.info()[["user"]] user_masked <- formatC("USER", width = nchar(user_name)) function(x, options) { if (isTRUE(options$mask_user)) x <- gsub(user_name, user_masked, x, fixed = TRUE) return(hook_output(x, options)) } })) ``` ```{r windows_note, echo = FALSE} if (!flag_eval) { knitr::asis_output( "> **Note:** This machine cannot create symlinks (shortcuts) in its temp folder > (on Windows that needs a local disk). Code is shown but not evaluated on this system." ) } ``` ```{r helpers, include = FALSE} library(vmTools) library(data.table) print_tree <- function(x, level = Inf) vmTools:::dir_tree(x, level = level) ``` The Symlink Tool comes with four symlink types: `best`, `latest`, `keep` and `remove`. You can add your own, for example `stalled` for runs waiting on an input. A type you add is recorded in the root folder itself, so every user of that root sees it. | Built-in type | `is_singleton` | Symlink name in the root | |---|---|---| | `best` | `TRUE` | `best` | | `latest` | `TRUE` | `latest` | | `keep` | `FALSE` | `keep_` | | `remove` | `FALSE` | `remove_` | # Add a symlink type Build a tool on your root as usual, then add the type with one call: ```{r setup_root, include = FALSE, eval = flag_eval} demo_base <- file.path(tempdir(), "slt_types_demo") model_root <- file.path(demo_base, "model_root") dir.create(model_root, recursive = TRUE, showWarnings = FALSE) model_root <- clean_path(model_root) .opt_mc <- options(mc.cores = 1L) ``` ```{r add_type, eval = flag_eval} slt <- SLT$new(root_list = list("model_root" = model_root)) slt$update_symlink_registry( symlink_type = "stalled" , is_singleton = FALSE , operation = "add" ) names(slt$get_symlink_registry()) ``` That is the only step. The type is recorded in the root, so it can be used by any later R session and any other user of the root. ## The arguments | Argument | What to pass | |---|---| | `symlink_type` | The name of your type, in lowercase. It cannot be a built-in name. | | `is_singleton` | `TRUE` or `FALSE`. See the next table. | | `operation` | `"add"` for a type the root does not have yet. `"update"` and `"drop"` are covered under Editing the registry. | Without a tool, `update_symlink_disk_registry()` does the same for any root folder. It takes the same arguments, plus `root` and `verbose`. ## Choosing `is_singleton` `is_singleton` is the one decision a symlink type needs: can one version hold the mark, or many? | `is_singleton` | Symlink name in the root | Versions that can hold it | Built-in examples | |---|---|---|---| | `TRUE` | `` | one per root; marking another version moves it | `best`, `latest` | | `FALSE` | `_` | as many as you mark | `keep`, `remove` | Once a type is added, its name is reserved in that way: a version folder cannot be called `stalled_anything`. ## Use the type Mark with it like any built-in type. ```{r use_type, eval = flag_eval} slt$make_new_version_folder("2025_01_01.01") slt$make_new_version_folder("2025_01_01.02") slt$mark("stalled", "2025_01_01.01", user_entry = list(comment = "waiting on inputs")) slt$mark("stalled", "2025_01_01.02", user_entry = list(comment = "waiting on inputs")) slt$roundup("stalled", return_as = "version_name") ``` ```{r use_type_tree, eval = flag_eval} print_tree(model_root, level = 1) ``` --- # How a tool uses the registry file `slt` does not keep its own list of symlink types. At each call it reads the registry file in its root, so a type a teammate adds to the root is there at your next call. The calls fall into two groups: ``` slt <- SLT$new(root_list = list("model_root" = model_root)) read-only calls write calls --------------- ----------- SLT$new() slt$mark() slt$roundup() slt$unmark() slt$get_symlink_registry() slt$delete_version_folders() ``` ``` any call above | v slt reads model_root/.symlink_registry.csv (no file yet: nothing to read, built-in types only) | v slt's symlink types = built-in types + user-defined types in the file (do not hand-edit) | v which kind of call? --- read-only -------> the call runs. | Nothing is written to model_root. | A missing registry file is NOT created. | - only built-in symlinks can be found when missing | i.e. 'best', 'latest', 'keep', 'remove' write | v does model_root have no a registry file? ------------------------> slt writes one, with the built-in | types, then the call runs. yes | v the call runs ``` Building a tool and `slt$roundup()` never write to the root, not even to create a missing registry file, so both work on a root you can _only_ read. ## Several roots **_SUNSET NOTICE:_** Most tools have one root. Support for several roots using one tool will likely be sunset in a future version, so prefer one tool per root for new work. Until then, a tool with several roots runs every safety check described here. A tool built on several roots runs the same flow, with two checks added. They are marked `[+]`. ```r slt <- SLT$new( root_list = list( "model_root" = model_root , "other_root" = other_root ) ) ``` ``` any call above | v slt reads .symlink_registry.csv in every root (a root with no file yet: nothing to read) | v [+] do two roots give one symlink yes type a different is_singleton? ------> STOP, nothing changed. | The error lists each root's no is_singleton and the call to run. | v slt's symlink types = built-in types + user-defined types in any root's file (do not hand-edit) | v which kind of call? --- read-only -------> the call runs. | Nothing is written to any root. | A missing registry file is NOT created. | - only built-in symlinks can be found when | every root's file is missing, | i.e. 'best', 'latest', 'keep', 'remove' write | v does every root have no a registry file? ------------------------> slt writes one in each root that | lacks it, with the built-in types. yes | v [+] does every root's file no list every type? --------------------> slt copies each missing row from | a root that has it. yes | v the call runs ``` The first added check is the one that can stop you. The roots of one tool must agree on each type's `is_singleton`. If they do not, the tool stops and prints the call that fixes it: ```{r roots_disagree, eval = flag_eval, error = TRUE} other_root <- file.path(demo_base, "other_root") dir.create(other_root) update_symlink_disk_registry( root = other_root , symlink_type = "stalled" , is_singleton = TRUE , operation = "add" , verbose = FALSE ) SLT$new(root_list = list("model_root" = model_root, "other_root" = other_root)) ``` A root that simply lacks a type is not a disagreement. That is the second added check: the root gets the type from its sibling roots the next time the tool marks, unmarks or deletes. --- # Editing the registry This section is for changing a type later, or for reading an error. `slt$update_symlink_registry()` edits every root of a tool; `update_symlink_disk_registry()` edits the roots you name. Both follow the same rules. ## The registry file Each root carries one small file, `.symlink_registry.csv`, with one row per symlink type. The built-in types are listed first. The tool writes this file; it is not meant to be edited by hand, and a hand-edited file is refused when it is read. ```{r show_file, eval = flag_eval, mask_user = TRUE} read.csv(file.path(model_root, ".symlink_registry.csv")) ``` The tool only manages symlinks whose type is in this file. A symlink of any other type is left alone, and `slt$make_reports()` lists it in `report_DISCREPANCIES.csv` as "non-tool symlinks in root folder". Add the type with `slt$update_symlink_registry()` to bring such symlinks back into the tool's understanding. ## Change `is_singleton`, or drop a type The same function does both, with a different `operation`: ```{r change_calls, eval = FALSE} # a type on file, with a different is_singleton slt$update_symlink_registry( "stalled" , is_singleton = TRUE , operation = "update" ) # remove a type from the file slt$update_symlink_registry( "stalled" , operation = "drop" ) ``` What happens depends on the `operation` argument and on the symlink type's row in the root's file: - `absent`: the file has no row for the symlink type - `matches`: a row exists and its `is_singleton` equals the argument - `differs`: a row exists and its `is_singleton` is the other value ```{r outcomes, echo = FALSE} knitr::kable(vmTools:::get_disk_registry_outcomes()) ``` `write` changes the file, `skip` leaves it, and `stop` refuses the whole call before any root's file is written. ## When a change is refused An `update` or a `drop` is refused while symlinks of that type exist. After the change they would belong to no symlink type the tool knows. ``` slt$update_symlink_registry(symlink_type, is_singleton, operation) | v in model_root/.symlink_registry.csv, the symlink type's row is absent, matches is_singleton, or differs | v is the operation refused? add where the row differs yes update where the row is absent ------> STOP, nothing written. | The error names the root and no the operation to use. | v update or drop: do links of the yes symlink type exist? ---------------------> STOP, nothing written. | The error prints the unmark() no calls to run first. | v slt writes model_root/.symlink_registry.csv (a file already as asked is left alone) ``` **_With several roots, every root is checked before any file is written. One refusal stops the whole call._** The two `stalled` symlinks made above block a change, and the error tells you which `unmark()` calls to run before repeating it: ```{r refused, eval = flag_eval, error = TRUE} slt$update_symlink_registry( "stalled" , is_singleton = TRUE , operation = "update" ) ``` ## Messages and what to do | The message says | What it means | What to do | |---|---|---| | `already on file with this is_singleton; nothing changed` | The root already has what you asked for. | Nothing. | | `already on file with a different is_singleton; use operation = 'update'` | You used `"add"` for a type the root has, with a different `is_singleton`. | Repeat the call with `operation = "update"`. | | `not on file; use operation = 'add'` | You used `"update"` for a type the root does not have. | Repeat the call with `operation = "add"`. | | `live and ... dangling link(s) of this type exist` | Symlinks of the type are in the way. | Run the `unmark()` lines printed in the error, then repeat the call. | | `Root folders disagree on is_singleton for a symlink (shortcut) type` | Two roots record the same type differently. | Run the printed `update_symlink_disk_registry()` call on the root you want to change. | | `Built-in symlink (shortcut) types are fixed by the package` | You passed `best`, `latest`, `keep` or `remove`. | Choose another name. | | `symlink_type must be lowercase` | The name has a capital letter. Windows and macOS compare names ignoring case. | Use the lowercase spelling the message suggests. | See also `?update_symlink_disk_registry`. ```{r cleanup, include = FALSE, eval = TRUE} if (flag_eval) { unlink(demo_base, recursive = TRUE) options(.opt_mc) } options(.opt_width) ```