Annotation of src/usr.bin/rsync/rsync.5, Revision 1.3
1.3 ! benno 1: .\" $OpenBSD: rsync.5,v 1.2 2019/02/10 23:24:14 benno Exp $
1.1 benno 2: .\"
3: .\" Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv>
4: .\"
5: .\" Permission to use, copy, modify, and distribute this software for any
6: .\" purpose with or without fee is hereby granted, provided that the above
7: .\" copyright notice and this permission notice appear in all copies.
8: .\"
9: .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16: .\"
1.3 ! benno 17: .Dd $Mdocdate: February 10 2019 $
1.1 benno 18: .Dt RSYNC 5
19: .Os
20: .Sh NAME
21: .Nm rsync
22: .Nd rsync wire protocol
23: .Sh DESCRIPTION
24: The
25: .Nm
26: protocol described in this relates to the BSD-licensed
27: .Xr openrsync 1 ,
28: a re-implementation of the GPL-licensed reference utility
29: .Xr rsync 1 .
30: It is compatible with version 27 of the reference.
31: .Pp
32: In this document, the
33: .Qq client process
34: refers to the utility as run on the operator's local computer.
35: The
36: .Qq server process
37: is run either on the local or remote computer, depending upon the
38: command-line given file locations.
39: .Pp
40: There are a number of options in the protocol that are dictated by command-line
41: flags.
42: These will be noted as
43: .Fl n
44: for dry-run,
1.3 ! benno 45: .Fl g
! 46: for group ids,
1.1 benno 47: .Fl l
48: for links,
49: .Fl r
50: for recursion,
51: .Fl v
52: for verbose, and
53: .Fl -delete
54: for deletion (before).
55: .Ss Data types
56: The binary protocol encodes all data in little-endian format.
57: Integers are signed 32-bit, shorts are signed 16-bit, bytes are unsigned
58: 8-bit.
59: A long is variable-length.
60: For values less than the maximum integer, the value is transmitted and
61: read as a 32-bit integer.
62: For values greater, the value is transmitted first as a maximum integer,
63: then a 64-bit signed integer.
64: .Pp
65: There are three types of checksums: long (slow), short (fast), and
66: whole-file.
67: The fast checksum is a derivative of Adler-32.
68: The slow checksum is MD4,
69: made over the checksum seed first (serialised in little-endian format),
70: then the data.
71: The whole-file applies MD4 to the file first, then the checksum seed at
72: the end (also serialised in little-endian format).
73: .Ss Multiplexing
74: Most
75: .Nm
76: transmissions are wrapped in a multiplexing envelope protocol.
77: It is composed as follows:
78: .Pp
79: .Bl -enum -compact
80: .It
81: envelope header (4 bytes)
82: .It
83: envelope payload (arbitrary length)
84: .El
85: .Pp
86: The first byte of the envelope header consists of a tag.
87: If the tag is 7, the payload is normal data.
88: Otherwise, the payload is out-of-band server messages.
89: If the tag is 1, it is an error on the sender's part and must trigger an
90: exit.
91: This limits message payloads to 24 bit integer size,
92: .Li 0x0fffffff .
93: .Pp
94: The only data not using this envelope are the initial handshake between
95: client and server.
96: .Ss File list
97: A central part of the protocol is the file list, which is generated by
98: the sender.
99: It consists of all files that must be sent to the receiver, either
100: explicitly as given or recursively generated.
101: .Pp
102: The file list itself consists of filenames and attributes (mode, time,
103: size, etc.).
104: Filenames must be relative to the destination root and not be absolute
105: or contain backtracking.
106: So if a file is given to the sender as
107: .Pa ../../foo/bar ,
108: it must be sent as
109: .Pa foo/bar .
110: .Pp
111: The file list should be cleaned of inappropriate files prior to sending.
112: For example, if
113: .Fl l
114: is not specified, symbolic links may be omitted.
115: Directory entries without
116: .Fl r
117: may also be omitted.
118: Duplicates may be omitted.
119: .Pp
120: The receiver
121: .Em must not
122: assume that the file list is clean.
123: It should not omit inappropriate files from the file list (which would
124: affect the indexing), but may omit them during processing.
125: .Pp
126: Prior to be sent from sender to receiver, and upon being received, the
127: file list must be lexicographically sorted such as with
128: .Xr strcmp 3 .
129: Subsequent references to the file are by index in the sorted list.
130: .Ss Client process
131: The client can operate in sender or receiver mode depending upon the
132: command-line source and destination.
133: .Pp
134: If the destination directory (sink) is remote, the client is in sender
135: mode: the client will push its data to the server.
136: If the source file is remote, it is in receiver mode: the server pushes
137: to the client.
138: If neither are remote, the client operates in sender mode.
139: These are all mutually exclusive.
140: .Pp
141: When the client starts, regardless its mode, it first handshakes the
142: server.
143: This exchange is
144: .Em not
145: multiplexed.
146: .Pp
147: .Bl -enum -compact
148: .It
149: send local version (integer)
150: .It
151: receive remote version (integer)
152: .It
153: receive random seed (integer)
154: .El
155: .Pp
156: Following this, the client multiplexes when reading from the server.
157: Transmissions sent from client to server are not multiplexed.
158: It then enters the
159: .Sx Update exchange
160: protocol.
161: .Ss Server process
162: The server can operate in sender or receiver mode depending upon how the
163: client starts the server.
164: This may be directly from the parent process (when invoked for local
165: files) or indirectly via a remote shell.
166: .Pp
167: When in sender mode, the server pushes data to the client.
168: (This is equivalent to receiver mode for the client.)
169: In receiver, the opposite is true.
170: .Pp
171: When the server starts, regardless the mode, it first handshakes the
172: client.
173: This exchange is
174: .Em not
175: multiplexed.
176: .Pp
177: .Bl -enum -compact
178: .It
179: send local version (integer)
180: .It
181: receive remote version (integer)
182: .It
183: send random seed (integer)
184: .El
185: .Pp
186: Following this, the server multiplexes when writing to the client.
187: (Transmissions received from the client are not multiplexed.)
188: It then enters the
189: .Sx Update exchange
190: protocol.
191: .Ss Update exchange
192: When the client or server is in sender mode, it begins by conditionally
193: sending the exclusion list.
194: At this time, this is always empty.
195: .Pp
196: .Bl -enum -compact
197: .It
198: if
199: .Fl -delete
200: and the client, exclusion list zero (integer)
201: .El
202: .Pp
203: It then sends the
204: .Sx File list .
205: Prior to being sent, the file list should be lexicographically sorted.
206: .Pp
207: .Bl -enum -compact
208: .It
209: status byte (integer)
210: .It
211: inherited filename length (optional, byte)
212: .It
213: filename length (integer or byte)
214: .It
215: file (byte array)
216: .It
217: file length (long)
218: .It
219: file modification time (optional, time_t, integer)
220: .It
221: file mode (optional, mode_t, integer)
222: .It
1.3 ! benno 223: if
! 224: .Fl g ,
! 225: the group id (integer)
! 226: .It
1.1 benno 227: if a symbolic link and
228: .Fl l ,
229: the link target's length (integer)
230: .It
231: if a symbolic link and
232: .Fl l ,
233: the link target (byte array)
234: .El
235: .Pp
236: The status byte may consist of the following bits and determines which
237: of the optional fields are transmitted.
238: .Pp
239: .Bl -tag -compact -width Ds
240: .It 0x02
241: Do not send the file mode: it is a repeat of the last file's mode.
1.3 ! benno 242: .It 0x10
! 243: Like
! 244: .Li 0x02 ,
! 245: but for the group id.
1.1 benno 246: .It 0x20
247: Inherit some of the prior file name.
248: Enables the inherited filename length transmission.
249: .It 0x40
250: Use full integer length for file name.
251: Otherwise, use only the byte length.
252: .It 0x80
253: Do not send the file modification time: it is a repeat of the last
254: file's.
255: .El
256: .Pp
257: If the status byte is zero, the file-list has terminated.
258: The sender then sends any IO error values, which for
259: .Xr openrsync 1
260: is always zero.
261: .Pp
262: .Bl -enum -compact
263: .It
264: constant zero (integer)
265: .El
266: .Pp
267: The server sender then reads the exclusion list, which is always zero.
268: .Pp
269: .Bl -enum -compact
270: .It
271: if server, constant zero (integer)
272: .El
273: .Pp
274: Following that, the sender receives data regarding the receiver's copy
275: of the file list contents.
276: This data is not ordered in any way.
277: Each of these requests starts as follows:
278: .Pp
279: .Bl -enum -compact
280: .It
281: file index or -1 to signal a change of phase (integer)
282: .El
283: .Pp
284: The phase starts in phase 1, then proceeds to phase 2, and phase 3
285: signals an end of transmission (no subsequent blocks).
286: If a phase change occurs, the sender must write back the -1 constant
287: integer value and increment its phase state.
288: .Pp
289: Blocks are read as follows:
290: .Pp
291: .Bl -enum -compact
292: .It
293: block index (integer)
294: .El
295: .Pp
296: In
297: .Pq Fl n
298: mode, the sender may immediately write back the index (integer) to skip
299: the following.
300: .Pp
301: .Bl -enum -compact
302: .It
303: number of blocks (integer)
304: .It
305: block length in the file (integer)
306: .It
307: long checksum length (integer)
308: .It
309: terminal (remainder) block length (integer)
310: .El
311: .Pp
312: And for each block:
313: .Pp
314: .Bl -enum -compact
315: .It
316: short checksum (integer)
317: .It
318: long checksum (bytes of checksum length)
319: .El
320: .Pp
321: The client then compares the two files, block by block, and updates the
322: server with mismatches as follows.
323: .Pp
324: .Bl -enum -compact
325: .It
326: file index (integer)
327: .It
328: number of blocks (integer)
329: .It
330: block length (integer)
331: .It
332: long checksum length (integer)
333: .It
334: remainder block length (integer)
335: .El
336: .Pp
337: Then for each block:
338: .Pp
339: .Bl -enum -compact
340: .It
341: data chunk size (integer)
342: .It
343: data chunk (bytes)
344: .It
345: block index subsequent to chunk or zero for finished (integer)
346: .El
347: .Pp
348: Following this sequence, the sender sends the followng:
349: .Pp
350: .Bl -enum -compact
351: .It
352: whole-file long checksum (16 bytes)
353: .El
354: .Pp
355: The sender then either handles the next queued file or, if the receiver
356: has written a phase change, the phase change step.
357: .Pp
358: If the sender is the server and
359: .Fl v
360: has been specified, the sender must send statistics.
361: .Pp
362: .Bl -enum -compact
363: .It
364: total bytes read (long)
365: .It
366: total bytes written (long)
367: .It
368: total size of files (long)
369: .El
370: .Pp
371: Finally, the sender must read a final constant-value integer.
372: .Pp
373: .Bl -enum -compact
374: .It
375: end-of-sequence -1 value (integer)
376: .El
377: .Pp
378: If in receiver mode, the inverse above (write instead of read, read
379: instead of write) is performed.
380: .Pp
381: The receiver begins by conditionally writing, then reading, the
382: exclusion list count, which is always zero.
383: .Pp
384: .Bl -enum -compact
385: .It
386: if client, send zero (integer)
387: .It
388: if receiver and
389: .Fl -delete ,
390: read zero (integer)
391: .El
392: .Pp
393: The receiver then proceeds with reading the
394: .Sx File list
395: as already
396: defined.
397: Following the list, the receiver reads the IO error, which must be zero.
398: .Pp
399: .Bl -enum -compact
400: .It
401: constant zero (integer)
402: .El
403: .Pp
404: The receiver must then sort the file names lexicographically.
405: .Pp
406: If there are no files in the file list at this time, the receiver must
407: exit prior to sending per-file data.
408: It then proceeds with the file blocks.
409: .Pp
410: For file blocks, the receiver must look at each file that is not up to
411: date, defined by having the same file size and timestamp, and send it to
412: the server.
413: Symbolic links and directory entries are never sent to the server.
414: .Pp
415: After the second phase has completed and prior to writing the
416: end-of-data signal, the client receiver reads statistics.
417: This is only performed with
418: .Pq Fl v .
419: .Pp
420: .Bl -enum -compact
421: .It
422: total bytes read (long)
423: .It
424: total bytes written (long)
425: .It
426: total size of files (long)
427: .El
428: .Pp
429: Finally, the receiver must send the constant end-of-sequence marker.
430: .Pp
431: .Bl -enum -compact
432: .It
433: end-of-sequence -1 value (integer)
434: .El
435: .Ss Sender and receiver asynchrony
436: The sender and receiver need not work in lockstep.
437: The receiver may send file update requests as quickly as it parses them,
438: and respond to the sender's update notices on demand.
439: Similarly, the sender may read as many update requests as it can, and
440: service them in any order it wishes.
441: .Pp
442: The sender and receiver synchronise state only at the end of phase.
443: .Pp
444: The reference
445: .Xr rsync 1
446: takes advantage of this with a two-process receiver, one for sending
447: update requests (the generator) and another for receiving.
448: .Xr openrsync 1
449: uses an event-loop model instead.
450: .\" .Sh CONTEXT
451: .\" For section 9 functions only.
452: .\" .Sh RETURN VALUES
453: .\" For sections 2, 3, and 9 function return values only.
454: .\" .Sh ENVIRONMENT
455: .\" For sections 1, 6, 7, and 8 only.
456: .\" .Sh FILES
457: .\" .Sh EXIT STATUS
458: .\" For sections 1, 6, and 8 only.
459: .\" .Sh EXAMPLES
460: .\" .Sh DIAGNOSTICS
461: .\" For sections 1, 4, 6, 7, 8, and 9 printf/stderr messages only.
462: .\" .Sh ERRORS
463: .\" For sections 2, 3, 4, and 9 errno settings only.
464: .Sh SEE ALSO
465: .Xr openrsync 1 ,
466: .Xr rsync 1 ,
467: .Xr rsyncd 5
468: .\" .Sh STANDARDS
469: .\" .Sh HISTORY
470: .\" .Sh AUTHORS
471: .\" .Sh CAVEATS
472: .Sh BUGS
473: Time values are sent as 32-bit integers.
474: .Pp
475: When in server mode
476: .Em and
477: when communicating to a client with a newer protocol (>27), the phase
478: change integer (-1) acknowledgement must be sent twice by the sender.
479: The is probably a bug in the reference implementation.