Skip to content

Commit

Permalink
rework post processing of files
Browse files Browse the repository at this point in the history
Use groups of worker to make it faster and ensure encrypted files are
checksumed if required.
  • Loading branch information
orgrim committed Dec 2, 2021
1 parent 36d8997 commit fcaabd2
Show file tree
Hide file tree
Showing 3 changed files with 306 additions and 92 deletions.
19 changes: 10 additions & 9 deletions hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ func computeChecksum(path string, h hash.Hash) (string, error) {
return string(h.Sum(nil)), nil
}

func checksumFile(path string, algo string) error {
func checksumFile(path string, algo string) (string, error) {
var h hash.Hash

switch algo {
case "none":
return nil
return "", nil
case "sha1":
h = sha1.New()
case "sha224":
Expand All @@ -68,19 +68,20 @@ func checksumFile(path string, algo string) error {
case "sha512":
h = sha512.New()
default:
return fmt.Errorf("unsupported hash algorithm: %s", algo)
return "", fmt.Errorf("unsupported hash algorithm: %s", algo)
}

i, err := os.Stat(path)
if err != nil {
return err
return "", err
}

l.Verbosef("create checksum file: %s.%s", path, algo)
o, err := os.Create(fmt.Sprintf("%s.%s", path, algo))
sumFile := fmt.Sprintf("%s.%s", path, algo)
l.Verbosef("create checksum file: %s", sumFile)
o, err := os.Create(sumFile)
if err != nil {
l.Errorln(err)
return err
return "", err
}
defer o.Close()

Expand All @@ -102,7 +103,7 @@ func checksumFile(path string, algo string) error {
})

if err != nil {
return fmt.Errorf("error walking the path %q: %v\n", path, err)
return "", fmt.Errorf("error walking the path %q: %v\n", path, err)
}
} else {

Expand All @@ -113,7 +114,7 @@ func checksumFile(path string, algo string) error {
r, _ := computeChecksum(path, h)
fmt.Fprintf(o, "%x %s\n", r, path)
}
return nil
return sumFile, nil
}

func checksumFileList(paths []string, algo string, sumFilePrefix string) error {
Expand Down
12 changes: 6 additions & 6 deletions hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,18 @@ func TestChecksumFile(t *testing.T) {
}

// bad algo
if err := checksumFile("", "none"); err != nil {
if _, err := checksumFile("", "none"); err != nil {
t.Errorf("expected <nil>, got %q\n", err)
}

if err := checksumFile("", "other"); err == nil {
if _, err := checksumFile("", "other"); err == nil {
t.Errorf("expected err, got <nil>\n")
}

// test each algo with the file
for i, st := range tests {
t.Run(fmt.Sprintf("f%v", i), func(t *testing.T) {
if err := checksumFile("test", st.algo); err != nil {
if _, err := checksumFile("test", st.algo); err != nil {
t.Errorf("checksumFile returned: %v", err)
}

Expand All @@ -111,12 +111,12 @@ func TestChecksumFile(t *testing.T) {
// bad files
var e *os.PathError
l.logger.SetOutput(ioutil.Discard)
if err := checksumFile("", "sha1"); !errors.As(err, &e) {
if _, err := checksumFile("", "sha1"); !errors.As(err, &e) {
t.Errorf("expected an *os.PathError, got %q\n", err)
}

os.Chmod("test.sha1", 0444)
if err := checksumFile("test", "sha1"); !errors.As(err, &e) {
if _, err := checksumFile("test", "sha1"); !errors.As(err, &e) {
t.Errorf("expected an *os.PathError, got %q\n", err)
}
os.Chmod("test.sha1", 0644)
Expand All @@ -138,7 +138,7 @@ func TestChecksumFile(t *testing.T) {
// test each algo with the directory
for i, st := range tests {
t.Run(fmt.Sprintf("d%v", i), func(t *testing.T) {
if err := checksumFile("test.d", st.algo); err != nil {
if _, err := checksumFile("test.d", st.algo); err != nil {
t.Errorf("checksumFile returned: %v", err)
}

Expand Down
Loading

0 comments on commit fcaabd2

Please sign in to comment.