8 * - "#rcopy-repeatedly#\n"
9 * - length of declaration, as 4 hex digits, zero prefixed,
10 * and a newline [5 bytes]. In this protocol version this
11 * will be "0002" but client _must_ parse it.
12 * server sends declaration
13 * - one of "u " or "d" [1 byte]
14 * - optionally, some more ascii text, reserved for future use
15 * must be ignored by declaree (but not sent by declarer)
16 * - a newline [1 byte]
19 * n 2 bytes big endian declaration length
20 * ... client's declaration (ascii text, including newline)
22 * then for each update
24 * - 0x03 destination file should be deleted
25 * but note that contents must be retained by receiver
26 * as it may be used for rle updates
27 * - 0x04 complete new destination file follows, 64-bit length
28 * l 8 bytes big endian length
30 * receiver must then reply with 0x01 ACK
46 #include <sys/types.h>
52 #define REPLMSG_ACK 0x01
53 #define REPLMSG_START 0x02
54 #define REPLMSG_RM 0x03
55 #define REPLMSG_FILE64 0x04
57 static const char banner[]= "#rcopy-repeatedly#\n";
59 static FILE *commsi, *commso;
61 static double max_bw_prop_mean= 0.2;
62 static double max_bw_prop_burst= 0.8;
63 static int txblocksz= INT_MAX, verbose=1;
64 static int min_interval_usec= 100000; /* 100ms */
67 static const char **sargs;
69 static const char *rsh_program= 0;
70 static const char *rcopy_repeatedly_program= "rcopy-repeatedly";
71 static int server_upcopy=-1; /* -1 means not yet known; 0 means download */
72 /* `up' means towards the client,
73 * since we regard the subprocess as `down' */
76 static double stream_allow_secsperbyte= 1/1e6; /* for initial transfer */
78 static char mainbuf[65536]; /* must be at least 2^16 */
80 #define NORETURN __attribute__((noreturn))
82 static void vdie(int ec, const char *pfx, int eno,
83 const char *fmt, va_list al) NORETURN;
84 static void vdie(int ec, const char *pfx, int eno,
85 const char *fmt, va_list al) {
86 fputs("rcopy-repeatedly: ",stderr);
87 if (server_upcopy>=0) fputs("server: ",stderr);
88 if (pfx) fprintf(stderr,"%s: ",pfx);
89 vfprintf(stderr,fmt,al);
90 if (eno!=-1) fprintf(stderr,": %s",strerror(eno));
94 static void die(int ec, const char *pfx, int eno,
95 const char *fmt, ...) NORETURN;
96 static void die(int ec, const char *pfx, int eno,
97 const char *fmt, ...) {
100 vdie(ec,pfx,eno,fmt,al);
103 static void diem(void) NORETURN;
104 static void diem(void) { die(16,0,errno,"malloc failed"); }
105 static void *xmalloc(size_t sz) {
111 static void *xrealloc(void *p, size_t sz) {
118 static void diee(const char *fmt, ...) NORETURN;
119 static void diee(const char *fmt, ...) {
122 vdie(12,0,errno,fmt,al);
124 static void die_protocol(const char *fmt, ...) NORETURN;
125 static void die_protocol(const char *fmt, ...) {
128 vdie(10,"protocol error",-1,fmt,al);
131 static void die_badrecv(const char *what) NORETURN;
132 static void die_badrecv(const char *what) {
133 if (ferror(commsi)) diee("communication failed while receiving %s", what);
134 if (feof(commsi)) die_protocol("receiver got unexpected EOF in %s", what);
137 static void die_badsend(void) NORETURN;
138 static void die_badsend(void) {
139 diee("transmission failed");
142 static void send_flush(void) {
143 if (ferror(commso) || fflush(commso))
146 static void sendbyte(int c) {
147 if (putc(c,commso)==EOF)
151 static void mfreadcommsi(void *buf, int l, const char *what) {
152 int r= fread(buf,1,l,commsi); if (r!=l) die_badrecv(what);
154 static void mfwritecommso(const void *buf, int l) {
155 int r= fwrite(buf,1,l,commso); if (r!=l) die_badsend();
158 static void mpipe(int p[2]) { if (pipe(p)) diee("could not create pipe"); }
159 static void mdup2(int fd, int fd2) {
160 if (dup2(fd,fd2)!=fd2) diee("could not dup2(%d,%d)",fd,fd2);
163 typedef void copyfile_die_fn(FILE *f, const char *xi);
165 struct timespec ts_sendstart;
167 static void mgettime(struct timespec *ts) {
168 int r= clock_gettime(CLOCK_MONOTONIC, ts);
169 if (r) diee("clock_gettime failed");
172 static void bandlimit_sendstart(void) {
173 mgettime(&ts_sendstart);
176 static double mgettime_elapsed(struct timespec ts_base,
177 struct timespec *ts_ret) {
179 return (ts_ret->tv_sec - ts_base.tv_sec) +
180 (ts_ret->tv_nsec - ts_base.tv_nsec)*1e-9;
183 static void flushstderr(void) {
184 if (ferror(stderr) || fflush(stderr))
185 diee("could not write progress to stderr");
188 static void verbosespinprintf(const char *fmt, ...) {
189 static const char spinnerchars[]= "/-\\";
190 static int spinnerchar;
197 fprintf(stderr," %c ",spinnerchars[spinnerchar]);
198 spinnerchar++; spinnerchar %= sizeof(spinnerchars)-1;
199 vfprintf(stderr,fmt,al);
204 static void bandlimit_sendend(uint64_t bytes, int *interval_usec_update) {
205 struct timespec ts_buf;
206 double elapsed= mgettime_elapsed(ts_sendstart, &ts_buf);
207 double secsperbyte_observed= elapsed / bytes;
209 stream_allow_secsperbyte=
210 secsperbyte_observed * max_bw_prop_mean / max_bw_prop_burst;
212 double min_update= elapsed / max_bw_prop_mean;
213 if (min_update > 1e3) min_update= 1e3;
214 int min_update_usec= min_update * 1e6;
216 if (*interval_usec_update > min_update_usec)
217 *interval_usec_update= min_update_usec;
219 verbosespinprintf("%12lluby %10.3fs %13.2fkby/s",
220 (unsigned long long)bytes, elapsed,
221 1e-3/secsperbyte_observed);
224 static void copyfile(FILE *sf, copyfile_die_fn *sdie, const char *sxi,
225 FILE *df, copyfile_die_fn *ddie, const char *dxi,
226 uint64_t lstart, int amsender) {
227 struct timespec ts_last;
229 uint64_t l=lstart, done=0;
231 ts_last= ts_sendstart;
234 now= l < sizeof(mainbuf) ? l : sizeof(mainbuf);
235 if (now > txblocksz) now= txblocksz;
238 fprintf(stderr," %3d%% \r",
239 (int)(done*100.0/lstart));
244 double elapsed_want= now * stream_allow_secsperbyte;
245 double elapsed= mgettime_elapsed(ts_last, &ts_last);
246 double needwait= elapsed_want - elapsed;
247 if (needwait > 1) needwait= 1;
248 if (needwait > 0) usleep(ceil(needwait * 1e6));
251 r= fread(mainbuf,1,now,sf); if (r!=now) sdie(sf,sxi);
252 r= fwrite(mainbuf,1,now,df); if (r!=now) ddie(df,dxi);
258 static void copydie_inputfile(FILE *f, const char *filename) {
259 diee("read failed on source file `%s'", filename);
261 static void copydie_tmpwrite(FILE *f, const char *tmpfilename) {
262 diee("write failed to temporary receiving file `%s'", tmpfilename);
264 static void copydie_commsi(FILE *f, const char *what) {
267 static void copydie_commso(FILE *f, const char *what) {
271 static int generate_declaration(void) {
272 /* returns length; declaration is left in mainbuf */
279 static void read_declaration(int decllen) {
280 assert(decllen <= sizeof(mainbuf));
281 if (decllen<2) die_protocol("declaration too short");
282 mfreadcommsi(mainbuf,decllen,"declaration");
283 if (mainbuf[decllen-1] != '\n')
284 die_protocol("declaration missing final newline");
285 if (mainbuf[0] != udchar)
286 die_protocol("declaration incorrect direction indicator");
289 static void receiver(const char *filename) {
294 char *lastslash= strrchr(filename,'/');
296 r= asprintf(&tmpfilename, ".rcopy-repeatedly.#%s#", filename);
298 r= asprintf(&tmpfilename, "%.*s/.rcopy-repeatedly.#%s#",
299 (int)(lastslash-filename), filename, lastslash+1);
302 r= unlink(tmpfilename);
303 if (r && errno!=ENOENT)
304 diee("could not remove temporary receiving file `%s'", tmpfilename);
313 if (ferror(commsi)) die_badrecv("transfer message code");
314 assert(feof(commsi));
319 if (r && errno!=ENOENT)
320 diee("source file removed but could not remove destination file `%s'",
325 newfile= fopen(tmpfilename, "wb");
326 if (!newfile) diee("could not create temporary receiving file `%s'",
329 mfreadcommsi(lbuf,8,"FILE64 l");
332 (lbuf[0] << 28 << 28) |
333 (lbuf[1] << 24 << 24) |
334 (lbuf[2] << 16 << 24) |
335 (lbuf[3] << 8 << 24) |
341 copyfile(commsi, copydie_commsi,"FILE64 file data",
342 newfile, copydie_tmpwrite,tmpfilename,
345 if (fclose(newfile)) diee("could not flush and close temporary"
346 " receiving file `%s'", tmpfilename);
347 if (rename(tmpfilename, filename))
348 diee("could not install new version of destination file `%s'",
351 sendbyte(REPLMSG_ACK);
355 die_protocol("unknown transfer message code 0x%02x",c);
361 static void sender(const char *filename) {
363 int interval_usec, r, c;
364 struct stat stabtest, stab;
365 enum { told_nothing, told_file, told_remove } told;
374 usleep(interval_usec);
376 interval_usec= min_interval_usec;
378 r= stat(filename, &stabtest);
382 if (told == told_file &&
383 stabtest.st_mode == stab.st_mode &&
384 stabtest.st_dev == stab.st_dev &&
385 stabtest.st_ino == stab.st_ino &&
386 stabtest.st_mtime == stab.st_mtime &&
387 stabtest.st_size == stab.st_size)
389 f= fopen(filename, "rb");
393 if (errno!=ENOENT) diee("could not access source file `%s'",filename);
394 if (told != told_remove) {
395 verbosespinprintf(" ENOENT ");
396 sendbyte(REPLMSG_RM);
402 if (fold) fclose(fold);
405 r= fstat(fileno(f),&stab);
406 if (r) diee("could not fstat source file `%s'",filename);
408 if (!S_ISREG(stab.st_mode))
409 die(8,0,-1,"source file `%s' is not a plain file",filename);
413 stab.st_size >> 28 >> 28,
414 stab.st_size >> 24 >> 24,
415 stab.st_size >> 16 >> 24,
416 stab.st_size >> 8 >> 24,
423 bandlimit_sendstart();
425 mfwritecommso(hbuf,9);
427 copyfile(f, copydie_inputfile,filename,
428 commso, copydie_commso,0,
433 c= fgetc(commsi); if (c==EOF) die_badrecv("ack");
434 if (c!=REPLMSG_ACK) die_protocol("got %#02x instead of ACK",c);
436 bandlimit_sendend(stab.st_size, &interval_usec);
444 const char *userhost, *path;
447 static FileSpecification srcspec, dstspec;
449 static void of__server(const struct cmdinfo *ci, const char *val) {
450 int ncount= nsargs + 1 + !!val;
451 sargs= xrealloc(sargs, sizeof(*sargs) * ncount);
452 sargs[nsargs++]= ci->olong;
454 sargs[nsargs++]= val;
457 static int of__server_int(const struct cmdinfo *ci, const char *val) {
461 errno= 0; v= strtol(val,&ep,10);
462 if (!*val || *ep || errno || v<INT_MIN || v>INT_MAX)
463 badusage("bad integer argument `%s' for --%s",val,ci->olong);
467 static void of_help(const struct cmdinfo *ci, const char *val) {
469 if (ferror(stdout)) diee("could not write usage message to stdout");
473 static void of_bw(const struct cmdinfo *ci, const char *val) {
474 int pct= of__server_int(ci,val);
475 if (pct<1 || pct>100)
476 badusage("bandwidth percentage must be between 1 and 100 inclusive");
477 *(double*)ci->parg= pct * 0.01;
480 static void of_server_int(const struct cmdinfo *ci, const char *val) {
481 *(int*)ci->parg= of__server_int(ci,val);
484 void usagemessage(void) {
486 "usage: rcopy-repeatedly [<options>] <file> <file>\n"
487 " <file> may be <local-file> or [<user>@]<host>:<file>\n"
488 " exactly one of each of the two forms must be provided\n"
489 " a file is taken as remote if it has a : before the first /\n"
493 "options for bandwidth (and cpu time) control:\n"
494 " --max-bandwidth-percent-mean (default %d)\n"
495 " --max-bandwidth-percent-burst (default %d)\n"
496 " --tx-block-size (default/max %d)\n"
497 " --min-interval-usec (default %d)\n"
498 "options for finding programs:\n"
499 " --rcopy-repeatedly (default: rcopy-repeatedly)\n"
500 " --rsh-program (default: $RCOPY_REPEATEDLY_RSH or $RSYNC_RSH or ssh)\n"
501 "options passed to server side via ssh:\n"
502 " --receiver --sender, bandwidth control options\n",
503 (int)(max_bw_prop_mean*100), (int)(max_bw_prop_burst*100),
504 (int)sizeof(mainbuf), min_interval_usec);
507 static const struct cmdinfo cmdinfos[]= {
508 { "help", .call= of_help },
509 { "max-bandwidth-percent-mean", 0,1,.call=of_bw,.parg=&max_bw_prop_mean },
510 { "max-bandwidth-percent-burst",0,1,.call=of_bw,.parg=&max_bw_prop_burst },
511 { "tx-block-size",0, 1,.call=of_server_int, .parg=&txblocksz },
512 { "min-interval-usec",0, 1,.call=of_server_int, .parg=&min_interval_usec },
513 { "rcopy-repeatedly",0, 1, .sassignto=&rcopy_repeatedly_program },
514 { "rsh-program",0, 1, .sassignto=&rsh_program },
515 { "quiet",'q', .iassignto= &verbose, .arg=0 },
516 { "receiver", .iassignto= &server_upcopy, .arg=0 },
517 { "sender", .iassignto= &server_upcopy, .arg=1 },
521 static void server(const char *filename) {
525 udchar= server_upcopy?'u':'d';
529 l= generate_declaration();
530 fprintf(commso, "%s%04x\n", banner, l);
531 mfwritecommso(mainbuf, l);
536 if (feof(commsi)) exit(14);
537 assert(ferror(commsi)); die_badrecv("initial START message");
539 if (c!=REPLMSG_START) die_protocol("initial START was %#02x instead",c);
541 mfreadcommsi(buf,2,"START l");
542 l= (buf[0] << 8) | buf[1];
552 static void client(void) {
553 int uppipe[2], downpipe[2], r;
555 FileSpecification *remotespec;
556 const char *remotemode;
561 if (srcspec.userhost) {
563 remotespec= &srcspec;
564 remotemode= "--sender";
567 remotespec= &dstspec;
568 remotemode= "--receiver";
571 sargs= xrealloc(sargs, sizeof(*sargs) * (7 + nsargs));
572 memmove(sargs+5, sargs, sizeof(*sargs) * nsargs);
573 sargs[0]= rsh_program;
574 sargs[1]= remotespec->userhost;
575 sargs[2]= rcopy_repeatedly_program;
576 sargs[3]= remotemode;
578 sargs[5+nsargs]= remotespec->path;
582 if (child==-1) diee("fork failed");
584 mdup2(downpipe[0],0);
586 close(uppipe[0]); close(downpipe[0]);
587 close(uppipe[1]); close(downpipe[1]);
589 execvp(rsh_program, (char**)sargs);
590 diee("failed to execute rsh program `%s'",rsh_program);
593 commso= fdopen(downpipe[1],"wb");
594 commsi= fdopen(uppipe[0],"rb");
595 if (!commso || !commsi) diee("fdopen failed");
599 char banbuf[sizeof(banner)-1 + 5 + 1];
600 r= fread(banbuf,1,sizeof(banbuf)-1,commsi);
601 if (ferror(commsi)) die_badrecv("read banner");
603 if (r!=sizeof(banbuf)-1 ||
604 memcmp(banbuf,banner,sizeof(banner)-1) ||
605 banbuf[sizeof(banner)-1 + 4] != '\n') {
608 for (count=0, sap=sargs; *sap; sap++) count+= strlen(*sap)+1;
609 char *cmdline= xmalloc(count+1);
611 for (sap=sargs; *sap; sap++) {
613 strcat(cmdline,*sap);
616 die(8,0,-1,"did not receive banner as expected -"
617 " shell dirty? ssh broken?\n"
620 " and expect the first line to be\n"
625 banbuf[sizeof(banbuf)-1]= 0;
627 long decllen= strtoul(banbuf + sizeof(banner)-1, &ep, 16);
628 if (ep != banbuf + sizeof(banner)-1 + 4)
629 die_protocol("declaration length syntax error");
631 read_declaration(decllen);
633 int l= generate_declaration();
634 sendbyte(REPLMSG_START);
635 sendbyte((l >> 8) & 0x0ff);
636 sendbyte( l & 0x0ff);
637 mfwritecommso(mainbuf,l);
639 if (remotespec==&srcspec)
640 receiver(dstspec.path);
642 sender(srcspec.path);
645 static void parse_file_specification(FileSpecification *fs, const char *arg,
649 if (!arg) badusage("too few arguments - missing %s\n",what);
651 for (colon=arg; ; colon++) {
652 if (!*colon || *colon=='/') {
658 char *uh= xmalloc(colon-arg + 1);
659 memcpy(uh,arg, colon-arg); uh[colon-arg]= 0;
667 int main(int argc, const char *const *argv) {
668 setvbuf(stderr,0,_IOLBF,BUFSIZ);
670 myopt(&argv, cmdinfos);
672 if (!rsh_program) rsh_program= getenv("RCOPY_REPEATEDLY_RSH");
673 if (!rsh_program) rsh_program= getenv("RSYNC_RSH");
674 if (!rsh_program) rsh_program= "ssh";
676 if (max_bw_prop_burst / max_bw_prop_mean < 1.1)
677 badusage("max bandwidth prop burst must be at least 1.1x"
678 " max bandwidth prop mean");
680 if (txblocksz<1) badusage("transmit block size must be at least 1");
681 if (min_interval_usec<0) badusage("minimum update interval may not be -ve");
683 if (server_upcopy>=0) {
684 if (!argv[0] || argv[1])
685 badusage("server mode must have just the filename as non-option arg");
688 parse_file_specification(&srcspec, argv[0], "source");
689 parse_file_specification(&dstspec, argv[1], "destination");
690 if (argv[2]) badusage("too many non-option arguments");
691 if (!!srcspec.userhost == !!dstspec.userhost)
692 badusage("need exactly one remote file argument");