forked from kristapsdz/openrsync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.c
251 lines (218 loc) · 6.67 KB
/
session.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* Copyright (c) 2019 Kristaps Dzonsons <[email protected]>
* Copyright (c) 2024, Klara, Inc.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#include <assert.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include "extern.h"
/* To make the summary log output more closely resemble that of
* standard rsync we remove the space from between the number
* and the suffix character, and eliminate any 'B' suffix.
*/
static void
stats_log_fixup(const size_t bufsz, char *buf)
{
char *p;
p = strchr(buf, ' ');
if (p != NULL && p[1] != '\0') {
if (p[1] == 'B') {
p[0] = '\0';
} else {
p[0] = p[1];
p[1] = '\0';
}
}
}
/*
* Accept how much we've read, written, and file-size, and print them in
* a human-readable fashion (with GB, MB, etc. prefixes).
* This only prints as the client.
*/
static void
stats_log(struct sess *sess,
uint64_t tread, uint64_t twrite, uint64_t tsize, uint64_t fbuild,
uint64_t fxfer)
{
if (sess->opts->server)
return;
if (verbose > 0 || sess->opts->stats) {
char rbuf[32], wbuf[32], sbuf[32], ratebuf[32];
int64_t rate;
rate = (tread + twrite) / ((sess->xfer_time + 0.1) / 1000);
rsync_humanize(sess, rbuf, sizeof(rbuf), tread);
rsync_humanize(sess, wbuf, sizeof(wbuf), twrite);
rsync_humanize(sess, sbuf, sizeof(sbuf), tsize);
rsync_humanize(sess, ratebuf, sizeof(ratebuf), rate);
stats_log_fixup(sizeof(rbuf), rbuf);
stats_log_fixup(sizeof(wbuf), wbuf);
stats_log_fixup(sizeof(sbuf), sbuf);
stats_log_fixup(sizeof(ratebuf), ratebuf);
LOG0("\nsent %s bytes received %s bytes %s bytes/sec",
wbuf, rbuf, ratebuf);
LOG0("total size is %s speedup is %.2lf",
sbuf, tsize / (tread + twrite + 0.001));
}
LOG3("File list generation time: %.3f seconds, "
"transfer time: %.3f seconds",
(double)sess->flist_build / 1000,
(double)sess->flist_xfer / 1000);
}
static void
stats_output(struct sess *sess)
{
char *tbuf[32];
LOG0("Number of files: %" PRIu64, sess->total_files);
LOG0("Number of files transferred: %" PRIu64, sess->total_files_xfer);
rsync_humanize(sess, (char*)&tbuf, sizeof(tbuf), sess->total_size);
LOG0("Total file size: %s", (char*)&tbuf);
rsync_humanize(sess, (char*)&tbuf, sizeof(tbuf), sess->total_xfer_size);
LOG0("Total transferred file size: %s", (char*)&tbuf);
rsync_humanize(sess, (char*)&tbuf, sizeof(tbuf), sess->total_unmatched);
LOG0("Unmatched data: %s", (char*)&tbuf);
rsync_humanize(sess, (char*)&tbuf, sizeof(tbuf), sess->total_matched);
LOG0("Matched data: %s", (char*)&tbuf);
rsync_humanize(sess, (char*)&tbuf, sizeof(tbuf), sess->flist_size);
LOG0("File list size: %s", (char*)&tbuf);
if (sess->flist_build) {
LOG0("File list generation time: %.3f seconds",
(double)sess->flist_build / 1000);
LOG0("File list transfer time: %.3f seconds",
(double)sess->flist_xfer / 1000);
}
rsync_humanize(sess, (char*)&tbuf, sizeof(tbuf), sess->total_write);
LOG0("Total sent: %s", (char*)&tbuf);
rsync_humanize(sess, (char*)&tbuf, sizeof(tbuf), sess->total_read);
LOG0("Total received: %s", (char*)&tbuf);
}
/*
* At the end of transmission, we write our statistics if we're the
* server, then log only if we're not the server.
* Either way, only do this if we're in verbose mode.
* Returns zero on failure, non-zero on success.
*/
int
sess_stats_send(struct sess *sess, int fd)
{
uint64_t tw, tr, ts, fb, fx;
int statfd = -1;
tw = sess->total_write;
tr = sess->total_read;
ts = sess->total_size;
fb = sess->flist_build;
fx = sess->flist_xfer;
if (sess->opts->stats)
stats_output(sess);
if (verbose > 0 || sess->opts->stats)
stats_log(sess, tr, tw, ts, fb, fx);
if (!sess->opts->server && sess->wbatch_fd == -1 && verbose == 0)
return 1;
/*
* The client-sender doesn't need to send stats, unless we're writing a
* batch file; they're going to be read by a client-receiver, and will
* be expecting stats. In that case, we'll just write the stats
* directly to the batch file.
*/
if (sess->opts->server || sess->wbatch_fd != -1) {
if (sess->opts->server)
statfd = fd;
else
statfd = sess->wbatch_fd;
}
if (statfd != -1) {
if (!io_write_ulong(sess, statfd, tr)) {
ERRX1("io_write_ulong");
return 0;
} else if (!io_write_ulong(sess, statfd, tw)) {
ERRX1("io_write_ulong");
return 0;
} else if (!io_write_ulong(sess, statfd, ts)) {
ERRX1("io_write_ulong");
return 0;
}
if (protocol_fliststats) {
if (!io_write_ulong(sess, statfd, fb)) {
ERRX1("io_write_ulong");
return 0;
} else if (!io_write_ulong(sess, statfd, fx)) {
ERRX1("io_write_ulong");
return 0;
}
}
}
return 1;
}
/*
* At the end of the transmission, we have some statistics to read.
* Only do this (1) if we're in verbose mode and (2) if we're the
* server.
* Then log the findings.
* Return zero on failure, non-zero on success.
*/
int
sess_stats_recv(struct sess *sess, int fd)
{
uint64_t tr, tw, ts, fb, fx;
if (sess->opts->server)
return 1;
if (!io_read_ulong(sess, fd, &tw)) {
ERRX1("io_read_ulong");
return 0;
} else if (!io_read_ulong(sess, fd, &tr)) {
ERRX1("io_read_ulong");
return 0;
} else if (!io_read_ulong(sess, fd, &ts)) {
ERRX1("io_read_ulong");
return 0;
}
if (protocol_fliststats) {
if (!io_read_ulong(sess, fd, &fb)) {
ERRX1("io_read_ulong");
return 0;
} else if (!io_read_ulong(sess, fd, &fx)) {
ERRX1("io_read_ulong");
return 0;
}
}
if (verbose > 0 || sess->opts->stats)
stats_log(sess, tr, tw, ts, fb, fx);
if (sess->opts->stats)
stats_output(sess);
return 1;
}
/*
* sess_cleanup() should be called before discarding a session
* object to ensure all accumlated resources are released.
*/
void
sess_cleanup(struct sess *sess)
{
free(sess->token_dbuf);
sess->token_dbuf = NULL;
free(sess->token_cbuf);
sess->token_cbuf = NULL;
free(sess->token_buf);
sess->token_buf = NULL;
free(sess->fuzzy_root);
sess->fuzzy_root = NULL;
if (sess->fuzzy_dirp != NULL) {
closedir(sess->fuzzy_dirp);
sess->fuzzy_rootfd = -1;
sess->fuzzy_dirp = NULL;
}
}