storage/reflink: simplify volume.usage()

This commit is contained in:
Rusty Bird 2019-06-15 16:03:43 +00:00
parent 9f5d05bfde
commit 30b92f8845
No known key found for this signature in database
GPG Key ID: 469D78F47AAF2ADF
2 changed files with 9 additions and 13 deletions

View File

@ -36,7 +36,6 @@ from contextlib import contextmanager, suppress
import qubes.storage import qubes.storage
BLKSIZE = 512
FICLONE = 1074041865 # defined in <linux/fs.h> FICLONE = 1074041865 # defined in <linux/fs.h>
LOOP_SET_CAPACITY = 0x4C07 # defined in <linux/loop.h> LOOP_SET_CAPACITY = 0x4C07 # defined in <linux/loop.h>
LOGGER = logging.getLogger('qubes.storage.reflink') LOGGER = logging.getLogger('qubes.storage.reflink')
@ -342,10 +341,9 @@ class ReflinkVolume(qubes.storage.Volume):
''' Return volume disk usage from the VM's perspective. It is ''' Return volume disk usage from the VM's perspective. It is
usually much lower from the host's perspective due to CoW. usually much lower from the host's perspective due to CoW.
''' '''
with suppress(FileNotFoundError): for path in (self._path_dirty, self._path_clean):
return _get_file_disk_usage(self._path_dirty) with suppress(FileNotFoundError):
with suppress(FileNotFoundError): return os.stat(path).st_blocks * 512
return _get_file_disk_usage(self._path_clean)
return 0 return 0
@ -369,10 +367,6 @@ def _replace_file(dst):
_remove_file(tmp.name) _remove_file(tmp.name)
raise raise
def _get_file_disk_usage(path):
''' Return real disk usage (not logical file size) of a file. '''
return os.stat(path).st_blocks * BLKSIZE
def _fsync_dir(path): def _fsync_dir(path):
dir_fd = os.open(path, os.O_RDONLY | os.O_DIRECTORY) dir_fd = os.open(path, os.O_RDONLY | os.O_DIRECTORY)
try: try:

View File

@ -62,15 +62,17 @@ class ReflinkMixin:
os.symlink(img_real, img_sym) os.symlink(img_real, img_sym)
reflink._create_sparse_file(img_real, size_initial) reflink._create_sparse_file(img_real, size_initial)
self.assertEqual(reflink._get_file_disk_usage(img_real), 0) stat = os.stat(img_real)
self.assertEqual(os.stat(img_real).st_size, size_initial) self.assertEqual(stat.st_blocks, 0)
self.assertEqual(stat.st_size, size_initial)
dev_from_real = setup_loopdev(img_real, cleanup_via=self.addCleanup) dev_from_real = setup_loopdev(img_real, cleanup_via=self.addCleanup)
dev_from_sym = setup_loopdev(img_sym, cleanup_via=self.addCleanup) dev_from_sym = setup_loopdev(img_sym, cleanup_via=self.addCleanup)
reflink._resize_file(img_real, size_resized) reflink._resize_file(img_real, size_resized)
self.assertEqual(reflink._get_file_disk_usage(img_real), 0) stat = os.stat(img_real)
self.assertEqual(os.stat(img_real).st_size, size_resized) self.assertEqual(stat.st_blocks, 0)
self.assertEqual(stat.st_size, size_resized)
reflink_update_loopdev_sizes(os.path.join(self.test_dir, 'unrelated')) reflink_update_loopdev_sizes(os.path.join(self.test_dir, 'unrelated'))