Debian Codegraph @

lib/ansible/module_utils/common/file.py

raw
1 # Copyright (c) 2018, Ansible Project
2 # Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause)
3
4 from __future__ import annotations
5
6 import os
7 import stat
8 import re
9
10
11 FILE_ATTRIBUTES = {
12 'A': 'noatime',
13 'a': 'append',
14 'c': 'compressed',
15 'C': 'nocow',
16 'd': 'nodump',
17 'D': 'dirsync',
18 'e': 'extents',
19 'E': 'encrypted',
20 'h': 'blocksize',
21 'i': 'immutable',
22 'I': 'indexed',
23 'j': 'journalled',
24 'N': 'inline',
25 's': 'zero',
26 'S': 'synchronous',
27 't': 'notail',
28 'T': 'blockroot',
29 'u': 'undelete',
30 'X': 'compressedraw',
31 'Z': 'compresseddirty',
32 }
33
34
35 # Used for parsing symbolic file perms
36 MODE_OPERATOR_RE = re.compile(r'[+=-]')
37 USERS_RE = re.compile(r'[^ugo]')
38 PERMS_RE = re.compile(r'[^rwxXstugo]')
39
40
41 S_IRANY = 0o0444 # read by user, group, others
42 S_IWANY = 0o0222 # write by user, group, others
43 S_IXANY = 0o0111 # execute by user, group, others
44 S_IRWU_RWG_RWO = S_IRANY | S_IWANY # read, write by user, group, others
45 S_IRWU_RG_RO = S_IRANY | stat.S_IWUSR # read by user, group, others and write only by user
46 S_IRWXU_RXG_RXO = S_IRANY | S_IXANY | stat.S_IWUSR # read, execute by user, group, others and write only by user
47 _PERM_BITS = 0o7777 # file mode permission bits
48 _EXEC_PERM_BITS = S_IXANY # execute permission bits
49 _DEFAULT_PERM = S_IRWU_RWG_RWO # default file permission bits
50
51
52 def is_executable(path):
53 # This function's signature needs to be repeated
54 # as the first line of its docstring.
55 # This method is reused by the basic module,
56 # the repetition helps the basic module's html documentation come out right.
57 # http://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#confval-autodoc_docstring_signature
58 """is_executable(path)
59
60 is the given path executable?
61
62 :arg path: The path of the file to check.
63
64 Limitations:
65
66 * Does not account for FSACLs.
67 * Most times we really want to know "Can the current user execute this
68 file". This function does not tell us that, only if any execute bit is set.
69 """
70 # These are all bitfields so first bitwise-or all the permissions we're
71 # looking for, then bitwise-and with the file's mode to determine if any
72 # execute bits are set.
73 return ((stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) & os.stat(path)[stat.ST_MODE])
74
75
76 def format_attributes(attributes):
77 attribute_list = [FILE_ATTRIBUTES.get(attr) for attr in attributes if attr in FILE_ATTRIBUTES]
78 return attribute_list
79
80
81 def get_flags_from_attributes(attributes):
82 flags = [key for key, attr in FILE_ATTRIBUTES.items() if attr in attributes]
83 return ''.join(flags)
84
85
86 def get_file_arg_spec():
87 arg_spec = dict(
88 mode=dict(type='raw'),
89 owner=dict(),
90 group=dict(),
91 seuser=dict(),
92 serole=dict(),
93 selevel=dict(),
94 setype=dict(),
95 attributes=dict(aliases=['attr']),
96 )
97 return arg_spec