git » summer » commit 7a34a0c

generate: Do not touch files with checksums

author Alberto Bertogli
2023-04-05 22:42:49 UTC
committer Alberto Bertogli
2023-04-05 22:42:49 UTC
parent 1f0fa40208566de16addad87b11ed14826b03712

generate: Do not touch files with checksums

Overwriting checksums is not very useful, and can accidentally cause us
to lose valuable checksum information.

Change the generate subcommand to only compute checksums for files that
don't have one.

This is very useful when generating checksums for a lot of files for the
first time, because it will not waste time on files that already have
been processed.

summer.go +14 -2
test/basic.t +5 -7
test/help.t +15 -6
test/sqlite.t +2 -2

diff --git a/summer.go b/summer.go
index d7e04bc..e61df6e 100644
--- a/summer.go
+++ b/summer.go
@@ -20,14 +20,17 @@ Checksums are written to/read from each files' extended attributes by default,
 or to a separate database file (with the -db flag).
 
 Usage:
+
   summer update <dir>
       Verify checksums in the given directory, and update them for new or
       changed files.
   summer verify <dir>
       Verify checksums in the given directory.
   summer generate <dir>
-      Write checksums for the given directory. Pre-existing checksums are
-      overwritten without verification.
+      Write checksums for the given directory. Files with pre-existing
+      checksums are left untouched, and checksums are not verified.
+      Useful when generating checksums for a lot of files for the first time,
+      as is faster to resume work if interrupted.
   summer version
       Print software version information.
 
@@ -147,6 +150,15 @@ func generate(db DB, root string) error {
 		}
 		defer fd.Close()
 
+		hasAttr, err := db.Has(fd)
+		if err != nil {
+			return err
+		}
+		if hasAttr {
+			// Skip files that already have a checksum.
+			return nil
+		}
+
 		h := crc32.New(crc32c)
 		_, err = io.Copy(h, fd)
 		if err != nil {
diff --git a/test/basic.t b/test/basic.t
index b7abe63..dd65c48 100644
--- a/test/basic.t
+++ b/test/basic.t
@@ -57,11 +57,12 @@ it.
   detected 1 corrupted files
   [1]
 
-But "generate" does override it.
+Editing the file makes us ignore the previous checksum.
 
-  $ summer generate .
+  $ touch hola
+  $ summer update .
   \r (no-eol) (esc)
-  0s: 0 matched, 0 modified, 3 new, 0 corrupted
+  0s: 2 matched, 1 modified, 0 new, 0 corrupted
   $ summer verify .
   \r (no-eol) (esc)
   0s: 3 matched, 0 modified, 0 new, 0 corrupted
@@ -77,10 +78,7 @@ Check verbose and quiet.
   0s: 3 matched, 0 modified, 1 new, 0 corrupted
   $ summer -v generate .
   "denuevo": writing checksum \(checksum:\w+, mtime:\d+\) (re)
-  "empty": writing checksum \(checksum:0, mtime:\d+\) (re)
-  "hola": writing checksum \(checksum:\w+, mtime:\d+\) (re)
-  "nueva": writing checksum \(checksum:\w+, mtime:\d+\) (re)
-  0s: 0 matched, 0 modified, 4 new, 0 corrupted
+  0s: 0 matched, 0 modified, 1 new, 0 corrupted
   $ summer -q verify .
   $ summer -q generate .
   $ summer -q update .
diff --git a/test/help.t b/test/help.t
index beaefd4..a14fefa 100644
--- a/test/help.t
+++ b/test/help.t
@@ -14,14 +14,17 @@ No arguments.
   or to a separate database file (with the -db flag).
   
   Usage:
+  
     summer update <dir>
         Verify checksums in the given directory, and update them for new or
         changed files.
     summer verify <dir>
         Verify checksums in the given directory.
     summer generate <dir>
-        Write checksums for the given directory. Pre-existing checksums are
-        overwritten without verification.
+        Write checksums for the given directory. Files with pre-existing
+        checksums are left untouched, and checksums are not verified.
+        Useful when generating checksums for a lot of files for the first time,
+        as is faster to resume work if interrupted.
     summer version
         Print software version information.
   
@@ -47,14 +50,17 @@ Too few arguments.
   or to a separate database file (with the -db flag).
   
   Usage:
+  
     summer update <dir>
         Verify checksums in the given directory, and update them for new or
         changed files.
     summer verify <dir>
         Verify checksums in the given directory.
     summer generate <dir>
-        Write checksums for the given directory. Pre-existing checksums are
-        overwritten without verification.
+        Write checksums for the given directory. Files with pre-existing
+        checksums are left untouched, and checksums are not verified.
+        Useful when generating checksums for a lot of files for the first time,
+        as is faster to resume work if interrupted.
     summer version
         Print software version information.
   
@@ -80,14 +86,17 @@ No valid path (the argument is given, but it is empty).
   or to a separate database file (with the -db flag).
   
   Usage:
+  
     summer update <dir>
         Verify checksums in the given directory, and update them for new or
         changed files.
     summer verify <dir>
         Verify checksums in the given directory.
     summer generate <dir>
-        Write checksums for the given directory. Pre-existing checksums are
-        overwritten without verification.
+        Write checksums for the given directory. Files with pre-existing
+        checksums are left untouched, and checksums are not verified.
+        Useful when generating checksums for a lot of files for the first time,
+        as is faster to resume work if interrupted.
     summer version
         Print software version information.
   
diff --git a/test/sqlite.t b/test/sqlite.t
index 35db2b5..056f317 100644
--- a/test/sqlite.t
+++ b/test/sqlite.t
@@ -26,9 +26,9 @@ Check that the root path doesn't confuse us.
 
 Force a write error to check it is appropriately handled.
 
-  $ summer "-db=file:db.sqlite3?mode=ro" generate .
+  $ summer "-db=file:db.sqlite3?mode=ro" update .
   \r (no-eol) (esc)
-  0s: 0 matched, 0 modified, 0 new, 0 corrupted
+  0s: 0 matched, 1 modified, 0 new, 0 corrupted
   attempt to write a readonly database
   [1]