1# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
2#
3# This file is part of Ansible
4#
5# Ansible is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# Ansible is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
17from __future__ import (absolute_import, division, print_function)
18__metaclass__ = type
19
20import os
21import shutil
22
23from errno import EEXIST, ENOENT
24from ansible.errors import AnsibleError
25from ansible.module_utils._text import to_bytes, to_native, to_text
26
27
28__all__ = ['unfrackpath', 'makedirs_safe', 'cs_exists', 'cs_isdir', 'cs_isfile']
29
30if not os.path.exists(__file__):
31    raise Exception('unable to determine filesystem case-sensitivity ({0} does not exist)'.format(__file__))
32
33_is_case_insensitive_fs = os.path.exists(__file__.upper())
34
35
36def unfrackpath(path, follow=True, basedir=None):
37    '''
38    Returns a path that is free of symlinks (if follow=True), environment variables, relative path traversals and symbols (~)
39
40    :arg path: A byte or text string representing a path to be canonicalized
41    :arg follow: A boolean to indicate of symlinks should be resolved or not
42    :raises UnicodeDecodeError: If the canonicalized version of the path
43        contains non-utf8 byte sequences.
44    :rtype: A text string (unicode on pyyhon2, str on python3).
45    :returns: An absolute path with symlinks, environment variables, and tilde
46        expanded.  Note that this does not check whether a path exists.
47
48    example::
49        '$HOME/../../var/mail' becomes '/var/spool/mail'
50    '''
51
52    b_basedir = to_bytes(basedir, errors='surrogate_or_strict', nonstring='passthru')
53
54    if b_basedir is None:
55        b_basedir = to_bytes(os.getcwd(), errors='surrogate_or_strict')
56    elif os.path.isfile(b_basedir):
57        b_basedir = os.path.dirname(b_basedir)
58
59    b_final_path = os.path.expanduser(os.path.expandvars(to_bytes(path, errors='surrogate_or_strict')))
60
61    if not os.path.isabs(b_final_path):
62        b_final_path = os.path.join(b_basedir, b_final_path)
63
64    if follow:
65        b_final_path = os.path.realpath(b_final_path)
66
67    return to_text(os.path.normpath(b_final_path), errors='surrogate_or_strict')
68
69
70def makedirs_safe(path, mode=None):
71    '''
72    A *potentially insecure* way to ensure the existence of a directory chain. The "safe" in this function's name
73    refers only to its ability to ignore `EEXIST` in the case of multiple callers operating on the same part of
74    the directory chain. This function is not safe to use under world-writable locations when the first level of the
75    path to be created contains a predictable component. Always create a randomly-named element first if there is any
76    chance the parent directory might be world-writable (eg, /tmp) to prevent symlink hijacking and potential
77    disclosure or modification of sensitive file contents.
78
79    :arg path: A byte or text string representing a directory chain to be created
80    :kwarg mode: If given, the mode to set the directory to
81    :raises AnsibleError: If the directory cannot be created and does not already exist.
82    :raises UnicodeDecodeError: if the path is not decodable in the utf-8 encoding.
83    '''
84
85    rpath = unfrackpath(path)
86    b_rpath = to_bytes(rpath)
87    if not os.path.exists(b_rpath):
88        try:
89            if mode:
90                os.makedirs(b_rpath, mode)
91            else:
92                os.makedirs(b_rpath)
93        except OSError as e:
94            if e.errno != EEXIST:
95                raise AnsibleError("Unable to create local directories(%s): %s" % (to_native(rpath), to_native(e)))
96
97
98def basedir(source):
99    """ returns directory for inventory or playbook """
100    source = to_bytes(source, errors='surrogate_or_strict')
101    dname = None
102    if os.path.isdir(source):
103        dname = source
104    elif source in [None, '', '.']:
105        dname = os.getcwd()
106    elif os.path.isfile(source):
107        dname = os.path.dirname(source)
108
109    if dname:
110        # don't follow symlinks for basedir, enables source re-use
111        dname = os.path.abspath(dname)
112
113    return to_text(dname, errors='surrogate_or_strict')
114
115
116def cleanup_tmp_file(path, warn=False):
117    """
118    Removes temporary file or directory. Optionally display a warning if unable
119    to remove the file or directory.
120
121    :arg path: Path to file or directory to be removed
122    :kwarg warn: Whether or not to display a warning when the file or directory
123        cannot be removed
124    """
125    try:
126        if os.path.exists(path):
127            try:
128                if os.path.isdir(path):
129                    shutil.rmtree(path)
130                elif os.path.isfile(path):
131                    os.unlink(path)
132            except Exception as e:
133                if warn:
134                    # Importing here to avoid circular import
135                    from ansible.utils.display import Display
136                    display = Display()
137                    display.display(u'Unable to remove temporary file {0}'.format(to_text(e)))
138    except Exception:
139        pass
140
141
142def is_subpath(child, parent):
143    """
144    Compares paths to check if one is contained in the other
145    :arg: child: Path to test
146    :arg parent; Path to test against
147     """
148    test = False
149
150    abs_child = unfrackpath(child, follow=False)
151    abs_parent = unfrackpath(parent, follow=False)
152
153    c = abs_child.split(os.path.sep)
154    p = abs_parent.split(os.path.sep)
155
156    try:
157        test = c[:len(p)] == p
158    except IndexError:
159        # child is shorter than parent so cannot be subpath
160        pass
161
162    return test
163
164
165def _explicit_case_sensitive_exists(path):
166    """
167    Standalone case-sensitive existence check for case-insensitive filesystems. This assumes the parent
168    dir exists and is otherwise accessible by the caller.
169    :param path: a bytes or text path string to check for existence
170    :return: True if the path exists and the paths pass a case-sensitive comparison
171    """
172    parent, leaf = os.path.split(path)
173
174    if not leaf:
175        # root directory or '.', of course it exists
176        return True
177
178    # ensure that the leaf matches, and that the parent dirs match (recursively)
179    return any(p for p in os.listdir(parent) if p == leaf) and cs_isdir(parent)
180
181
182def cs_open(file, *args, **kwargs):
183    """
184    A replacement for open that behaves case-sensitively on case-insensitive filesystems (passes through all args to underlying platform open)
185    :param file: a bytes or text path string to open
186    :return: a file descriptor if the file exists and the path passes a case-sensitive comparison
187    """
188    fd = open(file, *args, **kwargs)
189    try:
190        if _is_case_insensitive_fs and not _explicit_case_sensitive_exists(file):
191            try:
192                extype = FileNotFoundError
193            except NameError:
194                extype = IOError
195            raise extype(ENOENT, os.strerror(ENOENT), file)
196    except Exception:
197        fd.close()
198        raise
199
200    return fd
201
202
203def cs_exists(path):
204    """
205    A replacement for os.path.exists that behaves case-sensitive on case-insensitive filesystems
206    :param path: a bytes or text path string to check for existence
207    :return: True if the path exists and the paths pass a case-sensitive comparison
208    """
209    raw_exists = os.path.exists(path)
210    if not _is_case_insensitive_fs or not raw_exists:
211        return raw_exists
212
213    # we're on a case-insensitive filesystem and the file exists, verify its case matches
214    return _explicit_case_sensitive_exists(path)
215
216
217def cs_isdir(path):
218    """
219    A replacement for os.path.isdir that behaves case-sensitive on case-insensitive filesystems
220    :param path: a bytes or text path string to check if isdir
221    :return: True if the path is a dir (or resolves to one) and the paths pass a case-sensitive comparison
222    """
223    return os.path.isdir(path) and (not _is_case_insensitive_fs or _explicit_case_sensitive_exists(path))
224
225
226def cs_isfile(path):
227    """
228    A replacement for os.path.isfile that behaves case-sensitive on case-insensitive filesystems
229    :param path: a bytes or text path string to check if isfile
230    :return: True if the path is a file (or resolves to one) and the paths pass a case-sensitive comparison
231    """
232    return os.path.isfile(path) and (not _is_case_insensitive_fs or _explicit_case_sensitive_exists(path))
233