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= 0.2;
62 static int txblocksz= INT_MAX, verbose=1;
63 static int min_interval_usec= 100000; /* 100ms */
66 static const char **sargs;
68 static const char *rsh_program= 0;
69 static const char *rcopy_repeatedly_program= "rcopy-repeatedly";
70 static int server_upcopy=-1; /* -1 means not yet known; 0 means download */
71 /* `up' means towards the client,
72 * since we regard the subprocess as `down' */
76 static char mainbuf[65536]; /* must be at least 2^16 */
78 #define NORETURN __attribute__((noreturn))
80 static void vdie(int ec, const char *pfx, int eno,
81 const char *fmt, va_list al) NORETURN;
82 static void vdie(int ec, const char *pfx, int eno,
83 const char *fmt, va_list al) {
84 fputs("rcopy-repeatedly: ",stderr);
85 if (server_upcopy>=0) fputs("server: ",stderr);
86 if (pfx) fprintf(stderr,"%s: ",pfx);
87 vfprintf(stderr,fmt,al);
88 if (eno!=-1) fprintf(stderr,": %s",strerror(eno));
92 static void die(int ec, const char *pfx, int eno,
93 const char *fmt, ...) NORETURN;
94 static void die(int ec, const char *pfx, int eno,
95 const char *fmt, ...) {
98 vdie(ec,pfx,eno,fmt,al);
101 static void diem(void) NORETURN;
102 static void diem(void) { die(16,0,errno,"malloc failed"); }
103 static void *xmalloc(size_t sz) {
109 static void *xrealloc(void *p, size_t sz) {
116 static void diee(const char *fmt, ...) NORETURN;
117 static void diee(const char *fmt, ...) {
120 vdie(12,0,errno,fmt,al);
122 static void die_protocol(const char *fmt, ...) NORETURN;
123 static void die_protocol(const char *fmt, ...) {
126 vdie(10,"protocol error",-1,fmt,al);
129 static void die_badrecv(const char *what) NORETURN;
130 static void die_badrecv(const char *what) {
131 if (ferror(commsi)) diee("communication failed while receiving %s", what);
132 if (feof(commsi)) die_protocol("receiver got unexpected EOF in %s", what);
135 static void die_badsend(void) NORETURN;
136 static void die_badsend(void) {
137 diee("transmission failed");
140 static void send_flush(void) {
141 if (ferror(commso) || fflush(commso))
144 static void sendbyte(int c) {
145 if (putc(c,commso)==EOF)
149 static void mfreadcommsi(void *buf, int l, const char *what) {
150 int r= fread(buf,1,l,commsi); if (r!=l) die_badrecv(what);
152 static void mfwritecommso(const void *buf, int l) {
153 int r= fwrite(buf,1,l,commso); if (r!=l) die_badsend();
156 static void mpipe(int p[2]) { if (pipe(p)) diee("could not create pipe"); }
157 static void mdup2(int fd, int fd2) {
158 if (dup2(fd,fd2)!=fd2) diee("could not dup2(%d,%d)",fd,fd2);
161 typedef void copyfile_die_fn(FILE *f, const char *xi);
163 struct timespec ts_sendstart;
165 static void mgettime(struct timespec *ts) {
166 int r= clock_gettime(CLOCK_MONOTONIC, ts);
167 if (r) diee("clock_gettime failed");
170 static void bandlimit_sendstart(void) {
171 mgettime(&ts_sendstart);
174 static double mgettime_elapsed(struct timespec ts_base,
175 struct timespec *ts_ret) {
177 return (ts_ret->tv_sec - ts_base.tv_sec) +
178 (ts_ret->tv_nsec - ts_base.tv_nsec)*1e-9;
181 static void flushstderr(void) {
182 if (ferror(stderr) || fflush(stderr))
183 diee("could not write progress to stderr");
186 static void verbosespinprintf(const char *fmt, ...) {
187 static const char spinnerchars[]= "/-\\";
188 static int spinnerchar;
195 fprintf(stderr," %c ",spinnerchars[spinnerchar]);
196 spinnerchar++; spinnerchar %= sizeof(spinnerchars)-1;
197 vfprintf(stderr,fmt,al);
202 static void bandlimit_sendend(uint64_t bytes, int *interval_usec_update) {
203 struct timespec ts_buf;
204 double elapsed= mgettime_elapsed(ts_sendstart, &ts_buf);
205 double secsperbyte_observed= elapsed / bytes;
207 double min_update= elapsed / max_bw_prop;
208 if (min_update > 1e3) min_update= 1e3;
209 int min_update_usec= min_update * 1e6;
211 if (*interval_usec_update < min_update_usec)
212 *interval_usec_update= min_update_usec;
214 verbosespinprintf("%12lluby %10.3fs %13.2fkby/s %8dms",
215 (unsigned long long)bytes, elapsed,
216 1e-3/secsperbyte_observed, *interval_usec_update/1000);
219 static void copyfile(FILE *sf, copyfile_die_fn *sdie, const char *sxi,
220 FILE *df, copyfile_die_fn *ddie, const char *dxi,
221 uint64_t lstart, int amsender) {
222 struct timespec ts_last;
224 uint64_t l=lstart, done=0;
226 ts_last= ts_sendstart;
229 now= l < sizeof(mainbuf) ? l : sizeof(mainbuf);
230 if (now > txblocksz) now= txblocksz;
232 r= fread(mainbuf,1,now,sf); if (r!=now) sdie(sf,sxi);
233 r= fwrite(mainbuf,1,now,df); if (r!=now) ddie(df,dxi);
238 fprintf(stderr," %3d%% \r",
239 (int)(done*100.0/lstart));
245 static void copydie_inputfile(FILE *f, const char *filename) {
246 diee("read failed on source file `%s'", filename);
248 static void copydie_tmpwrite(FILE *f, const char *tmpfilename) {
249 diee("write failed to temporary receiving file `%s'", tmpfilename);
251 static void copydie_commsi(FILE *f, const char *what) {
254 static void copydie_commso(FILE *f, const char *what) {
258 static int generate_declaration(void) {
259 /* returns length; declaration is left in mainbuf */
266 static void read_declaration(int decllen) {
267 assert(decllen <= sizeof(mainbuf));
268 if (decllen<2) die_protocol("declaration too short");
269 mfreadcommsi(mainbuf,decllen,"declaration");
270 if (mainbuf[decllen-1] != '\n')
271 die_protocol("declaration missing final newline");
272 if (mainbuf[0] != udchar)
273 die_protocol("declaration incorrect direction indicator");
276 static void receiver(const char *filename) {
281 char *lastslash= strrchr(filename,'/');
283 r= asprintf(&tmpfilename, ".rcopy-repeatedly.#%s#", filename);
285 r= asprintf(&tmpfilename, "%.*s/.rcopy-repeatedly.#%s#",
286 (int)(lastslash-filename), filename, lastslash+1);
289 r= unlink(tmpfilename);
290 if (r && errno!=ENOENT)
291 diee("could not remove temporary receiving file `%s'", tmpfilename);
300 if (ferror(commsi)) die_badrecv("transfer message code");
301 assert(feof(commsi));
306 if (r && errno!=ENOENT)
307 diee("source file removed but could not remove destination file `%s'",
312 newfile= fopen(tmpfilename, "wb");
313 if (!newfile) diee("could not create temporary receiving file `%s'",
316 mfreadcommsi(lbuf,8,"FILE64 l");
319 (lbuf[0] << 28 << 28) |
320 (lbuf[1] << 24 << 24) |
321 (lbuf[2] << 16 << 24) |
322 (lbuf[3] << 8 << 24) |
328 copyfile(commsi, copydie_commsi,"FILE64 file data",
329 newfile, copydie_tmpwrite,tmpfilename,
332 if (fclose(newfile)) diee("could not flush and close temporary"
333 " receiving file `%s'", tmpfilename);
334 if (rename(tmpfilename, filename))
335 diee("could not install new version of destination file `%s'",
338 sendbyte(REPLMSG_ACK);
342 die_protocol("unknown transfer message code 0x%02x",c);
348 static void sender(const char *filename) {
350 int interval_usec, r, c;
351 struct stat stabtest, stab;
352 enum { told_nothing, told_file, told_remove } told;
361 usleep(interval_usec);
363 interval_usec= min_interval_usec;
365 r= stat(filename, &stabtest);
369 if (told == told_file &&
370 stabtest.st_mode == stab.st_mode &&
371 stabtest.st_dev == stab.st_dev &&
372 stabtest.st_ino == stab.st_ino &&
373 stabtest.st_mtime == stab.st_mtime &&
374 stabtest.st_size == stab.st_size)
376 f= fopen(filename, "rb");
380 if (errno!=ENOENT) diee("could not access source file `%s'",filename);
381 if (told != told_remove) {
384 sendbyte(REPLMSG_RM);
390 if (fold) fclose(fold);
393 r= fstat(fileno(f),&stab);
394 if (r) diee("could not fstat source file `%s'",filename);
396 if (!S_ISREG(stab.st_mode))
397 die(8,0,-1,"source file `%s' is not a plain file",filename);
401 stab.st_size >> 28 >> 28,
402 stab.st_size >> 24 >> 24,
403 stab.st_size >> 16 >> 24,
404 stab.st_size >> 8 >> 24,
411 bandlimit_sendstart();
413 mfwritecommso(hbuf,9);
415 copyfile(f, copydie_inputfile,filename,
416 commso, copydie_commso,0,
421 c= fgetc(commsi); if (c==EOF) die_badrecv("ack");
422 if (c!=REPLMSG_ACK) die_protocol("got %#02x instead of ACK",c);
424 bandlimit_sendend(stab.st_size, &interval_usec);
432 const char *userhost, *path;
435 static FileSpecification srcspec, dstspec;
437 static void of__server(const struct cmdinfo *ci, const char *val) {
438 int ncount= nsargs + 1 + !!val;
439 sargs= xrealloc(sargs, sizeof(*sargs) * ncount);
440 sargs[nsargs++]= ci->olong;
442 sargs[nsargs++]= val;
445 static int of__server_int(const struct cmdinfo *ci, const char *val) {
449 errno= 0; v= strtol(val,&ep,10);
450 if (!*val || *ep || errno || v<INT_MIN || v>INT_MAX)
451 badusage("bad integer argument `%s' for --%s",val,ci->olong);
455 static void of_help(const struct cmdinfo *ci, const char *val) {
457 if (ferror(stdout)) diee("could not write usage message to stdout");
461 static void of_bw(const struct cmdinfo *ci, const char *val) {
462 int pct= of__server_int(ci,val);
463 if (pct<1 || pct>100)
464 badusage("bandwidth percentage must be between 1 and 100 inclusive");
465 *(double*)ci->parg= pct * 0.01;
468 static void of_server_int(const struct cmdinfo *ci, const char *val) {
469 *(int*)ci->parg= of__server_int(ci,val);
472 void usagemessage(void) {
474 "usage: rcopy-repeatedly [<options>] <file> <file>\n"
475 " <file> may be <local-file> or [<user>@]<host>:<file>\n"
476 " exactly one of each of the two forms must be provided\n"
477 " a file is taken as remote if it has a : before the first /\n"
481 "options for bandwidth (and cpu time) control:\n"
482 " --max-bandwidth-percent (default %d)\n"
483 " --tx-block-size (default/max %d)\n"
484 " --min-interval-usec (default %d)\n"
485 "options for finding programs:\n"
486 " --rcopy-repeatedly (default: rcopy-repeatedly)\n"
487 " --rsh-program (default: $RCOPY_REPEATEDLY_RSH or $RSYNC_RSH or ssh)\n"
488 "options passed to server side via ssh:\n"
489 " --receiver --sender, bandwidth control options\n",
490 (int)(max_bw_prop*100), (int)sizeof(mainbuf), min_interval_usec);
493 static const struct cmdinfo cmdinfos[]= {
494 { "help", .call= of_help },
495 { "max-bandwidth-percent", 0,1,.call=of_bw,.parg=&max_bw_prop },
496 { "tx-block-size",0, 1,.call=of_server_int, .parg=&txblocksz },
497 { "min-interval-usec",0, 1,.call=of_server_int, .parg=&min_interval_usec },
498 { "rcopy-repeatedly",0, 1, .sassignto=&rcopy_repeatedly_program },
499 { "rsh-program",0, 1, .sassignto=&rsh_program },
500 { "quiet",'q', .iassignto= &verbose, .arg=0 },
501 { "receiver", .iassignto= &server_upcopy, .arg=0 },
502 { "sender", .iassignto= &server_upcopy, .arg=1 },
506 static void server(const char *filename) {
510 udchar= server_upcopy?'u':'d';
514 l= generate_declaration();
515 fprintf(commso, "%s%04x\n", banner, l);
516 mfwritecommso(mainbuf, l);
521 if (feof(commsi)) exit(14);
522 assert(ferror(commsi)); die_badrecv("initial START message");
524 if (c!=REPLMSG_START) die_protocol("initial START was %#02x instead",c);
526 mfreadcommsi(buf,2,"START l");
527 l= (buf[0] << 8) | buf[1];
537 static void client(void) {
538 int uppipe[2], downpipe[2], r;
540 FileSpecification *remotespec;
541 const char *remotemode;
546 if (srcspec.userhost) {
548 remotespec= &srcspec;
549 remotemode= "--sender";
552 remotespec= &dstspec;
553 remotemode= "--receiver";
556 sargs= xrealloc(sargs, sizeof(*sargs) * (7 + nsargs));
557 memmove(sargs+5, sargs, sizeof(*sargs) * nsargs);
558 sargs[0]= rsh_program;
559 sargs[1]= remotespec->userhost;
560 sargs[2]= rcopy_repeatedly_program;
561 sargs[3]= remotemode;
563 sargs[5+nsargs]= remotespec->path;
567 if (child==-1) diee("fork failed");
569 mdup2(downpipe[0],0);
571 close(uppipe[0]); close(downpipe[0]);
572 close(uppipe[1]); close(downpipe[1]);
574 execvp(rsh_program, (char**)sargs);
575 diee("failed to execute rsh program `%s'",rsh_program);
578 commso= fdopen(downpipe[1],"wb");
579 commsi= fdopen(uppipe[0],"rb");
580 if (!commso || !commsi) diee("fdopen failed");
584 char banbuf[sizeof(banner)-1 + 5 + 1];
585 r= fread(banbuf,1,sizeof(banbuf)-1,commsi);
586 if (ferror(commsi)) die_badrecv("read banner");
588 if (r!=sizeof(banbuf)-1 ||
589 memcmp(banbuf,banner,sizeof(banner)-1) ||
590 banbuf[sizeof(banner)-1 + 4] != '\n') {
593 for (count=0, sap=sargs; *sap; sap++) count+= strlen(*sap)+1;
594 char *cmdline= xmalloc(count+1);
596 for (sap=sargs; *sap; sap++) {
598 strcat(cmdline,*sap);
601 die(8,0,-1,"did not receive banner as expected -"
602 " shell dirty? ssh broken?\n"
605 " and expect the first line to be\n"
610 banbuf[sizeof(banbuf)-1]= 0;
612 long decllen= strtoul(banbuf + sizeof(banner)-1, &ep, 16);
613 if (ep != banbuf + sizeof(banner)-1 + 4)
614 die_protocol("declaration length syntax error");
616 read_declaration(decllen);
618 int l= generate_declaration();
619 sendbyte(REPLMSG_START);
620 sendbyte((l >> 8) & 0x0ff);
621 sendbyte( l & 0x0ff);
622 mfwritecommso(mainbuf,l);
624 if (remotespec==&srcspec)
625 receiver(dstspec.path);
627 sender(srcspec.path);
630 static void parse_file_specification(FileSpecification *fs, const char *arg,
634 if (!arg) badusage("too few arguments - missing %s\n",what);
636 for (colon=arg; ; colon++) {
637 if (!*colon || *colon=='/') {
643 char *uh= xmalloc(colon-arg + 1);
644 memcpy(uh,arg, colon-arg); uh[colon-arg]= 0;
652 int main(int argc, const char *const *argv) {
653 setvbuf(stderr,0,_IOLBF,BUFSIZ);
655 myopt(&argv, cmdinfos);
657 if (!rsh_program) rsh_program= getenv("RCOPY_REPEATEDLY_RSH");
658 if (!rsh_program) rsh_program= getenv("RSYNC_RSH");
659 if (!rsh_program) rsh_program= "ssh";
661 if (txblocksz<1) badusage("transmit block size must be at least 1");
662 if (min_interval_usec<0) badusage("minimum update interval may not be -ve");
664 if (server_upcopy>=0) {
665 if (!argv[0] || argv[1])
666 badusage("server mode must have just the filename as non-option arg");
669 parse_file_specification(&srcspec, argv[0], "source");
670 parse_file_specification(&dstspec, argv[1], "destination");
671 if (argv[2]) badusage("too many non-option arguments");
672 if (!!srcspec.userhost == !!dstspec.userhost)
673 badusage("need exactly one remote file argument");