Skip to content

Commit

Permalink
safefile: stfile: enhance special permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
zoumingzhe committed Nov 20, 2024
1 parent dba5702 commit 35a84ca
Showing 1 changed file with 50 additions and 20 deletions.
70 changes: 50 additions & 20 deletions xarg/safefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,14 @@ def human_mode(self) -> str:
┃ ┃ ┗━━━━━━ Group ┣━ Permissions
┃ ┗━━━━━━━━━ Owner ━┛
┗━━━━━━━━━━━ File type
'''
return stat.filemode(self.stat.st_mode)

File type:
@property
def human_file_type(self) -> str:
'''File type
4 (13 - 16 bits) file type bitmask:
| - | regular file | stat.S_ISREG | S_IFREG | 0o100000 |
| d | directory | stat.S_ISDIR | S_IFDIR | 0o040000 |
Expand All @@ -172,25 +178,49 @@ def human_mode(self) -> str:
| s | socket | stat.S_ISSOCK | S_IFSOCK | 0o140000 |
| p | FIFO | stat.S_ISFIFO | S_IFIFO | 0o010000 |
'''
st_mode: int = self.stat.st_mode

file_type: str = {
stat.S_IFREG: "-",
stat.S_IFDIR: "d",
stat.S_IFLNK: "l",
stat.S_IFBLK: "b",
stat.S_IFCHR: "c",
stat.S_IFIFO: "p",
stat.S_IFSOCK: "s",
}[stat.S_IFMT(st_mode)]

permissions: str = "".join(
what.lower() if getattr(stat, f"S_I{what}{who}") & st_mode else "-"
for who in ["USR", "GRP", "OTH"]
for what in ["R", "W", "X"]
)

return file_type + permissions
return self.human_mode[0]

@property
def human_all_permissions(self) -> str:
'''File all users permissions
9 (1 - 9 bits) users permissions bitmask and
3 (10 - 12 bits) special permissions
'''
return self.human_mode[-9:]

@property
def human_owner_permissions(self) -> str:
'''File owner permissions
3 (7 - 9 bits) owner permissions bitmask and setuid bit:
- Read permission: -/r
- Write permission: -/w
- Execute permission: -/x/s(x + SUID)/S(only SUID)
'''
return self.human_all_permissions[0:3]

@property
def human_group_permissions(self) -> str:
'''File group permissions
3 (4 - 6 bits) group permissions bitmask and setgid bit:
- Read permission: -/r
- Write permission: -/w
- Execute permission: -/x/s(x + SGID)/S(only SGID)
'''
return self.human_all_permissions[3:6]

@property
def human_other_permissions(self) -> str:
'''File other (not in group) permissions
3 (1 - 3 bits) other permissions bitmask and sticky bit:
- Read permission: -/r
- Write permission: -/w
- Execute permission: -/x/t(x + SBIT)/T(only SBIT)
'''
return self.human_all_permissions[6:9]

def chmod(self, mode: Union[int, str]):
'''change file mode bits
Expand Down

0 comments on commit 35a84ca

Please sign in to comment.