2 Futz with files like a pro.
5 import sys, exceptions, os, stat, tempfile, time, binascii
6 from collections import namedtuple
7 from errno import ENOENT
9 if sys.platform == "win32":
10 from ctypes import WINFUNCTYPE, WinError, windll, POINTER, byref, c_ulonglong, \
11 create_unicode_buffer, get_last_error
12 from ctypes.wintypes import BOOL, DWORD, LPCWSTR, LPWSTR, LPVOID, HANDLE
14 from twisted.python import log
16 from pycryptopp.cipher.aes import AES
19 def rename(src, dst, tries=4, basedelay=0.1):
20 """ Here is a superkludge to workaround the fact that occasionally on
21 Windows some other process (e.g. an anti-virus scanner, a local search
22 engine, etc.) is looking at your file when you want to delete or move it,
23 and hence you can't. The horrible workaround is to sit and spin, trying
24 to delete it, for a short time and then give up.
26 With the default values of tries and basedelay this can block for less
29 @param tries: number of tries -- each time after the first we wait twice
30 as long as the previous wait
31 @param basedelay: how long to wait before the second try
33 for i in range(tries-1):
35 return os.rename(src, dst)
36 except EnvironmentError, le:
37 # XXX Tighten this to check if this is a permission denied error (possibly due to another Windows process having the file open and execute the superkludge only in this case.
38 log.msg("XXX KLUDGE Attempting to move file %s => %s; got %s; sleeping %s seconds" % (src, dst, le, basedelay,))
41 return os.rename(src, dst) # The last try.
43 def remove(f, tries=4, basedelay=0.1):
44 """ Here is a superkludge to workaround the fact that occasionally on
45 Windows some other process (e.g. an anti-virus scanner, a local search
46 engine, etc.) is looking at your file when you want to delete or move it,
47 and hence you can't. The horrible workaround is to sit and spin, trying
48 to delete it, for a short time and then give up.
50 With the default values of tries and basedelay this can block for less
53 @param tries: number of tries -- each time after the first we wait twice
54 as long as the previous wait
55 @param basedelay: how long to wait before the second try
58 os.chmod(f, stat.S_IWRITE | stat.S_IEXEC | stat.S_IREAD)
61 for i in range(tries-1):
64 except EnvironmentError, le:
65 # XXX Tighten this to check if this is a permission denied error (possibly due to another Windows process having the file open and execute the superkludge only in this case.
66 if not os.path.exists(f):
68 log.msg("XXX KLUDGE Attempting to remove file %s; got %s; sleeping %s seconds" % (f, le, basedelay,))
71 return os.remove(f) # The last try.
73 class ReopenableNamedTemporaryFile:
75 This uses tempfile.mkstemp() to generate a secure temp file. It then closes
76 the file, leaving a zero-length file as a placeholder. You can get the
77 filename with ReopenableNamedTemporaryFile.name. When the
78 ReopenableNamedTemporaryFile instance is garbage collected or its shutdown()
79 method is called, it deletes the file.
81 def __init__(self, *args, **kwargs):
82 fd, self.name = tempfile.mkstemp(*args, **kwargs)
86 return "<%s instance at %x %s>" % (self.__class__.__name__, id(self), self.name)
89 return self.__repr__()
97 class EncryptedTemporaryFile:
98 # not implemented: next, readline, readlines, xreadlines, writelines
101 self.file = tempfile.TemporaryFile()
102 self.key = os.urandom(16) # AES-128
104 def _crypt(self, offset, data):
105 offset_big = offset // 16
106 offset_small = offset % 16
107 iv = binascii.unhexlify("%032x" % offset_big)
108 cipher = AES(self.key, iv=iv)
109 cipher.process("\x00"*offset_small)
110 return cipher.process(data)
118 def seek(self, offset, whence=0): # 0 = SEEK_SET
119 self.file.seek(offset, whence)
122 offset = self.file.tell()
125 def read(self, size=-1):
126 """A read must not follow a write, or vice-versa, without an intervening seek."""
127 index = self.file.tell()
128 ciphertext = self.file.read(size)
129 plaintext = self._crypt(index, ciphertext)
132 def write(self, plaintext):
133 """A read must not follow a write, or vice-versa, without an intervening seek.
134 If seeking and then writing causes a 'hole' in the file, the contents of the
135 hole are unspecified."""
136 index = self.file.tell()
137 ciphertext = self._crypt(index, plaintext)
138 self.file.write(ciphertext)
140 def truncate(self, newsize):
141 """Truncate or extend the file to 'newsize'. If it is extended, the contents after the
142 old end-of-file are unspecified. The file position after this operation is unspecified."""
143 self.file.truncate(newsize)
146 def make_dirs(dirname, mode=0777):
148 An idempotent version of os.makedirs(). If the dir already exists, do
149 nothing and return without raising an exception. If this call creates the
150 dir, return without raising an exception. If there is an error that
151 prevents creation or if the directory gets deleted after make_dirs() creates
152 it and before make_dirs() checks that it exists, raise an exception.
156 os.makedirs(dirname, mode)
160 if not os.path.isdir(dirname):
163 raise exceptions.IOError, "unknown error prevented creation of directory, or deleted the directory immediately after creation: %s" % dirname # careful not to construct an IOError with a 2-tuple, as that has a special meaning...
167 A threadsafe and idempotent version of shutil.rmtree(). If the dir is
168 already gone, do nothing and return without raising an exception. If this
169 call removes the dir, return without raising an exception. If there is an
170 error that prevents deletion or if the directory gets created again after
171 rm_dir() deletes it and before rm_dir() checks that it is gone, raise an
176 os.chmod(dirname, stat.S_IWRITE | stat.S_IEXEC | stat.S_IREAD)
177 for f in os.listdir(dirname):
178 fullname = os.path.join(dirname, f)
179 if os.path.isdir(fullname):
184 except Exception, le:
185 # Ignore "No such file or directory"
186 if (not isinstance(le, OSError)) or le.args[0] != 2:
189 # Okay, now we've recursively removed everything, ignoring any "No
190 # such file or directory" errors, and collecting any other errors.
192 if os.path.exists(dirname):
196 raise OSError, "Failed to remove dir for unknown reason."
200 def remove_if_possible(f):
209 for root, dirs, files in os.walk(basedir):
211 fn = os.path.join(root, f)
212 size += os.path.getsize(fn)
216 def move_into_place(source, dest):
217 """Atomically replace a file, or as near to it as the platform allows.
218 The dest file may or may not exist."""
219 if "win32" in sys.platform.lower():
220 remove_if_possible(dest)
221 os.rename(source, dest)
223 def write_atomically(target, contents, mode="b"):
224 f = open(target+".tmp", "w"+mode)
229 move_into_place(target+".tmp", target)
231 def write(path, data, mode="wb"):
232 wf = open(path, mode)
239 rf = open(path, "rb")
245 def put_file(path, inf):
246 precondition_abspath(path)
248 # TODO: create temporary file and move into place?
249 outf = open(path, "wb")
252 data = inf.read(32768)
260 def precondition_abspath(path):
261 if not isinstance(path, unicode):
262 raise AssertionError("an abspath must be a Unicode string")
264 if sys.platform == "win32":
265 # This intentionally doesn't view absolute paths starting with a drive specification, or
266 # paths relative to the current drive, as acceptable.
267 if not path.startswith("\\\\"):
268 raise AssertionError("an abspath should be normalized using abspath_expanduser_unicode")
270 # This intentionally doesn't view the path '~' or paths starting with '~/' as acceptable.
271 if not os.path.isabs(path):
272 raise AssertionError("an abspath should be normalized using abspath_expanduser_unicode")
274 # Work around <http://bugs.python.org/issue3426>. This code is adapted from
275 # <http://svn.python.org/view/python/trunk/Lib/ntpath.py?revision=78247&view=markup>
276 # with some simplifications.
278 _getfullpathname = None
280 from nt import _getfullpathname
284 def abspath_expanduser_unicode(path, base=None):
286 Return the absolute version of a path. If 'base' is given and 'path' is relative,
287 the path will be expanded relative to 'base'.
288 'path' must be a Unicode string. 'base', if given, must be a Unicode string
289 corresponding to an absolute path as returned by a previous call to
290 abspath_expanduser_unicode.
292 if not isinstance(path, unicode):
293 raise AssertionError("paths must be Unicode strings")
295 precondition_abspath(base)
297 path = expanduser(path)
300 # On Windows, os.path.isabs will incorrectly return True
301 # for paths without a drive letter (that are not UNC paths),
302 # e.g. "\\". See <http://bugs.python.org/issue1669539>.
305 path = _getfullpathname(path or u".")
307 path = _getfullpathname(os.path.join(base, path))
311 if not os.path.isabs(path):
313 path = os.path.join(os.getcwdu(), path)
315 path = os.path.join(base, path)
317 # We won't hit <http://bugs.python.org/issue5827> because
318 # there is always at least one Unicode path component.
319 path = os.path.normpath(path)
321 if sys.platform == "win32":
322 path = to_windows_long_path(path)
326 def to_windows_long_path(path):
327 # '/' is normally a perfectly valid path component separator in Windows.
328 # However, when using the "\\?\" syntax it is not recognized, so we
329 # replace it with '\' here.
330 path = path.replace(u"/", u"\\")
332 # Note that other normalizations such as removing '.' and '..' should
333 # be done outside this function.
335 if path.startswith(u"\\\\?\\") or path.startswith(u"\\\\.\\"):
337 elif path.startswith(u"\\\\"):
338 return u"\\\\?\\UNC\\" + path[2 :]
340 return u"\\\\?\\" + path
343 have_GetDiskFreeSpaceExW = False
344 if sys.platform == "win32":
345 # <http://msdn.microsoft.com/en-us/library/windows/desktop/ms683188%28v=vs.85%29.aspx>
346 GetEnvironmentVariableW = WINFUNCTYPE(
347 DWORD, LPCWSTR, LPWSTR, DWORD,
349 )(("GetEnvironmentVariableW", windll.kernel32))
352 # <http://msdn.microsoft.com/en-us/library/aa383742%28v=VS.85%29.aspx>
353 PULARGE_INTEGER = POINTER(c_ulonglong)
355 # <http://msdn.microsoft.com/en-us/library/aa364937%28VS.85%29.aspx>
356 GetDiskFreeSpaceExW = WINFUNCTYPE(
357 BOOL, LPCWSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER,
359 )(("GetDiskFreeSpaceExW", windll.kernel32))
361 have_GetDiskFreeSpaceExW = True
364 traceback.print_exc()
366 def expanduser(path):
367 # os.path.expanduser is hopelessly broken for Unicode paths on Windows (ticket #1674).
368 if sys.platform == "win32":
369 return windows_expanduser(path)
371 return os.path.expanduser(path)
373 def windows_expanduser(path):
374 if not path.startswith('~'):
377 home_dir = windows_getenv(u'USERPROFILE')
379 home_drive = windows_getenv(u'HOMEDRIVE')
380 home_path = windows_getenv(u'HOMEPATH')
381 if home_drive is None or home_path is None:
382 raise OSError("Could not find home directory: neither %USERPROFILE% nor (%HOMEDRIVE% and %HOMEPATH%) are set.")
383 home_dir = os.path.join(home_drive, home_path)
387 elif path.startswith('~/') or path.startswith('~\\'):
388 return os.path.join(home_dir, path[2 :])
392 # <https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382%28v=vs.85%29.aspx>
393 ERROR_ENVVAR_NOT_FOUND = 203
395 def windows_getenv(name):
396 # Based on <http://stackoverflow.com/questions/2608200/problems-with-umlauts-in-python-appdata-environvent-variable/2608368#2608368>,
397 # with improved error handling. Returns None if there is no enivronment variable of the given name.
398 if not isinstance(name, unicode):
399 raise AssertionError("name must be Unicode")
401 n = GetEnvironmentVariableW(name, None, 0)
402 # GetEnvironmentVariableW returns DWORD, so n cannot be negative.
404 err = get_last_error()
405 if err == ERROR_ENVVAR_NOT_FOUND:
407 raise OSError("WinError: %s\n attempting to read size of environment variable %r"
408 % (WinError(err), name))
410 # Avoid an ambiguity between a zero-length string and an error in the return value of the
411 # call to GetEnvironmentVariableW below.
414 buf = create_unicode_buffer(u'\0'*n)
415 retval = GetEnvironmentVariableW(name, buf, n)
417 err = get_last_error()
418 if err == ERROR_ENVVAR_NOT_FOUND:
420 raise OSError("WinError: %s\n attempting to read environment variable %r"
421 % (WinError(err), name))
423 raise OSError("Unexpected result %d (expected less than %d) from GetEnvironmentVariableW attempting to read environment variable %r"
428 def get_disk_stats(whichdir, reserved_space=0):
429 """Return disk statistics for the storage disk, in the form of a dict
430 with the following fields.
431 total: total bytes on disk
432 free_for_root: bytes actually free on disk
433 free_for_nonroot: bytes free for "a non-privileged user" [Unix] or
434 the current user [Windows]; might take into
435 account quotas depending on platform
436 used: bytes used on disk
437 avail: bytes available excluding reserved space
438 An AttributeError can occur if the OS has no API to get disk information.
439 An EnvironmentError can occur if the OS call fails.
441 whichdir is a directory on the filesystem in question -- the
442 answer is about the filesystem, not about the directory, so the
443 directory is used only to specify which filesystem.
445 reserved_space is how many bytes to subtract from the answer, so
446 you can pass how many bytes you would like to leave unused on this
447 filesystem as reserved_space.
450 if have_GetDiskFreeSpaceExW:
451 # If this is a Windows system and GetDiskFreeSpaceExW is available, use it.
452 # (This might put up an error dialog unless
453 # SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX) has been called,
454 # which we do in allmydata.windows.fixups.initialize().)
456 n_free_for_nonroot = c_ulonglong(0)
457 n_total = c_ulonglong(0)
458 n_free_for_root = c_ulonglong(0)
459 retval = GetDiskFreeSpaceExW(whichdir, byref(n_free_for_nonroot),
461 byref(n_free_for_root))
463 raise OSError("WinError: %s\n attempting to get disk statistics for %r"
464 % (WinError(get_last_error()), whichdir))
465 free_for_nonroot = n_free_for_nonroot.value
466 total = n_total.value
467 free_for_root = n_free_for_root.value
469 # For Unix-like systems.
470 # <http://docs.python.org/library/os.html#os.statvfs>
471 # <http://opengroup.org/onlinepubs/7990989799/xsh/fstatvfs.html>
472 # <http://opengroup.org/onlinepubs/7990989799/xsh/sysstatvfs.h.html>
473 s = os.statvfs(whichdir)
476 # statvfs(2) is a wrapper around statfs(2).
477 # statvfs.f_frsize = statfs.f_bsize :
478 # "minimum unit of allocation" (statvfs)
479 # "fundamental file system block size" (statfs)
480 # statvfs.f_bsize = statfs.f_iosize = stat.st_blocks : preferred IO size
481 # on an encrypted home directory ("FileVault"), it gets f_blocks
482 # wrong, and s.f_blocks*s.f_frsize is twice the size of my disk,
483 # but s.f_bavail*s.f_frsize is correct
485 total = s.f_frsize * s.f_blocks
486 free_for_root = s.f_frsize * s.f_bfree
487 free_for_nonroot = s.f_frsize * s.f_bavail
489 # valid for all platforms:
490 used = total - free_for_root
491 avail = max(free_for_nonroot - reserved_space, 0)
493 return { 'total': total,
494 'free_for_root': free_for_root,
495 'free_for_nonroot': free_for_nonroot,
500 def get_available_space(whichdir, reserved_space):
501 """Returns available space for share storage in bytes, or None if no
502 API to get this information is available.
504 whichdir is a directory on the filesystem in question -- the
505 answer is about the filesystem, not about the directory, so the
506 directory is used only to specify which filesystem.
508 reserved_space is how many bytes to subtract from the answer, so
509 you can pass how many bytes you would like to leave unused on this
510 filesystem as reserved_space.
513 return get_disk_stats(whichdir, reserved_space)['avail']
514 except AttributeError:
516 except EnvironmentError:
517 log.msg("OS call to get disk statistics failed")
521 if sys.platform == "win32":
522 # <http://msdn.microsoft.com/en-us/library/aa363858%28v=vs.85%29.aspx>
523 CreateFileW = WINFUNCTYPE(HANDLE, LPCWSTR, DWORD, DWORD, LPVOID, DWORD, DWORD, HANDLE) \
524 (("CreateFileW", windll.kernel32))
526 GENERIC_WRITE = 0x40000000
527 FILE_SHARE_READ = 0x00000001
528 FILE_SHARE_WRITE = 0x00000002
530 INVALID_HANDLE_VALUE = 0xFFFFFFFF
532 # <http://msdn.microsoft.com/en-us/library/aa364439%28v=vs.85%29.aspx>
533 FlushFileBuffers = WINFUNCTYPE(BOOL, HANDLE)(("FlushFileBuffers", windll.kernel32))
535 # <http://msdn.microsoft.com/en-us/library/ms724211%28v=vs.85%29.aspx>
536 CloseHandle = WINFUNCTYPE(BOOL, HANDLE)(("CloseHandle", windll.kernel32))
538 # <http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/4465cafb-f4ed-434f-89d8-c85ced6ffaa8/>
539 def flush_volume(path):
540 drive = os.path.splitdrive(os.path.realpath(path))[0]
542 hVolume = CreateFileW(u"\\\\.\\" + drive,
544 FILE_SHARE_READ | FILE_SHARE_WRITE,
550 if hVolume == INVALID_HANDLE_VALUE:
553 if FlushFileBuffers(hVolume) == 0:
558 def flush_volume(path):
563 class ConflictError(Exception):
566 class UnableToUnlinkReplacementError(Exception):
569 def reraise(wrapper):
570 _, exc, tb = sys.exc_info()
571 wrapper_exc = wrapper("%s: %s" % (exc.__class__.__name__, exc))
572 raise wrapper_exc.__class__, wrapper_exc, tb
574 if sys.platform == "win32":
575 # <https://msdn.microsoft.com/en-us/library/windows/desktop/aa365512%28v=vs.85%29.aspx>
576 ReplaceFileW = WINFUNCTYPE(
578 LPCWSTR, LPCWSTR, LPCWSTR, DWORD, LPVOID, LPVOID,
580 )(("ReplaceFileW", windll.kernel32))
582 REPLACEFILE_IGNORE_MERGE_ERRORS = 0x00000002
584 def rename_no_overwrite(source_path, dest_path):
585 os.rename(source_path, dest_path)
587 def replace_file(replaced_path, replacement_path, backup_path):
588 precondition_abspath(replaced_path)
589 precondition_abspath(replacement_path)
590 precondition_abspath(backup_path)
592 r = ReplaceFileW(replaced_path, replacement_path, backup_path,
593 REPLACEFILE_IGNORE_MERGE_ERRORS, None, None)
595 # The UnableToUnlinkReplacementError case does not happen on Windows;
596 # all errors should be treated as signalling a conflict.
597 err = get_last_error()
598 raise ConflictError("WinError: %s" % (WinError(err)))
600 def rename_no_overwrite(source_path, dest_path):
601 # link will fail with EEXIST if there is already something at dest_path.
602 os.link(source_path, dest_path)
604 os.unlink(source_path)
605 except EnvironmentError:
606 reraise(UnableToUnlinkReplacementError)
608 def replace_file(replaced_path, replacement_path, backup_path):
609 precondition_abspath(replaced_path)
610 precondition_abspath(replacement_path)
611 precondition_abspath(backup_path)
613 if not os.path.exists(replacement_path):
614 raise ConflictError("Replacement file not found: %r" % (replacement_path,))
617 os.rename(replaced_path, backup_path)
619 if e.errno != ENOENT:
622 rename_no_overwrite(replacement_path, replaced_path)
623 except EnvironmentError:
624 reraise(ConflictError)
626 PathInfo = namedtuple('PathInfo', 'isdir isfile islink exists size mtime ctime')
628 def get_pathinfo(path_u, now=None):
630 statinfo = os.lstat(path_u)
631 mode = statinfo.st_mode
632 return PathInfo(isdir =stat.S_ISDIR(mode),
633 isfile=stat.S_ISREG(mode),
634 islink=stat.S_ISLNK(mode),
636 size =statinfo.st_size,
637 mtime =statinfo.st_mtime,
638 ctime =statinfo.st_ctime,
641 if e.errno == ENOENT:
644 return PathInfo(isdir =False,