2020-04-05 00:37:13 +03:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module os
|
|
|
|
|
|
|
|
enum FileType {
|
|
|
|
regular
|
|
|
|
directory
|
|
|
|
character_device
|
|
|
|
block_device
|
|
|
|
fifo
|
|
|
|
symbolic_link
|
|
|
|
socket
|
|
|
|
}
|
|
|
|
|
|
|
|
struct FilePermission {
|
2020-05-09 19:34:04 +03:00
|
|
|
pub:
|
2020-04-05 00:37:13 +03:00
|
|
|
read bool
|
|
|
|
write bool
|
|
|
|
execute bool
|
|
|
|
}
|
|
|
|
|
|
|
|
struct FileMode {
|
2020-05-09 19:34:04 +03:00
|
|
|
pub:
|
2020-04-05 00:37:13 +03:00
|
|
|
typ FileType
|
|
|
|
owner FilePermission
|
|
|
|
group FilePermission
|
|
|
|
others FilePermission
|
|
|
|
}
|
|
|
|
|
|
|
|
// inode returns the mode of the file/inode containing inode type and permission information
|
|
|
|
// it supports windows for regular files but it doesn't matter if you use owner, group or others when checking permissions on windows
|
|
|
|
pub fn inode(path string) FileMode {
|
|
|
|
mut attr := C.stat{}
|
2020-07-14 01:16:31 +03:00
|
|
|
C.stat(charptr(path.str), &attr)
|
2020-04-05 00:37:13 +03:00
|
|
|
|
|
|
|
mut typ := FileType.regular
|
2020-05-27 06:42:48 +03:00
|
|
|
if attr.st_mode & u32(C.S_IFMT) == u32(C.S_IFDIR) {
|
2020-04-05 00:37:13 +03:00
|
|
|
typ = .directory
|
|
|
|
}
|
|
|
|
$if !windows {
|
2020-05-27 06:42:48 +03:00
|
|
|
if attr.st_mode & u32(C.S_IFMT) == u32(C.S_IFCHR) {
|
2020-04-05 00:37:13 +03:00
|
|
|
typ = .character_device
|
2020-05-27 06:42:48 +03:00
|
|
|
} else if attr.st_mode & u32(C.S_IFMT) == u32(C.S_IFBLK) {
|
2020-04-05 00:37:13 +03:00
|
|
|
typ = .block_device
|
2020-05-27 06:42:48 +03:00
|
|
|
} else if attr.st_mode & u32(C.S_IFMT) == u32(C.S_IFIFO) {
|
2020-04-05 00:37:13 +03:00
|
|
|
typ = .fifo
|
2020-05-27 06:42:48 +03:00
|
|
|
} else if attr.st_mode & u32(C.S_IFMT) == u32(C.S_IFLNK) {
|
2020-04-05 00:37:13 +03:00
|
|
|
typ = .symbolic_link
|
2020-05-27 06:42:48 +03:00
|
|
|
} else if attr.st_mode & u32(C.S_IFMT) == u32(C.S_IFSOCK) {
|
2020-04-05 00:37:13 +03:00
|
|
|
typ = .socket
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$if windows {
|
|
|
|
return FileMode{
|
|
|
|
typ: typ
|
|
|
|
owner: FilePermission{
|
2020-05-27 06:42:48 +03:00
|
|
|
read: bool(attr.st_mode & u32(C.S_IREAD))
|
|
|
|
write: bool(attr.st_mode & u32(C.S_IWRITE))
|
|
|
|
execute: bool(attr.st_mode & u32(C.S_IEXEC))
|
2020-04-05 00:37:13 +03:00
|
|
|
}
|
|
|
|
group: FilePermission{
|
2020-05-27 06:42:48 +03:00
|
|
|
read: bool(attr.st_mode & u32(C.S_IREAD))
|
|
|
|
write: bool(attr.st_mode & u32(C.S_IWRITE))
|
|
|
|
execute: bool(attr.st_mode & u32(C.S_IEXEC))
|
2020-04-05 00:37:13 +03:00
|
|
|
}
|
|
|
|
others: FilePermission{
|
2020-05-27 06:42:48 +03:00
|
|
|
read: bool(attr.st_mode & u32(C.S_IREAD))
|
|
|
|
write: bool(attr.st_mode & u32(C.S_IWRITE))
|
|
|
|
execute: bool(attr.st_mode & u32(C.S_IEXEC))
|
2020-04-05 00:37:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} $else {
|
|
|
|
return FileMode{
|
|
|
|
typ: typ
|
|
|
|
owner: FilePermission{
|
2020-05-27 06:42:48 +03:00
|
|
|
read: bool(attr.st_mode & u32(C.S_IRUSR))
|
|
|
|
write: bool(attr.st_mode & u32(C.S_IWUSR))
|
|
|
|
execute: bool(attr.st_mode & u32(C.S_IXUSR))
|
2020-04-05 00:37:13 +03:00
|
|
|
}
|
|
|
|
group: FilePermission{
|
2020-05-27 06:42:48 +03:00
|
|
|
read: bool(attr.st_mode & u32(C.S_IRGRP))
|
|
|
|
write: bool(attr.st_mode & u32(C.S_IWGRP))
|
|
|
|
execute: bool(attr.st_mode & u32(C.S_IXGRP))
|
2020-04-05 00:37:13 +03:00
|
|
|
}
|
|
|
|
others: FilePermission{
|
2020-05-27 06:42:48 +03:00
|
|
|
read: bool(attr.st_mode & u32(C.S_IROTH))
|
|
|
|
write: bool(attr.st_mode & u32(C.S_IWOTH))
|
|
|
|
execute: bool(attr.st_mode & u32(C.S_IXOTH))
|
2020-04-05 00:37:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|