2 syncer -- stateful file/device data syncer.
3 Copyright (C) 2015 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, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 // Stateful file/device data syncer.
32 "github.com/dchest/blake2b"
36 blkSize = flag.Int64("blk", 2*1<<10, "Block size (KiB)")
37 statePath = flag.String("state", "state.bin", "Path to statefile")
38 dstPath = flag.String("dst", "", "Path to destination disk")
39 srcPath = flag.String("src", "/dev/da0", "Path to source disk")
42 type SyncEvent struct {
49 os.Stdout.Write([]byte(s))
55 bs := *blkSize * int64(1<<10)
58 log.Fatalln("Not destination is specified")
61 // Open source, calculate number of blocks
63 src, err := os.Open(*srcPath)
65 log.Fatalln("Unable to open src:", err)
70 log.Fatalln("Unable to read src stat:", err)
72 if fi.Mode()&os.ModeDevice == os.ModeDevice {
73 size, err = src.Seek(0, 2)
75 log.Fatalln("Unable to seek src:", err)
85 log.Println(blocks, bs, "byte blocks")
88 dst, err := os.OpenFile(*dstPath, os.O_WRONLY|os.O_CREATE, 0600)
90 log.Fatalln("Unable to open dst:", err)
94 // Check if we already have statefile and read the state
95 state := make([]byte, blake2b.Size*blocks)
98 if _, err := os.Stat(*statePath); err == nil {
99 log.Println("State file found")
100 stateFile, err := os.Open(*statePath)
102 log.Fatalln("Unable to read statefile:", err)
105 // Check previously used size and block size
106 tmp = make([]byte, 8)
107 n, err := stateFile.Read(tmp)
108 if err != nil || n != 8 {
109 log.Fatalln("Invalid statefile")
111 prevSize := int64(binary.BigEndian.Uint64(tmp))
112 if size != prevSize {
114 "Size differs with state file:",
115 prevSize, "instead of", size,
118 tmp = make([]byte, 8)
119 n, err = stateFile.Read(tmp)
120 if err != nil || n != 8 {
121 log.Fatalln("Invalid statefile")
123 prevBs := int64(binary.BigEndian.Uint64(tmp))
126 "Blocksize differs with state file:",
127 prevBs, "instead of", bs,
131 n, err = stateFile.Read(state)
132 if err != nil || n != len(state) {
133 log.Fatalln("Corrupted statefile")
137 stateFile, err := ioutil.TempFile(".", "syncer")
139 log.Fatalln("Unable to create temporary file:", err)
141 tmp = make([]byte, 8)
142 binary.BigEndian.PutUint64(tmp, uint64(size))
144 tmp = make([]byte, 8)
145 binary.BigEndian.PutUint64(tmp, uint64(bs))
148 // Create buffers and event channel
149 workers := runtime.NumCPU()
150 log.Println(workers, "workers")
151 bufs := make(chan []byte, workers)
152 for i := 0; i < workers; i++ {
153 bufs <- make([]byte, int(bs))
155 syncs := make(chan chan SyncEvent, workers)
159 finished := make(chan struct{})
162 for sync := range syncs {
164 if event.data != nil {
165 dst.Seek(event.i*bs, 0)
166 dst.Write(event.data)
175 for i = 0; i < blocks; i++ {
177 n, err := src.Read(buf)
180 log.Fatalln("Error during src read:", err)
184 sync := make(chan SyncEvent)
187 sum := blake2b.Sum512(buf[:n])
188 sumState := state[i*blake2b.Size : i*blake2b.Size+blake2b.Size]
189 if bytes.Compare(sumState, sum[:]) != 0 {
190 sync <- SyncEvent{i, buf, buf[:n]}
193 sync <- SyncEvent{i, buf, nil}
196 copy(sumState, sum[:])
204 log.Println("Saving state")
205 stateFile.Write(state)
207 if err = os.Rename(stateFile.Name(), *statePath); err != nil {
209 "Unable to overwrite statefile:", err,
210 "saved state is in:", stateFile.Name(),