From 2144c162c4e46ad42146c54e11832693b7e325d7 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 4 Dec 2019 23:53:11 +0300 Subject: [PATCH] os: is_link() --- vlib/builtin/cfns.v | 1 + vlib/os/const.v | 1 + vlib/os/os.v | 20 ++++++++++++++------ 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/vlib/builtin/cfns.v b/vlib/builtin/cfns.v index 3eb17de03d..e2203a4db1 100644 --- a/vlib/builtin/cfns.v +++ b/vlib/builtin/cfns.v @@ -50,6 +50,7 @@ fn C.chdir() int fn C.fread() int fn C.rewind() int fn C.stat() int +fn C.lstat() int fn C.rename() int fn C.fgets() int fn C.memset() int diff --git a/vlib/os/const.v b/vlib/os/const.v index cbee9d5918..4cef4213aa 100644 --- a/vlib/os/const.v +++ b/vlib/os/const.v @@ -19,6 +19,7 @@ const ( const ( S_IFMT = 0xF000 // type of file S_IFDIR = 0x4000 // directory + S_IFLNK = 0xa000 // link ) const( diff --git a/vlib/os/os.v b/vlib/os/os.v index b2a5862d5f..8edc0ef0f1 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -735,7 +735,7 @@ pub fn write_file(path, text string) { f.close() } -// clear will clear current terminal screen. +// clear clears current terminal screen. pub fn clear() { $if !windows { C.printf('\x1b[2J') @@ -838,25 +838,33 @@ pub fn is_dir(path string) bool { $if windows { _path := path.replace('/', '\\') attr := C.GetFileAttributesW(_path.to_wide()) - if int(attr) == int(C.INVALID_FILE_ATTRIBUTES) { + if int(attr) == C.INVALID_FILE_ATTRIBUTES { return false } - if (int(attr) & C.FILE_ATTRIBUTE_DIRECTORY) != 0 { + if int(attr) & C.FILE_ATTRIBUTE_DIRECTORY != 0 { return true } return false } $else { statbuf := C.stat{} - cstr := path.str - if C.stat(cstr, &statbuf) != 0 { + if C.stat(path.str, &statbuf) != 0 { return false } // ref: https://code.woboq.org/gcc/include/sys/stat.h.html - return (int(statbuf.st_mode) & S_IFMT) == S_IFDIR + return int(statbuf.st_mode) & S_IFMT == S_IFDIR } } +// is_link returns a boolean indicating whether the given path is a link. +pub fn is_link(path string) bool { + statbuf := C.stat{} + if C.lstat(path.str, &statbuf) != 0 { + return false + } + return int(statbuf.st_mode) & S_IFMT == S_IFLNK +} + // chdir changes the current working directory to the new directory path. pub fn chdir(path string) { $if windows {