mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
os: cp_r fn to copy files recursively
This commit is contained in:
committed by
Alexander Medvednikov
parent
8dbeab9a7b
commit
bd18f50c8a
35
vlib/os/os.v
35
vlib/os/os.v
@@ -151,7 +151,40 @@ pub fn cp(old, new string) ?bool {
|
||||
}
|
||||
}
|
||||
|
||||
fn vfopen(path, mode string) voidptr { //*C.FILE {
|
||||
pub fn cp_r(source_path, dest_path string, overwrite bool) ?bool{
|
||||
if !os.file_exists(source_path) {
|
||||
return error('Source path doesn\'t exist')
|
||||
}
|
||||
//single file copy
|
||||
if !os.is_dir(source_path) {
|
||||
adjasted_path := if os.is_dir(dest_path) {
|
||||
filepath.join(dest_path, os.basedir(source_path)) } else { dest_path }
|
||||
if os.file_exists(adjasted_path) {
|
||||
if overwrite { os.rm(adjasted_path) }
|
||||
else { return error('Destination file path already exist') }
|
||||
}
|
||||
os.cp(source_path, adjasted_path) or { return error(err) }
|
||||
return true
|
||||
}
|
||||
if !os.is_dir(dest_path) {
|
||||
return error('Destination path is not a valid directory')
|
||||
}
|
||||
files := os.ls(source_path) or { return error(err) }
|
||||
for file in files {
|
||||
sp := filepath.join(source_path, file)
|
||||
dp := filepath.join(dest_path, file)
|
||||
if os.is_dir(sp) {
|
||||
os.mkdir(dp)
|
||||
}
|
||||
cp_r(sp, dp, overwrite) or {
|
||||
os.rmdir(dp)
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
fn vfopen(path, mode string) *C.FILE {
|
||||
$if windows {
|
||||
return C._wfopen(path.to_wide(), mode.to_wide())
|
||||
} $else {
|
||||
|
||||
Reference in New Issue
Block a user