r9313 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r9312‎ | r9313 | r9314 >
Date:13:02, 3 June 2005
Author:kateturner
Status:old
Tags:
Comment:
- cleanups
- pad output to 20 blocks (10240 bytes)
- display fix
- don't try to set utimes/perms for archive
- don't copy symlinks
Modified paths:
  • /trunk/tools/trickle/Makefile (modified) (history)
  • /trunk/tools/trickle/tar.c (modified) (history)
  • /trunk/tools/trickle/trickle.c (modified) (history)
  • /trunk/tools/trickle/trickle.h (modified) (history)
  • /trunk/tools/trickle/util.c (modified) (history)

Diff [purge]

Index: trunk/tools/trickle/trickle.c
@@ -137,12 +137,12 @@
138138
139139 if (Zflag) {
140140 if (isatty(0))
141 - fatal("-Z should not be used directly");
 141+ fatal("m1", "-Z should not be used directly");
142142 discuss_files();
143143 }
144144
145145 if ((rflag || uflag) && archive) {
146 - fprintf(stderr, "%s: -u/-r and -t may not be specified together\n", progname);
 146+ fatal("m2", "-u/-r and -t may not be specified together");
147147 usage();
148148 }
149149
@@ -188,13 +188,13 @@
189189 if (!strcmp(dest, "-"))
190190 archfile = stdout;
191191 else if ((archfile = fopen(dest, "w")) == NULL) {
192 - perror(dest);
 192+ pfatal("m3", dest);
193193 exit(8);
194194 }
195195 }
196196
197197 if (stat(src, &sb) < 0) {
198 - perror(src);
 198+ pfatal("m4", dest);
199199 exit(8);
200200 }
201201
@@ -209,7 +209,7 @@
210210
211211 errno = 0;
212212 if (stat(dest, &sd) < 0 && errno != ENOENT) {
213 - perror(dest);
 213+ pfatal("m5", dest);
214214 exit(8);
215215 }
216216
@@ -227,7 +227,7 @@
228228 } ok:
229229 if ((c = fgetc(stdin)) != '\n') ungetc(c, stdin);
230230 if (unlink(dest) < 0) {
231 - perror(dest);
 231+ pfatal("m6", dest);
232232 exit(8);
233233 }
234234 }
@@ -241,7 +241,7 @@
242242 }
243243
244244 if (chdir(src) < 0) {
245 - perror(src);
 245+ pfatal("m7", src);
246246 exit(8);
247247 }
248248
@@ -272,7 +272,7 @@
273273 char *oldcur;
274274
275275 if (chdir(dir) < 0) {
276 - perror("chdir");
 276+ pfatal("cd1", dir);
277277 exit(8);
278278 }
279279
@@ -280,7 +280,7 @@
281281 curdir = allocf("%s%s/", oldcur, dir);
282282
283283 if ((dirp = opendir(".")) == NULL) {
284 - perror(dir);
 284+ pfatal("cd2", dir);
285285 exit(8);
286286 }
287287
@@ -289,11 +289,16 @@
290290 continue;
291291
292292 if (lstat(dp->d_name, &sb) < 0) {
293 - perror(dp->d_name);
 293+ pfatal("cd3", dp->d_name);
294294 exit(8);
295295 }
296296
297 - if (sb.st_mode & S_IFDIR) {
 297+ if ((sb.st_mode & S_IFLNK) == S_IFLNK) {
 298+ if (!qflag)
 299+ fprintf(stderr, "fs %s%s\n", curdir, dp->d_name);
 300+ /* XXX should be possible to include symlinks in the output */
 301+ continue;
 302+ } else if (sb.st_mode & S_IFDIR) {
298303 char *dpath;
299304 if (exclude(dp->d_name)) {
300305 if (!qflag)
@@ -312,7 +317,7 @@
313318 * exists, just leave it.
314319 */
315320 if (mkdir(dpath, sb.st_mode) < 0 && errno != EEXIST) {
316 - perror(dpath);
 321+ pfatal("cd4", dpath);
317322 exit(8);
318323 }
319324 free(dpath);
@@ -323,7 +328,11 @@
324329 }
325330 copy_directory(dp->d_name, cf);
326331 } else if (sb.st_mode & S_IFREG) {
327 - char *outname = allocf("%s/%s%s", dest, curdir, dp->d_name);
 332+ char *outname;
 333+ if (archive)
 334+ outname = allocf("%s%s", curdir, dp->d_name);
 335+ else
 336+ outname = allocf("%s/%s%s", dest, curdir, dp->d_name);
328337 cf(dp->d_name, outname);
329338 free(outname);
330339 usleep(filesleep);
@@ -353,7 +362,7 @@
354363 struct utimbuf ut;
355364
356365 if (lstat(name, &sb) < 0) {
357 - perror(name);
 366+ pfatal("cf1", name);
358367 exit(8);
359368 }
360369
@@ -379,28 +388,27 @@
380389 fprintf(stderr, "%s: %s exists and I didn't expect it to\n",
381390 progname, outname);
382391 }
383 - perror(outname);
 392+ pfatal("cf2", outname);
384393 exit(8);
385394 }
386395 }
387396 if ((in = open(name, O_RDONLY)) == -1) {
388 - perror(name);
 397+ pfatal("cf3", name);
389398 exit(8);
390399 }
391400 copy_from_to(in, out, outname);
392401
393 - ut.actime = sb.st_atime;
394 - ut.modtime = sb.st_mtime;
395 - if (utime(outname, &ut) < 0) {
396 - perror(outname);
397 - }
 402+ if (!archive) {
 403+ ut.actime = sb.st_atime;
 404+ ut.modtime = sb.st_mtime;
 405+ if (utime(outname, &ut) < 0)
 406+ fprintf(stderr, "%s: %s: %s (cf4)\n", progname, outname, strerror(errno));
398407
399 - if (Pflag) {
400 - if (fchown(out, sb.st_uid, sb.st_gid) < 0) {
401 - perror(outname);
 408+ if (Pflag) {
 409+ if (fchown(out, sb.st_uid, sb.st_gid) < 0)
 410+ fprintf(stderr, "%s: %s: %s (cf5)\n", progname, outname, strerror(errno));
402411 }
403412 }
404 -
405413 }
406414
407415 void
@@ -409,19 +417,20 @@
410418 const char *destname;
411419 {
412420 static char *buf;
413 - int bytes, blocks, bsize;
 421+ int bytes = 0, blocks = 0, bsize;
 422+
414423 if (buf == NULL)
415424 buf = malloc(blocksize);
416425
417 - while ((bsize = read(from, buf, blocksize)) != -1) {
 426+ while ((bsize = read(from, buf, blocksize)) > 0) {
418427 if (tflag) {
419428 if (write_blocked(buf, bsize, archfile) < 1) {
420 - perror(dest);
 429+ pfatal("ct1", destname);
421430 exit(8);
422431 }
423432 } else {
424433 if (write(to, buf, bsize) < 1) {
425 - perror(destname);
 434+ pfatal("ct2", destname);
426435 exit(8);
427436 }
428437 }
@@ -456,7 +465,7 @@
457466 char *p = buf;
458467 size_t ret = 0, tow;
459468
460 - while (size) {
 469+ do {
461470 tow = min(size, sizeof(block));
462471
463472 memset(block, 0, sizeof block);
@@ -465,7 +474,8 @@
466475 return ret;
467476 p += tow;
468477 size -= tow;
469 - }
 478+ ++records;
 479+ } while (size > 0);
470480 return ret;
471481 }
472482
@@ -478,7 +488,7 @@
479489 if (lstat(name, &sa) < 0) {
480490 if (errno == ENOENT)
481491 return 1;
482 - perror(name);
 492+ pfatal("co1", name);
483493 exit(8);
484494 }
485495
@@ -493,14 +503,14 @@
494504 if (lstat(fa, &sa) < 0) {
495505 if (errno == ENOENT)
496506 return 0;
497 - perror(fa);
 507+ pfatal("sf1", fa);
498508 exit(8);
499509 }
500510
501511 if (lstat(fb, &sb) < 0) {
502512 if (errno == ENOENT)
503513 return 0;
504 - perror(fb);
 514+ pfatal("sf2", fb);
505515 exit(8);
506516 }
507517
@@ -595,7 +605,7 @@
596606 sock = proto_rsh(remote, rsh);
597607 proto_neg(sock);
598608 if (chdir(src) < 0) {
599 - perror(src);
 609+ pfatal("sf1", src);
600610 exit(8);
601611 }
602612 frame.rf_buf = dest;
@@ -617,11 +627,11 @@
618628 if (buf == NULL)
619629 buf = malloc(blocksize);
620630 if ((in = open(from, O_RDONLY)) == -1) {
621 - perror(from);
 631+ pfatal("cn1", from);
622632 exit(8);
623633 }
624634 if (fstat(in, &sb) < 0) {
625 - perror(from);
 635+ pfatal("cn2", from);
626636 exit(8);
627637 }
628638 if (!proto_offer(from, &sb)) {
Index: trunk/tools/trickle/util.c
@@ -10,6 +10,8 @@
1111 #include <unistd.h>
1212 #include <varargs.h>
1313 #include <stdlib.h>
 14+#include <errno.h>
 15+#include <string.h>
1416
1517 #include "trickle.h"
1618
@@ -50,3 +52,11 @@
5153 fputs("\n", stderr);
5254 exit(8);
5355 }
 56+
 57+void
 58+pfatal(c, e)
 59+ const char *c, *e;
 60+{
 61+ fprintf(stderr, "%s: %s (%s)\n", e, strerror(errno), c);
 62+ exit(8);
 63+}
Index: trunk/tools/trickle/trickle.h
@@ -16,14 +16,21 @@
1717 #define min(x,y) ((x) < (y) ? (x) : (y))
1818 #define max(x,y) ((x) < (y) ? (y) : (x))
1919
 20+#define RECORD_SIZE 20 /* one record = RECORD_SIZE blocks */
 21+
2022 extern int pflag;
2123 extern char *dest; /* destination name */
2224 extern const char *progname; /* argv[0] */
2325 extern char *curdir; /* cwd name */
2426 extern char *trickle;
 27+extern int records;
2528
2629 size_t write_blocked(void *buf, size_t size, FILE *file);
 30+char *allocf();
 31+void fatal();
 32+void pfatal(const char *, const char *);
2733
 34+/** Protocol support */
2835 #define PROTO_VERS 1
2936 #define P_ACCEPT 1
3037 #define P_DECLINE 2
@@ -41,19 +48,19 @@
4249 struct rdcp_frame;
4350 struct stat;
4451
45 -int proto_neg(int);
46 -int proto_rsh(const char *, const char *);
47 -struct pfile *proto_getfile(void);
48 -void proto_decline(void);
49 -void proto_accept(void);
50 -int proto_read(struct rdcp_frame *);
51 -void proto_write(struct rdcp_frame *);
52 -int proto_offer(const char *file, struct stat *);
53 -void proto_eof(void);
54 -void proto_writeblock(void *, size_t);
55 -char *proto_readdir();
56 -char *allocf();
57 -void fatal();
 52+int proto_neg (int);
 53+int proto_rsh (const char *, const char *);
 54+struct pfile *proto_getfile (void);
 55+void proto_decline (void);
 56+void proto_accept (void);
 57+int proto_read (struct rdcp_frame *);
 58+void proto_write (struct rdcp_frame *);
 59+int proto_offer (const char *file, struct stat *);
 60+void proto_eof (void);
 61+void proto_writeblock (void *, size_t);
 62+char *proto_readdir (void);
 63+void proto_ack (void);
 64+void proto_nack (const char *error);
5865
5966 /** Tar support */
6067
Index: trunk/tools/trickle/tar.c
@@ -16,6 +16,8 @@
1717
1818 #include "trickle.h"
1919
 20+int records;
 21+
2022 void
2123 tar_writeheader(file, name)
2224 FILE *file;
@@ -27,10 +29,8 @@
2830 int sum = 0, i;
2931 int truncate = 0;
3032
31 - if (lstat(name, &sb) < 0) {
32 - perror(name);
33 - exit(8);
34 - }
 33+ if (lstat(name, &sb) < 0)
 34+ pfatal("tw1", name);
3535
3636 /*
3737 * This is a very lax tar header. It's accepted by Solaris tar
@@ -86,14 +86,10 @@
8787 sum += *buf++;
8888 snprintf(hdr.tr_chksum, 8, "%06o", sum);
8989
90 - if (write_blocked(&hdr, sizeof(hdr), file) < 1) {
91 - perror(dest);
92 - exit(8);
93 - }
94 - if (write_blocked(tardata, strlen(tardata), file) < 1) {
95 - perror(dest);
96 - exit(8);
97 - }
 90+ if (write_blocked(&hdr, sizeof(hdr), file) < 1)
 91+ pfatal("tw2", dest);
 92+ if (write_blocked(tardata, strlen(tardata), file) < 1)
 93+ pfatal("tw3", dest);
9894 }
9995
10096 memset(&hdr, 0, sizeof(hdr));
@@ -121,10 +117,8 @@
122118 sum += *buf++;
123119 snprintf(hdr.tr_chksum, 8, "%06o", sum);
124120
125 - if (write_blocked(&hdr, sizeof(hdr), file) < 1) {
126 - perror(dest);
127 - exit(8);
128 - }
 121+ if (write_blocked(&hdr, sizeof(hdr), file) < 1)
 122+ pfatal("tw4", dest);
129123 }
130124
131125 void
@@ -138,4 +132,6 @@
139133 */
140134 write_blocked(zbuf, 1, file);
141135 write_blocked(zbuf, 1, file);
 136+ while (records % 20)
 137+ write_blocked(zbuf, 1, file);
142138 }
Index: trunk/tools/trickle/Makefile
@@ -2,8 +2,8 @@
33
44 trickle: trickle.o tar.o proto.o util.o rdcp.o
55 cc -g trickle.o tar.o proto.o util.o rdcp.o -o $@ -lsocket -lnsl
6 -.c.o: Makefile
7 - cc -g -O -D_FILE_OFFSET_BITS=64 -c $<
 6+.c.o: Makefile $<
 7+ cc -g -D_FILE_OFFSET_BITS=64 -c $<
88 clean:
9 - rm -f trickle trickle.o tar.o
 9+ rm -f trickle trickle.o tar.o util.o rdcp.o proto.o
1010 .KEEP_STATE:

Status & tagging log