Blame SOURCES/BZ-1645173-clean-all-disk-usage-fixes.patch

5e9bef
diff -up yum-3.4.3/cli.py.orig yum-3.4.3/cli.py
5e9bef
--- yum-3.4.3/cli.py.orig	2019-03-27 16:55:58.034750506 +0100
5e9bef
+++ yum-3.4.3/cli.py	2019-03-27 16:57:54.515257711 +0100
5e9bef
@@ -1740,6 +1740,7 @@ class YumBaseCli(yum.YumBase, output.Yum
5e9bef
             paths = glob.glob(cacheglob + '/*')
5e9bef
             table = ([], [], [], [])  # (enabled, disabled, untracked, other)
5e9bef
             repos = self.repos.repos
5e9bef
+            empty = True
5e9bef
             for path in paths:
5e9bef
                 base = os.path.basename(path)
5e9bef
                 if os.path.isdir(path):
5e9bef
@@ -1750,18 +1751,29 @@ class YumBaseCli(yum.YumBase, output.Yum
5e9bef
                         col = 0
5e9bef
                     else:
5e9bef
                         col = 1
5e9bef
-                    # Recursively gather all files in this repodir
5e9bef
-                    files = yum.misc.getFileList(path, '', [])
5e9bef
                 else:
5e9bef
                     # Ordinary file (such as timedhosts)
5e9bef
                     col = 3
5e9bef
-                    files = [path]
5e9bef
-                usage = sum(map(os.path.getsize, files))
5e9bef
-                if usage > 0:
5e9bef
-                    table[col].append((usage, path))
5e9bef
+                usage = yum.misc.disk_usage(path)
5e9bef
+                if not usage:
5e9bef
+                    continue
5e9bef
+                table[col].append((usage, path))
5e9bef
+                # Detect any uncleaned data.
5e9bef
+                #
5e9bef
+                # We never remove directories or any unrecognized repodata
5e9bef
+                # files, so there always will be a few kilobytes left behind.
5e9bef
+                # To avoid a constant false alarm, let's ignore such files if
5e9bef
+                # they are really tiny (such as "productid").  The easiest way
5e9bef
+                # is to look at "usage" as it covers both directories and
5e9bef
+                # files.  Given that a typical cleaned repodir (4K) consists of
5e9bef
+                # the gen/ (4K) and packages/ (4K) subdirs and possibly the
5e9bef
+                # productid file (8K), let's "round" it up to 64K and use that
5e9bef
+                # as our threshold.
5e9bef
+                if col < 3 and usage > 64*1024:
5e9bef
+                    empty = False
5e9bef
 
5e9bef
             # Print the table (verbose mode only)
5e9bef
-            lines = [_('Disk usage of %s after cleanup:') % cacheglob]
5e9bef
+            lines = [_('Disk usage under %s after cleanup:') % cacheglob]
5e9bef
             headers = ('enabled repos', 'disabled repos', 'untracked repos',
5e9bef
                        'other data')
5e9bef
             totals = [0, 0, 0, 0]
5e9bef
@@ -1783,12 +1795,11 @@ class YumBaseCli(yum.YumBase, output.Yum
5e9bef
             msg = '\n'.join(lines)
5e9bef
             self.verbose_logger.log(yum.logginglevels.DEBUG_3, msg)
5e9bef
 
5e9bef
-            # Print a short hint for leftover repos specifically (non-verbose
5e9bef
-            # mode only)
5e9bef
-            total = sum(totals[:3])
5e9bef
-            if self.conf.debuglevel == 6 or not total:
5e9bef
+            # Print a short hint if leftover repos are found (non-verbose mode
5e9bef
+            # only).
5e9bef
+            if empty or self.conf.debuglevel == 6:
5e9bef
                 return code, []
5e9bef
-            total = self.format_number(total)
5e9bef
+            total = self.format_number(sum(totals[:3]))
5e9bef
             if total[-1] == ' ':
5e9bef
                 total = total[:-1] + 'bytes'
5e9bef
             msg = (_('Other repos take up %s of disk space '
5e9bef
diff -up yum-3.4.3/docs/yum.8.orig yum-3.4.3/docs/yum.8
5e9bef
--- yum-3.4.3/docs/yum.8.orig	2019-03-27 16:55:58.034750506 +0100
5e9bef
+++ yum-3.4.3/docs/yum.8	2019-03-27 16:57:54.515257711 +0100
5e9bef
@@ -1032,35 +1032,13 @@ Or:          \fByum list available 'foo*
5e9bef
 .SH "CLEAN OPTIONS"
5e9bef
 The following are the ways which you can invoke \fByum\fP in clean mode.
5e9bef
 
5e9bef
-Note that these commands only operate on files in currently enabled
5e9bef
-repositories.
5e9bef
-If you use substitution variables (such as $releasever) in your \fBcachedir\fP
5e9bef
-configuration, the operation is further restricted to the current values of
5e9bef
-those variables.
5e9bef
-
5e9bef
-For fine-grained control over what is being cleaned, you can use
5e9bef
-\fB\-\-enablerepo\fP, \fB\-\-disablerepo\fP and \fB\-\-releasever\fP as
5e9bef
-desired.
5e9bef
-Note, however, that you cannot use \fB\-\-releasever='*'\fP to do the cleaning
5e9bef
-for all values previously used.
5e9bef
-Also note that untracked (no longer configured) repositories will not be
5e9bef
-automatically cleaned.
5e9bef
-
5e9bef
-To purge the entire cache in one go, the easiest way is to delete the files
5e9bef
-manually.
5e9bef
-Depending on your \fBcachedir\fP configuration, this usually means treating any
5e9bef
-variables as shell wildcards and recursively removing matching directories.
5e9bef
-For example, if your \fBcachedir\fP is /var/cache/yum/$basearch/$releasever,
5e9bef
-then the whole /var/cache/yum directory has to be removed.
5e9bef
-If you do this, \fByum\fP will rebuild the cache as required the next time it
5e9bef
-is run (this may take a while).
5e9bef
-
5e9bef
-As a convenience, when you run \fByum clean all\fP, a recursive lookup will be
5e9bef
-done to detect any repositories not cleaned due to the above restrictions.
5e9bef
-If some are found, a message will be printed stating how much disk space they
5e9bef
-occupy and thus how much you can reclaim by cleaning them.
5e9bef
-If you also supply \fB\-\-verbose\fP, a more detailed breakdown will be
5e9bef
-printed.
5e9bef
+Note that these commands only operate on the currently enabled repositories
5e9bef
+within the current \fBcachedir\fR (that is, with any substitution variables
5e9bef
+such as $releasever expanded to their runtime values).
5e9bef
+To clean specific repositories, use \fB\-\-enablerepo\fP, \fB\-\-disablerepo\fP
5e9bef
+or \fB\-\-releasever\fP accordingly.
5e9bef
+Note, however, that untracked (no longer configured) repositories cannot be
5e9bef
+cleaned this way; they have to be removed manually.
5e9bef
 
5e9bef
 .IP "\fByum clean expire-cache\fP"
5e9bef
 Eliminate the local data saying when the metadata and mirrorlists were downloaded for each repo. This means yum will revalidate the cache for each repo. next time it is used. However if the cache is still valid, nothing significant was deleted.
5e9bef
@@ -1090,6 +1068,13 @@ Tell any enabled plugins to eliminate th
5e9bef
 
5e9bef
 .IP "\fByum clean all\fP"
5e9bef
 Does all of the above.
5e9bef
+As a convenience, if this command does not result in a completely empty cache
5e9bef
+due to the restrictions outlined at the beginning of this section, a message
5e9bef
+will be printed, saying how much disk space can be reclaimed by cleaning the
5e9bef
+remaining repos manually.
5e9bef
+For this purpose, a repo is considered clean when its disk usage doesn't exceed
5e9bef
+64KB (that is to account for directory entries and tiny metadata files such as
5e9bef
+"productid" that are never cleaned).
5e9bef
 
5e9bef
 .SH "EXAMPLES"
5e9bef
 .PP
5e9bef
diff -up yum-3.4.3/yum/misc.py.orig yum-3.4.3/yum/misc.py
5e9bef
--- yum-3.4.3/yum/misc.py.orig	2019-03-27 16:55:58.010750196 +0100
5e9bef
+++ yum-3.4.3/yum/misc.py	2019-03-27 16:57:54.515257711 +0100
5e9bef
@@ -1252,3 +1252,15 @@ def validate_repoid(repoid):
5e9bef
             return char
5e9bef
     else:
5e9bef
         return None
5e9bef
+
5e9bef
+def disk_usage(path):
5e9bef
+    """Return disk usage of the given filename, recursively for dirs."""
5e9bef
+    def usage(path):
5e9bef
+        return os.stat(path).st_blocks * 512
5e9bef
+    total = usage(path)
5e9bef
+    if not os.path.isdir(path):
5e9bef
+        return total
5e9bef
+    for root, dirs, files in os.walk(path):
5e9bef
+        paths = (os.path.join(root, entry) for entry in dirs + files)
5e9bef
+        total += sum(usage(path) for path in paths)
5e9bef
+    return total