2 Futz with files like a pro.
5 import sys, exceptions, os, stat, tempfile, time, binascii
7 from twisted.python import log
9 from pycryptopp.cipher.aes import AES
12 def rename(src, dst, tries=4, basedelay=0.1):
13 """ Here is a superkludge to workaround the fact that occasionally on
14 Windows some other process (e.g. an anti-virus scanner, a local search
15 engine, etc.) is looking at your file when you want to delete or move it,
16 and hence you can't. The horrible workaround is to sit and spin, trying
17 to delete it, for a short time and then give up.
19 With the default values of tries and basedelay this can block for less
22 @param tries: number of tries -- each time after the first we wait twice
23 as long as the previous wait
24 @param basedelay: how long to wait before the second try
26 for i in range(tries-1):
28 return os.rename(src, dst)
29 except EnvironmentError, le:
30 # 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.
31 log.msg("XXX KLUDGE Attempting to move file %s => %s; got %s; sleeping %s seconds" % (src, dst, le, basedelay,))
34 return os.rename(src, dst) # The last try.
36 def remove(f, tries=4, basedelay=0.1):
37 """ Here is a superkludge to workaround the fact that occasionally on
38 Windows some other process (e.g. an anti-virus scanner, a local search
39 engine, etc.) is looking at your file when you want to delete or move it,
40 and hence you can't. The horrible workaround is to sit and spin, trying
41 to delete it, for a short time and then give up.
43 With the default values of tries and basedelay this can block for less
46 @param tries: number of tries -- each time after the first we wait twice
47 as long as the previous wait
48 @param basedelay: how long to wait before the second try
51 os.chmod(f, stat.S_IWRITE | stat.S_IEXEC | stat.S_IREAD)
54 for i in range(tries-1):
57 except EnvironmentError, le:
58 # 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.
59 if not os.path.exists(f):
61 log.msg("XXX KLUDGE Attempting to remove file %s; got %s; sleeping %s seconds" % (f, le, basedelay,))
64 return os.remove(f) # The last try.
66 class ReopenableNamedTemporaryFile:
68 This uses tempfile.mkstemp() to generate a secure temp file. It then closes
69 the file, leaving a zero-length file as a placeholder. You can get the
70 filename with ReopenableNamedTemporaryFile.name. When the
71 ReopenableNamedTemporaryFile instance is garbage collected or its shutdown()
72 method is called, it deletes the file.
74 def __init__(self, *args, **kwargs):
75 fd, self.name = tempfile.mkstemp(*args, **kwargs)
79 return "<%s instance at %x %s>" % (self.__class__.__name__, id(self), self.name)
82 return self.__repr__()
90 class NamedTemporaryDirectory:
92 This calls tempfile.mkdtemp(), stores the name of the dir in
93 self.name, and rmrf's the dir when it gets garbage collected or
96 def __init__(self, cleanup=True, *args, **kwargs):
97 """ If cleanup, then the directory will be rmrf'ed when the object is shutdown. """
98 self.cleanup = cleanup
99 self.name = tempfile.mkdtemp(*args, **kwargs)
102 return "<%s instance at %x %s>" % (self.__class__.__name__, id(self), self.name)
105 return self.__repr__()
112 traceback.print_exc()
115 if self.cleanup and hasattr(self, 'name'):
118 class EncryptedTemporaryFile:
119 # not implemented: next, readline, readlines, xreadlines, writelines
122 self.file = tempfile.TemporaryFile()
123 self.key = os.urandom(16) # AES-128
125 def _crypt(self, offset, data):
126 offset_big = offset // 16
127 offset_small = offset % 16
128 iv = binascii.unhexlify("%032x" % offset_big)
129 cipher = AES(self.key, iv=iv)
130 cipher.process("\x00"*offset_small)
131 return cipher.process(data)
139 def seek(self, offset, whence=0): # 0 = SEEK_SET
140 self.file.seek(offset, whence)
143 offset = self.file.tell()
146 def read(self, size=-1):
147 """A read must not follow a write, or vice-versa, without an intervening seek."""
148 index = self.file.tell()
149 ciphertext = self.file.read(size)
150 plaintext = self._crypt(index, ciphertext)
153 def write(self, plaintext):
154 """A read must not follow a write, or vice-versa, without an intervening seek.
155 If seeking and then writing causes a 'hole' in the file, the contents of the
156 hole are unspecified."""
157 index = self.file.tell()
158 ciphertext = self._crypt(index, plaintext)
159 self.file.write(ciphertext)
161 def truncate(self, newsize):
162 """Truncate or extend the file to 'newsize'. If it is extended, the contents after the
163 old end-of-file are unspecified. The file position after this operation is unspecified."""
164 self.file.truncate(newsize)
167 def make_dirs(dirname, mode=0777):
169 An idempotent version of os.makedirs(). If the dir already exists, do
170 nothing and return without raising an exception. If this call creates the
171 dir, return without raising an exception. If there is an error that
172 prevents creation or if the directory gets deleted after make_dirs() creates
173 it and before make_dirs() checks that it exists, raise an exception.
177 os.makedirs(dirname, mode)
181 if not os.path.isdir(dirname):
184 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...
188 A threadsafe and idempotent version of shutil.rmtree(). If the dir is
189 already gone, do nothing and return without raising an exception. If this
190 call removes the dir, return without raising an exception. If there is an
191 error that prevents deletion or if the directory gets created again after
192 rm_dir() deletes it and before rm_dir() checks that it is gone, raise an
197 os.chmod(dirname, stat.S_IWRITE | stat.S_IEXEC | stat.S_IREAD)
198 for f in os.listdir(dirname):
199 fullname = os.path.join(dirname, f)
200 if os.path.isdir(fullname):
205 except Exception, le:
206 # Ignore "No such file or directory"
207 if (not isinstance(le, OSError)) or le.args[0] != 2:
210 # Okay, now we've recursively removed everything, ignoring any "No
211 # such file or directory" errors, and collecting any other errors.
213 if os.path.exists(dirname):
217 raise OSError, "Failed to remove dir for unknown reason."
221 def remove_if_possible(f):
230 for root, dirs, files in os.walk(basedir):
232 fn = os.path.join(root, f)
233 size += os.path.getsize(fn)
237 def move_into_place(source, dest):
238 """Atomically replace a file, or as near to it as the platform allows.
239 The dest file may or may not exist."""
240 if "win32" in sys.platform.lower():
241 remove_if_possible(dest)
242 os.rename(source, dest)
244 def write_atomically(target, contents, mode="b"):
245 f = open(target+".tmp", "w"+mode)
250 move_into_place(target+".tmp", target)
252 def write(path, data, mode="wb"):
253 wf = open(path, mode)
260 rf = open(path, "rb")
266 def put_file(pathname, inf):
267 # TODO: create temporary file and move into place?
268 outf = open(os.path.expanduser(pathname), "wb")
271 data = inf.read(32768)
279 def precondition_abspath(path):
280 if not isinstance(path, unicode):
281 raise AssertionError("an abspath must be a Unicode string")
283 if sys.platform == "win32":
284 # This intentionally doesn't view absolute paths starting with a drive specification, or
285 # paths relative to the current drive, as acceptable.
286 if not path.startswith("\\\\"):
287 raise AssertionError("an abspath should be normalized using abspath_expanduser_unicode")
289 # This intentionally doesn't view the path '~' or paths starting with '~/' as acceptable.
290 if not os.path.isabs(path):
291 raise AssertionError("an abspath should be normalized using abspath_expanduser_unicode")
293 # Work around <http://bugs.python.org/issue3426>. This code is adapted from
294 # <http://svn.python.org/view/python/trunk/Lib/ntpath.py?revision=78247&view=markup>
295 # with some simplifications.
297 _getfullpathname = None
299 from nt import _getfullpathname
303 def abspath_expanduser_unicode(path, base=None):
305 Return the absolute version of a path. If 'base' is given and 'path' is relative,
306 the path will be expanded relative to 'base'.
307 'path' must be a Unicode string. 'base', if given, must be a Unicode string
308 corresponding to an absolute path as returned by a previous call to
309 abspath_expanduser_unicode.
311 if not isinstance(path, unicode):
312 raise AssertionError("paths must be Unicode strings")
314 precondition_abspath(base)
316 path = os.path.expanduser(path)
319 # On Windows, os.path.isabs will return True for paths without a drive letter,
320 # e.g. "\\". See <http://bugs.python.org/issue1669539>.
322 path = _getfullpathname(path or u".")
326 if not os.path.isabs(path):
328 path = os.path.join(os.getcwdu(), path)
330 path = os.path.join(base, path)
332 # We won't hit <http://bugs.python.org/issue5827> because
333 # there is always at least one Unicode path component.
334 path = os.path.normpath(path)
336 if sys.platform == "win32":
337 path = to_windows_long_path(path)
341 def to_windows_long_path(path):
342 # '/' is normally a perfectly valid path component separator in Windows.
343 # However, when using the "\\?\" syntax it is not recognized, so we
344 # replace it with '\' here.
345 path = path.replace(u"/", u"\\")
347 # Note that other normalizations such as removing '.' and '..' should
348 # be done outside this function.
350 if path.startswith(u"\\\\?\\") or path.startswith(u"\\\\.\\"):
352 elif path.startswith(u"\\\\"):
353 return u"\\\\?\\UNC\\" + path[2 :]
355 return u"\\\\?\\" + path
358 have_GetDiskFreeSpaceExW = False
359 if sys.platform == "win32":
361 from ctypes import WINFUNCTYPE, windll, POINTER, byref, c_ulonglong
362 from ctypes.wintypes import BOOL, DWORD, LPCWSTR
364 # <http://msdn.microsoft.com/en-us/library/aa383742%28v=VS.85%29.aspx>
365 PULARGE_INTEGER = POINTER(c_ulonglong)
367 # <http://msdn.microsoft.com/en-us/library/aa364937%28VS.85%29.aspx>
368 GetDiskFreeSpaceExW = WINFUNCTYPE(BOOL, LPCWSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER)(
369 ("GetDiskFreeSpaceExW", windll.kernel32))
371 # <http://msdn.microsoft.com/en-us/library/ms679360%28v=VS.85%29.aspx>
372 GetLastError = WINFUNCTYPE(DWORD)(("GetLastError", windll.kernel32))
374 have_GetDiskFreeSpaceExW = True
377 traceback.print_exc()
379 def get_disk_stats(whichdir, reserved_space=0):
380 """Return disk statistics for the storage disk, in the form of a dict
381 with the following fields.
382 total: total bytes on disk
383 free_for_root: bytes actually free on disk
384 free_for_nonroot: bytes free for "a non-privileged user" [Unix] or
385 the current user [Windows]; might take into
386 account quotas depending on platform
387 used: bytes used on disk
388 avail: bytes available excluding reserved space
389 An AttributeError can occur if the OS has no API to get disk information.
390 An EnvironmentError can occur if the OS call fails.
392 whichdir is a directory on the filesystem in question -- the
393 answer is about the filesystem, not about the directory, so the
394 directory is used only to specify which filesystem.
396 reserved_space is how many bytes to subtract from the answer, so
397 you can pass how many bytes you would like to leave unused on this
398 filesystem as reserved_space.
401 if have_GetDiskFreeSpaceExW:
402 # If this is a Windows system and GetDiskFreeSpaceExW is available, use it.
403 # (This might put up an error dialog unless
404 # SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX) has been called,
405 # which we do in allmydata.windows.fixups.initialize().)
407 n_free_for_nonroot = c_ulonglong(0)
408 n_total = c_ulonglong(0)
409 n_free_for_root = c_ulonglong(0)
410 retval = GetDiskFreeSpaceExW(whichdir, byref(n_free_for_nonroot),
412 byref(n_free_for_root))
414 raise OSError("Windows error %d attempting to get disk statistics for %r"
415 % (GetLastError(), whichdir))
416 free_for_nonroot = n_free_for_nonroot.value
417 total = n_total.value
418 free_for_root = n_free_for_root.value
420 # For Unix-like systems.
421 # <http://docs.python.org/library/os.html#os.statvfs>
422 # <http://opengroup.org/onlinepubs/7990989799/xsh/fstatvfs.html>
423 # <http://opengroup.org/onlinepubs/7990989799/xsh/sysstatvfs.h.html>
424 s = os.statvfs(whichdir)
427 # statvfs(2) is a wrapper around statfs(2).
428 # statvfs.f_frsize = statfs.f_bsize :
429 # "minimum unit of allocation" (statvfs)
430 # "fundamental file system block size" (statfs)
431 # statvfs.f_bsize = statfs.f_iosize = stat.st_blocks : preferred IO size
432 # on an encrypted home directory ("FileVault"), it gets f_blocks
433 # wrong, and s.f_blocks*s.f_frsize is twice the size of my disk,
434 # but s.f_bavail*s.f_frsize is correct
436 total = s.f_frsize * s.f_blocks
437 free_for_root = s.f_frsize * s.f_bfree
438 free_for_nonroot = s.f_frsize * s.f_bavail
440 # valid for all platforms:
441 used = total - free_for_root
442 avail = max(free_for_nonroot - reserved_space, 0)
444 return { 'total': total,
445 'free_for_root': free_for_root,
446 'free_for_nonroot': free_for_nonroot,
451 def get_available_space(whichdir, reserved_space):
452 """Returns available space for share storage in bytes, or None if no
453 API to get this information is available.
455 whichdir is a directory on the filesystem in question -- the
456 answer is about the filesystem, not about the directory, so the
457 directory is used only to specify which filesystem.
459 reserved_space is how many bytes to subtract from the answer, so
460 you can pass how many bytes you would like to leave unused on this
461 filesystem as reserved_space.
464 return get_disk_stats(whichdir, reserved_space)['avail']
465 except AttributeError:
467 except EnvironmentError:
468 log.msg("OS call to get disk statistics failed")