2 syncer -- stateful file/device data syncer.
3 Copyright (C) 2015-2020 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 // Stateful file/device data syncer.
31 "github.com/dchest/blake2b"
35 blkSize = flag.Int64("blk", 2*1<<10, "Block size (KiB)")
36 statePath = flag.String("state", "state.bin", "Path to statefile")
37 dstPath = flag.String("dst", "", "Path to destination disk")
38 srcPath = flag.String("src", "/dev/da0", "Path to source disk")
41 type SyncEvent struct {
49 bs := *blkSize * int64(1<<10)
52 log.Fatalln("Not destination is specified")
55 // Open source, calculate number of blocks
57 src, err := os.Open(*srcPath)
59 log.Fatalln("Unable to open src:", err)
64 log.Fatalln("Unable to read src stat:", err)
66 if fi.Mode()&os.ModeDevice == os.ModeDevice {
67 size, err = src.Seek(0, 2)
69 log.Fatalln("Unable to seek src:", err)
79 log.Println(blocks, bs, "byte blocks")
82 dst, err := os.OpenFile(*dstPath, os.O_WRONLY|os.O_CREATE, 0600)
84 log.Fatalln("Unable to open dst:", err)
88 // Check if we already have statefile and read the state
89 state := make([]byte, blake2b.Size*blocks)
92 if _, err := os.Stat(*statePath); err == nil {
93 log.Println("State file found")
94 stateFile, err := os.Open(*statePath)
96 log.Fatalln("Unable to read statefile:", err)
99 // Check previously used size and block size
100 tmp = make([]byte, 8)
101 n, err := stateFile.Read(tmp)
102 if err != nil || n != 8 {
103 log.Fatalln("Invalid statefile")
105 prevSize := int64(binary.BigEndian.Uint64(tmp))
106 if size != prevSize {
108 "Size differs with state file:",
109 prevSize, "instead of", size,
112 tmp = make([]byte, 8)
113 n, err = stateFile.Read(tmp)
114 if err != nil || n != 8 {
115 log.Fatalln("Invalid statefile")
117 prevBs := int64(binary.BigEndian.Uint64(tmp))
120 "Blocksize differs with state file:",
121 prevBs, "instead of", bs,
125 n, err = stateFile.Read(state)
126 if err != nil || n != len(state) {
127 log.Fatalln("Corrupted statefile")
131 stateFile, err := ioutil.TempFile(".", "syncer")
133 log.Fatalln("Unable to create temporary file:", err)
135 tmp = make([]byte, 8)
136 binary.BigEndian.PutUint64(tmp, uint64(size))
138 tmp = make([]byte, 8)
139 binary.BigEndian.PutUint64(tmp, uint64(bs))
142 // Create buffers and event channel
143 workers := runtime.NumCPU()
144 log.Println(workers, "workers")
145 bufs := make(chan []byte, workers)
146 for i := 0; i < workers; i++ {
147 bufs <- make([]byte, int(bs))
149 syncs := make(chan chan SyncEvent, workers)
152 prn := NewPrinter(blocks)
153 finished := make(chan struct{})
156 for sync := range syncs {
158 if event.data != nil {
159 dst.Seek(event.i*bs, 0)
160 dst.Write(event.data)
169 for i = 0; i < blocks; i++ {
171 n, err := src.Read(buf)
174 log.Fatalln("Error during src read:", err)
178 sync := make(chan SyncEvent)
181 sum := blake2b.Sum512(buf[:n])
182 sumState := state[i*blake2b.Size : i*blake2b.Size+blake2b.Size]
183 if bytes.Compare(sumState, sum[:]) != 0 {
184 sync <- SyncEvent{i, buf, buf[:n]}
187 sync <- SyncEvent{i, buf, nil}
190 copy(sumState, sum[:])
198 log.Println("Saving state")
199 stateFile.Write(state)
201 if err = os.Rename(stateFile.Name(), *statePath); err != nil {
203 "Unable to overwrite statefile:", err,
204 "saved state is in:", stateFile.Name(),