| 1 | package sender |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "sort" |
| 6 | |
| 7 | "github.com/gokrazy/rsync/internal/rsyncopts" |
| 8 | "github.com/gokrazy/rsync/internal/rsyncstats" |
| 9 | "github.com/gokrazy/rsync/internal/rsyncwire" |
| 10 | ) |
| 11 | |
| 12 | // rsync/main.c:handle_stats |
| 13 | func (st *Transfer) handleStats(crd *rsyncwire.CountingReader, cwr *rsyncwire.CountingWriter, fileList *fileList) error { |
| 14 | if !st.Opts.Server() || !st.Opts.Sender() { |
| 15 | return nil |
| 16 | } |
| 17 | |
| 18 | // send statistics: |
| 19 | // total bytes read (from network connection) |
| 20 | if err := st.Conn.WriteInt64(crd.BytesRead); err != nil { |
| 21 | return err |
| 22 | } |
| 23 | // total bytes written (to network connection) |
| 24 | if err := st.Conn.WriteInt64(cwr.BytesWritten); err != nil { |
| 25 | return err |
| 26 | } |
| 27 | // total size of files |
| 28 | if err := st.Conn.WriteInt64(fileList.TotalSize); err != nil { |
| 29 | return err |
| 30 | } |
| 31 | return nil |
| 32 | } |
| 33 | |
| 34 | // rsync/main.c:client_run am_sender |
| 35 | func (st *Transfer) Do(crd *rsyncwire.CountingReader, cwr *rsyncwire.CountingWriter, modPath string, paths []string, exclusionList *filterRuleList) (*rsyncstats.TransferStats, error) { |
| 36 | if exclusionList == nil { |
| 37 | exclusionList = &filterRuleList{} |
| 38 | } |
| 39 | |
| 40 | // “Update exchange” as per |
| 41 | // https://github.com/kristapsdz/openrsync/blob/master/rsync.5 |
| 42 | |
| 43 | // send file list |
| 44 | st.Logger.Printf("SendFileList(modPath=%q, paths=%q)", modPath, paths) |
| 45 | fileList, err := st.SendFileList(modPath, paths, exclusionList) |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | defer fileList.Close() |
| 50 | |
| 51 | if st.Opts.DebugGTE(rsyncopts.DEBUG_FLIST, 3) { |
| 52 | st.Logger.Printf("file list sent") |
| 53 | } |
| 54 | |
| 55 | // Sort the file list. The client sorts, so we need to sort, too (in the |
| 56 | // same way!), otherwise our indices do not match what the client will |
| 57 | // request. |
| 58 | sort.Slice(fileList.Files, func(i, j int) bool { |
| 59 | return fileList.Files[i].Wpath < fileList.Files[j].Wpath |
| 60 | }) |
| 61 | |
| 62 | if err := st.SendFiles(fileList); err != nil { |
| 63 | return nil, err |
| 64 | } |
| 65 | |
| 66 | if err := st.handleStats(crd, cwr, fileList); err != nil { |
| 67 | return nil, err |
| 68 | } |
| 69 | |
| 70 | if st.Opts.DebugGTE(rsyncopts.DEBUG_PROTO, 1) { |
| 71 | st.Logger.Printf("reading final int32") |
| 72 | } |
| 73 | |
| 74 | finish, err := st.Conn.ReadInt32() |
| 75 | if err != nil { |
| 76 | return nil, err |
| 77 | } |
| 78 | if finish != -1 { |
| 79 | return nil, fmt.Errorf("protocol error: expected final -1, got %d", finish) |
| 80 | } |
| 81 | |
| 82 | return &rsyncstats.TransferStats{ |
| 83 | Read: crd.BytesRead, |
| 84 | Written: cwr.BytesWritten, |
| 85 | Size: fileList.TotalSize, |
| 86 | }, nil |
| 87 | } |