version 1.3, 2005/02/25 06:34:24
|
version 1.4, 2005/02/25 10:35:55
|
|
|
#define MAXFNAME 255 | #define MAXFNAME 255 |
#define MAXFLEN 65536 | #define MAXFLEN 65536 |
| |
|
/* |
#define TVFS_MAIN | #define TVFS_MAIN |
|
*/ |
#ifdef TVFS_MAIN |
|
#include "cabinet_files.h" |
|
#include <fcntl.h> | #include <fcntl.h> |
#endif |
#include <stdlib.h> /* For _MAX_PATH definition */ |
|
#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 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++; |
|
|
/* mode and flags are not handled */ | /* 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]) |
|
|
} | } |
inode = tvfs_create(fname, 0, 0); | inode = tvfs_create(fname, 0, 0); |
} | } |
struct tvfs_fcb *handler; |
|
handler = malloc(sizeof(struct tvfs_fcb)); | handler = malloc(sizeof(struct tvfs_fcb)); |
handler->inode = inode; | handler->inode = inode; |
handler->pos=0; | handler->pos=0; |
|
|
int pos = handles[h]->pos; | int pos = handles[h]->pos; |
int size = files[inode]->bytes_used; | 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 */ |
/* Edge Case 1 : Request beyond boundary of file */ | /* Edge Case 1 : Request beyond boundary of file */ |
if (pos + len > size) { | if (pos + len > size) { |
|
|
} | } |
| |
void tvfs_free(){ | void tvfs_free(){ |
|
|
int inode; | int inode; |
|
|
|
#ifdef DEBUG |
|
printf("tvfs_free\n"); |
|
#endif |
|
|
for (inode=0; inode<nfiles; inode++) { | for (inode=0; inode<nfiles; inode++) { |
if (!files[inode]) | if (!files[inode]) |
continue; | continue; |
|
|
} | } |
} | } |
| |
|
/* Compare given file with given contents, return 0 on equal, else nonzero */ |
|
int tvfs_compare(const char *fname, const char *buf, int len){ |
|
#ifdef DEBUG |
|
printf("tvfs_compare called with %s, %d, %d\n", fname, buf, len); |
|
#endif |
|
|
|
return (memcmp(fname,buf,len)); |
|
/* doesnt check for out of bound */ |
|
} |
|
|
|
long tvfs_lseek(int h, long whither, int whence){ |
|
|
|
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) |
|
{ |
|
printf("tvfs_write called with %d, %d, %d\n", h, buf, len); |
|
return len; |
|
/* return -1 to simulate diskfull or some other error */ |
|
} |
|
|
|
|
| |
| |
| |
#ifdef TVFS_MAIN | #ifdef TVFS_MAIN |
|
|
const static char name_test_txt[] = "test.txt"; | const static char name_test_txt[] = "test.txt"; |
const static char file_test_txt[] = "This is a test. Don't Panic!"; | const static char file_test_txt[] = "This is a test. Don't Panic!"; |
|
const static int size_test_txt = sizeof(file_test_txt); |
| |
main(){ | main(){ |
int result; | int result; |
|
|
filebuf = malloc(MAXFLEN); | filebuf = malloc(MAXFLEN); |
| |
printf("Testing TVFS implementation, creating %s\n",name_test_txt); | printf("Testing TVFS implementation, creating %s\n",name_test_txt); |
result = tvfs_create( dummy_filename, file_test_txt, sizeof(file_test_txt)); |
result = tvfs_create( dummy_filename, file_test_txt, size_test_txt); |
result = tvfs_create( name_test_txt, file_test_txt,sizeof(file_test_txt)); |
result = tvfs_create( name_test_txt, file_test_txt,size_test_txt); |
printf("Created virtual file with inode %d\n",result); | printf("Created virtual file with inode %d\n",result); |
| |
/* This test failes because strcmp returns 0 incorrectly! */ | /* This test failes because strcmp returns 0 incorrectly! */ |
|
|
printf("Testing reading from file %s\n",name_test_txt); | printf("Testing reading from file %s\n",name_test_txt); |
result = tvfs_read(active_handler, filebuf, 9); | result = tvfs_read(active_handler, filebuf, 9); |
printf("Read _%s_\n", filebuf); | printf("Read _%s_\n", filebuf); |
if ( strcmp(filebuf,"This is a")) { |
if ( strcmp(filebuf,"This is a")) |
printf("File read check failed!\n"); | printf("File read check failed!\n"); |
} |
|
| |
printf("Testing sequential reading from file %s\n",name_test_txt); | printf("Testing sequential reading from file %s\n",name_test_txt); |
result = tvfs_read(active_handler, filebuf, 12); | result = tvfs_read(active_handler, filebuf, 12); |
printf("Read _%s_\n", filebuf); | printf("Read _%s_\n", filebuf); |
if ( strcmp(filebuf," test. Don't")) { |
if ( strcmp(filebuf," test. Don't")) |
printf("File read check failed!\n"); | printf("File read check failed!\n"); |
} |
|
| |
printf("Testing edge reading from file %s\n",name_test_txt); | printf("Testing edge reading from file %s\n",name_test_txt); |
result = tvfs_read(active_handler, filebuf, 20); |
result = tvfs_read(active_handler, filebuf, 42); |
printf("Read %d bytes - _%s_\n", result, filebuf); | printf("Read %d bytes - _%s_\n", result, filebuf); |
if ( result != 8 ) { |
if ( result != 8 ) |
printf("File read check failed!\n"); | printf("File read check failed!\n"); |
} |
|
|
|
| |
|
printf("Testing direct file compare and lseek of %s\n", name_test_txt); |
|
printf("Seeking to 0 - "); |
|
tvfs_lseek( active_handler, SEEK_SET, 0); |
|
printf("Reading and comparing file\n"); |
|
result = tvfs_read(active_handler, filebuf, size_test_txt); |
|
result = tvfs_compare( filebuf , 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(); | tvfs_free(); |
free(filebuf); | free(filebuf); |