|
|
1.1 ! root 1: /* @(#)holey.c 1.2 90/01/03 NFS Rev 2 Testsuite ! 2: * 1.3 Lachman ONC Test Suite source ! 3: * ! 4: * test read/write of holey files ! 5: */ ! 6: #include <sys/param.h> ! 7: #ifndef major ! 8: #include <sys/types.h> ! 9: #endif ! 10: #include <sys/stat.h> ! 11: #include <stdio.h> ! 12: #ifdef SVR3 ! 13: #include <fcntl.h> ! 14: #else ! 15: #include <sys/file.h> ! 16: #endif ! 17: ! 18: #ifndef L_INCR ! 19: #define L_INCR 1 ! 20: #define L_SET 0 ! 21: #endif ! 22: ! 23: #ifndef MIN ! 24: #define MIN(a, b) ((a) < (b) ? (a) : (b)) ! 25: #endif ! 26: ! 27: #define BUFSZ 8192 ! 28: #define FILESZ 70000 ! 29: #define DATASZ 4321 ! 30: #define HOLESZ 9012 ! 31: #define FILENM "holeyfile" ! 32: int Debug = 0; ! 33: char *Prog; ! 34: ! 35: main(argc, argv) ! 36: int argc; ! 37: char *argv[]; ! 38: { ! 39: int fd, i, tot, ct, sz, bytes, ret; ! 40: char *filenm = FILENM; ! 41: char buf[BUFSZ]; ! 42: extern int errno; ! 43: int filesz = FILESZ; ! 44: int datasz = DATASZ; ! 45: int holesz = HOLESZ; ! 46: extern long lseek(); ! 47: ! 48: Prog = argv[0]; ! 49: if (argc > 1 && !strcmp(argv[1], "-d")) { ! 50: Debug = 1; ! 51: argc--; ! 52: argv++; ! 53: } ! 54: if (argc > 5 || (argc > 1 && !strcmp(argv[1], "-h"))) { ! 55: fprintf(stderr, "\ ! 56: usage: %s [filename filesize datasize holesize]\n", Prog); ! 57: exit(1); ! 58: } ! 59: if (argc > 1 && strcmp(argv[1], "-")) ! 60: filenm = argv[1]; ! 61: if (argc > 2 && strcmp(argv[2], "-")) ! 62: filesz = atoi(argv[2]); ! 63: if (argc > 3 && strcmp(argv[3], "-")) ! 64: datasz = atoi(argv[3]); ! 65: if (argc > 4 && strcmp(argv[4], "-")) ! 66: holesz = atoi(argv[4]); ! 67: ! 68: umask(0); ! 69: if (datasz > BUFSZ) { ! 70: fprintf(stderr, "%s: datasize (%d) greater than maximum (%d)\n", ! 71: Prog, datasz, BUFSZ); ! 72: exit(1); ! 73: } ! 74: if ((fd = creat(filenm, 0666)) < 0) { ! 75: sprintf(buf, "%s: creat of %s", Prog, filenm); ! 76: perror(buf); ! 77: exit(1); ! 78: } ! 79: if (close(fd)) { ! 80: sprintf(buf, "%s: close of %s after creat", Prog, filenm); ! 81: perror(buf); ! 82: exit(1); ! 83: } ! 84: if ((fd = open(filenm, 2)) < 0) { ! 85: sprintf(buf, "%s: open of %s", Prog, filenm); ! 86: perror(buf); ! 87: exit(1); ! 88: } ! 89: for (i=0; i < BUFSZ / sizeof(int); i++) ! 90: ((int *)buf)[i] = i; ! 91: ! 92: for (sz = filesz; sz > 0; ) { ! 93: if (datasz || sz == 1) { ! 94: bytes = MIN(sz, datasz); ! 95: if (bytes == 0) ! 96: bytes = 1; ! 97: if ((ret = write(fd, buf, bytes)) != bytes) { ! 98: fprintf(stderr, "write ret %d (expect %d)\n", ! 99: ret, bytes); ! 100: if (errno) perror("write"); ! 101: exit(1); ! 102: } ! 103: sz -= bytes; ! 104: } ! 105: if (sz && holesz) { ! 106: bytes = MIN(sz - 1, holesz); ! 107: if (lseek(fd, bytes, L_INCR) == -1L) { ! 108: perror("lseek (write)"); ! 109: exit(1); ! 110: } ! 111: sz -= bytes; ! 112: } ! 113: } ! 114: if (lseek(fd, 0, L_SET) == -1L) { ! 115: perror("lseek (rewind)"); ! 116: exit(1); ! 117: } ! 118: ! 119: for (sz = filesz; sz > 0; ) { ! 120: if (datasz || sz == 1) { ! 121: bytes = MIN(sz, datasz); ! 122: if (bytes == 0) ! 123: bytes = 1; ! 124: sz -= bytes; ! 125: for ( ; bytes > 0; bytes -= ret) { ! 126: if (Debug) ! 127: fprintf(stderr, "\ ! 128: --data read: offset %d, sz = %d, bytes = %d\n", filesz - sz - bytes, sz, bytes); ! 129: if ((ret = read(fd, buf, bytes)) <= 0) { ! 130: fprintf(stderr, "\ ! 131: read (data) offset %d, sz = %d, bytes = %d (ret = %d), datasz = %d\n", ! 132: filesz - sz - bytes, sz, bytes, ret, datasz); ! 133: if (ret < 0) ! 134: perror("read"); ! 135: exit(1); ! 136: } ! 137: ct = bytes - (bytes % sizeof(int)); ! 138: if (Debug) ! 139: fprintf(stderr, "\ ! 140: ret = %d, ct = %d\n", ret, ct); ! 141: for (i=0; i < ct / sizeof(int); i++) { ! 142: if (((int *)buf)[i] != i) { ! 143: fprintf(stderr, "\ ! 144: bad data in %s\n", filenm); ! 145: exit(1); ! 146: } ! 147: } ! 148: } ! 149: } ! 150: if (sz && holesz) { ! 151: tot = MIN(holesz, sz - 1); ! 152: sz -= tot; ! 153: for (ct = 0; tot > 0; tot -= ret, ct += ret) { ! 154: bytes = MIN(tot, BUFSZ); ! 155: if (Debug) ! 156: fprintf(stderr, "\ ! 157: ++hole read: offset %d, sz = %d, tot = %d, bytes = %d\n", ! 158: filesz - sz - tot, sz, tot, bytes); ! 159: if ((ret = read(fd, buf, bytes)) <= 0) { ! 160: fprintf(stderr, "\ ! 161: read (hole) offset %d, sz = %d, bytes = %d (ret %d), holesz = %d\n", ! 162: filesz - sz - tot, sz, bytes, ret, holesz); ! 163: if (ret < 0) ! 164: perror("read"); ! 165: exit(1); ! 166: } ! 167: if (Debug) ! 168: fprintf(stderr, " ret = %d\n", ret); ! 169: for (i = 0; i < ret; i++) { ! 170: if (buf[i] != '\0') { ! 171: fprintf(stderr, "\ ! 172: non-zero data read back from hole (offset %d)\n", filesz - sz + ct + i); ! 173: exit(1); ! 174: } ! 175: } ! 176: } ! 177: } ! 178: } ! 179: printf("\nHoley file test ok\n"); ! 180: exit(0); ! 181: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.