/*---------------------------------------------------------------------------*/ int __fastcall__ pfs_opendir(struct cfs_dir *dirp, register const char *name) { static int fd; static char buf[2 + 1] = "$"; /* Set up the actual file name that is sent to the DOS. ** We accept "0:", "1:", "/", and "." as directory names. */ if(name == NULL || name[0] == '\0' || (name[0] == '.' || name[0] == '/') && name[1] == '\0') { buf[1] = '\0'; } else if((name[0] == '0' || name[0] == '1') && name[1] == ':' && name[2] == '\0') { buf[1] = name[0]; buf[2] = '\0'; } else { return -1; } /* Open the directory on a disk, for reading. */ fd = pfs_open(buf, CFS_READ); if(fd >= 0) { ((DIR *)dirp)->fd = fd; /* Skip the load address. */ if(_pfs_dirread(dirp, buf + 1, 2)) { return 0; } else { pfs_close(fd); } } return -1; }
int main(int argc, char *argv[]) { int ifdes, fdes; int err_value; char input_fname[20]; char *buf; ssize_t nread; struct pfs_stat mystat; int cache_hit; // Initialize the client initialize(argc, argv); // the command line arguments include an input filename if (argc < 2) { printf("usage: a.out <input filename>\n"); exit(0); } strcpy(input_fname, argv[1]); ifdes = open(input_fname, O_RDONLY); buf = (char *)malloc(4*ONEKB); nread = pread(ifdes, (void *)buf, 3*ONEKB,8*ONEKB); // All the clients open the pfs file fdes = pfs_open("pfs_file1", 'w'); if(fdes < 0) { printf("Error opening file\n"); exit(0); } //At client 3: print the file metadata pfs_fstat(fdes, &mystat); printf("File Metadata:\n"); printf("Time of creation: %s\n", ctime(&(mystat.pst_ctime))); printf("Time of last modification: %s\n", ctime(&(mystat.pst_mtime))); printf("File Size: %d\n", mystat.pst_size); //Write the next 3 kbytes of data from the input file onto pfs_file err_value = pfs_write(fdes, (void *)buf, 3*ONEKB, 8*ONEKB, &cache_hit); printf("Wrote %d bytes to the file\n", err_value); err_value = pfs_read(fdes, (void *)buf, 2*ONEKB, ONEKB, &cache_hit); printf("Read %d bytes of data from the file\n", err_value); printf("%s\n",buf); pfs_close(fdes); pfs_delete("pfs_file1"); free(buf); close(ifdes); return 0; }
int main(int argc, char *argv[]) { int fdes; int num_misses=0; off_t myoff = 0; char *buf; int cache_hit, go_on=1; struct pfs_stat mystat; ssize_t nread; int file_size; // Open the file in the write mode fdes = pfs_open("file", 'w'); if(fdes < 0) { printf("Error opening file\n"); exit(0); } pfs_fstat(fdes, &mystat); file_size = mystat.pst_size; buf = (char*)malloc(200*ONEKB); while(go_on) { nread = pfs_read(fdes, (void *)buf, 100*ONEKB, myoff%, &cache_hit); if(!cahce_hit) num_misses++; printf("Read %d bytes from the file\n", nread); printf("Number of cache misses = %d\n", num_misses); if ( (myoff+(100*ONEKB)) > file_size) myoff=0; else myoff+=(100*ONEKB); printf("Read again? (1 or 0):"); scanf("%d", &go_on); } free(buf); pfs_close(fdes); }
int main2(int argc, char *argv[]) { argc=2;argv[1]="1"; initialize(argc,argv); int fdes; int err_value; off_t myoff; char *buf; int cache_hit, go_on; ssize_t nread; // Open the file in the write mode fdes = pfs_open("pfs_file1", "w"); if(fdes < 0) { printf("Error opening file\n"); exit(0); } //Do overlapping writes onto the same pfs_file from multiple clients buf = (char*)malloc(ONEKB); while(go_on) { printf("Enter text to be written to the file\ntext>"); scanf("%[^\n]s", buf); printf("Enter the offset:"); scanf("%d", &myoff); err_value = pfs_write(fdes, (void *)buf, strlen(buf), myoff, &cache_hit); printf("Wrote %d bytes to the file\n", err_value); printf("Enter more text? (1 or 0):"); scanf("%d", &go_on); } free(buf); pfs_close(fdes); }