os: update mv fns, improve performance, add params struct to control overwrite behavior (#20156)

This commit is contained in:
Turiiya 2023-12-13 08:43:18 +01:00 committed by GitHub
parent 936790e43e
commit e5e26db5c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 12 deletions

View file

@ -109,7 +109,7 @@ fn process_file(input_file string, options Options) ! {
os.rm(backup_file) or {} os.rm(backup_file) or {}
// Rename the original to the backup. // Rename the original to the backup.
os.mv(input_file, backup_file) or { return error('Failed to copy file: ${input_file}') } os.mv(input_file, backup_file) or { return error('Failed to move file: ${input_file}') }
// Process the old file and write it back to the original. // Process the old file and write it back to the original.
os.write_file(input_file, new_lines.join_lines()) or { os.write_file(input_file, new_lines.join_lines()) or {

View file

@ -134,13 +134,9 @@ fn (m Module) install() InstallResult {
} }
} }
os.mv(m.tmp_path, m.install_path) or { os.mv(m.tmp_path, m.install_path) or {
// `os.mv` / `os.mv_by_cp` from the temp dir to the vmodules dir may fail on some linux systems.
// In such cases, fall back on `os.cp_app`.
os.cp_all(m.tmp_path, m.install_path, true) or {
vpm_error('failed to install `${m.name}`.', details: err.msg()) vpm_error('failed to install `${m.name}`.', details: err.msg())
return .failed return .failed
} }
}
return .installed return .installed
} }

View file

@ -119,11 +119,16 @@ pub fn cp_all(src string, dst string, overwrite bool) ! {
} }
} }
@[params]
pub struct MvParams {
overwrite bool = true
}
// mv_by_cp copies files or folders from `source` to `target`. // mv_by_cp copies files or folders from `source` to `target`.
// If copying is successful, `source` is deleted. // If copying is successful, `source` is deleted.
// It may be used when the paths are not on the same mount/partition. // It may be used when the paths are not on the same mount/partition.
pub fn mv_by_cp(source string, target string) ! { pub fn mv_by_cp(source string, target string, opts MvParams) ! {
cp_all(source, target, true)! cp_all(source, target, opts.overwrite)!
if is_dir(source) { if is_dir(source) {
rmdir_all(source)! rmdir_all(source)!
return return
@ -132,8 +137,11 @@ pub fn mv_by_cp(source string, target string) ! {
} }
// mv moves files or folders from `src` to `dst`. // mv moves files or folders from `src` to `dst`.
pub fn mv(source string, target string) ! { pub fn mv(source string, target string, opts MvParams) ! {
rename(source, target) or { mv_by_cp(source, target)! } if !opts.overwrite && exists(target) {
return error('target path already exist')
}
rename(source, target) or { mv_by_cp(source, target, opts)! }
} }
// read_lines reads the file in `path` into an array of lines. // read_lines reads the file in `path` into an array of lines.

View file

@ -955,7 +955,7 @@ fn test_reading_from_empty_file() {
os.rm(empty_file)! os.rm(empty_file)!
} }
fn move_across_partitions_using_function(f fn (src string, dst string) !) ! { fn move_across_partitions_using_function(f fn (src string, dst string, opts os.MvParams) !) ! {
bindfs := os.find_abs_path_of_executable('bindfs') or { bindfs := os.find_abs_path_of_executable('bindfs') or {
eprintln('skipping test_mv_by_cp, because bindfs was not present') eprintln('skipping test_mv_by_cp, because bindfs was not present')
return return