Path: blob/master/user/testbin/fileonlytest/fileonlytest.c
734 views
/*1* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 20092* The President and Fellows of Harvard College.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12* 3. Neither the name of the University nor the names of its contributors13* may be used to endorse or promote products derived from this software14* without specific prior written permission.15*16* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829/*30* rmtest.c31*32* Tests file system synchronization by deleting an open file and33* then attempting to read it.34*35* This should run correctly when the file system assignment is complete.36*/3738#include <stdlib.h>39#include <stdio.h>40#include <string.h>41#include <unistd.h>42#include <err.h>4344// 23 Mar 2012 : GWA : BUFFER_COUNT must be even.4546#define BUFFER_COUNT 12847#define BUFFER_SIZE 1284849static int writebuf[BUFFER_SIZE];50static int readbuf[BUFFER_SIZE];5152int53main(int argc, char **argv)54{5556// 23 Mar 2012 : GWA : Assume argument passing is *not* supported.5758(void) argc;59(void) argv;60int i, j;61int fh, len;62off_t pos, target;6364const char * filename = "fileonlytest.dat";6566// 23 Mar 2012 : GWA : Test that open works.6768printf("Opening %s\n", filename);6970fh = open(filename, O_RDWR|O_CREAT|O_TRUNC);71if (fh < 0) {72err(1, "create failed");73}7475printf("Writing %d bytes.\n", BUFFER_SIZE * BUFFER_COUNT);7677// 23 Mar 2012 : GWA : Do the even-numbered writes. Test read() and78// lseek(SEEK_END).7980for (i = 0; i < BUFFER_COUNT / 2; i++) {81for (j = 0; j < BUFFER_SIZE; j++) {82writebuf[j] = i * 2 * j;83}84len = write(fh, writebuf, sizeof(writebuf));85if (len != sizeof(writebuf)) {86err(1, "write failed");87}8889// 23 Mar 2012 : GWA : Use lseek() to skip the odd guys.9091target = (i + 1) * 2 * sizeof(writebuf);92pos = lseek(fh, sizeof(writebuf), SEEK_END);93if (pos != target) {94err(1, "(even) lseek failed: %llu != %llu", pos, target);95}96}9798target = 0;99pos = lseek(fh, target, SEEK_SET);100if (pos != target) {101err(1, "(reset) lseek failed: %llu != %llu", pos, target);102}103104// 23 Mar 2012 : GWA : Do the odd-numbered writes. Test write() and105// lseek(SEEK_CUR).106107for (i = 0; i < BUFFER_COUNT / 2; i++) {108109// 23 Mar 2012 : GWA : Use lseek() to skip the even guys.110111target = ((i * 2) + 1) * sizeof(writebuf);112pos = lseek(fh, sizeof(writebuf), SEEK_CUR);113if (pos != target) {114err(1, "(odd) lseek failed: %llu != %llu", pos, target);115}116117for (j = 0; j < BUFFER_SIZE; j++) {118writebuf[j] = ((i * 2) + 1) * j;119}120len = write(fh, writebuf, sizeof(writebuf));121if (len != sizeof(writebuf)) {122err(1, "write failed");123}124}125126// 23 Mar 2012 : GWA : Read the test data back and make sure what we wrote127// ended up where we wrote it. Tests read() and lseek(SEEK_SET).128129printf("Verifying write.\n");130131for (i = BUFFER_COUNT - 1; i >= 0; i--) {132target = i * sizeof(writebuf);133pos = lseek(fh, target, SEEK_SET);134if (pos != target) {135err(1, "(verify) lseek failed: %llu != %llu", pos, target);136}137len = read(fh, readbuf, sizeof(readbuf));138if (len != sizeof(readbuf)) {139err(1, "read failed");140}141for (j = BUFFER_SIZE - 1; j >= 0; j--) {142if (readbuf[j] != i * j) {143err(1, "read mismatch: pos=%llu, readbuf[j]=%d, i*j=%d, i=%d, j=%d", pos, readbuf[j], i * j, i, j);144}145}146}147148// 23 Mar 2012 : GWA : Close the file.149150printf("Closing %s\n", filename);151close(fh);152153// 23 Mar 2012 : GWA : Make sure the file is actually closed.154155pos = lseek(fh, (off_t) 0, SEEK_SET);156if (pos == 0) {157err(1, "seek after close succeeded");158}159160// 23 Mar 2012 : GWA : FIXME : Spin until exit() works.161162printf("Spinning in case exit() doesn't work.\n");163while (1) {};164165return 0;166}167168169