From 87ba271f296df6a5a3925501ea7f26b1e8b1f9b6 Mon Sep 17 00:00:00 2001 From: Brandon Liu Date: Wed, 3 Jan 2024 15:25:56 +0800 Subject: [PATCH] makesync: add checksum option, change mb -> kb, store cli version --- main.go | 9 +++++---- pmtiles/makesync.go | 22 ++++++++++++++++++++-- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 936a47f..d15c2ed 100644 --- a/main.go +++ b/main.go @@ -62,9 +62,10 @@ var cli struct { } `cmd:"" help:"Verify the correctness of an archive structure, without verifying individual tile contents."` Makesync struct { - Input string `arg:"" type:"existingfile"` - BlockSizeMegabytes int `default:1 help:"The approximate block size, in megabytes. 0 means 1 tile = 1 block."` - HashFunction string `default:fnv1a help:"The hash function."` + Input string `arg:"" type:"existingfile"` + BlockSizeKb int `default:1000 help:"The approximate block size, in kilobytes. 0 means 1 tile = 1 block."` + HashFunction string `default:fnv1a help:"The hash function."` + Checksum string `help:"Store a checksum in the syncfile."` } `cmd:"" hidden:""` Sync struct { @@ -193,7 +194,7 @@ func main() { logger.Fatalf("Failed to verify archive, %v", err) } case "makesync ": - err := pmtiles.Makesync(logger, cli.Makesync.Input, cli.Makesync.BlockSizeMegabytes) + err := pmtiles.Makesync(logger, version, cli.Makesync.Input, cli.Makesync.BlockSizeKb, cli.Makesync.Checksum) if err != nil { logger.Fatalf("Failed to makesync archive, %v", err) } diff --git a/pmtiles/makesync.go b/pmtiles/makesync.go index 4c09dd1..b0753df 100644 --- a/pmtiles/makesync.go +++ b/pmtiles/makesync.go @@ -4,6 +4,7 @@ import ( "bufio" "bytes" "context" + "crypto/md5" "fmt" "github.com/dustin/go-humanize" "github.com/schollz/progressbar/v3" @@ -30,12 +31,12 @@ type Result struct { Hash uint64 } -func Makesync(logger *log.Logger, file string, block_size_megabytes int) error { +func Makesync(logger *log.Logger, cli_version string, file string, block_size_kb int, checksum string) error { ctx := context.Background() start := time.Now() bucketURL, key, err := NormalizeBucketKey("", "", file) - block_size_bytes := uint64(1024 * 1024 * block_size_megabytes) + block_size_bytes := uint64(1000 * block_size_kb) if err != nil { return err @@ -93,6 +94,23 @@ func Makesync(logger *log.Logger, file string, block_size_megabytes int) error { panic(err) } defer output.Close() + output.Write([]byte(fmt.Sprintf("version=%s\n", cli_version))) + + if checksum == "md5" { + localfile, err := os.Open(file) + if err != nil { + panic(err) + } + defer localfile.Close() + reader := bufio.NewReaderSize(localfile, 64*1024*1024) + md5hasher := md5.New() + if _, err := io.Copy(md5hasher, reader); err != nil { + panic(err) + } + md5checksum := md5hasher.Sum(nil) + fmt.Printf("Completed md5 in %v.\n", time.Since(start)) + output.Write([]byte(fmt.Sprintf("md5=%x\n", md5checksum))) + } output.Write([]byte("hash=fnv1a\n")) output.Write([]byte(fmt.Sprintf("blocksize=%d\n", block_size_bytes)))