(file) Return to tvfs.c CVS log (file) (dir) Up to [RizwankCVS] / group3 / wine / dlls / cabinet / tests

Diff for /group3/wine/dlls/cabinet/tests/tvfs.c between version 1.1 and 1.7

version 1.1, 2005/02/21 07:24:47 version 1.7, 2005/02/27 05:21:08
Line 1 
Line 1 
 /*-------------------------------------------------------------------------- /*--------------------------------------------------------------------------
 Trivial virtual file system for cabinet conformance test. Trivial virtual file system for cabinet conformance test.
   (C) 2005 Dan Kegel and Rizwan Kassim
   LGPL License
 --------------------------------------------------------------------------*/ --------------------------------------------------------------------------*/
  
 #include "tvfs.h" #include "tvfs.h"
Line 12 
Line 14 
 #define MAXFNAME 255 #define MAXFNAME 255
 #define MAXFLEN 65536 #define MAXFLEN 65536
  
   /*
   #define TVFS_MAIN
   */
   #include <fcntl.h>
   #include <stdlib.h>
   #include <stdio.h>
   #include <malloc.h>
   /*
   #define DEBUG
   */
   #define SEEK_SET 0
   #define SEEK_CUR 1
   #define SEEK_END 2
   
 struct tvfs_file { struct tvfs_file {
        char fname[MAXFNAME];        char fname[MAXFNAME];
        int bytes_used;        int bytes_used;
Line 27 
Line 43 
 static struct tvfs_fcb *handles[MAXHANDLES]; static struct tvfs_fcb *handles[MAXHANDLES];
 int nhandles = 0; int nhandles = 0;
  
   /* tvfs_create does NOT correspond to _creat - it is an internal function
   use to put a file directly into our virtual filesystem. */
   
 int tvfs_create(const char *fname, const char *buf, int len) int tvfs_create(const char *fname, const char *buf, int len)
 { {
        int inode;        int inode;
        struct tvfs_file *f;        struct tvfs_file *f;
   
                   #ifdef DEBUG
                   printf("tvfs_create called with %s, %d, %d\n", fname, buf, len);
                   #endif
   
        if (nfiles >= MAXFILES)        if (nfiles >= MAXFILES)
                return -1;                return -1;
   
        inode = nfiles++;        inode = nfiles++;
        f = calloc(sizeof(struct tvfs_file));                  /* calloc didn't work here, while malloc did */
              f = malloc(sizeof(struct tvfs_file));
        strcpy(f->fname, fname);        strcpy(f->fname, fname);
        f->len = len;         f->bytes_used = len;
        if (buf)        if (buf)
                memcpy(f->buf, buf, len);                memcpy(f->buf, buf, len);
        files[inode] = f;        files[inode] = f;
Line 46 
Line 70 
  
 int tvfs_open(const char *fname, int flags, int mode) int tvfs_open(const char *fname, int flags, int mode)
 { {
   
                   /* mode and flags are not handled */
        int h;        int h;
        int inode;        int inode;
              struct tvfs_fcb *handler;
   
           #ifdef DEBUG
           printf("tvfs_open called with %s, %d, %d\n", fname, flags, mode);
           #endif
   
        /* Existing file? */        /* Existing file? */
        for (inode=0; inode<nfiles; inode++) {        for (inode=0; inode<nfiles; inode++) {
                if (!files[inode])                if (!files[inode])
                        continue;                        continue;
                if (strcmp(files[inode].fname, fname))                 if (!strcmp(files[inode]->fname, fname))
                        break;                                                  break;       }
        }  
        if (inode == nfiles) {        if (inode == nfiles) {
                /* File did not exist */                /* File did not exist */
                if ((flags & O_CREAT) == 0) {                if ((flags & O_CREAT) == 0) {
                        /* ENOENT */                        /* ENOENT */
           #ifdef DEBUG
           printf("tvfs_open returning -1\n");
           #endif
                        return -1;                        return -1;
                }                }
                inode = tvfs_create(fname, 0, 0);                inode = tvfs_create(fname, 0, 0);
        }        }
        h = nhandles++;             handler = malloc(sizeof(struct tvfs_fcb));
        handles[h].inode = inode;             handler->inode = inode;
        handles[h].pos = 0;             handler->pos=0;
              h = nfiles++;
              handles[h] = handler;
           #ifdef DEBUG
           printf("tvfs_open returning with %d\n", h);
           #endif
   
        return h;        return h;
 } }
  
 unsigned int tvfs_read(int h, void *buf, unsigned int len) unsigned int tvfs_read(int h, void *buf, unsigned int len)
 { {
        int inode = handles[h].inode;         int inode = handles[h]->inode;
        int pos = handles[h].pos;         int pos = handles[h]->pos;
              int size = files[inode]->bytes_used;
   
           #ifdef DEBUG
           printf("tvfs_read called with %d, %d, %d\n", h, buf, len);
           #endif
  
        /* FIXME: handle edge cases */        /* FIXME: handle edge cases */
        memcpy(buf, files[inode].buf, len);             /* Edge Case 1 : Request beyond boundary of file */
        handles[h].pos += len;             if (pos + len > size) {
                      len = size-pos;
              }
   
          memcpy(buf, files[inode]->buf+pos, len);
          handles[h]->pos += len;
  
        return len;        return len;
 } }
   
   void tvfs_free(){
   
           int inode;
   
           #ifdef DEBUG
           printf("tvfs_free\n");
           #endif
   
           nfiles=0;
           nhandles=0;
   
       for (inode=0; inode<nfiles; inode++) {
                           if (!files[inode])
                                   continue;
                           free(files[inode]);
                   }
           }
   
   /* Compare given file with given contents, return 0 on equal, else nonzero */
   int tvfs_compare(const char *fname, const char *buf, int len){
   
           int inode;
   
           #ifdef DEBUG
           printf("tvfs_compare called with %s, %d, %d\n", fname, buf, len);
           #endif
   
           for (inode=0; inode<nfiles; inode++) {
           if (!files[inode])
           continue;
       if (!strcmp(files[inode]->fname, fname))
                   break;       }
       if (inode == nfiles) {
                  /* File did not exist */
                   #ifdef DEBUG
                   printf("tvfs_compare returning -1 (FAILURE)\n");
                   #endif
           return -1;
       }
   
           return (memcmp(files[inode]->buf,buf,len));
           /* doesnt check for out of bound */
   }
   
   long tvfs_lseek(int h, long whence, int whither ){
   
         int inode = handles[h]->inode;
             int size = files[inode]->bytes_used;
           #ifdef DEBUG
           printf("tvfs_lseek called with %d, %d, %d\n", h, whither, whence);
           #endif
   /*                if (whence > size)
                             whence = size;*/
   /*                      Legit lseek does NOT do boundry checking */
   
             switch(whither) {
                     case SEEK_SET: {
                             handles[h]->pos = whence;
                             break;
                     }
                     case SEEK_CUR: {
                             handles[h]->pos += whence;
                             break;
                     }
                     case SEEK_END: {
                             handles[h]->pos = size+whence;
                             break;
                     }
                     case 5: {
                             handles[h]->pos = size+whence;
                             break;
                     }
                     case 44: {
                             handles[h]->pos = size+whence;
                             break;
                     }
                     default:
                     {
                             printf("lseek was called with an invalid whither %d\n",whither);
                             return -1;
                     }
             }
             return handles[h]->pos;
     }
   
   int tvfs_close(int h){
            int inode = handles[h]->inode;
           #ifdef DEBUG
           printf("tvfs_close called with %d\n", h);
           #endif
            if (!files[inode]){
                    return -1;
            }
            free(handles[h]);
            /* Currently does NOT enabled open to reuse this handle */
            return 0;
    }
   
   unsigned int tvfs_write(int h, void *buf, unsigned int len)
   {
   
      int inode = handles[h]->inode;
      int pos = handles[h]->pos;
   
           printf("tvfs_write called with %d, %d, %d\n", h, buf, len);
   
           memcpy(files[inode]->buf+pos, buf, len);
           files[inode]->bytes_used += len;
   
           return len;
           /* return -1 to simulate diskfull or some other error */
   }
   
   
   
   
   
   #ifdef TVFS_MAIN
   
   const static char name_test_txt[] = "test.txt";
   const static char file_test_txt[] = "This is a test. Don't Panic!";
   const static int size_test_txt = sizeof(file_test_txt);
   
   main(){
           int result;
           int active_handler;
   
           char *filebuf;
   
           char dummy_filename[] = "dummy.txt";
           char bad_filename[] = "chicken.txt";
   
           filebuf = malloc(MAXFLEN);
   
           printf("Testing TVFS implementation, creating %s\n",name_test_txt);
           result = tvfs_create( dummy_filename, file_test_txt, size_test_txt);
           result = tvfs_create( name_test_txt, file_test_txt,size_test_txt);
           printf("Created virtual file with inode %d\n",result);
   
           /* This test failes because strcmp returns 0 incorrectly! */
           printf("Attempting to open non-existant file\n");
           result = tvfs_open(bad_filename, _O_BINARY, 0 );
           printf("Result code %d\n",result);
           printf("Attempting to open existant file\n");
           result = tvfs_open(name_test_txt, _O_BINARY, 0 );
           printf("Result code %d\n",result);
   
           active_handler = result;
   
           memset (filebuf,0,MAXFLEN);
   
           printf("Testing reading from file %s\n",name_test_txt);
           result = tvfs_read(active_handler, filebuf, 9);
           printf("Read _%s_\n", filebuf);
           if ( strcmp(filebuf,"This is a"))
                   printf("File read check failed!\n");
   
           printf("Testing sequential reading from file %s\n",name_test_txt);
           result = tvfs_read(active_handler, filebuf, 12);
           printf("Read _%s_\n", filebuf);
           if ( strcmp(filebuf," test. Don't"))
                   printf("File read check failed!\n");
   
           printf("Testing edge reading from file %s\n",name_test_txt);
           result = tvfs_read(active_handler, filebuf, 42);
           printf("Read %d bytes - _%s_\n", result, filebuf);
           if ( result != 8 )
                   printf("File read check failed!\n");
   
           printf("Testing direct file compare and lseek of %s\n", name_test_txt);
           printf("Seeking to 0 (not needed) - ");
           tvfs_lseek( active_handler, SEEK_SET, 0);
           printf("Comparing file\n");
           result = tvfs_compare( name_test_txt , file_test_txt, size_test_txt);
           if (result)
                   printf ("File compare failed!\n");
   
   
           printf("Closing %s\n",name_test_txt);
           tvfs_close(active_handler);
           if (result)
                   printf ("File close failed!\n");
   
           tvfs_free();
           free(filebuf);
   
   }
   
   #endif


Legend:
Removed from v.1.1  
changed lines
  Added in v.1.7

Rizwan Kassim
Powered by
ViewCVS 0.9.2