Permissions
provide.foundation.file.permissions
¶
File permission utilities for Unix-like systems.
Provides safe, cross-platform utilities for working with file permissions including parsing, formatting, and applying permission modes.
Functions¶
ensure_secure_permissions
¶
ensure_secure_permissions(
path: Path,
is_executable: bool = False,
file_mode: int = DEFAULT_FILE_PERMS,
dir_mode: int = DEFAULT_DIR_PERMS,
executable_mode: int = DEFAULT_EXECUTABLE_PERMS,
) -> None
Apply secure default permissions to a file or directory.
Automatically determines the appropriate permission mode based on whether the path is a file, directory, or executable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to file or directory |
required |
is_executable
|
bool
|
Whether file should be executable (ignored for directories) |
False
|
file_mode
|
int
|
Permission mode for regular files |
DEFAULT_FILE_PERMS
|
dir_mode
|
int
|
Permission mode for directories |
DEFAULT_DIR_PERMS
|
executable_mode
|
int
|
Permission mode for executable files |
DEFAULT_EXECUTABLE_PERMS
|
Examples:
>>> from pathlib import Path
>>> # Regular file gets 0o644
>>> p = Path("/tmp/file.txt")
>>> p.touch()
>>> ensure_secure_permissions(p)
>>> # Executable gets 0o755
>>> p2 = Path("/tmp/script.sh")
>>> p2.touch()
>>> ensure_secure_permissions(p2, is_executable=True)
>>> # Directory gets 0o755
>>> d = Path("/tmp/mydir")
>>> d.mkdir(exist_ok=True)
>>> ensure_secure_permissions(d)
Source code in provide/foundation/file/permissions.py
format_permissions
¶
Format permission bits as octal string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
int
|
Permission bits (can include file type bits) |
required |
Returns:
| Type | Description |
|---|---|
str
|
Formatted string like "0755" (last 3 octal digits only) |
Examples:
>>> format_permissions(0o755)
'0755'
>>> format_permissions(0o644)
'0644'
>>> format_permissions(493) # 0o755 in decimal
'0755'
Source code in provide/foundation/file/permissions.py
get_permissions
¶
Get current file permissions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
File or directory path |
required |
Returns:
| Type | Description |
|---|---|
int
|
Permission bits as integer (0 if file doesn't exist or error) |
Examples:
>>> from pathlib import Path
>>> p = Path("/tmp/test.txt")
>>> p.touch()
>>> p.chmod(0o644)
>>> get_permissions(p)
420
>>> format_permissions(get_permissions(p))
'0644'
Source code in provide/foundation/file/permissions.py
parse_permissions
¶
Parse permission string to octal integer.
Accepts various permission string formats: - Octal with prefix: "0o755", "0755" - Octal without prefix: "755" - Integer strings: "493" (decimal for 0o755)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
perms_str
|
str | None
|
Permission string (e.g., "0755", "755", "0o755") |
required |
default
|
int
|
Default permissions if parsing fails |
DEFAULT_FILE_PERMS
|
Returns:
| Type | Description |
|---|---|
int
|
Permission as integer (e.g., 0o755 = 493) |
Examples:
>>> parse_permissions("0755")
493
>>> parse_permissions("0o755")
493
>>> parse_permissions("755")
493
>>> parse_permissions(None)
420
>>> parse_permissions("invalid")
420
Source code in provide/foundation/file/permissions.py
set_file_permissions
¶
Set file permissions safely with error handling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
File or directory path |
required |
mode
|
int
|
Unix permission mode (e.g., 0o755) |
required |
Raises:
| Type | Description |
|---|---|
OSError
|
If setting permissions fails on the underlying filesystem |
Examples:
>>> from pathlib import Path
>>> p = Path("/tmp/test.txt")
>>> p.touch()
>>> set_file_permissions(p, 0o644)