/* * cmp(): * File Compare... * Use the standard TFS facilities to compare two different incoming files. * Return 0 if files match, else -1. */ int cmp(char *f1, char *f2) { TFILE finfo1, *tfp1; TFILE finfo2, *tfp2; int fd1, fd2, size, ret; char *buf1, *buf2; /* Check sizes first: */ tfp1 = &finfo1; if (mon_tfsfstat(f1,tfp1) == -1) die(); tfp2 = &finfo2; if (mon_tfsfstat(f2,tfp2) == -1) die(); if (tfp1->filsize != tfp2->filsize) return(-1); /* Copy f1 to buffer: */ buf1 = mon_malloc(TFS_SIZE(tfp1)); if (!buf1) die(); fd1 = mon_tfsopen(f1,TFS_RDONLY,0); if (fd1 < 0) tfsdie(fd1); size = mon_tfsread(fd1,buf1,TFS_SIZE(tfp1)); if (size != TFS_SIZE(tfp1)) tfsdie(size); ret = mon_tfsclose(fd1,0); if (ret != TFS_OKAY) tfsdie(ret); /* Copy f2 to buffer: */ buf2 = mon_malloc(TFS_SIZE(tfp2)); if (!buf2) die(); fd2 = mon_tfsopen(f2,TFS_RDONLY,0); if (fd2 < 0) tfsdie(fd2); size = mon_tfsread(fd2,buf2,TFS_SIZE(tfp2)); if (size != TFS_SIZE(tfp2)) tfsdie(size); ret = mon_tfsclose(fd2,0); if (ret != TFS_OKAY) tfsdie(ret); /* Compare the buffers: */ if (memcmp(buf1,buf2,TFS_SIZE(tfp2))) ret = -1; else ret = 0; mon_free(buf1); mon_free(buf2); return(ret); }
/* * trunctest(): * Test file truncation. Verify that file truncates to expected size * and that the data in the file is as expected. */ int trunctest(char *fname, char *data) { int tfd, err, size; TFILE *tfp; /* Copy incoming file name to a tempoary file... */ cp(TMPFILE,fname); /* Open the temporary file and truncate it. * First verify that a truncation size too big will fail, then * do a real truncation. Close the file then verify that the * file has been trunated. */ tfd = mon_tfsopen(TMPFILE,TFS_APPEND,buffer1); if (tfd < 0) tfsdie(tfd); err = mon_tfstruncate(tfd,9999999); if (err != TFSERR_BADARG) tfsdie(err); err = mon_tfstruncate(tfd,TRUNCATE_SIZE); if (err != TFS_OKAY) tfsdie(err); err = mon_tfsclose(tfd,0); if (err < 0) tfsdie(err); /* Make sure that the file was truncated to the proper size. */ tfp = mon_tfsstat(TMPFILE); if (!tfp) die(); if (TFS_SIZE(tfp) != TRUNCATE_SIZE) die(); /* Now reopen the file and verify that the data is correct... */ tfd = mon_tfsopen(TMPFILE,TFS_RDONLY,0); if (tfd < 0) tfsdie(tfd); size = mon_tfsread(tfd,buffer1,TFS_SIZE(tfp)); if (size != TFS_SIZE(tfp)) tfsdie(size); if (memcmp(buffer1,data,TRUNCATE_SIZE)) die(); /* Close and remove the temporary file. */ mon_tfsclose(tfd,0); err = mon_tfsunlink(TMPFILE); if (err != TFS_OKAY) tfsdie(err); return(0); }
/* appendtest(): * Create a file, then append to it and verify content after * closing the file. */ int appendtest(char *fname, char *data1, char *data2) { TFILE finfo, *tfp; int err, tfd, len; /* Create the first version of the file: */ err = mon_tfsadd(fname,0,0,data1,strlen(data1)); if (err != TFS_OKAY) tfsdie(err); /* Append to that first file: */ tfd = mon_tfsopen(fname,TFS_APPEND,buffer1); if (tfd < 0) tfsdie(tfd); err = mon_tfswrite(tfd,data2,strlen(data2)); if (err != TFS_OKAY) tfsdie(err); err = mon_tfsclose(tfd,0); if (err != TFS_OKAY) tfsdie(err); /* Verify that the file was properly appended to: */ tfp = &finfo; if (mon_tfsfstat(fname,tfp) == -1) die(); len = strlen(data1) + strlen(data2); if (len != TFS_SIZE(tfp)) die(); tfd = mon_tfsopen(fname,TFS_RDONLY,0); if (tfd < 0) tfsdie(tfd); err = mon_tfsread(tfd,buffer1,len); if (err != len) tfsdie(err); strcpy(buffer2,data1); strcat(buffer2,data2); if (memcmp(buffer1,buffer2,len) != 0) die(); mon_tfsclose(tfd,0); return(0); }
/* * Close the open tfs file. */ static int rtems_tfs_close( rtems_libio_t *iop ) { int fd; char *info; struct tfdinfo *tip; fd = (int)iop->data0; if (RTEMS_TFS_DEBUG) printk("rtems_tfs_close(%d)\n",fd); if ((fd < 3) || (fd >= MAXTFDS)) { rtems_set_errno_and_return_minus_one (EBADF); } tip = &tfdtable[fd]; if (tip->info[0]) info = tip->info; else info = (char *)0; mon_tfsclose(tip->tfd,info); if (tip->buf) free(tip->buf); newlib_tfdlock(); tip->inuse = 0; newlib_tfdunlock(); return RTEMS_SUCCESSFUL; }
/* * cp(): * File Copy... * Use standard TFS facilities to copy one file to another. * If successful, return the size of the copy; else die. */ int cp(char *to, char *from) { TFILE finfo, *tfp; int ffd, tfd, size, ret; char *buffer; /* Open the source file: */ ffd = mon_tfsopen(from,TFS_RDONLY,0); if (ffd < 0) tfsdie(ffd); /* Retrieve stats of the source file: */ tfp = &finfo; if (mon_tfsfstat(from,tfp) == -1) die(); /* The buffer used to open the destination file must be as large as * the source file ... */ buffer = mon_malloc(TFS_SIZE(tfp)); if (!buffer) die(); /* Open the destination file for creation with the same flags * as the source file: */ tfd = mon_tfsopen(to,TFS_CREATE | finfo.flags,buffer); if (tfd < 0) tfsdie(tfd); /* Read the entire source file into buffer, then write the entire * buffer to the destination file... */ size = mon_tfsread(ffd,buffer,TFS_SIZE(tfp)); if (size < 0) tfsdie(size); ret = mon_tfswrite(tfd,buffer,TFS_SIZE(tfp)); if (ret != TFS_OKAY) tfsdie(ret); mon_tfsclose(ffd,0); mon_tfsclose(tfd,finfo.info); mon_free(buffer); return(TFS_SIZE(tfp)); }
/* * inusetest(): * Open a file, then try to remove it or add a file with the same name. * This should fail... */ int inusetest(char *fname) { int tfd, err; /* Open a file, then try to run tfsadd() on it or try to remove it... */ tfd = mon_tfsopen(fname,TFS_RDONLY,0); if (tfd < 0) tfsdie(tfd); err = mon_tfsunlink(fname); if (err != TFSERR_FILEINUSE) tfsdie(err); err = mon_tfsadd(fname,0,0,buffer1,10); if (err != TFSERR_FILEINUSE) tfsdie(err); mon_tfsclose(tfd,0); return(0); }
int main(int argc,char *argv[]) { int i, tfd; char line[80], *fname; /* If argument count is greater than one, then dump out the * set of CLI arguments... */ if (argc > 1) { mon_printf("Argument list...\n"); for(i=0; i<argc; i++) mon_printf(" arg[%d]: %s\n",i,argv[i]); } /* If the shell variable "USE_THIS_FILE" exists, then use the * content of that shell variable as a filename; else use "monrc"... */ fname = mon_getenv("USE_THIS_FILE"); if (!fname) fname = "monrc"; /* If the file exists, the assume it is ASCII and dump it * line by line... */ if (mon_tfsstat(fname)) { mon_printf("Dumping content of '%s'...\n",fname); tfd = mon_tfsopen(fname,TFS_RDONLY,0); if (tfd >= 0) { while(mon_tfsgetline(tfd,line,sizeof(line))) mon_printf("%s",line); mon_tfsclose(tfd,0); } else { mon_printf("TFS error: %s\n", (char *)mon_tfsctrl(TFS_ERRMSG,tfd,0)); } } return(0); }
/* * getlinetest(): * The incoming filename is assumed to have been previously created with * the data pointed to by the incoming data pointer. * Lines are read from the file via tfsgetline() and the buffer is filled * with what should be the same data as is pulled in by tfsgetline(). */ void getlinetest(char *file, char *data) { int llen, tfd, err; tfd = mon_tfsopen(file,TFS_RDONLY,0); if (tfd < 0) tfsdie(tfd); /* Retrieve a line from the file: */ err = mon_tfsgetline(tfd,buffer1,sizeof(buffer1)); if (err < 0) tfsdie(err); /* Retrieve the same line from the data: */ for(llen=0;*data;llen++,data++) { buffer2[llen] = *data; if (*data == '\n') break; } buffer2[llen+1] = 0; /* They should match... */ if (memcmp(buffer1,buffer2,llen)) die(); /* Again... */ /* Retrieve a line from the file: */ err = mon_tfsgetline(tfd,buffer1,sizeof(buffer1)); if (err < 0) tfsdie(err); /* Retrieve the same line from the data: */ for(llen=0;*data;llen++,data++) { buffer2[llen] = *data; if (*data == '\n') break; } buffer2[llen+1] = 0; /* They should match... */ if (memcmp(buffer1,buffer2,llen)) die(); /* One more line... */ /* Retrieve a line from the file: */ err = mon_tfsgetline(tfd,buffer1,sizeof(buffer1)); if (err < 0) tfsdie(err); /* Retrieve the same line from the data: */ for(llen=0;*data;llen++,data++) { buffer2[llen] = *data; if (*data == '\n') break; } buffer2[llen+1] = 0; /* They should match... */ if (memcmp(buffer1,buffer2,llen)) die(); mon_tfsclose(tfd,0); }
/* * ctrltest(): * Test various aspects of the tfsctrl() function. */ void ctrltest(char *fname, char *data) { int tfd, ret; char flags[16]; /* Copy fname to TMPFILE... */ cp (TMPFILE,fname); /* Verify TFS_UNOPEN... * Open a file, modify it, then prior to calling tfsclose(), call * tfsctrl(TFS_UNOPEN) on that file descriptor and make sure the * file is in its original state and the file descriptor has been * closed... */ tfd = mon_tfsopen(fname,TFS_APPEND,buffer1); if (tfd < 0) tfsdie(tfd); ret = mon_tfswrite(tfd,"abcdefg",7); if (ret != TFS_OKAY) tfsdie(ret); ret = mon_tfsctrl(TFS_UNOPEN,tfd,0); if (ret != TFS_OKAY) tfsdie(ret); ret = mon_tfsclose(tfd,0); if (ret != TFSERR_BADFD) tfsdie(ret); if (cmp(TMPFILE,fname)) die(); /* Verify TFS_TELL... * Open a file, seek to a known point, then make sure that * tfsctrl(TFS_TELL) returns the expected offset. */ tfd = mon_tfsopen(fname,TFS_RDONLY,0); if (tfd < 0) tfsdie(tfd); ret = mon_tfsseek(tfd,5,TFS_BEGIN); if (ret != 5) tfsdie(ret); ret = mon_tfsctrl(TFS_TELL,tfd,0); if (ret != 5) tfsdie(ret); ret = mon_tfsread(tfd,buffer1,3); if (ret != 3) tfsdie(ret); ret = mon_tfsctrl(TFS_TELL,tfd,0); if (ret != 5+3) tfsdie(ret); ret = mon_tfsclose(tfd,0); if (ret != TFS_OKAY) tfsdie(ret); /* Test all "flag-ascii-to-binary" conversions... */ if ((ret = mon_tfsctrl(TFS_FATOB,(long)"e",0)) != TFS_EXEC) tfsdie(ret); if ((ret = mon_tfsctrl(TFS_FATOB,(long)"b",0)) != TFS_BRUN) tfsdie(ret); if ((ret = mon_tfsctrl(TFS_FATOB,(long)"B",0)) != TFS_QRYBRUN) tfsdie(ret); #if 0 if ((ret = mon_tfsctrl(TFS_FATOB,(long)"C",0)) != TFS_COFF) tfsdie(ret); if ((ret = mon_tfsctrl(TFS_FATOB,(long)"E",0)) != TFS_ELF) tfsdie(ret); if ((ret = mon_tfsctrl(TFS_FATOB,(long)"A",0)) != TFS_AOUT) tfsdie(ret); #else if ((ret = mon_tfsctrl(TFS_FATOB,(long)"E",0)) != TFS_EBIN) tfsdie(ret); #endif if ((ret = mon_tfsctrl(TFS_FATOB,(long)"c",0)) != TFS_CPRS) tfsdie(ret); if ((ret = mon_tfsctrl(TFS_FATOB,(long)"i",0)) != TFS_IPMOD) tfsdie(ret); if ((ret = mon_tfsctrl(TFS_FATOB,(long)"u",0)) != TFS_UNREAD) tfsdie(ret); if ((ret = mon_tfsctrl(TFS_FATOB,(long)"1",0)) != TFS_ULVL1) tfsdie(ret); if ((ret = mon_tfsctrl(TFS_FATOB,(long)"2",0)) != TFS_ULVL2) tfsdie(ret); if ((ret = mon_tfsctrl(TFS_FATOB,(long)"3",0)) != TFS_ULVL3) tfsdie(ret); /* Test all "flag-binary-to-ascii" conversions... */ ret = mon_tfsctrl(TFS_FBTOA,TFS_EXEC,(long)flags); if ((ret == TFSERR_BADARG) || (strcmp(flags,"e"))) tfsdie(ret); ret = mon_tfsctrl(TFS_FBTOA,TFS_BRUN,(long)flags); if ((ret == TFSERR_BADARG) || (strcmp(flags,"b"))) tfsdie(ret); ret = mon_tfsctrl(TFS_FBTOA,TFS_QRYBRUN,(long)flags); if ((ret == TFSERR_BADARG) || (strcmp(flags,"B"))) tfsdie(ret); #if 0 ret = mon_tfsctrl(TFS_FBTOA,TFS_COFF,(long)flags); if ((ret == TFSERR_BADARG) || (strcmp(flags,"C"))) tfsdie(ret); ret = mon_tfsctrl(TFS_FBTOA,TFS_ELF,(long)flags); if ((ret == TFSERR_BADARG) || (strcmp(flags,"E"))) tfsdie(ret); ret = mon_tfsctrl(TFS_FBTOA,TFS_AOUT,(long)flags); if ((ret == TFSERR_BADARG) || (strcmp(flags,"A"))) tfsdie(ret); #else ret = mon_tfsctrl(TFS_FBTOA,TFS_EBIN,(long)flags); if ((ret == TFSERR_BADARG) || (strcmp(flags,"E"))) tfsdie(ret); #endif ret = mon_tfsctrl(TFS_FBTOA,TFS_CPRS,(long)flags); if ((ret == TFSERR_BADARG) || (strcmp(flags,"c"))) tfsdie(ret); ret = mon_tfsctrl(TFS_FBTOA,TFS_IPMOD,(long)flags); if ((ret == TFSERR_BADARG) || (strcmp(flags,"i"))) tfsdie(ret); ret = mon_tfsctrl(TFS_FBTOA,TFS_UNREAD,(long)flags); if ((ret == TFSERR_BADARG) || (strcmp(flags,"u"))) tfsdie(ret); ret = mon_tfsctrl(TFS_FBTOA,TFS_ULVL1,(long)flags); if ((ret == TFSERR_BADARG) || (strcmp(flags,"1"))) tfsdie(ret); ret = mon_tfsctrl(TFS_FBTOA,TFS_ULVL2,(long)flags); if ((ret == TFSERR_BADARG) || (strcmp(flags,"2"))) tfsdie(ret); ret = mon_tfsctrl(TFS_FBTOA,TFS_ULVL3,(long)flags); if ((ret == TFSERR_BADARG) || (strcmp(flags,"3"))) tfsdie(ret); ret = mon_tfsunlink(TMPFILE); if (ret != TFS_OKAY) tfsdie(ret); }
/* writetest(): * Open the specified file in APPEND (modify) mode. Seek into the file * and read 1 byte. Increment that byte by one and then write it back * to the same location from which it was read. Then, close the file. * Build a new file that is what "should" be the content of the file we * just modified and compare the two files. They better match. * This function also verifies TFS_HEADROOM. */ int writetest(char *fname, char *newdata) { TFILE *tfp; char c; long headroom; int size, tfd, err; /* Open the source file: */ tfp = mon_tfsstat(fname); if (!tfp) die(); size = TFS_SIZE(tfp); tfd = mon_tfsopen(fname,TFS_APPEND,buffer1); if (tfd < 0) tfsdie(tfd); headroom = mon_tfsctrl(TFS_HEADROOM,tfd,0); if (headroom != 0) die(); err = mon_tfsseek(tfd,3,TFS_BEGIN); if (err != 3) tfsdie(err); headroom = mon_tfsctrl(TFS_HEADROOM,tfd,0); if (headroom != (size-3)) die(); err = mon_tfsread(tfd,&c,1); if (err != 1) tfsdie(err); headroom = mon_tfsctrl(TFS_HEADROOM,tfd,0); if (headroom != (size-4)) die(); c++; err = mon_tfsseek(tfd,-1,TFS_CURRENT); if (err != 3) tfsdie(err); headroom = mon_tfsctrl(TFS_HEADROOM,tfd,0); if (headroom != (size-3)) die(); err = mon_tfswrite(tfd,&c,1); if (err != TFS_OKAY) tfsdie(err); headroom = mon_tfsctrl(TFS_HEADROOM,tfd,0); if (headroom != (size-4)) die(); mon_tfsclose(tfd,0); /* Add a new file that "should" be identical to the modified file; * then compare them and delete the newfile... */ err = mon_tfsadd(TMPFILE,"newdata1","2",newdata,strlen(newdata)); if (err != TFS_OKAY) tfsdie(err); if (cmp(fname,TMPFILE) != 0) die(); err = mon_tfsunlink(TMPFILE); if (err != TFS_OKAY) tfsdie(err); return(0); }
/* seektest(): * Run some tests on tfsseek(). * Verify that the character in the file specified at the incoming offset * is as expected. Seek to beyond the end of file and verify error, etc... */ int seektest(char *fname, int offset, char value) { char c, buf1[16], buf2[16]; TFILE *tfp; int tfd, end, err; /* Open the source file: */ tfd = mon_tfsopen(fname,TFS_RDONLY,0); if (tfd < 0) tfsdie(tfd); if (mon_tfsseek(tfd,offset,TFS_BEGIN) != offset) tfsdie(tfd); if (mon_tfsread(tfd,&c,1) != 1) tfsdie(tfd); if (c != value) die(); tfp = mon_tfsstat(fname); if (!tfp) die(); end = TFS_SIZE(tfp); /* * Seek to various limits in the file and verify proper return * value... */ err = mon_tfsseek(tfd,0,TFS_BEGIN); if (err != 0) tfsdie(err); err = mon_tfsseek(tfd,end,TFS_CURRENT); if (err != end) tfsdie(err); err = mon_tfsseek(tfd,1,TFS_CURRENT); if (err != TFSERR_EOF) tfsdie(err); err = mon_tfsseek(tfd,end,TFS_BEGIN); if (err != end) tfsdie(err); err = mon_tfsseek(tfd,end+1,TFS_BEGIN); if (err != TFSERR_EOF) tfsdie(err); err = mon_tfsseek(tfd,0,TFS_BEGIN); if (err != 0) tfsdie(err); err = mon_tfsseek(tfd,-1,TFS_CURRENT); if (err != TFSERR_EOF) tfsdie(err); err = mon_tfsseek(tfd,end,TFS_BEGIN); if (err != end) tfsdie(err); err = mon_tfsseek(tfd,-1,TFS_CURRENT); if (err != end-1) tfsdie(err); err = mon_tfsseek(tfd,2,TFS_CURRENT); if (err != TFSERR_EOF) tfsdie(err); /* Seek to beginning, read 10, seek to beginning again read 10 again. * Verify that both reads have same data. */ err = mon_tfsseek(tfd,0,TFS_BEGIN); if (err != 0) tfsdie(err); err = mon_tfsread(tfd,buf1,10); if (err != 10) tfsdie(err); err = mon_tfsseek(tfd,0,TFS_BEGIN); if (err != 0) tfsdie(err); err = mon_tfsread(tfd,buf2,10); if (err != 10) tfsdie(err); if (memcmp(buf1,buf2,10)) die(); /* * Seek to end, then verify that read() returns EOF and tfs_eof() * returns true. */ err = mon_tfsseek(tfd,end,TFS_BEGIN); if (err != end) tfsdie(err); err = mon_tfsread(tfd,buf1,1); if (err != TFSERR_EOF) tfsdie(err); if (mon_tfseof(tfd) != 1) die(); mon_tfsclose(tfd,0); return(0); }