File: pathutils.py

package info (click to toggle)
python-os-win 5.9.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,556 kB
  • sloc: python: 19,740; makefile: 22; sh: 2
file content (281 lines) | stat: -rw-r--r-- 9,830 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# Copyright 2013 Cloudbase Solutions Srl
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import contextlib
import ctypes
import os
import shutil
import tempfile

from oslo_log import log as logging
from oslo_utils import fileutils

from os_win._i18n import _
from os_win import _utils
import os_win.conf
from os_win import exceptions
from os_win.utils import _acl_utils
from os_win.utils.io import ioutils
from os_win.utils import win32utils
from os_win.utils.winapi import constants as w_const
from os_win.utils.winapi import libs as w_lib
from os_win.utils.winapi.libs import advapi32 as advapi32_def
from os_win.utils.winapi.libs import kernel32 as kernel32_def
from os_win.utils.winapi import wintypes

kernel32 = w_lib.get_shared_lib_handle(w_lib.KERNEL32)

CONF = os_win.conf.CONF
LOG = logging.getLogger(__name__)

file_in_use_retry_decorator = _utils.retry_decorator(
    exceptions=exceptions.WindowsError,
    extract_err_code_func=lambda x: x.winerror,
    error_codes=[w_const.ERROR_SHARING_VIOLATION,
                 w_const.ERROR_DIR_IS_NOT_EMPTY],
    timeout=CONF.os_win.file_in_use_timeout,
    max_retry_count=None)


class PathUtils(object):

    def __init__(self):
        self._win32_utils = win32utils.Win32Utils()
        self._acl_utils = _acl_utils.ACLUtils()
        self._io_utils = ioutils.IOUtils()

    def open(self, path, mode):
        """Wrapper on __builtin__.open used to simplify unit testing."""
        from six.moves import builtins
        return builtins.open(path, mode)

    def exists(self, path):
        return os.path.exists(path)

    def makedirs(self, path):
        os.makedirs(path)

    @file_in_use_retry_decorator
    def remove(self, path):
        os.remove(path)

    @file_in_use_retry_decorator
    def rename(self, src, dest):
        os.rename(src, dest)

    def copy_dir(self, src, dest):
        shutil.copytree(src, dest)

    def copyfile(self, src, dest):
        self.copy(src, dest)

    def copy(self, src, dest, fail_if_exists=True):
        """Copies a file to a specified location.

        :param fail_if_exists: if set to True, the method fails if the
                               destination path exists.
        """
        # With large files this is 2x-3x faster than shutil.copy(src, dest),
        # especially when copying to a UNC target.
        if os.path.isdir(dest):
            src_fname = os.path.basename(src)
            dest = os.path.join(dest, src_fname)

        try:
            self._win32_utils.run_and_check_output(
                kernel32.CopyFileW,
                ctypes.c_wchar_p(src),
                ctypes.c_wchar_p(dest),
                wintypes.BOOL(fail_if_exists),
                kernel32_lib_func=True)
        except exceptions.Win32Exception as exc:
            err_msg = _('The file copy from %(src)s to %(dest)s failed.'
                        'Exception: %(exc)s')
            raise IOError(err_msg % dict(src=src, dest=dest, exc=exc))

    def copy_folder_files(self, src_dir, dest_dir):
        """Copies the files of the given src_dir to dest_dir.

        It will ignore any nested folders.

        :param src_dir: Given folder from which to copy files.
        :param dest_dir: Folder to which to copy files.
        """

        self.check_create_dir(dest_dir)

        for fname in os.listdir(src_dir):
            src = os.path.join(src_dir, fname)
            # ignore subdirs.
            if os.path.isfile(src):
                self.copy(src, os.path.join(dest_dir, fname))

    def move_folder_files(self, src_dir, dest_dir):
        """Moves the files of the given src_dir to dest_dir.

        It will ignore any nested folders.

        :param src_dir: Given folder from which to move files.
        :param dest_dir: Folder to which to move files.
        """

        for fname in os.listdir(src_dir):
            src = os.path.join(src_dir, fname)
            # ignore subdirs.
            if os.path.isfile(src):
                self.rename(src, os.path.join(dest_dir, fname))

    @file_in_use_retry_decorator
    def rmtree(self, path):
        shutil.rmtree(path)

    def check_create_dir(self, path):
        if not self.exists(path):
            LOG.debug('Creating directory: %s', path)
            self.makedirs(path)

    def check_remove_dir(self, path):
        if self.exists(path):
            LOG.debug('Removing directory: %s', path)
            self.rmtree(path)

    def is_symlink(self, path):
        return os.path.islink(path)

    def create_sym_link(self, link, target, target_is_dir=True):
        """If target_is_dir is True, a junction will be created.

        NOTE: Junctions only work on same filesystem.
        """

        self._win32_utils.run_and_check_output(kernel32.CreateSymbolicLinkW,
                                               link,
                                               target,
                                               target_is_dir,
                                               kernel32_lib_func=True)

    def create_temporary_file(self, suffix=None, *args, **kwargs):
        fd, tmp_file_path = tempfile.mkstemp(suffix=suffix, *args, **kwargs)
        os.close(fd)
        return tmp_file_path

    @contextlib.contextmanager
    def temporary_file(self, suffix=None, *args, **kwargs):
        """Creates a random, temporary, closed file, returning the file's

        path. It's different from tempfile.NamedTemporaryFile which returns
        an open file descriptor.
        """

        tmp_file_path = None
        try:
            tmp_file_path = self.create_temporary_file(suffix, *args, **kwargs)
            yield tmp_file_path
        finally:
            if tmp_file_path:
                fileutils.delete_if_exists(tmp_file_path)

    def add_acl_rule(self, path, trustee_name,
                     access_rights, access_mode,
                     inheritance_flags=0):
        """Adds the requested access rule to a file or object.

        Can be used for granting/revoking access.
        """
        p_to_free = []

        try:
            sec_info = self._acl_utils.get_named_security_info(
                obj_name=path,
                obj_type=w_const.SE_FILE_OBJECT,
                security_info_flags=w_const.DACL_SECURITY_INFORMATION)
            p_to_free.append(sec_info['pp_sec_desc'].contents)

            access = advapi32_def.EXPLICIT_ACCESS()
            access.grfAccessPermissions = access_rights
            access.grfAccessMode = access_mode
            access.grfInheritance = inheritance_flags
            access.Trustee.TrusteeForm = w_const.TRUSTEE_IS_NAME
            access.Trustee.pstrName = ctypes.c_wchar_p(trustee_name)

            pp_new_dacl = self._acl_utils.set_entries_in_acl(
                entry_count=1,
                p_explicit_entry_list=ctypes.pointer(access),
                p_old_acl=sec_info['pp_dacl'].contents)
            p_to_free.append(pp_new_dacl.contents)

            self._acl_utils.set_named_security_info(
                obj_name=path,
                obj_type=w_const.SE_FILE_OBJECT,
                security_info_flags=w_const.DACL_SECURITY_INFORMATION,
                p_dacl=pp_new_dacl.contents)
        finally:
            for p in p_to_free:
                self._win32_utils.local_free(p)

    def copy_acls(self, source_path, dest_path):
        p_to_free = []

        try:
            sec_info_flags = w_const.DACL_SECURITY_INFORMATION
            sec_info = self._acl_utils.get_named_security_info(
                obj_name=source_path,
                obj_type=w_const.SE_FILE_OBJECT,
                security_info_flags=sec_info_flags)
            p_to_free.append(sec_info['pp_sec_desc'].contents)

            self._acl_utils.set_named_security_info(
                obj_name=dest_path,
                obj_type=w_const.SE_FILE_OBJECT,
                security_info_flags=sec_info_flags,
                p_dacl=sec_info['pp_dacl'].contents)
        finally:
            for p in p_to_free:
                self._win32_utils.local_free(p)

    def is_same_file(self, path_a, path_b):
        """Check if two paths point to the same file."""

        file_a_id = self.get_file_id(path_a)
        file_b_id = self.get_file_id(path_b)

        return file_a_id == file_b_id

    def get_file_id(self, path):
        """Return a dict containing the file id and volume id."""
        handle = None
        info = kernel32_def.FILE_ID_INFO()

        try:
            handle = self._io_utils.open(
                path,
                desired_access=0,
                share_mode=(w_const.FILE_SHARE_READ |
                            w_const.FILE_SHARE_WRITE |
                            w_const.FILE_SHARE_DELETE),
                creation_disposition=w_const.OPEN_EXISTING)
            self._win32_utils.run_and_check_output(
                kernel32.GetFileInformationByHandleEx,
                handle,
                w_const.FileIdInfo,
                ctypes.byref(info),
                ctypes.sizeof(info),
                kernel32_lib_func=True)
        finally:
            if handle:
                self._io_utils.close_handle(handle)

        return dict(volume_serial_number=info.VolumeSerialNumber,
                    file_id=bytearray(info.FileId.Identifier))