Skip to main content

SymbolicMode -- Code to handle symbolic file permissions

This python library parses symbolic file permission modes as used by GNU chmod, part of the coreutils package. For example:

>>> from symbolicmode import *
>>> oct(symbolic_to_numeric_permissions('a=rx,u+w'))
'0o755'   

It also has a "chmod" function:

>>> chmod('a=rx,u+w', '/tmp/foo')
>>> chmod('755', '/tmp/foo')
>>> chmod(0o755, '/tmp/foo')

For convenience it can take permissions in the form of an integer, a numeric string or the symbolic permissions.

Status

This library is fully compatible with GNU Coreutils "chmod" command. It fully implements all mode specifiers except for the purely numeric versions ("755") that chmod does, as verified by manual, unit, and extensive fuzz testing.

My fuzz testing was against version 8.32-4.1ubuntu1). Fuzz testing tools are in the "fuzzchmod" directory.

Docstring - symbolic_to_numeric_permissions

Convert a symbolic file permission string to its numeric equivalent.

The function takes a symbolic permission description string in the format of user[=,+,-]permissions,group[=,+,-]permissions,other[=,+,-]permissions. The available permission characters are r (read), w (write), x (execute), X (execute if a directory), s (setuid/setgid), and t (sticky bit), or a single character from: 'u', 'g', 'o'.

Args:

  • symbolic_perm (str): The symbolic permission description string.
  • initial_mode (int, optional): The mode to start off with. If changing mode of an existing file, this is it's current mode, and can also impact 'X'.
  • is_directory (bool, optional): A boolean indicating whether the file is a directory. This affects the behavior of the X permission. Defaults to False.
  • umask (int, optional): Umask to use for "=[modes]" operation. If not specified, the system umask will be used.

Returns:

  • int: The numeric (octal) representation of the file permissions.

Raises:

  • ValueError: When the permissions contain some invalid instruction.

Examples:

>>> symbolic_to_numeric_permissions("u=rwx,g=rx,o=r")
0o754
>>> symbolic_to_numeric_permissions("u=rwX", is_directory=True)
0o700
>>> symbolic_to_numeric_permissions("u=rws,g=rx,o=r")
0o4754
>>> symbolic_to_numeric_permissions("=rw", initial_mode=0o4777, is_directory=False, umask=0o027)
0o640

Docstring - chmod

Change the mode (permissions) of a specified file or directory.

The mode can be specified as an integer, a string representing an octal integer or as a string representing symbolic permissions (e.g., 'u=rwx,g=r,o=r').

Parameters:

  • mode : int or str The mode (permissions) to be applied to the file or directory. The mode can be specified either as an integer, a string of digits (which are parsed as an octal integer), or as a string representing symbolic permissions (e.g., 'u=rwx,g=r,o=r').
  • path : str or Path The path to the file or directory whose mode is to be changed.

Returns: None

Raises:

  • FileNotFoundError If the specified file or directory does not exist.
  • PermissionError If the user does not have sufficient privileges to change the mode.
  • ValueError If the specified mode is invalid.

Examples:

# Change the mode of a file using an octal integer:
chmod(0o755, '/path/to/file')

# Change the mode of a file using a digit string:
chmod('755', '/path/to/file')

# Change the mode of a directory using symbolic permissions
chmod('u=rwx,g=rx,o=r', '/path/to/directory')

Permissions Instructions

Permission instructions are 1 or more comma separated values of the form: "[ugoa...][[=+-][PERMS...]...]".

USERS can be:

  • u: Set permissions for the owner.
  • g: Set permissions for group access.
  • o: Set permissions for all others.
  • a: All of the above.
  • "": The empty string, only allowed with "=" for the operator. Applies to all (like "a"), but applies the umask to the permissions set.

Operators are:

  • -: Remove the PERMS to the file permissions.
  • +: Add the PERMS to the file permissions.
  • =: Set the permissions to exactly PERMS.

PERMS can be a combination of the following (except for u/g/o which if specified must be the only, single, PERM provided:

  • r: Allow read access, if a directory allow reading files within.
  • w: Allow write access, if a directory allow writing or creating files.
  • x: Allow execute, or list contents if a directory.
  • X: Set "x" but only if file permissions already had an "x" set for any user, or if the object is a directory.
  • s: Set UID/GID on execution, allows a program to take on the user/group permissions of the executable file.
  • t: Sticky bit or restrict deletion to the file owner if a directory. Note that some system settings may prevent root from writing to non-root files in a restricted deletion directory. See "fs.protected_regular" sysctl setting.
  • u: Take on the permissions granted to the user ("go=u"). Must be the only PERM if specified.
  • g: Take on the group permissions. Must be the only PERM if specified.
  • o: Take on the permissions granted to others. Must be the only PERM if specified.

Notes on instructions:

  • "=[PERMS]" sets the permissions based on the umask. This acts like "a" was specified for the USER, the PERMS are masked with the umask when applied for u/g/o.
  • "-[PERMS]" and "+[PERMS]" are not defined as meaning anything in the chmod manpage. If given to chmod, chmod will make a permissions change and then error out saying that the change was not the expected change. Because of this, SymbolicMode will raise a ValueError on either of these instructions.

Fuzz Testing

In the "fuzzchmod" directory is a set of programs for fuzz testing SymbolicMode against the system "chmod" to try to ensure correctness for even unusual inputs.

License

CC0 1.0 Universal, see LICENSE file for more information.

Metadata

Release files for symbolicmode 2.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for symbolicmode 2.0.1
File Size Uploaded
symbolicmode-2.0.1.tar.gz 10.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for symbolicmode 2.0.1
File Interpreter ABI Platform
symbolicmode-2.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 20.6 kB

Release files / symbolicmode-2.0.1.tar.gz

Download URL symbolicmode-2.0.1.tar.gz
Size 10.6 kB
Tags Source
SHA-256 checksum
How to use checksums
9f3e4bfa7ad0709fd3b21cfb0a6e6ef9d842eca29460e4796b5e65e72d49ba55
BLAKE2b-256 checksum
How to use checksums
91fbb7dff13369dfd64c58253b44355d4ba1553487407a965ebd15e4a5a3b277
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.6

Release files / symbolicmode-2.0.1-py3-none-any.whl

Download URL symbolicmode-2.0.1-py3-none-any.whl
Size 10.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1f88547b48eb7551c19b459b771b8f4de27f0109dedf68c7f0aea4caeadf85d1
BLAKE2b-256 checksum
How to use checksums
007a8b1ab26a95299b372fa225b046b993017a64ea90020e33754ce66b6d8f6d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.6

Release history Release notifications | RSS feed

This release

2.0.1 This release

2 release files

2.0.0

2 release files

1.0.1

2 release files

1.0

2 release files

0.9.7

2 release files

0.9.6

2 release files

0.9.5

2 release files

0.9.1

2 release files

0.9

2 release files

0.7

2 release files

0.4

2 release files

0.3

2 release files

0.2

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page