os: implement os.is_writable_folder/1

pull/4246/head
Delyan Angelov 2020-04-05 16:56:54 +03:00
parent f139e98745
commit 206c1f4ca1
2 changed files with 26 additions and 0 deletions

View File

@ -543,6 +543,23 @@ pub fn is_executable(path string) bool {
return C.access(path.str, X_OK) != -1
}
// `is_writable_folder` - `folder` exists and is writable to the process
pub fn is_writable_folder(folder string) ?bool {
if !os.exists(folder) {
return error('`$folder` does not exist')
}
if !os.is_dir(folder) {
return error('`folder` is not a folder')
}
tmp_perm_check := os.join_path(folder, 'tmp_perm_check')
f := os.open_file(tmp_perm_check, 'w+', 0o700) or {
return error('cannot write to folder `$folder`: $err')
}
f.close()
os.rm(tmp_perm_check)
return true
}
// `is_writable` returns `true` if `path` is writable.
pub fn is_writable(path string) bool {
$if windows {

View File

@ -208,6 +208,15 @@ fn test_tmpdir() {
os.rm(tfile)
}
fn test_is_writable_folder() {
tmp := os.temp_dir()
f := os.is_writable_folder(tmp) or {
eprintln('err: $err')
assert false
}
assert f
}
fn test_make_symlink_check_is_link_and_remove_symlink() {
$if windows {
// TODO