diff --git a/cmd/update.go b/cmd/update.go new file mode 100644 index 0000000..ce94cee --- /dev/null +++ b/cmd/update.go @@ -0,0 +1,43 @@ +package cmd + +import ( + "bytes" + "io" + "io/ioutil" + "log" + "os" + "path/filepath" + "strings" + + "lukechampine.com/blake3" +) + +func CreateCheckSumFile(writePath string, filename string, reader io.ReadCloser) { + fileExtBlake3 := addBlakeExtension(filename) + file, _ := os.Create(filepath.Join(writePath, fileExtBlake3)) + var fileBytes []byte + fileBytes, _ = ioutil.ReadAll(reader) + byteHash := blake3.Sum256(fileBytes) + + hashReader := bytes.NewReader(byteHash[:]) + + if _, err := io.Copy(file, hashReader); err != nil { // that side effect I mentioned in the above for loop makes this possible to do out of loop. + log.Fatal(err) + } +} + +func isEqualFiles(writePath string, filename string, reader io.ReadCloser) bool { + fileExtBlake3 := addBlakeExtension(filename) + previousHash, _ := ioutil.ReadFile(filepath.Join(writePath, fileExtBlake3)) + var fileBytes []byte + fileBytes, _ = ioutil.ReadAll(reader) + newHash := blake3.Sum256(fileBytes) + return bytes.Equal(previousHash, newHash[:]) +} + +func addBlakeExtension(fileName string) string { + if pos := strings.LastIndexByte(fileName, '.'); pos != -1 { + return fileName[:pos] + ".blake3" + } + return fileName + ".blake3" +} diff --git a/cmd/update_test.go b/cmd/update_test.go new file mode 100644 index 0000000..0691aa3 --- /dev/null +++ b/cmd/update_test.go @@ -0,0 +1,19 @@ +package cmd + +import ( + "fmt" + "log" + "net/http" +) + +func ExampleIsEqualFiles() { + response, err := http.Get("https://ftp.expasy.org/databases/rhea/rdf/rhea.rdf.gz") + + if err != nil { + log.Fatal(err) + } + + defer response.Body.Close() + fmt.Println(isEqualFiles("../data/build", "rhea.rdf.gz", response.Body)) + //Output: true +} diff --git a/data/build/rhea.rdf.blake3 b/data/build/rhea.rdf.blake3 new file mode 100644 index 0000000..a3057f7 --- /dev/null +++ b/data/build/rhea.rdf.blake3 @@ -0,0 +1,2 @@ +�䊰*Mg�T5p2 +k�-��||">�T�!�ro: \ No newline at end of file diff --git a/go.mod b/go.mod index c646280..1bdc0f8 100644 --- a/go.mod +++ b/go.mod @@ -22,4 +22,5 @@ require ( golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect gopkg.in/ini.v1 v1.62.0 // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect + lukechampine.com/blake3 v1.0.0 // indirect )