Browse Source

initial commit

master
n1trux 4 years ago
parent
commit
74ca89a2e5
  1. 85
      sneakerhash.go

85
sneakerhash.go

@ -0,0 +1,85 @@
package main
import (
"database/sql"
//"github.com/kalafut/imohash"
_ "github.com/mattn/go-sqlite3"
"log"
"io"
"fmt"
"os"
"crypto/sha1"
"time"
"path/filepath"
"strconv"
"encoding/hex"
)
func hash_directory (searchDir, database string) {
db, dberr := sql.Open("sqlite3", database)
if dberr != nil {
log.Fatal(dberr)
}
defer db.Close()
err := filepath.Walk(searchDir,
func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Fatal(err)
return err
}
if !info.IsDir() {
fmt.Printf("Name: %s\n", info.Name())
f, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
defer f.Close()
h := sha1.New()
if _, err := io.Copy(h, f); err != nil {
log.Fatal(err)
}
hashSum := hex.EncodeToString(h.Sum(nil))
fmt.Printf("SHA1: %x\n", hashSum)
fmt.Printf("Size: %d\n", info.Size())
fmt.Printf("Time: %s\n\n", info.ModTime().Format(time.RFC3339))
sqlStatement := "INSERT INTO hashes (hash_sha1, filename, filesize_bytes, path, changedate )"+
"VALUES ('"+hashSum+"','"+info.Name()+"','"+strconv.FormatInt(info.Size(),10)+"','"+path+"','"+info.ModTime().Format(time.RFC3339)+"');"
_, err = db.Exec(sqlStatement)
if err != nil {
log.Printf("%q: %s\n", err, sqlStatement)
return nil
}
}
return nil
})
if err != nil {
log.Fatal(err)
}
}
func main() {
dbname := "./sneakerhash.db"
os.Remove(dbname)
db, err := sql.Open("sqlite3", dbname)
if err != nil {
log.Fatal(err)
}
defer db.Close()
sqlStatement := `
create table hashes (
hash_sha1 text not null primary key,
filename text,
filesize_bytes integer,
path text,
changedate datetime
);`
_, err = db.Exec(sqlStatement)
if err != nil {
log.Printf("%q: %s\n", err, sqlStatement)
return
}
hash_directory(".", dbname)
}
Loading…
Cancel
Save