| 1 | package receiver |
| 2 | |
| 3 | import ( |
| 4 | "io/fs" |
| 5 | "os" |
| 6 | "sync" |
| 7 | |
| 8 | "github.com/gokrazy/rsync/internal/rsyncopts" |
| 9 | "github.com/gokrazy/rsync/internal/rsyncstats" |
| 10 | "github.com/gokrazy/rsync/internal/rsyncwire" |
| 11 | "golang.org/x/sync/errgroup" |
| 12 | ) |
| 13 | |
| 14 | func isTopDir(f *File) bool { |
| 15 | // TODO: once we check the f.Flags: |
| 16 | // if !f.FileMode().IsDir() { |
| 17 | // // non-directories can get the top_dir flag set, |
| 18 | // // but it must be ignored (only for protocol reasons). |
| 19 | // return false |
| 20 | // } |
| 21 | // return (f.Flags & TOP_DIR) != 0 |
| 22 | return f.Name == "." |
| 23 | } |
| 24 | |
| 25 | func (rt *Transfer) deleteFiles(fileList []*File) error { |
| 26 | if rt.IOErrors > 0 { |
| 27 | rt.Logger.Printf("IO error encountered, skipping file deletion") |
| 28 | return nil |
| 29 | } |
| 30 | |
| 31 | for _, f := range fileList { |
| 32 | if !isTopDir(f) { |
| 33 | continue |
| 34 | } |
| 35 | rt.Logger.Printf("deleting in %s", f.Name) |
| 36 | // Other rsync implementations generate a local file list and compare it |
| 37 | // with the remote file list, we re-implement the path→name mapping part |
| 38 | // of file list generation here. We could change it for consistency. |
| 39 | err := fs.WalkDir(rt.DestRoot.FS(), ".", func(path string, info fs.DirEntry, err error) error { |
| 40 | if err != nil { |
| 41 | return err |
| 42 | } |
| 43 | rt.Logger.Printf("WalkDir(%q)", path) |
| 44 | if findInFileList(fileList, path) { |
| 45 | return nil |
| 46 | } |
| 47 | if rt.Opts.Verbose { |
| 48 | rt.Logger.Printf(" deleting %s", path) |
| 49 | } |
| 50 | if rt.Opts.DryRun { |
| 51 | return nil |
| 52 | } |
| 53 | if err := rt.DestRoot.RemoveAll(path); err != nil { |
| 54 | rt.Logger.Printf(" deleting %s failed: %v", path, err) |
| 55 | // keep going |
| 56 | } |
| 57 | return fs.SkipDir // skip the just-deleted directory |
| 58 | }) |
| 59 | if err != nil { |
| 60 | if os.IsNotExist(err) { |
| 61 | return nil // destination does not exist, nothing to do |
| 62 | } |
| 63 | return err |
| 64 | } |
| 65 | } |
| 66 | return nil |
| 67 | } |
| 68 | |
| 69 | // rsync/main.c:do_recv |
| 70 | func (rt *Transfer) Do(c *rsyncwire.Conn, fileList []*File, noReport bool) (*rsyncstats.TransferStats, error) { |
| 71 | if rt.Opts.DeleteMode { |
| 72 | if err := rt.deleteFiles(fileList); err != nil { |
| 73 | return nil, err |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | var eg errgroup.Group |
| 78 | // Wrap both, the generator and the receiver goroutine, in waitFor() calls |
| 79 | // to ensure we don’t block on the generator when the receiver returns an |
| 80 | // error, or vice versa (instead, return and let the goroutine finish in the |
| 81 | // background). |
| 82 | // waitFor calls f and waits for it to complete, but only until the specified |
| 83 | // context is cancelled. |
| 84 | var closeOnce sync.Once |
| 85 | closeOnErr := func(err error) error { |
| 86 | if err != nil { |
| 87 | closeOnce.Do(func() { c.Close() }) |
| 88 | } |
| 89 | return err |
| 90 | } |
| 91 | eg.Go(func() error { return closeOnErr(rt.GenerateFiles(fileList)) }) |
| 92 | eg.Go(func() error { return closeOnErr(rt.RecvFiles(fileList)) }) |
| 93 | if err := eg.Wait(); err != nil { |
| 94 | return nil, err |
| 95 | } |
| 96 | if rt.retouchDirPerms /* || rt.retouchDirTimes */ { |
| 97 | if err := rt.touchUpDirs(fileList); err != nil { |
| 98 | return nil, err |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | var stats *rsyncstats.TransferStats |
| 103 | if !noReport { |
| 104 | var err error |
| 105 | stats, err = rt.report(c) |
| 106 | if err != nil { |
| 107 | return nil, err |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // send final goodbye message |
| 112 | if err := c.WriteInt32(-1); err != nil { |
| 113 | return nil, err |
| 114 | } |
| 115 | |
| 116 | return stats, nil |
| 117 | } |
| 118 | |
| 119 | // rsync/main.c:report |
| 120 | func (rt *Transfer) report(c *rsyncwire.Conn) (*rsyncstats.TransferStats, error) { |
| 121 | // read statistics: |
| 122 | // total bytes read (from network connection) |
| 123 | read, err := c.ReadInt64() |
| 124 | if err != nil { |
| 125 | return nil, err |
| 126 | } |
| 127 | // total bytes written (to network connection) |
| 128 | written, err := c.ReadInt64() |
| 129 | if err != nil { |
| 130 | return nil, err |
| 131 | } |
| 132 | // total size of files |
| 133 | size, err := c.ReadInt64() |
| 134 | if err != nil { |
| 135 | return nil, err |
| 136 | } |
| 137 | if rt.Opts.InfoGTE(rsyncopts.INFO_STATS, 1) { |
| 138 | rt.Logger.Printf("server sent stats: read=%d, written=%d, size=%d", read, written, size) |
| 139 | } |
| 140 | |
| 141 | return &rsyncstats.TransferStats{ |
| 142 | Read: read, |
| 143 | Written: written, |
| 144 | Size: size, |
| 145 | }, nil |
| 146 | } |