Annotation of src/usr.bin/rsync/extern.h, Revision 1.4
1.4 ! deraadt 1: /* $Id: extern.h,v 1.3 2019/02/12 14:07:00 deraadt 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: */
17: #ifndef EXTERN_H
18: #define EXTERN_H
19:
20: /*
21: * This is the rsync protocol version that we support.
22: */
23: #define RSYNC_PROTOCOL (27)
24:
25: /*
26: * Maximum amount of file data sent over the wire at once.
27: */
28: #define MAX_CHUNK (32 * 1024)
29:
30: /*
31: * This is the minimum size for a block of data not including those in
32: * the remainder block.
33: */
34: #define BLOCK_SIZE_MIN (700)
35:
36: /*
37: * The sender and receiver use a two-phase synchronisation process.
38: * The first uses two-byte hashes; the second, 16-byte.
39: * (The second must hold a full MD4 digest.)
40: */
41: #define CSUM_LENGTH_PHASE1 (2)
42: #define CSUM_LENGTH_PHASE2 (16)
43:
44: /*
45: * Operating mode for a client or a server.
46: * Sender means we synchronise local files with those from remote.
47: * Receiver is the opposite.
48: * This is relative to which host we're running on.
49: */
50: enum fmode {
51: FARGS_SENDER,
52: FARGS_RECEIVER
53: };
54:
55: /*
56: * File arguments given on the command line.
57: * See struct opts.
58: */
59: struct fargs {
60: char *host; /* hostname or NULL if local */
61: char **sources; /* transfer source */
62: size_t sourcesz; /* number of sources */
63: char *sink; /* transfer endpoint */
64: enum fmode mode; /* mode of operation */
65: int remote; /* uses rsync:// or :: for remote */
66: char *module; /* if rsync://, the module */
67: };
68:
69: /*
70: * The subset of stat(2) information that we need.
71: * (There are some parts we don't use yet.)
72: */
73: struct flstat {
74: mode_t mode; /* mode */
75: uid_t uid; /* user */
76: gid_t gid; /* group */
77: off_t size; /* size */
78: time_t mtime; /* modification */
79: unsigned int flags;
80: #define FLSTAT_TOP_DIR 0x01 /* a top-level directory */
81:
82: };
83:
84: /*
85: * A list of files with their statistics.
86: */
87: struct flist {
88: char *path; /* path relative to root */
89: const char *wpath; /* "working" path for receiver */
90: struct flstat st; /* file information */
91: char *link; /* symlink target or NULL */
92: };
93:
94: /*
95: * Options passed into the command line.
96: * See struct fargs.
97: */
98: struct opts {
1.3 deraadt 99: int sender; /* --sender */
100: int server; /* --server */
101: int recursive; /* -r */
102: int verbose; /* -v */
103: int dry_run; /* -n */
104: int preserve_times; /* -t */
105: int preserve_perms; /* -p */
106: int preserve_links; /* -l */
107: int del; /* --delete */
108: char *rsync_path; /* --rsync-path */
1.4 ! deraadt 109: char *ssh_prog; /* --rsh or -e */
1.1 benno 110: };
111:
112: /*
113: * An individual block description for a file.
114: * See struct blkset.
115: */
116: struct blk {
117: off_t offs; /* offset in file */
118: size_t idx; /* block index */
119: size_t len; /* bytes in block */
120: uint32_t chksum_short; /* fast checksum */
121: unsigned char chksum_long[CSUM_LENGTH_PHASE2]; /* slow checksum */
122: };
123:
124: /*
125: * When transferring file contents, we break the file down into blocks
126: * and work with those.
127: */
128: struct blkset {
129: off_t size; /* file size */
130: size_t rem; /* terminal block length if non-zero */
131: size_t len; /* block length */
132: size_t csum; /* checksum length */
133: struct blk *blks; /* all blocks */
134: size_t blksz; /* number of blks */
135: };
136:
137: /*
138: * Values required during a communication session.
139: */
140: struct sess {
141: const struct opts *opts; /* system options */
142: int32_t seed; /* checksum seed */
143: int32_t lver; /* local version */
144: int32_t rver; /* remote version */
145: uint64_t total_read; /* non-logging wire/reads */
146: uint64_t total_size; /* total file size */
147: uint64_t total_write; /* non-logging wire/writes */
148: int mplex_reads; /* multiplexing reads? */
149: size_t mplex_read_remain; /* remaining bytes */
150: int mplex_writes; /* multiplexing writes? */
151: };
152:
153: struct download;
154: struct upload;
155:
156: #define LOG0(_sess, _fmt, ...) \
157: rsync_log((_sess), __FILE__, __LINE__, -1, (_fmt), ##__VA_ARGS__)
158: #define LOG1(_sess, _fmt, ...) \
159: rsync_log((_sess), __FILE__, __LINE__, 0, (_fmt), ##__VA_ARGS__)
160: #define LOG2(_sess, _fmt, ...) \
161: rsync_log((_sess), __FILE__, __LINE__, 1, (_fmt), ##__VA_ARGS__)
162: #define LOG3(_sess, _fmt, ...) \
163: rsync_log((_sess), __FILE__, __LINE__, 2, (_fmt), ##__VA_ARGS__)
164: #define LOG4(_sess, _fmt, ...) \
165: rsync_log((_sess), __FILE__, __LINE__, 3, (_fmt), ##__VA_ARGS__)
166: #define ERRX1(_sess, _fmt, ...) \
167: rsync_errx1((_sess), __FILE__, __LINE__, (_fmt), ##__VA_ARGS__)
168: #define WARNX(_sess, _fmt, ...) \
169: rsync_warnx((_sess), __FILE__, __LINE__, (_fmt), ##__VA_ARGS__)
170: #define WARN(_sess, _fmt, ...) \
171: rsync_warn((_sess), 0, __FILE__, __LINE__, (_fmt), ##__VA_ARGS__)
172: #define WARN1(_sess, _fmt, ...) \
173: rsync_warn((_sess), 1, __FILE__, __LINE__, (_fmt), ##__VA_ARGS__)
174: #define WARN2(_sess, _fmt, ...) \
175: rsync_warn((_sess), 2, __FILE__, __LINE__, (_fmt), ##__VA_ARGS__)
176: #define ERR(_sess, _fmt, ...) \
177: rsync_err((_sess), __FILE__, __LINE__, (_fmt), ##__VA_ARGS__)
178: #define ERRX(_sess, _fmt, ...) \
179: rsync_errx((_sess), __FILE__, __LINE__, (_fmt), ##__VA_ARGS__)
180:
181: __BEGIN_DECLS
182:
183: void rsync_log(struct sess *,
184: const char *, size_t, int, const char *, ...)
185: __attribute__((format(printf, 5, 6)));
186: void rsync_warnx1(struct sess *,
187: const char *, size_t, const char *, ...)
188: __attribute__((format(printf, 4, 5)));
189: void rsync_warn(struct sess *, int,
190: const char *, size_t, const char *, ...)
191: __attribute__((format(printf, 5, 6)));
192: void rsync_warnx(struct sess *, const char *,
193: size_t, const char *, ...)
194: __attribute__((format(printf, 4, 5)));
195: void rsync_err(struct sess *, const char *,
196: size_t, const char *, ...)
197: __attribute__((format(printf, 4, 5)));
198: void rsync_errx(struct sess *, const char *,
199: size_t, const char *, ...)
200: __attribute__((format(printf, 4, 5)));
201: void rsync_errx1(struct sess *, const char *,
202: size_t, const char *, ...)
203: __attribute__((format(printf, 4, 5)));
204:
1.2 benno 205: int flist_del(struct sess *, int,
1.1 benno 206: const struct flist *, size_t);
207: int flist_gen(struct sess *, size_t, char **,
208: struct flist **, size_t *);
209: int flist_gen_local(struct sess *, const char *,
210: struct flist **, size_t *);
211: void flist_free(struct flist *, size_t);
212: int flist_recv(struct sess *, int,
213: struct flist **, size_t *);
214: int flist_send(struct sess *, int, int,
215: const struct flist *, size_t);
1.2 benno 216: int flist_gen_dels(struct sess *, const char *,
1.1 benno 217: struct flist **, size_t *,
218: const struct flist *, size_t);
219:
220: char **fargs_cmdline(struct sess *, const struct fargs *);
221:
222: int io_read_buf(struct sess *, int, void *, size_t);
223: int io_read_byte(struct sess *, int, uint8_t *);
224: int io_read_check(struct sess *, int);
225: int io_read_flush(struct sess *, int);
226: int io_read_int(struct sess *, int, int32_t *);
227: int io_read_long(struct sess *, int, int64_t *);
228: int io_read_size(struct sess *, int, size_t *);
229: int io_read_ulong(struct sess *, int, uint64_t *);
230: int io_write_buf(struct sess *, int, const void *, size_t);
231: int io_write_byte(struct sess *, int, uint8_t);
232: int io_write_int(struct sess *, int, int32_t);
233: int io_write_line(struct sess *, int, const char *);
234: int io_write_long(struct sess *, int, int64_t);
235:
1.2 benno 236: void io_buffer_int(struct sess *, void *,
1.1 benno 237: size_t *, size_t, int32_t);
1.2 benno 238: void io_buffer_buf(struct sess *, void *,
1.1 benno 239: size_t *, size_t, const void *, size_t);
240:
1.2 benno 241: void io_unbuffer_int(struct sess *, const void *,
1.1 benno 242: size_t *, size_t, int32_t *);
1.2 benno 243: int io_unbuffer_size(struct sess *, const void *,
1.1 benno 244: size_t *, size_t, size_t *);
1.2 benno 245: void io_unbuffer_buf(struct sess *, const void *,
1.1 benno 246: size_t *, size_t, void *, size_t);
247:
248: void rsync_child(const struct opts *, int, const struct fargs *)
249: __attribute__((noreturn));
250: int rsync_receiver(struct sess *, int, int, const char *);
251: int rsync_sender(struct sess *, int, int, size_t, char **);
252: int rsync_client(const struct opts *, int, const struct fargs *);
253: int rsync_socket(const struct opts *, const struct fargs *);
254: int rsync_server(const struct opts *, size_t, char *[]);
255: int rsync_downloader(struct download *, struct sess *, int *);
1.2 benno 256: int rsync_uploader(struct upload *,
1.1 benno 257: int *, struct sess *, int *);
258: int rsync_uploader_tail(struct upload *, struct sess *);
259:
260: struct download *download_alloc(struct sess *, int,
261: const struct flist *, size_t, int);
262: void download_free(struct download *);
1.2 benno 263: struct upload *upload_alloc(struct sess *, int, int, size_t,
1.1 benno 264: const struct flist *, size_t, mode_t);
265: void upload_free(struct upload *);
266:
267: struct blkset *blk_recv(struct sess *, int, const char *);
268: int blk_recv_ack(struct sess *,
269: int, const struct blkset *, int32_t);
270: int blk_match(struct sess *, int,
271: const struct blkset *, const char *);
272: int blk_send(struct sess *, int, size_t,
273: const struct blkset *, const char *);
274: int blk_send_ack(struct sess *, int, struct blkset *);
275: int blk_merge(struct sess *, int, int,
276: const struct blkset *, int, const char *,
277: const void *, size_t, float *);
278: void blkset_free(struct blkset *);
279:
280: uint32_t hash_fast(const void *, size_t);
281: void hash_slow(const void *, size_t,
282: unsigned char *, const struct sess *);
283: void hash_file(const void *, size_t,
284: unsigned char *, const struct sess *);
285:
286: int mkpath(struct sess *, char *);
287:
288: char *symlink_read(struct sess *, const char *);
289: char *symlinkat_read(struct sess *, int, const char *);
290:
291: int sess_stats_send(struct sess *, int);
292: int sess_stats_recv(struct sess *, int);
293:
294: __END_DECLS
295:
296: #endif /*!EXTERN_H*/