[entropy.tools,entropy.spm] provide hardlinks aware file size counting function and use it in entropy.spm

This commit is contained in:
Fabio Erculiani
2011-03-01 18:32:42 +01:00
parent 79c2bac529
commit 3a6be1ec97
2 changed files with 31 additions and 2 deletions
@@ -1100,7 +1100,7 @@ class PortagePlugin(SpmPlugin):
# fine too.
data['content_safety'] = self._extract_pkg_metadata_content_safety(
data['content'], pkg_dir)
data['disksize'] = entropy.tools.sum_file_sizes([
data['disksize'] = entropy.tools.sum_file_sizes_hardlinks([
os.path.join(pkg_dir, x) for x in data['content']])
data['provided_libs'] = self._extract_pkg_metadata_provided_libs(
pkg_dir, data['content'])
+30 -1
View File
@@ -511,6 +511,8 @@ def get_file_size(file_path):
def sum_file_sizes(file_list):
"""
Return file size sum of given list of paths.
NOTE: This function does NOT consider hardlinks, roughly summing up
file_list elements.
@param file_list: list of file paths
@type file_list: list
@@ -520,9 +522,36 @@ def sum_file_sizes(file_list):
size = 0
for myfile in file_list:
try:
size += get_file_size(myfile)
mystat = os.lstat(myfile)
except (OSError, IOError,):
continue
size += mystat.st_size
return size
def sum_file_sizes_hardlinks(file_list):
"""
Return file size sum of given list of paths.
NOTE: This function does consider hardlinks, not counting the same files
more than once.
@param file_list: list of file paths
@type file_list: list
@return: summed size in bytes
@rtype: int
"""
size = 0
inode_cache = set()
for myfile in file_list:
try:
mystat = os.lstat(myfile)
except (OSError, IOError,):
continue
inode = (mystat.st_ino, mystat.st_dev)
if inode in inode_cache:
continue
inode_cache.add(inode)
size += mystat.st_size
inode_cache.clear()
return size
def check_required_space(mountpoint, bytes_required):