- def get_disk_stats(self):
- """Return disk statistics for the storage disk, in the form of a dict
- with the following fields.
- total: total bytes on disk
- free_for_root: bytes actually free on disk
- free_for_nonroot: bytes free for "a non-privileged user" [Unix] or
- the current user [Windows]; might take into
- account quotas depending on platform
- used: bytes used on disk
- avail: bytes available excluding reserved space
- An AttributeError can occur if the OS has no API to get disk information.
- An EnvironmentError can occur if the OS call fails."""
-
- if self.windows:
- # For Windows systems, where os.statvfs is not available, use GetDiskFreeSpaceEx.
- # <http://docs.activestate.com/activepython/2.5/pywin32/win32api__GetDiskFreeSpaceEx_meth.html>
- #
- # Although the docs say that the argument should be the root directory
- # of a disk, GetDiskFreeSpaceEx actually accepts any path on that disk
- # (like its Win32 equivalent).
-
- (free_for_nonroot, total, free_for_root) = self.win32api.GetDiskFreeSpaceEx(self.storedir)
- else:
- # For Unix-like systems.
- # <http://docs.python.org/library/os.html#os.statvfs>
- # <http://opengroup.org/onlinepubs/7990989799/xsh/fstatvfs.html>
- # <http://opengroup.org/onlinepubs/7990989799/xsh/sysstatvfs.h.html>
- s = os.statvfs(self.storedir)
-
- # on my mac laptop:
- # statvfs(2) is a wrapper around statfs(2).
- # statvfs.f_frsize = statfs.f_bsize :
- # "minimum unit of allocation" (statvfs)
- # "fundamental file system block size" (statfs)
- # statvfs.f_bsize = statfs.f_iosize = stat.st_blocks : preferred IO size
- # on an encrypted home directory ("FileVault"), it gets f_blocks
- # wrong, and s.f_blocks*s.f_frsize is twice the size of my disk,
- # but s.f_bavail*s.f_frsize is correct
-
- total = s.f_frsize * s.f_blocks
- free_for_root = s.f_frsize * s.f_bfree
- free_for_nonroot = s.f_frsize * s.f_bavail
-
- # valid for all platforms:
- used = total - free_for_root
- avail = max(free_for_nonroot - self.reserved_space, 0)
-
- return { 'total': total, 'free_for_root': free_for_root,
- 'free_for_nonroot': free_for_nonroot,
- 'used': used, 'avail': avail, }
-