2020-04-04 23:37:13 +02: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 18:34:04 +02:00
|
|
|
pub:
|
2020-04-04 23:37:13 +02:00
|
|
|
read bool
|
|
|
|
write bool
|
|
|
|
execute bool
|
|
|
|
}
|
|
|
|
|
|
|
|
struct FileMode {
|
2020-05-09 18:34:04 +02:00
|
|
|
pub:
|
2020-04-04 23:37:13 +02: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-20 19:06:41 +02:00
|
|
|
unsafe {
|
|
|
|
C.stat(charptr(path.str), &attr)
|
|
|
|
}
|
2020-04-04 23:37:13 +02:00
|
|
|
|
|
|
|
mut typ := FileType.regular
|
2020-05-27 05:42:48 +02:00
|
|
|
if attr.st_mode & u32(C.S_IFMT) == u32(C.S_IFDIR) {
|
2020-04-04 23:37:13 +02:00
|
|
|
typ = .directory
|
|
|
|
}
|
|
|
|
$if !windows {
|
2020-05-27 05:42:48 +02:00
|
|
|
if attr.st_mode & u32(C.S_IFMT) == u32(C.S_IFCHR) {
|
2020-04-04 23:37:13 +02:00
|
|
|
typ = .character_device
|
2020-05-27 05:42:48 +02:00
|
|
|
} else if attr.st_mode & u32(C.S_IFMT) == u32(C.S_IFBLK) {
|
2020-04-04 23:37:13 +02:00
|
|
|
typ = .block_device
|
2020-05-27 05:42:48 +02:00
|
|
|
} else if attr.st_mode & u32(C.S_IFMT) == u32(C.S_IFIFO) {
|
2020-04-04 23:37:13 +02:00
|
|
|
typ = .fifo
|
2020-05-27 05:42:48 +02:00
|
|
|
} else if attr.st_mode & u32(C.S_IFMT) == u32(C.S_IFLNK) {
|
2020-04-04 23:37:13 +02:00
|
|
|
typ = .symbolic_link
|
2020-05-27 05:42:48 +02:00
|
|
|
} else if attr.st_mode & u32(C.S_IFMT) == u32(C.S_IFSOCK) {
|
2020-04-04 23:37:13 +02:00
|
|
|
typ = .socket
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$if windows {
|
|
|
|
return FileMode{
|
|
|
|
typ: typ
|
|
|
|
owner: FilePermission{
|
2020-08-19 14:37:55 +02:00
|
|
|
read: (attr.st_mode & u32(C.S_IREAD)) != 0
|
|
|
|
write: (attr.st_mode & u32(C.S_IWRITE)) != 0
|
|
|
|
execute: (attr.st_mode & u32(C.S_IEXEC)) != 0
|
2020-04-04 23:37:13 +02:00
|
|
|
}
|
|
|
|
group: FilePermission{
|
2020-08-19 14:37:55 +02:00
|
|
|
read: (attr.st_mode & u32(C.S_IREAD)) != 0
|
|
|
|
write: (attr.st_mode & u32(C.S_IWRITE)) != 0
|
|
|
|
execute: (attr.st_mode & u32(C.S_IEXEC)) != 0
|
2020-04-04 23:37:13 +02:00
|
|
|
}
|
|
|
|
others: FilePermission{
|
2020-08-19 14:37:55 +02:00
|
|
|
read: (attr.st_mode & u32(C.S_IREAD)) != 0
|
|
|
|
write: (attr.st_mode & u32(C.S_IWRITE)) != 0
|
|
|
|
execute: (attr.st_mode & u32(C.S_IEXEC)) != 0
|
2020-04-04 23:37:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} $else {
|
|
|
|
return FileMode{
|
|
|
|
typ: typ
|
|
|
|
owner: FilePermission{
|
2020-08-19 14:37:55 +02:00
|
|
|
read: (attr.st_mode & u32(C.S_IRUSR)) != 0
|
|
|
|
write: (attr.st_mode & u32(C.S_IWUSR)) != 0
|
|
|
|
execute: (attr.st_mode & u32(C.S_IXUSR)) != 0
|
2020-04-04 23:37:13 +02:00
|
|
|
}
|
|
|
|
group: FilePermission{
|
2020-08-19 14:37:55 +02:00
|
|
|
read: (attr.st_mode & u32(C.S_IRGRP)) != 0
|
|
|
|
write: (attr.st_mode & u32(C.S_IWGRP)) != 0
|
|
|
|
execute: (attr.st_mode & u32(C.S_IXGRP)) != 0
|
2020-04-04 23:37:13 +02:00
|
|
|
}
|
|
|
|
others: FilePermission{
|
2020-08-19 14:37:55 +02:00
|
|
|
read: (attr.st_mode & u32(C.S_IROTH)) != 0
|
|
|
|
write: (attr.st_mode & u32(C.S_IWOTH)) != 0
|
|
|
|
execute: (attr.st_mode & u32(C.S_IXOTH)) != 0
|
2020-04-04 23:37:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|