os: is_link()

pull/2978/head
Alexander Medvednikov 2019-12-04 23:53:11 +03:00
parent a57e29dfc5
commit 2144c162c4
3 changed files with 16 additions and 6 deletions

View File

@ -50,6 +50,7 @@ fn C.chdir() int
fn C.fread() int fn C.fread() int
fn C.rewind() int fn C.rewind() int
fn C.stat() int fn C.stat() int
fn C.lstat() int
fn C.rename() int fn C.rename() int
fn C.fgets() int fn C.fgets() int
fn C.memset() int fn C.memset() int

View File

@ -19,6 +19,7 @@ const (
const ( const (
S_IFMT = 0xF000 // type of file S_IFMT = 0xF000 // type of file
S_IFDIR = 0x4000 // directory S_IFDIR = 0x4000 // directory
S_IFLNK = 0xa000 // link
) )
const( const(

View File

@ -735,7 +735,7 @@ pub fn write_file(path, text string) {
f.close() f.close()
} }
// clear will clear current terminal screen. // clear clears current terminal screen.
pub fn clear() { pub fn clear() {
$if !windows { $if !windows {
C.printf('\x1b[2J') C.printf('\x1b[2J')
@ -838,25 +838,33 @@ pub fn is_dir(path string) bool {
$if windows { $if windows {
_path := path.replace('/', '\\') _path := path.replace('/', '\\')
attr := C.GetFileAttributesW(_path.to_wide()) attr := C.GetFileAttributesW(_path.to_wide())
if int(attr) == int(C.INVALID_FILE_ATTRIBUTES) { if int(attr) == C.INVALID_FILE_ATTRIBUTES {
return false return false
} }
if (int(attr) & C.FILE_ATTRIBUTE_DIRECTORY) != 0 { if int(attr) & C.FILE_ATTRIBUTE_DIRECTORY != 0 {
return true return true
} }
return false return false
} }
$else { $else {
statbuf := C.stat{} statbuf := C.stat{}
cstr := path.str if C.stat(path.str, &statbuf) != 0 {
if C.stat(cstr, &statbuf) != 0 {
return false return false
} }
// ref: https://code.woboq.org/gcc/include/sys/stat.h.html // 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. // chdir changes the current working directory to the new directory path.
pub fn chdir(path string) { pub fn chdir(path string) {
$if windows { $if windows {