| 1 | // Package rsyncclient implements an rsync client (only), but note that |
| 2 | // gokrazy/rsync contains a native Go rsync implementation that supports sending |
| 3 | // and receiving files as client or server, compatible with the original tridge |
| 4 | // rsync (from the samba project) or openrsync (used on OpenBSD and macOS 15+). |
| 5 | package rsyncclientscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/ |
| 6 | |
| 7 | import ( |
| 8 | "contextscip-go gomod github.com/golang/go/src go1.25.0 context/" |
| 9 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 10 | "ioscip-go gomod github.com/golang/go/src go1.25.0 io/" |
| 11 | "osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 12 | |
| 13 | "github.com/gokrazy/rsync/internal/maincmdscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/maincmd`/" |
| 14 | "github.com/gokrazy/rsync/internal/rsyncoptsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/" |
| 15 | "github.com/gokrazy/rsync/internal/rsyncosscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/" |
| 16 | "github.com/gokrazy/rsync/internal/rsyncstatsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncstats`/" |
| 17 | ) |
| 18 | |
| 19 | // Option specifies the client options. |
| 20 | type Optionscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Option# interface { |
| 21 | applyServerscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Option#applyServer.(*Clientscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#) |
| 22 | } |
| 23 | |
| 24 | type clientOptionFuncscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/clientOptionFunc# func(server *Clientscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#) |
| 25 | |
| 26 | func (f clientOptionFuncscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/clientOptionFunc#) applyServerscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/clientOptionFunc#applyServer().(s *Clientscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#) { |
| 27 | f(s) |
| 28 | } |
| 29 | |
| 30 | // WithStderr makes the [Client] write to the specified stderr instead of |
| 31 | // [os.Stderr]. |
| 32 | func WithStderrscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/WithStderr().(stderr ioscip-go gomod github.com/golang/go/src go1.25.0 io/.WriteCloserscip-go gomod github.com/golang/go/src go1.25.0 io/WriteCloser#) Optionscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Option# { |
| 33 | return clientOptionFuncscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/clientOptionFunc#(func(c *Clientscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#) { |
| 34 | c.osenvscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#osenv..Stderrscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#Stderr. = stderr |
| 35 | }) |
| 36 | } |
| 37 | |
| 38 | // WithSender enables sender mode (receiver by default). |
| 39 | func WithSenderscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/WithSender().() Optionscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Option# { |
| 40 | return clientOptionFuncscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/clientOptionFunc#(func(c *Clientscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#) { |
| 41 | c.senderscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#sender. = true |
| 42 | }) |
| 43 | } |
| 44 | |
| 45 | // WithoutNegotiate disables protocol version negotiation (enabled by default). |
| 46 | func WithoutNegotiatescip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/WithoutNegotiate().() Optionscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Option# { |
| 47 | return clientOptionFuncscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/clientOptionFunc#(func(c *Clientscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#) { |
| 48 | c.negotiatescip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#negotiate. = false |
| 49 | }) |
| 50 | } |
| 51 | |
| 52 | func DontRestrictscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/DontRestrict().() Optionscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Option# { |
| 53 | return clientOptionFuncscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/clientOptionFunc#(func(c *Clientscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#) { |
| 54 | c.osenvscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#osenv..DontRestrictscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#DontRestrict. = true |
| 55 | }) |
| 56 | } |
| 57 | |
| 58 | type Clientscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client# struct { |
| 59 | osenvscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#osenv. *rsyncosscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/.Envscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env# |
| 60 | optsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#opts. *rsyncoptsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/.Optionsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/Options# |
| 61 | negotiatescip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#negotiate. bool |
| 62 | senderscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#sender. bool |
| 63 | } |
| 64 | |
| 65 | // New creates a new [Client]. You can call [Client.Run] one or more times with |
| 66 | // the same [Client]. |
| 67 | func Newscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/New().(args []string, opts ...Optionscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Option#) (*Clientscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#, error) { |
| 68 | c := &Clientscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#{ |
| 69 | osenvscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#osenv.: &rsyncosscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/.Envscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#{ |
| 70 | Stdoutscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#Stdout.: osscip-go gomod github.com/golang/go/src go1.25.0 os/.Stdoutscip-go gomod github.com/golang/go/src go1.25.0 os/Stdout., |
| 71 | Stderrscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#Stderr.: osscip-go gomod github.com/golang/go/src go1.25.0 os/.Stderrscip-go gomod github.com/golang/go/src go1.25.0 os/Stderr., |
| 72 | }, |
| 73 | negotiatescip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#negotiate.: true, |
| 74 | } |
| 75 | |
| 76 | for _, opt := range opts { |
| 77 | opt.applyServerscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Option#applyServer.(c) |
| 78 | } |
| 79 | |
| 80 | pc := rsyncoptsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/.NewContextscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/NewContext().(rsyncoptsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/.NewOptionsWithGokrazyDefaultsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/NewOptionsWithGokrazyDefaults().(c.osenvscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#osenv.)) |
| 81 | if err := pc.ParseArgumentsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/Context#ParseArguments().(c.osenvscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#osenv., args); err != nil { |
| 82 | return nil, err |
| 83 | } |
| 84 | c.optsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#opts. = pc.Optionsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/Context#Options. |
| 85 | if len(pc.RemainingArgsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/Context#RemainingArgs.) > 0 { |
| 86 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("remaining args %q not permitted; specify them in Client.Run()", pc.RemainingArgsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/Context#RemainingArgs.) |
| 87 | } |
| 88 | if c.senderscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#sender. { |
| 89 | c.optsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#opts..SetSenderscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/Options#SetSender().() |
| 90 | } |
| 91 | |
| 92 | return c, nil |
| 93 | } |
| 94 | |
| 95 | // ServerCommandOptions returns the options that rsync would use to spawn the |
| 96 | // server process. |
| 97 | func (c *Clientscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#) ServerCommandOptionsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#ServerCommandOptions().(path string, paths ...string) []string { |
| 98 | return c.optsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#opts..CommandOptionsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/Options#CommandOptions().(path, paths...) |
| 99 | } |
| 100 | |
| 101 | // Result contains information about a transfer. |
| 102 | type Resultscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Result# struct { |
| 103 | Statsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Result#Stats. *rsyncstatsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncstats`/.TransferStatsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncstats`/TransferStats# |
| 104 | } |
| 105 | |
| 106 | // Run starts one run of the rsync protocol (not the rsync daemon protocol), see |
| 107 | // also https://michael.stapelberg.ch/posts/2022-07-02-rsync-how-does-it-work/. |
| 108 | // |
| 109 | // If you just want to transfer some data from an already running rsync server |
| 110 | // or remote system, use the [github.com/gokrazy/rsync/rsynccmd] package |
| 111 | // instead, which will take care of starting rsync as a subprocess (locally or |
| 112 | // remotely), or of connecting to an rsync daemon via TCP. |
| 113 | // |
| 114 | // The Run method operates on any kind of connection (using the |
| 115 | // [io.ReadWriteCloser] interface) and is meant to be used when you need more |
| 116 | // control over the setup. For example, maybe you want to set up some custom |
| 117 | // tunneling to an rsync process running deep in some remote cloud |
| 118 | // infrastructure. |
| 119 | // |
| 120 | // Or maybe you want to connect an rsync client and server to each other via a |
| 121 | // custom RPC protocol. In that case, you will need to transport the |
| 122 | // [Client.ServerCommandOptions] to the server and then arrange for two |
| 123 | // [io.ReadWriter] connections between client and server. |
| 124 | func (c *Clientscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#) Runscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#Run().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, conn ioscip-go gomod github.com/golang/go/src go1.25.0 io/.ReadWriteCloserscip-go gomod github.com/golang/go/src go1.25.0 io/ReadWriteCloser#, paths []string) (*Resultscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Result#, error) { |
| 125 | stats, err := maincmdscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/maincmd`/.ClientRunscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/maincmd`/ClientRun().(c.osenvscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#osenv., c.optsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#opts., conn, paths, c.negotiatescip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#negotiate.) |
| 126 | if err != nil { |
| 127 | return nil, err |
| 128 | } |
| 129 | return &Resultscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Result#{Statsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Result#Stats.: stats}, nil |
| 130 | } |
| 131 | |
| 132 | // RunDaemon starts one run of the rsync daemon protocol, meaning it performs |
| 133 | // the daemon protocol inband exchange (to negotiate the protocol version and |
| 134 | // select an rsync module) and then calls [Client.Run]. |
| 135 | // |
| 136 | // This method is useful when you want to connect to an rsync daemon, but |
| 137 | // establish the connection yourself, e.g. via the [golang.org/x/crypto/ssh] |
| 138 | // package. |
| 139 | func (c *Clientscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#) RunDaemonscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#RunDaemon().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, conn ioscip-go gomod github.com/golang/go/src go1.25.0 io/.ReadWriteCloserscip-go gomod github.com/golang/go/src go1.25.0 io/ReadWriteCloser#, remotePath string, paths []string) (*Resultscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Result#, error) { |
| 140 | done, err := maincmdscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/maincmd`/.StartInbandExchangescip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/maincmd`/StartInbandExchange().(c.osenvscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#osenv., c.optsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#opts., conn, remotePath) |
| 141 | if err != nil { |
| 142 | return nil, err |
| 143 | } |
| 144 | if done { // Server sent EXIT |
| 145 | return &Resultscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Result#{Statsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Result#Stats.: &rsyncstatsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncstats`/.TransferStatsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncstats`/TransferStats#{}}, nil |
| 146 | } |
| 147 | c.negotiatescip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#negotiate. = false // done as part of the inband exchange |
| 148 | return c.Runscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncclient`/Client#Run().(ctx, conn, paths) |
| 149 | } |