Keep an original by its path and its content, so a second original at one path is not discarded (hq ADR 0100)
This commit is contained in:
+11
-5
@@ -22,13 +22,19 @@ import (
|
||||
// first copy is the one that was there before the mesh.
|
||||
type Keep func(path string, content []byte, mode os.FileMode) (string, error)
|
||||
|
||||
// KeepIn keeps originals under dir/kept, each named for the path it came from, readable by root
|
||||
// alone — a predecessor's configuration may carry its credentials.
|
||||
// KeepIn keeps originals under dir/kept, each named for the path it came from AND for what was in
|
||||
// it, readable by root alone — a predecessor's configuration may carry its credentials.
|
||||
//
|
||||
// **By content as well as path, because a path has more than one original.** A file held, let go
|
||||
// when its module was unassigned, rewritten by the predecessor and found again is a second
|
||||
// original; named by path alone the second copy was silently discarded while the report said it
|
||||
// was kept (novox/hq ADR 0100). The same content at the same path is kept once.
|
||||
func KeepIn(dir string) Keep {
|
||||
return func(path string, content []byte, _ os.FileMode) (string, error) {
|
||||
sum := sha256.Sum256([]byte(path))
|
||||
kept := filepath.Join(dir, "kept",
|
||||
hex.EncodeToString(sum[:])[:16]+"-"+filepath.Base(path))
|
||||
where := sha256.Sum256([]byte(path))
|
||||
what := sha256.Sum256(content)
|
||||
kept := filepath.Join(dir, "kept", hex.EncodeToString(where[:])[:12]+"-"+
|
||||
hex.EncodeToString(what[:])[:12]+"-"+filepath.Base(path))
|
||||
if _, err := os.Lstat(kept); err == nil {
|
||||
return kept, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user