Skip to content

Commit

Permalink
makesync: add checksum option, change mb -> kb, store cli version
Browse files Browse the repository at this point in the history
  • Loading branch information
bdon committed Jan 3, 2024
1 parent 65e46f9 commit 87ba271
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
9 changes: 5 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -193,7 +194,7 @@ func main() {
logger.Fatalf("Failed to verify archive, %v", err)
}
case "makesync <input>":
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)
}
Expand Down
22 changes: 20 additions & 2 deletions pmtiles/makesync.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"context"
"crypto/md5"
"fmt"
"github.com/dustin/go-humanize"
"github.com/schollz/progressbar/v3"
Expand All @@ -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
Expand Down Expand Up @@ -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)))
Expand Down

0 comments on commit 87ba271

Please sign in to comment.