| 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 rsyncclient |
| 6 | |
| 7 | import ( |
| 8 | "context" |
| 9 | "fmt" |
| 10 | "io" |
| 11 | "os" |
| 12 | |
| 13 | "github.com/gokrazy/rsync/internal/maincmd" |
| 14 | "github.com/gokrazy/rsync/internal/rsyncopts" |
| 15 | "github.com/gokrazy/rsync/internal/rsyncos" |
| 16 | "github.com/gokrazy/rsync/internal/rsyncstats" |
| 17 | ) |
| 18 | |
| 19 | // Option specifies the client options. |
| 20 | type Option interface { |
| 21 | applyServer(*Client) |
| 22 | } |
| 23 | |
| 24 | type clientOptionFunc func(server *Client) |
| 25 | |
| 26 | func (f clientOptionFunc) applyServer(s *Client) { |
| 27 | f(s) |
| 28 | } |
| 29 | |
| 30 | // WithStderr makes the [Client] write to the specified stderr instead of |
| 31 | // [os.Stderr]. |
| 32 | func WithStderr(stderr io.WriteCloser) Option { |
| 33 | return clientOptionFunc(func(c *Client) { |
| 34 | c.osenv.Stderr = stderr |
| 35 | }) |
| 36 | } |
| 37 | |
| 38 | // WithSender enables sender mode (receiver by default). |
| 39 | func WithSender() Option { |
| 40 | return clientOptionFunc(func(c *Client) { |
| 41 | c.sender = true |
| 42 | }) |
| 43 | } |
| 44 | |
| 45 | // WithoutNegotiate disables protocol version negotiation (enabled by default). |
| 46 | func WithoutNegotiate() Option { |
| 47 | return clientOptionFunc(func(c *Client) { |
| 48 | c.negotiate = false |
| 49 | }) |
| 50 | } |
| 51 | |
| 52 | func DontRestrict() Option { |
| 53 | return clientOptionFunc(func(c *Client) { |
| 54 | c.osenv.DontRestrict = true |
| 55 | }) |
| 56 | } |
| 57 | |
| 58 | type Client struct { |
| 59 | osenv *rsyncos.Env |
| 60 | opts *rsyncopts.Options |
| 61 | negotiate bool |
| 62 | 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 New(args []string, opts ...Option) (*Client, error) { |
| 68 | c := &Client{ |
| 69 | osenv: &rsyncos.Env{ |
| 70 | Stdout: os.Stdout, |
| 71 | Stderr: os.Stderr, |
| 72 | }, |
| 73 | negotiate: true, |
| 74 | } |
| 75 | |
| 76 | for _, opt := range opts { |
| 77 | opt.applyServer(c) |
| 78 | } |
| 79 | |
| 80 | pc := rsyncopts.NewContext(rsyncopts.NewOptionsWithGokrazyDefaults(c.osenv)) |
| 81 | if err := pc.ParseArguments(c.osenv, args); err != nil { |
| 82 | return nil, err |
| 83 | } |
| 84 | c.opts = pc.Options |
| 85 | if len(pc.RemainingArgs) > 0 { |
| 86 | return nil, fmt.Errorf("remaining args %q not permitted; specify them in Client.Run()", pc.RemainingArgs) |
| 87 | } |
| 88 | if c.sender { |
| 89 | c.opts.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 *Client) ServerCommandOptions(path string, paths ...string) []string { |
| 98 | return c.opts.CommandOptions(path, paths...) |
| 99 | } |
| 100 | |
| 101 | // Result contains information about a transfer. |
| 102 | type Result struct { |
| 103 | Stats *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 *Client) Run(ctx context.Context, conn io.ReadWriteCloser, paths []string) (*Result, error) { |
| 125 | stats, err := maincmd.ClientRun(c.osenv, c.opts, conn, paths, c.negotiate) |
| 126 | if err != nil { |
| 127 | return nil, err |
| 128 | } |
| 129 | return &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 *Client) RunDaemon(ctx context.Context, conn io.ReadWriteCloser, remotePath string, paths []string) (*Result, error) { |
| 140 | done, err := maincmd.StartInbandExchange(c.osenv, c.opts, conn, remotePath) |
| 141 | if err != nil { |
| 142 | return nil, err |
| 143 | } |
| 144 | if done { // Server sent EXIT |
| 145 | return &Result{Stats: &rsyncstats.TransferStats{}}, nil |
| 146 | } |
| 147 | c.negotiate = false // done as part of the inband exchange |
| 148 | return c.Run(ctx, conn, paths) |
| 149 | } |