Skip to content

Commit

Permalink
fix -hash for multi and serve
Browse files Browse the repository at this point in the history
  • Loading branch information
richardlehane committed Nov 23, 2016
1 parent f6a0da7 commit dfe294f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 41 deletions.
51 changes: 38 additions & 13 deletions cmd/sf/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,51 +25,76 @@ import (

const hashChoices = "'md5', 'sha1', 'sha256', 'sha512', 'crc'"

func getHash(typ string) hash.Hash {
type hashTyp int

const (
md5Hash hashTyp = iota
sha1Hash
sha256Hash
sha512Hash
crcHash
)

func getHash(typ string) hashTyp {
switch typ {
case "", "false":
case "md5", "MD5":
return md5.New()
return md5Hash
case "sha1", "SHA1":
return sha1.New()
return sha1Hash
case "sha256", "SHA256":
return sha256.New()
return sha256Hash
case "sha512", "SHA512":
return sha512.New()
return sha512Hash
case "crc", "CRC":
return crcHash
}
return -1
}

func makeHash(typ hashTyp) hash.Hash {
switch typ {
case md5Hash:
return md5.New()
case sha1Hash:
return sha1.New()
case sha256Hash:
return sha256.New()
case sha512Hash:
return sha512.New()
case crcHash:
return crc32.NewIEEE()
}
return nil
}

func hashHeader(pad bool, typ string) string {
func (typ hashTyp) header(pad bool) string {
switch typ {
default:
return "no"
case "md5", "MD5":
case md5Hash:
if pad {
return "md5 "
}
return "md5"
case "sha1", "SHA1":
case sha1Hash:
if pad {
return "sha1 "
}
return "sha1"
case "sha256", "SHA256":
case sha256Hash:
if pad {
return "sha256"
}
return "sha256"
case "sha512", "SHA512":
case sha512Hash:
if pad {
return "sha512"
}
return "sha512"
case "crc", "CRC":
case crcHash:
if pad {
return "crc "
}
return "crc"
}
return "no"
}
20 changes: 10 additions & 10 deletions cmd/sf/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ func decodePath(s, b64 string) (string, error) {
return s[10:], nil
}

func parseRequest(w http.ResponseWriter, r *http.Request, s *siegfried.Siegfried, wg *sync.WaitGroup) (error, string, writer, bool, string, *siegfried.Siegfried, getFn) {
func parseRequest(w http.ResponseWriter, r *http.Request, s *siegfried.Siegfried, wg *sync.WaitGroup) (error, string, writer, bool, hashTyp, *siegfried.Siegfried, getFn) {
// json, csv, droid or yaml
paramsErr := func(field, expect string) (error, string, writer, bool, string, *siegfried.Siegfried, getFn) {
return fmt.Errorf("bad request; in param %s got %s; valid values %s", field, r.FormValue(field), expect), "", nil, false, "", nil, nil
paramsErr := func(field, expect string) (error, string, writer, bool, hashTyp, *siegfried.Siegfried, getFn) {
return fmt.Errorf("bad request; in param %s got %s; valid values %s", field, r.FormValue(field), expect), "", nil, false, -1, nil, nil
}
var (
mime string
Expand Down Expand Up @@ -133,12 +133,12 @@ func parseRequest(w http.ResponseWriter, r *http.Request, s *siegfried.Siegfried
if v := r.FormValue("hash"); v != "" {
h = v
}
cs := getHash(h)
ht := getHash(h)
// sig
sf := s
if v := r.FormValue("sig"); v != "" {
if _, err := os.Stat(config.Local(v)); err != nil {
return fmt.Errorf("bad request; sig param should be path to a signature file (absolute or relative to home); got %v", err), "", nil, false, "", nil, nil
return fmt.Errorf("bad request; sig param should be path to a signature file (absolute or relative to home); got %v", err), "", nil, false, -1, nil, nil
}
nsf, err := siegfried.Load(config.Local(v))
if err == nil {
Expand All @@ -148,15 +148,15 @@ func parseRequest(w http.ResponseWriter, r *http.Request, s *siegfried.Siegfried
gf := func(path, mime, mod string, sz int64) *context {
c := ctxPool.Get().(*context)
c.path, c.mime, c.mod, c.sz = path, mime, mod, sz
c.s, c.w, c.wg, c.h, c.z = sf, wr, wg, cs, z
c.s, c.w, c.wg, c.h, c.z = sf, wr, wg, makeHash(ht), z
return c
}
return nil, mime, wr, norec, h, sf, gf
return nil, mime, wr, norec, ht, sf, gf
}

func handleIdentify(w http.ResponseWriter, r *http.Request, s *siegfried.Siegfried, ctxts chan *context) {
wg := &sync.WaitGroup{}
err, mime, wr, nr, hh, sf, gf := parseRequest(w, r, s, wg)
err, mime, wr, nr, ht, sf, gf := parseRequest(w, r, s, wg)
if err != nil {
handleErr(w, http.StatusNotFound, err)
return
Expand All @@ -182,7 +182,7 @@ func handleIdentify(w http.ResponseWriter, r *http.Request, s *siegfried.Siegfri
sz = r.ContentLength
}
w.Header().Set("Content-Type", mime)
wr.writeHead(sf, hh)
wr.writeHead(sf, ht)
wg.Add(1)
ctx := gf(h.Filename, "", mod, sz)
ctxts <- ctx
Expand All @@ -200,7 +200,7 @@ func handleIdentify(w http.ResponseWriter, r *http.Request, s *siegfried.Siegfri
return
}
w.Header().Set("Content-Type", mime)
wr.writeHead(sf, hh)
wr.writeHead(sf, ht)
err = identify(ctxts, path, "", nr, gf)
wg.Wait()
wr.writeTail()
Expand Down
12 changes: 6 additions & 6 deletions cmd/sf/sf.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ func (we WalkError) Error() string {
return fmt.Sprintf("walking %s; got %v", we.path, we.err)
}

func setCtxPool(s *siegfried.Siegfried, w writer, wg *sync.WaitGroup, h hash.Hash, z bool) {
func setCtxPool(s *siegfried.Siegfried, w writer, wg *sync.WaitGroup, h hashTyp, z bool) {
ctxPool = &sync.Pool{
New: func() interface{} {
return &context{
s: s,
w: w,
wg: wg,
h: h,
h: makeHash(h),
z: z,
res: make(chan results, 1),
}
Expand Down Expand Up @@ -302,8 +302,8 @@ func main() {
return
}
// handle -hash error
checksum := getHash(*hashf)
if *hashf != "" && checksum == nil {
hashT := getHash(*hashf)
if *hashf != "" && hashT < 0 {
log.Fatalf("[FATAL] invalid hash type; choose from %s", hashChoices)
}
// load and handle signature errors
Expand Down Expand Up @@ -373,7 +373,7 @@ func main() {
// setup default waitgroup
wg := &sync.WaitGroup{}
// setup context pool
setCtxPool(s, w, wg, checksum, *archive)
setCtxPool(s, w, wg, hashT, *archive)
// handle -serve
if *serve != "" {
log.Printf("Starting server at %s. Use CTRL-C to quit.\n", *serve)
Expand All @@ -386,7 +386,7 @@ func main() {
log.Fatalln("[FATAL] expecting a single file or directory argument")
}

w.writeHead(s, *hashf)
w.writeHead(s, hashT)
// support reading list files from stdin
if flag.Arg(0) == "-" {
scanner := bufio.NewScanner(os.Stdin)
Expand Down
24 changes: 12 additions & 12 deletions cmd/sf/writers.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ import (
// i.Known()

type writer interface {
writeHead(s *siegfried.Siegfried, hh string)
writeHead(s *siegfried.Siegfried, ht hashTyp)
// if a directory give a negative sz
writeFile(name string, sz int64, mod string, checksum []byte, err error, ids []core.Identification)
writeTail()
}

type logWriter struct{}

func (l logWriter) writeHead(s *siegfried.Siegfried, hh string) {}
func (l logWriter) writeHead(s *siegfried.Siegfried, ht hashTyp) {}
func (l logWriter) writeFile(name string, sz int64, mod string, cs []byte, err error, ids []core.Identification) {
}
func (l logWriter) writeTail() {}
Expand All @@ -55,11 +55,11 @@ func newCSV(w io.Writer) *csvWriter {
return &csvWriter{w: csv.NewWriter(w)}
}

func (c *csvWriter) writeHead(s *siegfried.Siegfried, hh string) {
func (c *csvWriter) writeHead(s *siegfried.Siegfried, ht hashTyp) {
fields := s.Fields()
c.names = make([]string, len(fields))
l := 4
if *hashf != "" {
if ht >= 0 {
l++
}
for i, f := range fields {
Expand All @@ -70,8 +70,8 @@ func (c *csvWriter) writeHead(s *siegfried.Siegfried, hh string) {
c.recs[0] = make([]string, l)
c.recs[0][0], c.recs[0][1], c.recs[0][2], c.recs[0][3] = "filename", "filesize", "modified", "errors"
idx := 4
if *hashf != "" {
c.recs[0][4] = hashHeader(false, hh)
if ht >= 0 {
c.recs[0][4] = ht.header(false)
idx++
}
for _, f := range fields {
Expand Down Expand Up @@ -140,8 +140,8 @@ func newYAML(w io.Writer) *yamlWriter {
return &yamlWriter{strings.NewReplacer("'", "''"), bufio.NewWriter(w), ""}
}

func (y *yamlWriter) writeHead(s *siegfried.Siegfried, hh string) {
y.hh = hashHeader(true, hh)
func (y *yamlWriter) writeHead(s *siegfried.Siegfried, ht hashTyp) {
y.hh = ht.header(true)
y.w.WriteString(s.YAML())
}

Expand Down Expand Up @@ -174,8 +174,8 @@ func newJSON(w io.Writer) *jsonWriter {
return &jsonWriter{false, strings.NewReplacer(`"`, `\"`, `\\`, `\\`, `\`, `\\`), bufio.NewWriter(w), ""}
}

func (j *jsonWriter) writeHead(s *siegfried.Siegfried, hh string) {
j.hh = hashHeader(false, hh)
func (j *jsonWriter) writeHead(s *siegfried.Siegfried, ht hashTyp) {
j.hh = ht.header(false)
j.w.WriteString(s.JSON())
j.w.WriteString("\"files\":[")
}
Expand Down Expand Up @@ -234,11 +234,11 @@ func newDroid(w io.Writer) *droidWriter {
}

// "identifier", "id", "format name", "format version", "mimetype", "basis", "warning"
func (d *droidWriter) writeHead(s *siegfried.Siegfried, hh string) {
func (d *droidWriter) writeHead(s *siegfried.Siegfried, ht hashTyp) {
d.w.Write([]string{
"ID", "PARENT_ID", "URI", "FILE_PATH", "NAME",
"METHOD", "STATUS", "SIZE", "TYPE", "EXT",
"LAST_MODIFIED", "EXTENSION_MISMATCH", strings.ToUpper(hashHeader(false, hh)) + "_HASH", "FORMAT_COUNT",
"LAST_MODIFIED", "EXTENSION_MISMATCH", strings.ToUpper(ht.header(false)) + "_HASH", "FORMAT_COUNT",
"PUID", "MIME_TYPE", "FORMAT_NAME", "FORMAT_VERSION"})
}

Expand Down

0 comments on commit dfe294f

Please sign in to comment.