Skip to content

Commit

Permalink
Fix --size for -s option
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardobranco777 committed Oct 26, 2024
1 parent 1074d64 commit edca0d1
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
28 changes: 16 additions & 12 deletions hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"os"
)

func hashF(f io.ReadCloser, checksums []*Checksum) []*Checksum {
func hashF(f io.ReadCloser, checksums []*Checksum) ([]*Checksum, Size) {
if checksums == nil {
checksums = make([]*Checksum, len(chosen))
for i, h := range chosen {
Expand All @@ -22,11 +22,10 @@ func hashF(f io.ReadCloser, checksums []*Checksum) []*Checksum {
n, err := io.Copy(h, f)
if err != nil {
log.Print(err)
return nil
return nil, 0
}
h.written = n
h.sum = h.Sum(nil)
return checksums
return checksums, Size(n)
}

writers := make([]io.Writer, len(checksums))
Expand All @@ -40,16 +39,15 @@ func hashF(f io.ReadCloser, checksums []*Checksum) []*Checksum {
pipeWriters[i] = pw
g.Go(func() error {
defer pr.Close()
n, err := io.Copy(h, pr)
if err != nil {
if _, err := io.Copy(h, pr); err != nil {
return err
}
h.written = n
h.sum = h.Sum(nil)
return nil
})
}

var size Size
g.Go(func() error {
defer func() {
for _, pw := range pipeWriters {
Expand All @@ -59,15 +57,16 @@ func hashF(f io.ReadCloser, checksums []*Checksum) []*Checksum {
// build the multiwriter for all the pipes
mw := io.MultiWriter(writers...)
// copy the data into the multiwriter
_, err := io.Copy(mw, f)
n, err := io.Copy(mw, f)
size = Size(n)
return err
})
if err := g.Wait(); err != nil {
log.Print(err)
return nil
return nil, 0
}

return checksums
return checksums, size
}

func hashFile(file string, checksums []*Checksum) (*Checksums, error) {
Expand All @@ -85,16 +84,20 @@ func hashFile(file string, checksums []*Checksum) (*Checksums, error) {
return nil, fmt.Errorf("%s is a directory", file)
}

checksums, size := hashF(f, checksums)
return &Checksums{
file: file,
checksums: hashF(f, checksums),
size: size,
checksums: checksums,
}, nil
}

func hashStdin() *Checksums {
checksums, size := hashF(os.Stdin, nil)
return &Checksums{
file: "",
checksums: hashF(os.Stdin, nil),
size: size,
checksums: checksums,
}
}

Expand All @@ -108,6 +111,7 @@ func hashString(str string) *Checksums {
}
return &Checksums{
file: `"` + str + `"`,
size: Size(len(str)),
checksums: checksums,
}
}
16 changes: 8 additions & 8 deletions hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,23 @@ var xhmac = map[string][]*Checksum{
},
}

func testIt(t *testing.T, funcname string, f func(io.ReadCloser, []*Checksum) []*Checksum) {
func testIt(t *testing.T, funcname string, f func(io.ReadCloser, []*Checksum) ([]*Checksum, Size)) {
// SHA256("The quick brown fox jumps over the lazy dog")
foxString := "The quick brown fox jumps over the lazy dog"
foxSha256 := []byte{0xd7, 0xa8, 0xfb, 0xb3, 0x07, 0xd7, 0x80, 0x94, 0x69, 0xca, 0x9a, 0xbc, 0xb0, 0x08, 0x2e, 0x4f, 0x8d, 0x56, 0x51, 0xe4, 0x6d, 0x3c, 0xdb, 0x76, 0x2d, 0x02, 0xd0, 0xbf, 0x37, 0xc9, 0xe5, 0x92}

for checksum := range xchecksum {
reader := io.NopCloser(strings.NewReader(checksum))
got := f(reader, xchecksum[checksum][:1])
got, _ := f(reader, xchecksum[checksum][:1])
if !reflect.DeepEqual(got, xchecksum[checksum][:1]) {
t.Errorf("%s(%q) got %v, want %v", funcname, checksum, got, xchecksum[checksum][:1])
}
}

// Test with nil (should default to SHA-256)
reader := io.NopCloser(strings.NewReader(foxString))
got := f(reader, nil)
if got[0].hash != crypto.SHA256 || !bytes.Equal(got[0].sum, foxSha256) || got[0].written != int64(len(foxString)) {
got, _ := f(reader, nil)
if got[0].hash != crypto.SHA256 || !bytes.Equal(got[0].sum, foxSha256) {
t.Errorf("%s(%q) got %v, want %v", funcname, foxString, got[0].sum, foxSha256)
}

Expand All @@ -64,17 +64,17 @@ func testIt(t *testing.T, funcname string, f func(io.ReadCloser, []*Checksum) []
for checksum := range xhmac {
reader := io.NopCloser(strings.NewReader(checksum))
want := xhmac[checksum][:1]
got := f(reader, want)
got, _ := f(reader, want)
if !reflect.DeepEqual(got, want) {
t.Errorf("%s(%q) got %v, want %v", funcname, checksum, got, want)
}
}
}

func testIt2(t *testing.T, funcname string, f func(io.ReadCloser, []*Checksum) []*Checksum) {
func testIt2(t *testing.T, funcname string, f func(io.ReadCloser, []*Checksum) ([]*Checksum, Size)) {
for checksum, want := range xchecksum {
reader := io.NopCloser(strings.NewReader(checksum))
got := f(reader, want)
got, _ := f(reader, want)
if !reflect.DeepEqual(got, want) {
t.Errorf("%s(%q) got %v, want %v", funcname, checksum, got, want)
}
Expand All @@ -87,7 +87,7 @@ func testIt2(t *testing.T, funcname string, f func(io.ReadCloser, []*Checksum) [

for checksum, want := range xhmac {
reader := io.NopCloser(strings.NewReader(checksum))
got := f(reader, want)
got, _ := f(reader, want)
if !reflect.DeepEqual(got, want) {
t.Errorf("%s(%q) got %v, want %v", funcname, checksum, got, want)
}
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func getOutput(results *Checksums, noEscape bool) []*Output {
outputs = append(outputs, &Output{
File: file,
Name: "SIZE",
Sum: strconv.FormatInt(results.checksums[0].written, 10),
Sum: strconv.FormatInt(results.size, 10),
})
}
for i := range results.checksums {
Expand Down
8 changes: 5 additions & 3 deletions typesvars.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ type Algorithm struct {
type Checksum struct {
hash crypto.Hash
hash.Hash
written int64
sum []byte
csum []byte // Used by the -c option
sum []byte
csum []byte // Used by the -c option
}

type Size = int64

// Checksums type to hold the checksums for a file
type Checksums struct {
file string
size Size
checksums []*Checksum
}

Expand Down

0 comments on commit edca0d1

Please sign in to comment.