~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

(gz) Provide st_uid and st_gid on _Win32Stat so it looks more like a normal
 stat object (Alexander Belchenko)

Show diffs side-by-side

added added

removed removed

Lines of Context:
97
97
    mod = os.lstat(filename).st_mode
98
98
    if not stat.S_ISLNK(mod):
99
99
        mod = mod & 0777555
100
 
        os.chmod(filename, mod)
 
100
        chmod_if_possible(filename, mod)
101
101
 
102
102
 
103
103
def make_writable(filename):
104
104
    mod = os.lstat(filename).st_mode
105
105
    if not stat.S_ISLNK(mod):
106
106
        mod = mod | 0200
107
 
        os.chmod(filename, mod)
 
107
        chmod_if_possible(filename, mod)
 
108
 
 
109
 
 
110
def chmod_if_possible(filename, mode):
 
111
    # Set file mode if that can be safely done.
 
112
    # Sometimes even on unix the filesystem won't allow it - see
 
113
    # https://bugs.launchpad.net/bzr/+bug/606537
 
114
    try:
 
115
        # It is probably faster to just do the chmod, rather than
 
116
        # doing a stat, and then trying to compare
 
117
        os.chmod(filename, mode)
 
118
    except (IOError, OSError),e:
 
119
        # Permission/access denied seems to commonly happen on smbfs; there's
 
120
        # probably no point warning about it.
 
121
        # <https://bugs.launchpad.net/bzr/+bug/606537>
 
122
        if getattr(e, 'errno') in (errno.EPERM, errno.EACCES):
 
123
            trace.mutter("ignore error on chmod of %r: %r" % (
 
124
                filename, e))
 
125
            return
 
126
        raise
108
127
 
109
128
 
110
129
def minimum_path_selection(paths):
277
296
    # copy posixpath.abspath, but use os.getcwdu instead
278
297
    if not posixpath.isabs(path):
279
298
        path = posixpath.join(getcwd(), path)
280
 
    return posixpath.normpath(path)
 
299
    return _posix_normpath(path)
281
300
 
282
301
 
283
302
def _posix_realpath(path):
284
303
    return posixpath.realpath(path.encode(_fs_enc)).decode(_fs_enc)
285
304
 
286
305
 
 
306
def _posix_normpath(path):
 
307
    path = posixpath.normpath(path)
 
308
    # Bug 861008: posixpath.normpath() returns a path normalized according to
 
309
    # the POSIX standard, which stipulates (for compatibility reasons) that two
 
310
    # leading slashes must not be simplified to one, and only if there are 3 or
 
311
    # more should they be simplified as one. So we treat the leading 2 slashes
 
312
    # as a special case here by simply removing the first slash, as we consider
 
313
    # that breaking POSIX compatibility for this obscure feature is acceptable.
 
314
    # This is not a paranoid precaution, as we notably get paths like this when
 
315
    # the repo is hosted at the root of the filesystem, i.e. in "/".    
 
316
    if path.startswith('//'):
 
317
        path = path[1:]
 
318
    return path
 
319
 
 
320
 
287
321
def _win32_fixdrive(path):
288
322
    """Force drive letters to be consistent.
289
323
 
377
411
abspath = _posix_abspath
378
412
realpath = _posix_realpath
379
413
pathjoin = os.path.join
380
 
normpath = os.path.normpath
 
414
normpath = _posix_normpath
381
415
getcwd = os.getcwdu
382
416
rename = os.rename
383
417
dirname = os.path.dirname
2487
2521
    is_local_pid_dead = win32utils.is_local_pid_dead
2488
2522
else:
2489
2523
    is_local_pid_dead = _posix_is_local_pid_dead
 
2524
 
 
2525
 
 
2526
def fdatasync(fileno):
 
2527
    """Flush file contents to disk if possible.
 
2528
    
 
2529
    :param fileno: Integer OS file handle.
 
2530
    :raises TransportNotPossible: If flushing to disk is not possible.
 
2531
    """
 
2532
    fn = getattr(os, 'fdatasync', getattr(os, 'fsync', None))
 
2533
    if fn is not None:
 
2534
        fn(fileno)
 
2535
 
 
2536
 
 
2537
def ensure_empty_directory_exists(path, exception_class):
 
2538
    """Make sure a local directory exists and is empty.
 
2539
    
 
2540
    If it does not exist, it is created.  If it exists and is not empty, an
 
2541
    instance of exception_class is raised.
 
2542
    """
 
2543
    try:
 
2544
        os.mkdir(path)
 
2545
    except OSError, e:
 
2546
        if e.errno != errno.EEXIST:
 
2547
            raise
 
2548
        if os.listdir(path) != []:
 
2549
            raise exception_class(path)