2022-01-04 12:21:08 +03:00
|
|
|
// Copyright (c) 2019-2022 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
|
|
|
|
|
2022-11-03 10:24:52 +03:00
|
|
|
pub enum FileType {
|
2020-04-05 00:37:13 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-02-01 15:49:06 +03:00
|
|
|
// bitmask returns a 3 bit sequence in the order RWE where
|
|
|
|
// the bit is set to 1 if the value is true or 0 otherwise.
|
|
|
|
pub fn (p FilePermission) bitmask() u32 {
|
|
|
|
mut mask := u32(0)
|
|
|
|
if p.read {
|
|
|
|
mask |= 4
|
|
|
|
}
|
|
|
|
if p.write {
|
|
|
|
mask |= 2
|
|
|
|
}
|
|
|
|
if p.execute {
|
|
|
|
mask |= 1
|
|
|
|
}
|
|
|
|
return mask
|
|
|
|
}
|
|
|
|
|
2020-04-05 00:37:13 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-02-01 15:49:06 +03:00
|
|
|
// bitmask returns a 9 bit sequence in the order owner + group + others.
|
|
|
|
// This is a valid bitmask to use with `os.chmod`.
|
|
|
|
pub fn (m FileMode) bitmask() u32 {
|
|
|
|
return m.owner.bitmask() << 6 | m.group.bitmask() << 3 | m.others.bitmask()
|
|
|
|
}
|
|
|
|
|
2020-04-05 00:37:13 +03:00
|
|
|
// 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{}
|
2021-04-04 17:43:32 +03:00
|
|
|
unsafe { C.stat(&char(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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|