Anda di halaman 1dari 6

// FileSystem class // written by: Anthony Hume // Purpose: This class is responsible for maintaining the metadata of the

the // files on the hardrive as well as determining where new data should // be written. public class FileSystem extends HardDrive { public static final int max_files = 300; MyFile[] current_files; int num_of_files; int current_index; double total_write_time; double total_read_time; int scheme_type; int extent_size; public FileSystem(int scheme_type) { current_files = new MyFile[max_files]; num_of_files = 0; current_index = 0; total_write_time = 0; total_read_time = 0; sectors_remaining = total_sectors; this.scheme_type = scheme_type; extent_size = 10; } public int P4_fopen(String filename) // Creates a new file and adds in to current_files { if (sectors_remaining == 0) { System.out.println("There are no remaining sectors on the disk\n"); return -1; } MyFile new_file = new MyFile(filename); current_files[current_index] = new_file; current_index++; return 0; } public int P4_fwrite(int fileid, int length, int offset) // fileid represents the file id of the file we wish to write to, start represents // the sector where the file begins on the drive, length represents the number of // of bytes we wish to right to the file, and offset represents the offset in bytes // from start we wish to begin the write. Here we assume that length and offset // are multiples of 512 { int bytes_written = 0; // the number of bytes currently written int sector_offset; // the sector offset from the beginning of the current file int sector_array_value; // the index of sector_array if (current_files[fileid] == null) // checks if valid file { System.out.printf("There is no file associated with file id of %d\n", fileid); return -1; } sector_offset = offset / sector_size; current_files[fileid].current_index = sector_offset; if (scheme_type == 0) { // using Allocation scheme A

// the max amount of file allowed // // // // // // // array of current files current number of files in the system index for current_files the total write time in msecs the total read time in msecs the filesystem scheme size of an extent in scheme B

sector_array_value = current_track*num_of_sectors + current_sector; do { if (current_sector == num_of_sectors) { current_sector = 0; current_track += 1; // switches track if needed

total_write_time += seek_time_per_track; //current_files[fileid].seek_time += seek_time_per_track; System.out.println("Switching Track...\n"); } if (sector_array[sector_array_value] == 1) on disk { current_sector += 1; } if (sectors_remaining == 0) // if there are no more open sectors { System.out.println("Ran out of sectors!!!! Can't write anymore!!!"); return -1; } // free the sector on the hard drive if you are overwriting a file if (current_files[fileid].sectors_used[current_files[fileid].current_index] != 1) { DeleteFromDisk(current_files[fileid].sectors_used[current_files[fileid].current_index]); } // write to the sector WriteToDisk(sector_array_value, 1); bytes_written += 512; current_files[fileid].sectors_used[current_files[fileid].current_index] = sector_array_value; current_files[fileid].current_index += 1; current_sector += 1; sector_array_value += 1; } while (bytes_written < length); } else { // makes sure the space is available

// using Scheme B

sector_array_value = current_track*num_of_sectors + current_sector; if (sectors_remaining == 0) // makes sure the disk isn't full { System.out.println("Ran out of sectors!!!! Can't write anymore!!!"); return -1; } // check to see if the file has been written to already if (current_files[fileid].sector_of_last_write == -1) { // increment until you find free space while(sector_array[sector_array_value] != -1) { if (current_sector == num_of_sectors) { current_sector = 0; current_track++;

total_write_time += seek_time_per_track; //current_files[fileid].seek_time += seek_time_per_track; System.out.println("Switching Track...\n"); } current_sector++; sector_array_value++; } // allocate an extent for (int i = sector_array_value; i < (sector_array_value + extent_size); i++) { sector_array[i] = fileid; } } else if (sector_array_value < current_files[fileid].sector_of_last_write) // traverse up { // set sector_array_value to the sector of the last write while(sector_array_value != current_files[fileid].sector_of_last_write) { if (current_sector == num_of_sectors) { current_sector = 0; current_track++; total_write_time += seek_time_per_track; //current_files[fileid].seek_time += seek_time_per_track; System.out.println("Switching Track...\n"); } current_sector++; sector_array_value++; } } else if (sector_array_value > current_files[fileid].sector_of_last_write) // traverse down { while(sector_array_value != current_files[fileid].sector_of_last_write) { if (current_sector < 0) { current_sector = 49; current_track--; total_write_time += seek_time_per_track; //current_files[fileid].seek_time += seek_time_per_track; System.out.println("Switching Track...\n"); } current_sector--; sector_array_value--; } } // Start writing to the file do { if (current_sector == num_of_sectors) { current_sector = 0; current_track += 1;

// increment track if needed

total_write_time += seek_time_per_track; //current_files[fileid].seek_time += seek_time_per_track; System.out.println("Switching Track...\n"); }

if (sector_array[sector_array_value] != fileid) // if the current extent has filled up { // search for the next available extent while(sector_array[sector_array_value] != -1) { if (current_sector == num_of_sectors) { current_sector = 0; current_track++; //total_write_time += seek_time_per_track; //current_files[fileid].seek_time += seek_time_per_track; System.out.println("Switching Track...\n"); } current_sector++; sector_array_value++; } // allocate another extent for (int i = sector_array_value; i < sector_array_value + extent_size; i++) { sector_array[current_track*num_of_sectors + current_sector] = fileid; i++; } } if (sectors_remaining == 0) { System.out.println("Ran out of sectors!!!! Can't write anymore!!!"); return -1; } // free the sector on the hard drive if you are overwriting a file if (current_files[fileid].sectors_used[current_files[fileid].current_index] != 1) { DeleteFromDisk(current_files[fileid].sectors_used[current_files[fileid].current_index]); } // write to the disk WriteToDisk(sector_array_value, fileid); bytes_written += 512; current_files[fileid].sectors_used[current_files[fileid].current_index] = sector_array_value; current_files[fileid].current_index += 1; current_sector += 1; sector_array_value += 1; } while (bytes_written < length); current_files[fileid].sector_of_last_write = sector_array_value; current_files[fileid].head_of_last_write = current_sector; current_files[fileid].track_of_last_write = current_track; } current_files[fileid].file_size += length; return length; }

public int P4_fread(int fileid, int // fileid represents the file id of // number of bytes we wish to read // from start we wish to begin the // are multiples of 512 { int sector_offset; start reading from int sector_array_value;

length, int offset) the file we wish to read from, length represents the from the file, and offset represents the offset in bytes read. Here we assume that length and offset // the offset from beginning of file we to // the index of sector_array

if (current_files[fileid] == null) { System.out.printf("There is no file associated with file id of %d\n", fileid); return -1; } sector_offset = offset / sector_size; current_files[fileid].current_index = sector_offset; sector_array_value = current_track*num_of_sectors + current_sector; // move the read head to the next sector of the file until the entire file is read do { if (sector_array_value < current_files[fileid].sectors_used[current_files[fileid].current_index]) // traverse up { // set sector_array_value to the index of the current sector being read while(sector_array_value != current_files[fileid].sectors_used[current_files[fileid].current_index]) { if (current_sector == num_of_sectors) { current_sector = 0; current_track++; total_read_time += seek_time_per_track; current_files[fileid].seek_time += seek_time_per_track; System.out.println("Switching Track...\n"); } current_sector++; sector_array_value++; } } else if (sector_array_value > current_files[fileid].sectors_used[current_files[fileid].current_index]) // traverse down { while(sector_array_value != current_files[fileid].sectors_used[current_files[fileid].current_index]) { if (current_sector < 0) { current_sector = 49; current_track--; total_read_time += seek_time_per_track; current_files[fileid].seek_time += seek_time_per_track; System.out.println("Switching Track...\n"); } current_sector--; sector_array_value--; } } current_files[fileid].current_index++;

} while (current_files[fileid].sectors_used[current_files[fileid].current_index] != -1); return length; } }

Anda mungkin juga menyukai