2021-01-18 15:20:06 +03:00
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
2020-04-05 00:37:13 +03:00
|
|
|
// 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-12-16 12:02:36 +03:00
|
|
|
read bool
|
|
|
|
write bool
|
2020-04-05 00:37:13 +03:00
|
|
|
execute bool
|
|
|
|
}
|
|
|
|
|
|
|
|
struct FileMode {
|
2020-05-09 19:34:04 +03:00
|
|
|
pub:
|
2020-12-16 12:02:36 +03:00
|
|
|
typ FileType
|
|
|
|
owner FilePermission
|
|
|
|
group FilePermission
|
2020-04-05 00:37:13 +03:00
|
|
|
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-12-23 21:13:42 +03:00
|
|
|
unsafe { 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-08-19 15:37:55 +03: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-05 00:37:13 +03:00
|
|
|
}
|
|
|
|
group: FilePermission{
|
2020-08-19 15:37:55 +03: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-05 00:37:13 +03:00
|
|
|
}
|
|
|
|
others: FilePermission{
|
2020-08-19 15:37:55 +03: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-05 00:37:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} $else {
|
|
|
|
return FileMode{
|
|
|
|
typ: typ
|
|
|
|
owner: FilePermission{
|
2020-08-19 15:37:55 +03: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-05 00:37:13 +03:00
|
|
|
}
|
|
|
|
group: FilePermission{
|
2020-08-19 15:37:55 +03: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-05 00:37:13 +03:00
|
|
|
}
|
|
|
|
others: FilePermission{
|
2020-08-19 15:37:55 +03: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-05 00:37:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|