From f9dd6ea2daada40292c2425963842f81bf6a00b0 Mon Sep 17 00:00:00 2001 From: Jim MacArthur Date: Thu, 6 Dec 2018 11:56:59 +0000 Subject: storage: Add Directory.get_size() --- buildstream/storage/_casbaseddirectory.py | 12 ++++++++++++ buildstream/storage/_filebaseddirectory.py | 4 ++++ buildstream/storage/directory.py | 6 ++++++ 3 files changed, 22 insertions(+) (limited to 'buildstream/storage') diff --git a/buildstream/storage/_casbaseddirectory.py b/buildstream/storage/_casbaseddirectory.py index 7e8093ede..1bdd22351 100644 --- a/buildstream/storage/_casbaseddirectory.py +++ b/buildstream/storage/_casbaseddirectory.py @@ -627,6 +627,18 @@ class CasBasedDirectory(Directory): self._recalculate_recursing_up() self._recalculate_recursing_down() + def get_size(self): + total = len(self.pb2_directory.SerializeToString()) + for i in self.index.values(): + if isinstance(i.buildstream_object, CasBasedDirectory): + total += i.buildstream_object.get_size() + elif isinstance(i.pb_object, remote_execution_pb2.FileNode): + src_name = self.cas_cache.objpath(i.pb_object.digest) + filesize = os.stat(src_name).st_size + total += filesize + # Symlink nodes are encoded as part of the directory serialization. + return total + def _get_identifier(self): path = "" if self.parent: diff --git a/buildstream/storage/_filebaseddirectory.py b/buildstream/storage/_filebaseddirectory.py index b919413f0..0752a0e05 100644 --- a/buildstream/storage/_filebaseddirectory.py +++ b/buildstream/storage/_filebaseddirectory.py @@ -30,6 +30,7 @@ See also: :ref:`sandboxing`. import os import time from .directory import Directory, VirtualDirectoryError +from .. import utils from ..utils import link_files, copy_files, list_relative_paths, _get_link_mtime, _magic_timestamp from ..utils import _set_deterministic_user, _set_deterministic_mtime @@ -201,6 +202,9 @@ class FileBasedDirectory(Directory): return list_relative_paths(self.external_directory) + def get_size(self): + return utils._get_dir_size(self.external_directory) + def __str__(self): # This returns the whole path (since we don't know where the directory started) # which exposes the sandbox directory; we will have to assume for the time being diff --git a/buildstream/storage/directory.py b/buildstream/storage/directory.py index 66b93a7f1..f572257d7 100644 --- a/buildstream/storage/directory.py +++ b/buildstream/storage/directory.py @@ -177,3 +177,9 @@ class Directory(): """ raise NotImplementedError() + + def get_size(self): + """ Get an approximation of the storage space in bytes used by this directory + and all files and subdirectories in it. Storage space varies by implementation + and effective space used may be lower than this number due to deduplication. """ + raise NotImplementedError() -- cgit v1.2.1