changeset 219:19eaba51c632

Refactored the printing of FLAGS: print flags always and explicitely print symlink behaviour and encoding configuration
author Franz Glasner <fzglas.hg@dom66.de>
date Sat, 25 Jan 2025 13:49:12 +0100
parents dee891ed2307
children 8db78850d800
files cutils/treesum.py docs/notes.rst
diffstat 2 files changed, 36 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/cutils/treesum.py	Sat Jan 25 09:56:19 2025 +0100
+++ b/cutils/treesum.py	Sat Jan 25 13:49:12 2025 +0100
@@ -515,22 +515,23 @@
             flags.append("with-metadata-mode")
         if self._with_metadata_mtime:
             flags.append("with-metadata-mtime")
-        if self._follow_symlinks.command_line:
-            flags.append("follow-symlinks-commandline")
-        if self._follow_symlinks.directory:
-            flags.append("follow-symlinks-directory")
-        if self._follow_symlinks.file:
-            flags.append("follow-symlinks-file")
+        flags.append("follow-symlinks-commandline"
+                     if self._follow_symlinks.command_line
+                     else "no-follow-symlinks-commandline")
+        flags.append("follow-symlinks-directory"
+                     if self._follow_symlinks.directory
+                     else "no-follow-symlinks-directory")
+        flags.append("follow-symlinks-file"
+                     if self._follow_symlinks.file
+                     else "no-follow-symlinks-file")
         if self._size_only:
             flags.append("size-only")
-        if self._utf8_mode:
-            flags.append("utf8-mode")
+        flags.append("utf8-encoding" if self._utf8_mode else "fs-encoding")
         if self._print_size:
             flags.append("print-size")
-        if flags:
-            flags.sort()
-            self._outfp.write(
-                format_bsd_line("FLAGS", ",".join(flags), None, False))
+        flags.sort()
+        self._outfp.write(
+            format_bsd_line("FLAGS", ",".join(flags), None, False))
 
         if self._minimal is None:
             # Write execution timestamps in POSIX epoch and ISO format
@@ -865,6 +866,10 @@
                     "SIZE", None, opath, False, dir_size))
         else:
             if dir_tainted:
+                #
+                # IMPORTANT: Print errors BEFORE the associated digest line.
+                #            Otherwise the "info" command has a problem.
+                #
                 self._outfp.write(format_bsd_line(
                     b"ERROR", None, b"directory is tainted", False, None))
                 logging.error("Directory has filename problems: %r", opath)
@@ -1240,6 +1245,7 @@
         with reader:
             root = generator = flags = fsencoding = algorithm = digest \
                 = size = None
+            errors = set()
             comments = []
             in_block = False
             block_no = 0
@@ -1261,6 +1267,8 @@
                     root = record[1]
                 elif record[0] == "COMMENT":
                     comments.append(record[1])
+                elif record[0] == "ERROR":
+                    errors.add(record[1])
                 elif record[0] in ("TIMESTAMP", "ISOTIMESTAMP"):
                     pass
                 elif record[0] == "CRC32":
@@ -1283,22 +1291,25 @@
                             print_block_data(
                                 block_no,
                                 root, generator, fsencoding, flags, comments,
-                                algorithm, digest, size)
-                            root = flags = algorithm = digest = size = None
+                                errors, algorithm, digest, size)
+                            root = generator = flags = fsencoding = algorithm \
+                                = digest = size = None
+                            errors = set()
+                            comments = []
                         in_block = False
         if print_only_last_block:
             if not in_block:
                 if digest is not None or size is not None:
                     print_block_data(
                         block_no,
-                        root, generator, fsencoding, flags, comments,
+                        root, generator, fsencoding, flags, comments, errors,
                         algorithm, digest, size)
             else:
                 logging.warning("missing block end")
 
 
 def print_block_data(block_no, tag, generator, fsencoding, flags, comments,
-                     algorithm, digest, size):
+                     errors, algorithm, digest, size):
     digeststr = util.n(binascii.hexlify(digest)) if digest else "<no digest>"
     sizestr = str(size) if size is not None else "<no size>"
     print("BLOCK No %d:" % (block_no,))
@@ -1312,6 +1323,7 @@
     if algorithm != "SIZE":
         print("    Digest:", digeststr)
     print("    Size:", sizestr)
+    print("    Errors:", errors if errors else "<none>")
 
 
 if __name__ == "__main__":
--- a/docs/notes.rst	Sat Jan 25 09:56:19 2025 +0100
+++ b/docs/notes.rst	Sat Jan 25 13:49:12 2025 +0100
@@ -28,7 +28,7 @@
   ``FSENCODING``
 
       The result of :func:`os.getfilesystemencoding` that is in effect when
-      generating treesum digests
+      generating treesum digests (process environment)
 
    ``ERROR``
 
@@ -38,12 +38,9 @@
 
    ``FLAGS``
 
-      Defaults without any flags:
+      Some flags are always printed.
 
-      - not following any symlinks (but :option:`--no-follow-file-symlinks` is
-        not yet supported and coerced to :option:`--follow-file-symlinks`)
-      - no modes- no mtime
-      - filenames are in filesystem encoding
+      - no file modes and no file mtime is taken into account for digesting
       - no file size is printed
 
       Contains:
@@ -54,9 +51,11 @@
         for digest computation
       - ``with-metadata-mtime``: if the mtime as ISO-String and truncated to
         seconds is used in digest computation
-      - ``print-size``: if the filesize is to to be printed also
-      - ``utf8-mode``: the filenames are not in FS encoding but in UTF-8
-      - ``follow-symlinks-XXX`: if symlinks are followed on the command
+      - ``print-size``: if the filesize is to to be printed also -- does not
+                        change digests
+      - ``fs-encoding``: the filenames are given in FS encoding
+      - ``utf8-encoding``: the filenames are given in UTF-8 encoding
+      - ``follow-symlinks-XXX`` and ``no-follow-symlinkx-XXX``: if symlinks are followed on the command
         line, while directory walking or for files
       - ``size-only``: if no digest is to be computed and only a file's size
         is printed and the accumulated file sizes for a directory tree