Expand description
Shared helpers for writing command output to files.
Ports the legacy CLIโs CliUtils.saveFile / CliUtils.saveResultToFile
(apps/cli/src/utils.ts) path-resolution rules, which are part of the user-facing
contract for every flag that takes an output location (bw receive --obj,
bw export --output, bw get attachment --output). Kept separate from the commands
themselves so those rules are defined โ and tested โ exactly once.
Functionsยง
- contains_
separator ๐ truewhenoutputcontains a path separator. Windows accepts both\and/, and RustโsPathtreats both as separators there, so both are checked rather than juststd::path::MAIN_SEPARATOR.- create_
dir_ ๐all_ private - Create
dirand its missing parents, owner-only on unix (legacy creates them700). - default_
send_ ๐file_ name - Derive the file name to save a received Sendโs file under.
- ends_
with_ ๐separator - reject_
path_ ๐traversal - Reject paths containing a
..segment before they reach the filesystem: a script that forwards an unsanitized path through to this CLI should not be able to resolve outside the directory it intended, even thoughbwitself only ever reads and writes with the invoking userโs own permissions. - resolve_
output_ ๐path - Resolve where to write a commandโs file output, porting
CliUtils.saveFileโs rules exactly: - save_
file ๐ - Write
datato the locationresolve_output_pathpicks for(output, default_file_name)and return the path written, so the caller can report it back to the user. - write_
file_ ๐private - Write
datatopath, owner-only on unix (legacy writes0o600).