|
|
1.1 ! root 1: 2,6 c 2,3 ! 2: < * cpdir.c ! 3: < * DON"T USE THIS SOURCE! MAY REBOOT THE SYSTEM ON BIG CPDIRS! ! 4: < * Copy hierarchies in a file system, preserving structure. ! 5: < * Needs directory stuff in headers: cc -D__KERNEL__ cpdir.c ! 6: < * Define SLOW for 'block at a time copying' - not recommended. ! 7: --- ! 8: > * Cpdir. Copy hierarchies in a file system, preserving structure. ! 9: > * Define SLOW for 'block at a time copying' - not recommended tho. ! 10: 8,13 d 4 ! 11: < ! 12: < /* ! 13: < * Known bug: if dir1/a and dir1/b are linked, and dir2/b exists, ! 14: < * cpdir -u dir1 dir2 creates new dir2/a not linked to dir2/b. ! 15: < */ ! 16: < ! 17: 15,16 d 5 ! 18: < #include <stdlib.h> ! 19: < #include <string.h> ! 20: 21 c 10 ! 21: < #include <sys/uproc.h> ! 22: --- ! 23: > #include <sys/dir.h> ! 24: 26,33 c 15 ! 25: < extern long lseek(); ! 26: < ! 27: < /* Avoid conflicts with headers, bogus nachos. */ ! 28: < #undef hash ! 29: < #undef DIRBUF ! 30: < ! 31: < /* Manifest constants. */ ! 32: < #define VERSION "1.2" ! 33: --- ! 34: > #define VERSION "1.1" ! 35: 43,44 d 24 ! 36: < #define TRUE (0==0) ! 37: < #define FALSE (0==1) ! 38: 46 d 25 ! 39: < /* Macros. */ ! 40: 49,50 c 28,29 ! 41: < /* Type definitions. */ ! 42: < typedef char bool; ! 43: --- ! 44: > #define TRUE (0==0) ! 45: > #define FALSE (0==1) ! 46: 52 c 31,37 ! 47: < typedef union { ! 48: --- ! 49: > typedef char bool; ! 50: > typedef unsigned char uchar; ! 51: > typedef unsigned int uint; ! 52: > typedef unsigned short ushort; ! 53: > typedef unsigned long ulong; ! 54: > ! 55: > typedef union { ! 56: 59 d 43 ! 57: < /* Link tables keep track of whether links have been properly preserved. */ ! 58: 65 c 49 ! 59: < uint_t l_nlink; ! 60: --- ! 61: > uint l_nlink; ! 62: 74 a 59 ! 63: > ! 64: 77 a 63 ! 65: > extern int errno; ! 66: 125 a 112,113 ! 67: > char *dot = "."; ! 68: > char *dotdot = ".."; ! 69: 130 a 119 ! 70: > char *usage = "usage: cpdir [options] dir1 dir2"; ! 71: 136 a 126 ! 72: > char *nomemory = "out of memory"; ! 73: 147 a 138 ! 74: > long lseek(); ! 75: 149,154 c 140,141 ! 76: < bool dirchecks(); ! 77: < bool suppress(); ! 78: < LINK *linklocate(); ! 79: < LINK *linkinstall(); ! 80: < bool linkattempt(); ! 81: < bool tgtunlink(); ! 82: --- ! 83: > char *realloc(); ! 84: > char *rindex(); ! 85: 156 a 144 ! 86: > bool suppress(); ! 87: 157 a 146,150 ! 88: > bool dirchecks(); ! 89: > bool linkattempt(); ! 90: > bool tgtunlink(); ! 91: > LINK *linklocate(); ! 92: > LINK *linkinstall(); ! 93: 168 c 161 ! 94: < return exitval; ! 95: --- ! 96: > return (exitval); ! 97: 177,178 c 170,173 ! 98: < if ((cp = *++av) == NULL) ! 99: < usage(); ! 100: --- ! 101: > if ((cp = *++av) == NULL) { ! 102: > errprint(fmt1, usage); ! 103: > exit(1); ! 104: > } ! 105: 220 c 215,216 ! 106: < usage(); ! 107: --- ! 108: > errprint(fmt1, usage); ! 109: > exit(1); ! 110: 225,226 c 221,225 ! 111: < if ((dir1=*av++) == NULL || (dir2=*av++) == NULL || *av != NULL) ! 112: < usage(); ! 113: --- ! 114: > if ((dir1=*av++) == NULL || (dir2=*av++) == NULL || *av != NULL) { ! 115: > errprint(fmt1, usage); ! 116: > exit(1); ! 117: > } ! 118: > return; ! 119: 272,275 c 271,278 ! 120: < if (stat(dir1, &srcstat) < 0) ! 121: < fatal(fmt2, dir1, nofind); ! 122: < if ((srcstat.st_mode & S_IFMT) != S_IFDIR) ! 123: < fatal(fmt2, dir1, notdir); ! 124: --- ! 125: > if (stat(dir1, &srcstat) < 0) { ! 126: > errprint(fmt2, dir1, nofind); ! 127: > exit(1); ! 128: > } ! 129: > if ((srcstat.st_mode & S_IFMT) != S_IFDIR) { ! 130: > errprint(fmt2, dir1, notdir); ! 131: > exit(1); ! 132: > } ! 133: 279,282 c 282,289 ! 134: < if (stat(cp, &tgtstat) < 0) ! 135: < fatal(fmt2, dir2, "cannot find parent"); ! 136: < if (access(cp, AWRITE|ASRCH) < 0) ! 137: < fatal(fmt2, dir2, nomkdir); ! 138: --- ! 139: > if (stat(cp, &tgtstat) < 0) { ! 140: > errprint(fmt2, dir2, "cannot find parent"); ! 141: > exit(1); ! 142: > } ! 143: > if (access(cp, AWRITE|ASRCH) < 0) { ! 144: > errprint(fmt2, dir2, nomkdir); ! 145: > exit(1); ! 146: > } ! 147: 291 a 299 ! 148: > return; ! 149: 296,300 c 304,307 ! 150: < * Assume: ! 151: < * Source is a directory we can read and search. Srcstat contains its status. ! 152: < * Target is a directory we can write and search. Tgtstat contains its status. ! 153: < * Src_dev and tgt_dev are the devices of the parents of source and target. ! 154: < * If tflag is on target may not exist. In this case tgtstat.st_dev ! 155: --- ! 156: > * Assume: Source is a directory we can read and search. Srcstat contains its ! 157: > * status. Target is a directory we can write and search. Tgtstat contains its ! 158: > * status. Src_dev and tgt_dev are the devices of the parents of source and ! 159: > * target. If tflag is on target may not exist. In this case tgtstat.st_dev ! 160: 370,371 c 377,378 ! 161: < !strcmp(dbp->db_name, ".") || ! 162: < !strcmp(dbp->db_name, "..")) ! 163: --- ! 164: > !strcmp(dbp->db_name, dot) || ! 165: > !strcmp(dbp->db_name, dotdot)) ! 166: 426 c 433 ! 167: < "cannot reopen, copy incomplete"); ! 168: --- ! 169: > "cannot reopen, copy incomplete"); ! 170: 441 a 449 ! 171: > return; ! 172: 466 d 473 ! 173: < LINK *lp; ! 174: 473 c 480 ! 175: < if (uflag && stat(target, &tgtstat) == 0) ! 176: --- ! 177: > if (uflag && stat(target, &tgtstat) == 0) ! 178: 475,478 d 481 ! 179: < /* ! 180: < * Target exists and is not older, do not update. ! 181: < * But watch out for links and keep the link counts sane. ! 182: < */ ! 183: 481,492 d 483 ! 184: < if (srcstat.st_nlink > 1) { ! 185: < if ((lp = linklocate(SOURCE)) == NULL) ! 186: < linkinstall(SOURCE); ! 187: < else if (lp->l_nlink == 0) ! 188: < linkpurge(SOURCE, lp); ! 189: < } ! 190: < if (tgtstat.st_nlink > 1) { ! 191: < if ((lp = linklocate(TARGET)) == NULL) ! 192: < linkinstall(TARGET); ! 193: < else if (lp->l_nlink == 0) ! 194: < linkpurge(TARGET, lp); ! 195: < } ! 196: 547 c 538 ! 197: < return FALSE; ! 198: --- ! 199: > return (FALSE); ! 200: 558 c 549 ! 201: < return FALSE; ! 202: --- ! 203: > return (FALSE); ! 204: 574 c 565 ! 205: < return FALSE; ! 206: --- ! 207: > return (FALSE); ! 208: 590,591 c 581,582 ! 209: < else ! 210: < vprintf(); ! 211: --- ! 212: > else vprintf(); ! 213: > return; ! 214: 619 a 611 ! 215: > return; ! 216: 636,637 c 628,631 ! 217: < if ((source = realloc(source, a+b)) == NULL) ! 218: < nomemory(); ! 219: --- ! 220: > if ((source = realloc(source, a+b)) == NULL) { ! 221: > errprint(fmt1, nomemory); ! 222: > exit(1); ! 223: > } ! 224: 646,647 c 640,643 ! 225: < if ((target = realloc(target, a+b)) == NULL) ! 226: < nomemory(); ! 227: --- ! 228: > if ((target = realloc(target, a+b)) == NULL) { ! 229: > errprint(fmt1, nomemory); ! 230: > exit(1); ! 231: > } ! 232: 667 a 664 ! 233: > return; ! 234: 690 c 687 ! 235: < return sum; ! 236: --- ! 237: > return (sum); ! 238: 704 a 702 ! 239: > return; ! 240: 707,713 d 704 ! 241: < fatal(arg) ! 242: < char *arg[]; ! 243: < { ! 244: < errprint("%r", &arg); ! 245: < exit(1); ! 246: < } ! 247: < ! 248: 722,724 c 713,717 ! 249: < if ((cp = malloc(n)) == NULL) ! 250: < nomemory(); ! 251: < return cp; ! 252: --- ! 253: > if ((cp = malloc(n)) == NULL) { ! 254: > errprint(fmt1, nomemory); ! 255: > exit(1); ! 256: > } ! 257: > return (cp); ! 258: 740 a 734,736 ! 259: > register int n; ! 260: > static int status; ! 261: > ! 262: 743 c 739 ! 263: < return FALSE; ! 264: --- ! 265: > return (FALSE); ! 266: 749 c 745 ! 267: < return FALSE; ! 268: --- ! 269: > return (FALSE); ! 270: 753 c 749 ! 271: < return FALSE; ! 272: --- ! 273: > return (FALSE); ! 274: 755 c 751 ! 275: < return TRUE; ! 276: --- ! 277: > return (TRUE); ! 278: 760 c 756 ! 279: < return TRUE; ! 280: --- ! 281: > return (TRUE); ! 282: 765 c 761 ! 283: < return FALSE; ! 284: --- ! 285: > return (FALSE); ! 286: 770 c 766 ! 287: < return FALSE; ! 288: --- ! 289: > return (FALSE); ! 290: 772 c 768 ! 291: < return TRUE; ! 292: --- ! 293: > return (TRUE); ! 294: 788 a 785 ! 295: > return; ! 296: 803 c 800 ! 297: < return FALSE; ! 298: --- ! 299: > return (FALSE); ! 300: 810 c 807 ! 301: < return TRUE; ! 302: --- ! 303: > return (TRUE); ! 304: 815 c 812 ! 305: < return FALSE; ! 306: --- ! 307: > return (FALSE); ! 308: 819 c 816 ! 309: < register uchar_t *cp; ! 310: --- ! 311: > register uchar *cp; ! 312: 821 c 818 ! 313: < register uint_t sum = 0; ! 314: --- ! 315: > register uint sum = 0; ! 316: 824 c 821 ! 317: < return sum % HASHSIZE; ! 318: --- ! 319: > return (sum % HASHSIZE); ! 320: 827,831 d 823 ! 321: < /* ! 322: < * Look for a LINK entry in the source or target link table. ! 323: < * If found, decrement the link count field and return a pointer to the LINK. ! 324: < * If not found, return NULL. ! 325: < */ ! 326: 842 c 834,835 ! 327: < } else { ! 328: --- ! 329: > } ! 330: > else { ! 331: 853 c 846 ! 332: < return lp; ! 333: --- ! 334: > return (lp); ! 335: 855 c 848 ! 336: < return NULL; ! 337: --- ! 338: > return (NULL); ! 339: 858,861 d 850 ! 340: < /* ! 341: < * Create a new LINK table entry and add it to the source or target LINK table. ! 342: < * Initialize its link count to nlinks - 1. ! 343: < */ ! 344: 892 c 881 ! 345: < return lp; ! 346: --- ! 347: > return (lp); ! 348: 895,897 d 883 ! 349: < /* ! 350: < * Purge LINK entry lp from the source or target LINK table. ! 351: < */ ! 352: 920 a 907 ! 353: > return; ! 354: 923,927 d 909 ! 355: < /* ! 356: < * The source file has a link count greater than 1. ! 357: < * If the source file is not in the source LINK list already, ! 358: < * add it to the list and return FALSE. ! 359: < */ ! 360: 935 d 916 ! 361: < ret = TRUE; ! 362: 937,938 c 918,919 ! 363: < linkinstall(SOURCE); /* add new source LINK */ ! 364: < return FALSE; ! 365: --- ! 366: > linkinstall(SOURCE); ! 367: > return (FALSE); ! 368: 942 c 923 ! 369: < if (tflag) { /* test only */ ! 370: --- ! 371: > if (tflag) { ! 372: 947 c 928,929 ! 373: < } else ! 374: --- ! 375: > } ! 376: > else { ! 377: 949 c 931,934 ! 378: < } else if (link(cp, target) == 0) { /* link succeeded */ ! 379: --- ! 380: > ret = TRUE; ! 381: > } ! 382: > } ! 383: > else if (link(cp, target) == 0) { ! 384: 951 c 936,938 ! 385: < } else { /* link failed */ ! 386: --- ! 387: > ret = TRUE; ! 388: > } ! 389: > else { ! 390: 959 c 946 ! 391: < return ret; ! 392: --- ! 393: > return (ret); ! 394: 962,966 d 948 ! 395: < /* ! 396: < * Unlink the target. ! 397: < * Return TRUE if the unlink is successful (or target does not exist) ! 398: < * or FALSE if the unlink fails. ! 399: < */ ! 400: 973 c 955 ! 401: < return TRUE; ! 402: --- ! 403: > return (TRUE); ! 404: 976,979 c 958 ! 405: < return FALSE; ! 406: < } else if ((tgtstat.st_mode & S_IFMT) != (srcstat.st_mode & S_IFMT)) { ! 407: < errprint(fmt3, source, "file type mismatch with ", target); ! 408: < return FALSE; ! 409: --- ! 410: > return (FALSE); ! 411: 980 a 960,963 ! 412: > else if ((tgtstat.st_mode & S_IFMT) != (srcstat.st_mode & S_IFMT)) { ! 413: > errprint(fmt3, source, "file type mismatch with ", target); ! 414: > return (FALSE); ! 415: > } ! 416: 981 a 965,969 ! 417: > if (!tflag) ! 418: > if (unlink(target) < 0) { ! 419: > errprint(fmt2, target, nounlink); ! 420: > return (FALSE); ! 421: > } ! 422: 984,987 d 971 ! 423: < else if (unlink(target) < 0) { ! 424: < errprint(fmt2, target, nounlink); ! 425: < return FALSE; /* unlink failed */ ! 426: < } ! 427: 989,990 c 973 ! 428: < /* The unlink succeeded, decrement the target link count. */ ! 429: < if ((lp = linklocate(TARGET)) != NULL) { /* target LINK exists */ ! 430: --- ! 431: > if ((lp = linklocate(TARGET)) != NULL) { ! 432: 993,995 c 976,979 ! 433: < } else if (tgtstat.st_nlink > 1) ! 434: < linkinstall(TARGET); /* add new target LINK */ ! 435: < return TRUE; ! 436: --- ! 437: > } ! 438: > else if (tgtstat.st_nlink > 1) ! 439: > linkinstall(TARGET); ! 440: > return (TRUE); ! 441: 998,1001 d 981 ! 442: < /* ! 443: < * Report on link botches. ! 444: < * This information is not known until the actual copying is finished. ! 445: < */ ! 446: 1026 a 1007 ! 447: > return; ! 448: 1048 a 1030 ! 449: > return; ! 450: 1073 c 1055 ! 451: < return ret; ! 452: --- ! 453: > return (ret); ! 454: 1086 c 1068 ! 455: < return "."; ! 456: --- ! 457: > return (dot); ! 458: 1089 c 1071 ! 459: < return ret; ! 460: --- ! 461: > return (ret); ! 462: 1097 d 1078 ! 463: < ! 464: 1100,1101 c 1081,1082 ! 465: < return FALSE; ! 466: < return TRUE; ! 467: --- ! 468: > return (FALSE); ! 469: > return (TRUE); ! 470: 1142,1154 d 1122 ! 471: < ! 472: < usage() ! 473: < { ! 474: < fprintf(stderr, "Usage: cpdir [options] dir1 dir2\n"); ! 475: < exit(1); ! 476: < } ! 477: < ! 478: < nomemory() ! 479: < { ! 480: < fatal(fmt1, "out of memory"); ! 481: < } ! 482: < ! 483: < /* end of cpdir.c */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.