| 1 | package sender |
| 2 | |
| 3 | import ( |
| 4 | "encoding/binary" |
| 5 | "fmt" |
| 6 | "io" |
| 7 | "os" |
| 8 | "sort" |
| 9 | |
| 10 | "github.com/gokrazy/rsync" |
| 11 | "github.com/gokrazy/rsync/internal/rsyncchecksum" |
| 12 | "github.com/gokrazy/rsync/internal/rsynccommon" |
| 13 | "github.com/gokrazy/rsync/internal/rsyncopts" |
| 14 | "github.com/mmcloughlin/md4" |
| 15 | "golang.org/x/sync/errgroup" |
| 16 | ) |
| 17 | |
| 18 | // rsync/sender.c:send_files() |
| 19 | func (st *Transfer) SendFiles(fileList *fileList) error { |
| 20 | phase := 0 |
| 21 | for { |
| 22 | // receive data about receiver’s copy of the file list contents (not |
| 23 | // ordered) |
| 24 | // see (*rsync.Receiver).Generator() |
| 25 | fileIndex, err := st.Conn.ReadInt32() |
| 26 | if err != nil { |
| 27 | return err |
| 28 | } |
| 29 | if fileIndex == -1 { |
| 30 | if phase == 0 { |
| 31 | phase++ |
| 32 | // acknowledge phase change by sending -1 |
| 33 | if err := st.Conn.WriteInt32(-1); err != nil { |
| 34 | return err |
| 35 | } |
| 36 | continue |
| 37 | } |
| 38 | break |
| 39 | } |
| 40 | |
| 41 | if st.Opts.DryRun() { |
| 42 | if err := st.Conn.WriteInt32(fileIndex); err != nil { |
| 43 | return err |
| 44 | } |
| 45 | continue |
| 46 | } |
| 47 | |
| 48 | fl := fileList.Files[fileIndex] |
| 49 | st.Progress.Reset(uint64(fl.Length)) |
| 50 | |
| 51 | head, err := st.receiveSums() |
| 52 | if err != nil { |
| 53 | return err |
| 54 | } |
| 55 | |
| 56 | // The following quotes are citations from |
| 57 | // https://www.samba.org/~tridge/phd_thesis.pdf, section 3.2.6 The |
| 58 | // signature search algorithm (PDF page 64). |
| 59 | |
| 60 | // rsync/match.c:build_hash_table |
| 61 | targets := make([]target, len(head.Sums)) |
| 62 | tagTable := make(map[uint16]int) // TODO: or int32 more specifically? |
| 63 | { |
| 64 | // “The first step in the algorithm is to sort the received |
| 65 | // signatures by a 16 bit hash of the fast signature.” |
| 66 | for idx, sum := range head.Sums { |
| 67 | targets[idx] = target{ |
| 68 | index: int32(idx), |
| 69 | tag: rsyncchecksum.Tag(sum.Sum1), |
| 70 | } |
| 71 | } |
| 72 | sort.Slice(targets, func(i, j int) bool { |
| 73 | return targets[i].tag < targets[j].tag |
| 74 | }) |
| 75 | |
| 76 | // “A 16 bit index table is then formed which takes a 16 bit hash |
| 77 | // value and gives an index into the sorted signature table which |
| 78 | // points to the first entry in the table which has a matching |
| 79 | // hash.” |
| 80 | for idx := len(head.Sums) - 1; idx >= 0; idx-- { |
| 81 | tagTable[targets[idx].tag] = idx |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | st.lastMatch = 0 |
| 86 | if len(head.Sums) == 0 { |
| 87 | // fast path: send the whole file |
| 88 | err = st.sendFile(fileIndex, fl) |
| 89 | } else { |
| 90 | err = st.hashSearch(targets, tagTable, head, fileIndex, fl) |
| 91 | } |
| 92 | if err != nil { |
| 93 | if _, ok := err.(*os.PathError); ok { |
| 94 | // OpenFile() failed. Log the error (server side only) and |
| 95 | // proceed. Only starting with protocol 30, an I/O error flag is |
| 96 | // sent after the file transfer phase. |
| 97 | if os.IsNotExist(err) { |
| 98 | st.Logger.Printf("file has vanished: %s", fl.path) |
| 99 | } else { |
| 100 | st.Logger.Printf("sendFiles: %v", err) |
| 101 | } |
| 102 | continue |
| 103 | } else { |
| 104 | return err |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // phase done |
| 110 | if err := st.Conn.WriteInt32(-1); err != nil { |
| 111 | return err |
| 112 | } |
| 113 | |
| 114 | return nil |
| 115 | } |
| 116 | |
| 117 | // rsync/sender.c:receive_sums() |
| 118 | func (st *Transfer) receiveSums() (rsync.SumHead, error) { |
| 119 | var head rsync.SumHead |
| 120 | if err := head.ReadFrom(st.Conn); err != nil { |
| 121 | return head, err |
| 122 | } |
| 123 | var offset int64 |
| 124 | head.Sums = make([]rsync.SumBuf, int(head.ChecksumCount)) |
| 125 | for i := int32(0); i < head.ChecksumCount; i++ { |
| 126 | shortChecksum, err := st.Conn.ReadInt32() |
| 127 | if err != nil { |
| 128 | return head, err |
| 129 | } |
| 130 | sb := rsync.SumBuf{ |
| 131 | Index: i, |
| 132 | Offset: offset, |
| 133 | Sum1: uint32(shortChecksum), |
| 134 | } |
| 135 | if i == head.ChecksumCount-1 && head.RemainderLength != 0 { |
| 136 | sb.Len = int64(head.RemainderLength) |
| 137 | } else { |
| 138 | sb.Len = int64(head.BlockLength) |
| 139 | } |
| 140 | offset += sb.Len |
| 141 | n, err := io.ReadFull(st.Conn.Reader, sb.Sum2[:head.ChecksumLength]) |
| 142 | if err != nil { |
| 143 | return head, err |
| 144 | } |
| 145 | _ = n |
| 146 | // st.logger.Printf("chunk[%d] len=%d offset=%.0f sum1=%08x, sum2=%x", |
| 147 | // i, sb.len, float64(sb.offset), sb.sum1, sb.sum2[:n]) |
| 148 | head.Sums[i] = sb |
| 149 | } |
| 150 | return head, nil |
| 151 | } |
| 152 | |
| 153 | func (st *Transfer) sendFile(fileIndex int32, fl file) error { |
| 154 | // rsync/rsync.h defines chunkSize as 32 * 1024, but increasing it to 256K |
| 155 | // increases throughput with “tridge” rsync as client by 50 Mbit/s. |
| 156 | const chunkSize = 256 * 1024 |
| 157 | |
| 158 | f, err := fl.source.Open(fl.path) |
| 159 | if err != nil { |
| 160 | return err |
| 161 | } |
| 162 | defer f.Close() |
| 163 | |
| 164 | fi, err := f.Stat() |
| 165 | if err != nil { |
| 166 | return err |
| 167 | } |
| 168 | |
| 169 | if err := st.Conn.WriteInt32(fileIndex); err != nil { |
| 170 | return err |
| 171 | } |
| 172 | |
| 173 | sh := rsynccommon.SumSizesSqroot(fi.Size()) |
| 174 | if err := sh.WriteTo(st.Conn); err != nil { |
| 175 | return err |
| 176 | } |
| 177 | |
| 178 | if !st.Opts.Server() && |
| 179 | st.Opts.InfoGTE(rsyncopts.INFO_NAME, 1) && |
| 180 | st.Opts.InfoGTE(rsyncopts.INFO_PROGRESS, 1) { |
| 181 | fmt.Fprintln(st.Env.Stdout, fl.path) |
| 182 | } |
| 183 | |
| 184 | h := md4.New() |
| 185 | binary.Write(h, binary.LittleEndian, st.Seed) |
| 186 | |
| 187 | // Calculate the md4 hash in a goroutine. |
| 188 | // |
| 189 | // This allows an rsync connection to benefit from more than 1 core! |
| 190 | // |
| 191 | // We calculate the hash by opening the same file again and reading |
| 192 | // independently. This keeps the hot loop below focused on shoveling data |
| 193 | // into the network socket as quickly as possible. |
| 194 | var eg errgroup.Group |
| 195 | eg.Go(func() error { |
| 196 | f, err := fl.source.Open(fl.path) |
| 197 | if err != nil { |
| 198 | return err |
| 199 | } |
| 200 | defer f.Close() |
| 201 | var buf [chunkSize]byte |
| 202 | if _, err := io.CopyBuffer(h, f, buf[:]); err != nil { |
| 203 | return err |
| 204 | } |
| 205 | return nil |
| 206 | }) |
| 207 | |
| 208 | offset := 0 |
| 209 | buf := make([]byte, chunkSize) |
| 210 | for { |
| 211 | if st.Opts.InfoGTE(rsyncopts.INFO_PROGRESS, 1) { |
| 212 | st.Progress.MaybeShow(uint64(offset), false) |
| 213 | } |
| 214 | n, err := f.Read(buf) |
| 215 | if err != nil { |
| 216 | if err == io.EOF { |
| 217 | break |
| 218 | } |
| 219 | return err |
| 220 | } |
| 221 | chunk := buf[:n] |
| 222 | // chunk size (“rawtok” variable in openrsync) |
| 223 | if err := st.Conn.WriteInt32(int32(len(chunk))); err != nil { |
| 224 | return err |
| 225 | } |
| 226 | n, err = st.Conn.Writer.Write(chunk) |
| 227 | if err != nil { |
| 228 | return err |
| 229 | } |
| 230 | offset += n |
| 231 | } |
| 232 | if st.Opts.InfoGTE(rsyncopts.INFO_PROGRESS, 1) { |
| 233 | st.Progress.Show(uint64(offset), true) |
| 234 | } |
| 235 | // transfer finished: |
| 236 | if err := st.Conn.WriteInt32(0); err != nil { |
| 237 | return err |
| 238 | } |
| 239 | |
| 240 | // whole file long checksum (16 bytes) |
| 241 | if err := eg.Wait(); err != nil { |
| 242 | return err |
| 243 | } |
| 244 | sum := h.Sum(nil) |
| 245 | // st.logger.Printf("sum: %x (len = %d)", sum, len(sum)) |
| 246 | if _, err := st.Conn.Writer.Write(sum); err != nil { |
| 247 | return err |
| 248 | } |
| 249 | return nil |
| 250 | } |