]> Sergey Matveev's repositories - syncer.git/blobdiff - syncer.go
Raise copyright years
[syncer.git] / syncer.go
index 3d96ccd8e543fcebd3db5c1059a0da302e2c5a71..9cd1610e9895b5b317e77471f4bcd884064109e5 100644 (file)
--- a/syncer.go
+++ b/syncer.go
@@ -1,11 +1,10 @@
 /*
 syncer -- stateful file/device data syncer.
-Copyright (C) 2015 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2015-2023 Sergey Matveev <stargrave@stargrave.org>
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation, either version 3 of the License, or
-(at your option) any later version.
+the Free Software Foundation, version 3 of the License.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -29,13 +28,15 @@ import (
        "os"
        "runtime"
 
-       "github.com/dchest/blake2b"
+       "lukechampine.com/blake3"
 )
 
+const HashSize = 256 / 8
+
 var (
        blkSize   = flag.Int64("blk", 2*1<<10, "Block size (KiB)")
        statePath = flag.String("state", "state.bin", "Path to statefile")
-       dstPath   = flag.String("dst", "/dev/ada0", "Path to destination disk")
+       dstPath   = flag.String("dst", "", "Path to destination disk")
        srcPath   = flag.String("src", "/dev/da0", "Path to source disk")
 )
 
@@ -45,15 +46,14 @@ type SyncEvent struct {
        data []byte
 }
 
-func prn(s string) {
-       os.Stdout.Write([]byte(s))
-       os.Stdout.Sync()
-}
-
 func main() {
        flag.Parse()
        bs := *blkSize * int64(1<<10)
 
+       if *dstPath == "" {
+               log.Fatalln("Not destination is specified")
+       }
+
        // Open source, calculate number of blocks
        var size int64
        src, err := os.Open(*srcPath)
@@ -88,7 +88,7 @@ func main() {
        defer dst.Close()
 
        // Check if we already have statefile and read the state
-       state := make([]byte, blake2b.Size*blocks)
+       state := make([]byte, HashSize*blocks)
        var i int64
        var tmp []byte
        if _, err := os.Stat(*statePath); err == nil {
@@ -151,7 +151,7 @@ func main() {
        syncs := make(chan chan SyncEvent, workers)
 
        // Writer
-       prn("[")
+       prn := NewPrinter(blocks)
        finished := make(chan struct{})
        go func() {
                var event SyncEvent
@@ -180,14 +180,14 @@ func main() {
                sync := make(chan SyncEvent)
                syncs <- sync
                go func(i int64) {
-                       sum := blake2b.Sum512(buf[:n])
-                       sumState := state[i*blake2b.Size : i*blake2b.Size+blake2b.Size]
+                       sum := blake3.Sum256(buf[:n])
+                       sumState := state[i*HashSize : i*HashSize+HashSize]
                        if bytes.Compare(sumState, sum[:]) != 0 {
                                sync <- SyncEvent{i, buf, buf[:n]}
-                               prn("%")
+                               prn.Changed()
                        } else {
                                sync <- SyncEvent{i, buf, nil}
-                               prn(".")
+                               prn.Unchanged()
                        }
                        copy(sumState, sum[:])
                        close(sync)
@@ -195,7 +195,7 @@ func main() {
        }
        close(syncs)
        <-finished
-       prn("]\n")
+       prn.Output()
 
        log.Println("Saving state")
        stateFile.Write(state)