File: os_utils.py

package info (click to toggle)
mysql-workbench 6.3.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 113,932 kB
  • ctags: 87,814
  • sloc: ansic: 955,521; cpp: 427,465; python: 59,728; yacc: 59,129; xml: 54,204; sql: 7,091; objc: 965; makefile: 638; sh: 613; java: 237; perl: 30; ruby: 6; php: 1
file content (373 lines) | stat: -rw-r--r-- 13,222 bytes parent folder | download
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; version 2 of the
# License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301  USA

import os
import sys
import ctypes
if sys.platform == 'win32':
    from ctypes import wintypes
import errno
import subprocess
import shutil
import stat
from wb_common import PermissionDeniedError

class FunctionType:
    Success = 0
    Boolean = 1
    String = 2
    Data = 3


class FileUtils(object):
    """
    Provides implementation of functions to perform operations on the
    Windows File System.
    
    Exception handling is expected to be done by the callers:
    - FileOpsLocalWindows class for operations done as the CURRENT user
    - The wbadminhelper for operations done as ADMIN
    
    Some exception handling is done on these functions but only to be able
    to generate the PermissionDeniedError as it is used on further validations
    on the admin code.
    
    The functions can be divided in the next groups:
    - Success functions: they are expected to succeed, they don't return any value
    - Boolean functions: on success execution return a boolean value
    - String functions: on success execution return a string
    
    The functions will return the proper value on success or
    generate an exception in case of failure.
    
    The function names are self descriptive, however the function type
    will be indicated on each of them using the criteria explained above
    """
    def __init__(self):
        pass
        
    @classmethod
    def get_free_space(self, path):
        """
        Function Type : String
        """
        def get_readable_format(total):
            measures = ['B', 'KB', 'MB', 'GB', 'TB']
            index = 0
            while index < len(measures) and total > 1024:
                total = float(total / 1024)
                index = index + 1
                
            return "%.2f %s" % (total, measures[index])
            
        total_bytes, free_bytes = ctypes.c_ulonglong(0), ctypes.c_ulonglong(0)
        if ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(path), None, ctypes.pointer(total_bytes), ctypes.pointer(free_bytes)):
            free = get_readable_format(free_bytes.value)
            total = get_readable_format(total_bytes.value)
            ret_val = "%s of %s available" % (free, total)
        else:
            ret_val = "Could not determine"
            
        return ret_val
        
    @classmethod
    def check_dir_writable(self, path):
        """
        Function Type : Boolean
        """
        ret_val = True
        
        if os.path.isdir(path):
            dummy_file = os.path.join(path, '.wb_write_test')
            try:
                f = open(dummy_file, 'w+')
                f.close()
                os.remove(dummy_file)
            except IOError, e:
                if e.errno == errno.EACCES:
                    ret_val = False
                else:
                    raise
        elif os.path.exists(path):
            raise IOError(errno.ENOTDIR, 'The indicated path is not a directory')
        else:
            raise IOError(errno.ENOENT, 'The indicated path does not exist')
            
        return ret_val
                
    @classmethod
    def check_path_exists(self, path):
        """
        Function Type : Boolean
        """
        return os.path.exists(path)
      
    @classmethod
    def check_file_readable(self, path):
        """
        Function Type : Boolean
        """
        return os.access(path, os.R_OK)
      
    @classmethod
    def create_directory(self, path):
        """
        Function Type : Success
        """
        try:
            os.mkdir(path)
        except (IOError, OSError), err:
            if err.errno == errno.EACCES:
                raise PermissionDeniedError("Could not create directory %s" % path)
            raise
        
    @classmethod
    def create_directory_recursive(self, path):
        """
        Function Type : Success
        """
        try:
            os.makedirs(path)
        except (IOError, OSError), err:
            if err.errno == errno.EACCES:
                raise PermissionDeniedError("Could not create directory %s" % path)
            raise

    @classmethod
    def remove_directory(self, path):
        """
        Function Type : Success
        """
        try:
            os.rmdir(path)
        except (IOError, OSError), err:
            if err.errno == errno.EACCES:
                raise PermissionDeniedError("Could not remove directory %s" % path)
            raise err
        
    @classmethod
    def remove_directory_recursive(self, path):
        """
        Function Type : Success
        """
        try:
            shutil.rmtree(path)
        except (IOError, OSError), err:
            if err.errno == errno.EACCES:
                raise PermissionDeniedError("Could not remove directory %s" % path)
            raise err

    @classmethod
    def copy_file(self, source, target, target_backup = ""):
        """
        Function Type : Success
        """
        try:
            # Does a backup if it was required
            if target_backup:
                shutil.copy(target, target_backup)
                
            shutil.copy(source, target)
        except (IOError, OSError), e:
            if e.errno == errno.EACCES:
                raise PermissionDeniedError("Can't copy %s to %s" % (source, target))
            raise
        
    @classmethod
    def delete_file(self, path):
        """
        Function Type : Success
        """

        try:
            os.remove(path)
        except (IOError, OSError), err:
            if err.errno == errno.EACCES:
                raise PermissionDeniedError("Could not delete file %s" % path)
            raise err
    
    @classmethod
    def get_file_owner(self, path):
        """
        Function Type : String
        """
        # Grabs the functions that will be used to locate the owner of a specific path
        GetFileSecurityW = ctypes.windll.advapi32.GetFileSecurityW
        GetSecurityDescriptorOwner = ctypes.windll.advapi32.GetSecurityDescriptorOwner
        LookupAccountSidW = ctypes.windll.advapi32.LookupAccountSidW
        
        lookup_account = False
        get_owner_descriptor = False
        ret_val = ""
        
        SECURITY_INFORMATION_OWNER = 0X00000001
        length = wintypes.DWORD()
        GetFileSecurityW.restype = wintypes.BOOL
        GetFileSecurityW.argtypes = [
                    wintypes.LPCWSTR,
                    wintypes.DWORD,
                    ctypes.POINTER(wintypes.BYTE), 
                    wintypes.DWORD,
                    ctypes.POINTER(wintypes.DWORD),
                    ]

        # First: retrieves the security descriptor for the requested path
        # This call is to retrieve the size needed to store this descriptor
        GetFileSecurityW(path, SECURITY_INFORMATION_OWNER, None, 0, ctypes.byref(length))
        
        if length.value:
            sd = (wintypes.BYTE * length.value)()
            
            # This call retrieves the security descriptor
            if GetFileSecurityW(path, SECURITY_INFORMATION_OWNER, sd, length, ctypes.byref(length)):
                get_owner_descriptor = True
                
        # Second: Now gets the owner information based on the security descriptor retrieved
        #         on the first step
        if get_owner_descriptor:
            sdo = ctypes.POINTER(wintypes.BYTE)()
            sdo_defaulted = wintypes.BOOL()
            if GetSecurityDescriptorOwner(sd, ctypes.byref(sdo), ctypes.byref(sdo_defaulted)):
                lookup_account = True

        # Finally: using the security descriptor owner retrieved above, queries
        #          the user naeme and the first domain where the user is found
        if lookup_account:
            SIZE = 256
            name = ctypes.create_unicode_buffer(SIZE)
            domain = ctypes.create_unicode_buffer(SIZE) 
            cch_name = wintypes.DWORD(SIZE)
            cch_domain = wintypes.DWORD(SIZE)
            sdo_type = wintypes.DWORD()

            if LookupAccountSidW(None, sdo, name, 
                                    ctypes.byref(cch_name),
                                    domain, ctypes.byref(cch_domain),
                                    ctypes.byref(sdo_type)):
                                    
                ret_val = "%s\\%s" % (domain.value, name.value)
            else:
                print 'Failed to lookup user'
                
        if not ret_val:
            raise IOError(errno.EINVAL, "The given path is not a file or directory")
            
        return ret_val
        
    @classmethod
    def list_dir(self, path, include_size, output_handler):
        try:
            # This is needed on the SudoTail class
            if os.path.isfile(path):
                if include_size:
                    stat_info = os.stat(path)
                    stat_info.st_size
                    
                    line = "%i %s" % (stat_info.st_size, path)
                    output_handler(line)
                else:
                    output_handler(path)
            else:
                dlist = os.listdir(path)
                
                for item in dlist:
                    line = ""
                    item_path = os.path.join(path, item)
                    stat_info = os.stat(item_path)
                    item_stat = stat_info.st_mode
                    if stat.S_ISDIR(item_stat):
                        item += '/'
                        if include_size:
                            line = "%s %s" % (str(stat_info.st_size), item)
                        else:
                            line = item
                    elif stat.S_ISREG(item_stat) or stat.S_ISLNK(item_stat):
                        if include_size:
                            line = "%s %s" % (str(stat_info.st_size), item)
                        else:
                            line = item
                    
                    if line:
                        output_handler(line)
        except (IOError, OSError), e:
            if e.errno == errno.EACCES:
                raise PermissionDeniedError("Permission denied accessing %s" % path)
            raise
    
    @classmethod
    def get_file_lines(self, path, skip, output_handler):
        try:
            f = open(path, 'r')

            skipped = 0
            for line in f:
                if not skip or skipped == skip:
                    output_handler(line)
                else:
                    skipped = skipped + 1

            f.close()
        except (IOError, OSError), e:
            if e.errno == errno.EACCES:
                raise PermissionDeniedError("Can't open file '%s'" % path)
            raise
        

            
class OSUtils(object):
    """
    Provides the exec_command functiono wich will execute whatever command 
    is passed as long as it is valid for the OS.
    
    In case of failure this function will also raise the proper exception
    """
    @classmethod    
    def exec_command(self, command, output_handler):
        """
        Executes any OS valid command and sends the output to the command listener
        Syntax:
                EXEC <command>
                
                command: A valida OS command.
        """
        retcode = 1
        try:
            # Note that self._command contains the internal command so EXEC
            # and self._args is the real OS command to be executed
            # Executes the command, reading the output from the PIPE
            process = subprocess.Popen(command, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.STDOUT, shell=True)

            # Sends all the command output to the listener
            if output_handler:
                for line in iter(process.stdout.readline, ""):
                    output_handler(line)

            # Waits for the process completion
            process.wait()
            
            retcode = process.returncode
        except Exception, exc:
            raise
            
        return retcode