Skip to content

Commit

Permalink
Merge pull request #7 from boyter/mtime
Browse files Browse the repository at this point in the history
Mtime
  • Loading branch information
boyter authored Aug 12, 2024
2 parents 0345f5f + 0e6fb26 commit 39dfdc4
Show file tree
Hide file tree
Showing 30 changed files with 1,085 additions and 25 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/boyter/hashit
go 1.22

require (
github.com/djherbis/times v1.6.0
github.com/gosuri/uiprogress v0.0.1
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1
github.com/spf13/cobra v1.8.1
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/djherbis/times v1.6.0 h1:w2ctJ92J8fBvWPxugmXIv7Nz7Q3iDMKNx9v5ocVH20c=
github.com/djherbis/times v1.6.0/go.mod h1:gOHeRAz2h+VJNZ5Gmc/o7iD9k4wW7NMVqieYCY99oc0=
github.com/gosuri/uilive v0.0.4 h1:hUEBpQDj8D8jXgtCdBu7sWsy5sbW/5GhuO8KBwJ2jyY=
github.com/gosuri/uilive v0.0.4/go.mod h1:V/epo5LjjlDE5RJUcqx8dbw+zc93y5Ya3yg8tfZ74VI=
github.com/gosuri/uiprogress v0.0.1 h1:0kpv/XY/qTmFWl/SkaJykZXrBBzwwadmW8fRb7RJSxw=
Expand All @@ -25,6 +27,7 @@ github.com/zeebo/pcg v1.0.1 h1:lyqfGeWiv4ahac6ttHs+I5hwtH/+1mrhlCtVNQM2kHo=
github.com/zeebo/pcg v1.0.1/go.mod h1:09F0S9iiKrwn9rlI5yjLkmrug154/YRW6KnnXVDM/l4=
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
Expand Down
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ func main() {
runtime.NumCPU(),
"number of threads processing files, by default the number of CPU cores",
)
flags.BoolVar(
&processor.MTime,
"mtime",
false,
"enable mtime output",
)

if err := rootCmd.Execute(); err != nil {
os.Exit(1)
Expand Down
22 changes: 18 additions & 4 deletions processor/formatters.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,21 +256,35 @@ func toHashDeep(input chan Result) string {

str.WriteString("%%%% HASHDEEP-1.0\n")
if !contains(Hash, "sha256") && !contains(Hash, "all") {
str.WriteString("%%%% size,md5,filename\n")
str.WriteString("%%%% size,md5,filename")
} else {
str.WriteString("%%%% size,md5,sha256,filename\n")
str.WriteString("%%%% size,md5,sha256,filename")
}

if MTime {
str.WriteString(",mtime")
}
str.WriteString("\n")

str.WriteString(fmt.Sprintf("## Invoked from: %s\n", pwd))
str.WriteString(fmt.Sprintf("## $ %s\n", strings.Join(os.Args, " ")))
str.WriteString("##\n")

if !contains(Hash, "sha256") && !contains(Hash, "all") {
for res := range input {
str.WriteString(fmt.Sprintf("%d,%s,%s\n", res.Bytes, res.MD5, res.File))
str.WriteString(fmt.Sprintf("%d,%s,%s", res.Bytes, res.MD5, res.File))
if MTime {
str.WriteString(fmt.Sprintf(",%s", res.MTime.Format("2006-01-02 15:04:05")))
}
str.WriteString("\n")
}
} else {
for res := range input {
str.WriteString(fmt.Sprintf("%d,%s,%s,%s\n", res.Bytes, res.MD5, res.SHA256, res.File))
str.WriteString(fmt.Sprintf("%d,%s,%s,%s", res.Bytes, res.MD5, res.SHA256, res.File))
if MTime {
str.WriteString(fmt.Sprintf(",%s", res.MTime.Format("2006-01-02 15:04:05")))
}
str.WriteString("\n")
}
}

Expand Down
5 changes: 4 additions & 1 deletion processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

// Global Version
var Version = "1.2.0"
var Version = "1.3.0"

// Verbose enables verbose logging output
var Verbose = false
Expand All @@ -23,6 +23,9 @@ var Debug = false
// Trace enables trace logging output which is extremely verbose
var Trace = false

// MTime enable mtime calculation and output
var MTime = false

// Progress uses ui bar to display the progress of files
var Progress = false

Expand Down
35 changes: 17 additions & 18 deletions processor/structs.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package processor

import "time"

// Holds the result after processing the hashes for the file
type Result struct {
File string
MD4 string
MD5 string
SHA1 string
SHA256 string
SHA512 string
Blake2b256 string
Blake2b512 string
Blake3 string
Sha3224 string
Sha3256 string
Sha3384 string
Sha3512 string
Bytes int64
Description string
Version string
Date string
Urls []string
File string
MD4 string
MD5 string
SHA1 string
SHA256 string
SHA512 string
Blake2b256 string
Blake2b512 string
Blake3 string
Sha3224 string
Sha3256 string
Sha3384 string
Sha3512 string
Bytes int64
MTime *time.Time
}
17 changes: 15 additions & 2 deletions processor/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"crypto/sha512"
"encoding/hex"
"fmt"
"github.com/djherbis/times"
"github.com/gosuri/uiprogress"
"github.com/minio/blake2b-simd"
"github.com/zeebo/blake3"
Expand All @@ -19,6 +20,7 @@ import (
"os"
"strings"
"sync"
"time"
)

const (
Expand All @@ -44,14 +46,23 @@ func fileProcessorWorker(input chan string, output chan Result) {
// Open the file and determine if we should read it from disk or memory map
// based on how large it is reported as being
file, err := os.OpenFile(res, os.O_RDONLY, 0644)

if err != nil {
printError(fmt.Sprintf("Unable to process file %s with error %s", res, err.Error()))
continue
}

fi, err := file.Stat()
var mtime time.Time
if MTime {
stat, err := times.Stat(res)
if err != nil {
printError(fmt.Sprintf("Unable to read mtime file %s with error %s", res, err.Error()))
return
}
mtime = stat.ModTime()
//fmt.Println(stat.ModTime().Format("2006-01-02 15:04:05"))
}

fi, err := file.Stat()
if err != nil {
printError(fmt.Sprintf("Unable to get file info for file %s with error %s", res, err.Error()))
continue
Expand Down Expand Up @@ -81,6 +92,7 @@ func fileProcessorWorker(input chan string, output chan Result) {
if err == nil {
r.File = res
r.Bytes = fsize
r.MTime = &mtime
output <- r
}

Expand Down Expand Up @@ -117,6 +129,7 @@ func fileProcessorWorker(input chan string, output chan Result) {
if err == nil {
r.File = res
r.Bytes = fsize
r.MTime = &mtime
output <- r
}
}
Expand Down
22 changes: 22 additions & 0 deletions vendor/github.com/djherbis/times/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions vendor/github.com/djherbis/times/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 39dfdc4

Please sign in to comment.