From 91178bdf105db358a025ce36de1231f76875f3be Mon Sep 17 00:00:00 2001 From: Angelos Evripiotis Date: Tue, 2 Apr 2019 13:36:56 +0100 Subject: utils._get_volume_size: use shutil.disk_usage This is win32-friendly, less to read, and fixes a latent bug. Avoid dealing with low-level platform specifics by using the higher-level shutil.disk_usage() function to calc total and available bytes for us. shutil is also more correct - it uses f_frsize to convert from blocks to bytes, instead of f_bsize. I verified that the numbers match with the output from `df -k`, whereas the old implementation did not. Here are the meanings from `man statvfs`: f_frsize The size in bytes of the minimum unit of allocation on this file system. (This corresponds to the f_bsize member of struct statfs.) f_bsize The preferred length of I/O requests for files on this file system. (Corresponds to the f_iosize member of struct statfs.) Link to shutil.disk_usage() implementation: https://github.com/python/cpython/blob/3.5/Lib/shutil.py#L980 --- src/buildstream/utils.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/buildstream/utils.py b/src/buildstream/utils.py index f509ce998..775a11142 100644 --- a/src/buildstream/utils.py +++ b/src/buildstream/utils.py @@ -642,12 +642,11 @@ def _get_dir_size(path): # def _get_volume_size(path): try: - stat_ = os.statvfs(path) + usage = shutil.disk_usage(path) except OSError as e: raise UtilError("Failed to retrieve stats on volume for path '{}': {}" .format(path, e)) from e - - return stat_.f_bsize * stat_.f_blocks, stat_.f_bsize * stat_.f_bavail + return usage.total, usage.free # _parse_size(): -- cgit v1.2.1