Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add hashing #30

Closed
wants to merge 13 commits into from
4 changes: 3 additions & 1 deletion fsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (
const Version = "0.2.0"

var q *query.Query
var attrs = [4]string{"mode", "size", "time", "name"}

//Should be noted that this slice, is temporaly related to the order of printing
var attrs = [5]string{"mode", "size", "time", "hash", "name"}

// output prints the result value for each SELECTed attribute. Order is based
// on the order the attributes appear in attrs.
Expand Down
1 change: 1 addition & 0 deletions parser/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
var allAttributes = map[string]bool{
"mode": true,
"name": true,
"hash": true,
"size": true,
"time": true,
}
Expand Down
8 changes: 8 additions & 0 deletions transform/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,11 @@ func upper(name string) interface{} {
func lower(name string) interface{} {
return strings.ToLower(name)
}

func truncate(str string, n int) string {
if len(str) < n || n > len(str) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking the same thing twice! 😄

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol 🤦‍♂️

return str
}

return str[0:n]
}
43 changes: 43 additions & 0 deletions transform/format.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package transform

import (
"crypto"
_ "crypto/sha1" //Import SHA-1 hashing function
"encoding/hex"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"time"
)
Expand Down Expand Up @@ -31,6 +36,14 @@ func Format(p *FormatParams) (val interface{}, err error) {
val, err = p.fullPath()
case "SHORTPATH":
val, err = p.shortPath()
case "SHA1":
var hash string
hash, err = p.hash(crypto.SHA1)
if err == nil && len(p.Args) > 0 && p.Args[0] != "" {
if n, err := strconv.Atoi(p.Args[0]); err == nil {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want to reinitialize err here.

val = truncate(hash, n)
}
}
}

if err != nil {
Expand Down Expand Up @@ -109,6 +122,30 @@ func (p *FormatParams) shortPath() (interface{}, error) {
return p.Info.Name(), nil
}

func (p *FormatParams) hash(hasher crypto.SignerOpts) (string, error) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's return an interface{} here (instead of string).

return hash(p.Info, p.Path, hasher)
}

func hash(info os.FileInfo, path string, hasher crypto.SignerOpts) (string, error) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interface{} here as well.

if info.IsDir() {
return "----------------------------------------", nil
Copy link
Owner

@kashav kashav Jun 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use strings.Repeat with hasher.HashFunc().Size here, since sizes may vary between hash functions.

}

f, err := os.Open(path)
if err != nil {
return "", err
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can now be changed to return nil (below as well).

}
defer f.Close()
b, err := ioutil.ReadAll(f)
if err != nil {
return "", err
}

h := hasher.HashFunc().New()
h.Write(b)
return hex.EncodeToString(h.Sum(nil)), nil
}

// DefaultFormatValue returns the default format value for the provided
// attribute attr based on path and info.
func DefaultFormatValue(attr, path string, info os.FileInfo) interface{} {
Expand All @@ -121,6 +158,12 @@ func DefaultFormatValue(attr, path string, info os.FileInfo) interface{} {
return info.Size()
case "time":
return info.ModTime().Format(time.Stamp)
case "hash":
v, err := hash(info, path, crypto.SHA1)
if err != nil {
panic(err.Error())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how I feel about panicking, but you can leave this as is for now.

Might need to refactor this method to return an error.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I was thinking as well, I suppose we can let the caller handle the error?

}
return truncate(v, 7)
}
return nil
}