From 206c1f4ca1e7455aec6e4c5fd0b50053c8d79962 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sun, 5 Apr 2020 16:56:54 +0300 Subject: [PATCH] os: implement os.is_writable_folder/1 --- vlib/os/os.v | 17 +++++++++++++++++ vlib/os/os_test.v | 9 +++++++++ 2 files changed, 26 insertions(+) diff --git a/vlib/os/os.v b/vlib/os/os.v index 42733d9e99..ce678f68b8 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -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 { diff --git a/vlib/os/os_test.v b/vlib/os/os_test.v index 14442fbc79..ec32546bc0 100644 --- a/vlib/os/os_test.v +++ b/vlib/os/os_test.v @@ -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