hw4 test
This commit is contained in:
@@ -40,56 +40,108 @@
|
||||
#ifdef FILESYS_STUB // Temporarily implement file system calls as
|
||||
// calls to UNIX, until the real file system
|
||||
// implementation is available
|
||||
|
||||
typedef int OpenFileId;
|
||||
|
||||
class FileSystem {
|
||||
public:
|
||||
FileSystem() { for (int i = 0; i < 20; i++) fileDescriptorTable[i] = NULL; }
|
||||
public:
|
||||
FileSystem() { for (int i = 0; i < 20; i++) fileDescriptorTable[i] = NULL; }
|
||||
|
||||
bool Create(char *name) {
|
||||
int fileDescriptor = OpenForWrite(name);
|
||||
bool Create(char* name) {
|
||||
int fileDescriptor = OpenForWrite(name);
|
||||
if (fileDescriptor == -1) return FALSE;
|
||||
Close(fileDescriptor);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
if (fileDescriptor == -1) return FALSE;
|
||||
Close(fileDescriptor);
|
||||
return TRUE;
|
||||
}
|
||||
OpenFile* Open(char* name) {
|
||||
int fileDescriptor = OpenForReadWrite(name, FALSE);
|
||||
if (fileDescriptor == -1) return NULL;
|
||||
return new OpenFile(fileDescriptor);
|
||||
}
|
||||
|
||||
OpenFile* Open(char *name) {
|
||||
int fileDescriptor = OpenForReadWrite(name, FALSE);
|
||||
OpenFileId OpenFiles(char* name) {
|
||||
OpenFile* file = Open(name);
|
||||
if (!file) return -1;
|
||||
int freeIndex = -1;
|
||||
|
||||
if (fileDescriptor == -1) return NULL;
|
||||
return new OpenFile(fileDescriptor);
|
||||
}
|
||||
for (int i = 0; i < 20; i++)
|
||||
if (!fileDescriptorTable[i])
|
||||
freeIndex = i;
|
||||
|
||||
bool Remove(char *name) { return Unlink(name) == 0; }
|
||||
if (freeIndex == -1)
|
||||
return -1;
|
||||
OpenFileId fileDescriptor = file->GetFileDescriptor();
|
||||
fileDescriptorTable[freeIndex] = file;
|
||||
return fileDescriptor;
|
||||
}
|
||||
|
||||
int WriteFile(char* buffer, int size, OpenFileId fd) {
|
||||
for (int i = 0; i < 20; i++) {
|
||||
if (!fileDescriptorTable[i])
|
||||
continue;
|
||||
if (fileDescriptorTable[i]->GetFileDescriptor() == fd) {
|
||||
return fileDescriptorTable[i]->Write(buffer, size);
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ReadFile(char* buffer, int size, OpenFileId fd) {
|
||||
for (int i = 0; i < 20; i++) {
|
||||
if (!fileDescriptorTable[i])
|
||||
continue;
|
||||
if (fileDescriptorTable[i]->GetFileDescriptor() == fd)
|
||||
return fileDescriptorTable[i]->Read(buffer, size);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int CloseFile(OpenFileId fd) {
|
||||
for (int i = 0; i < 20; i++) {
|
||||
if (!fileDescriptorTable[i])
|
||||
continue;
|
||||
if (fileDescriptorTable[i]->GetFileDescriptor() == fd) {
|
||||
delete fileDescriptorTable[i];
|
||||
fileDescriptorTable[i] = NULL;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool Remove(char* name) { return Unlink(name) == 0; }
|
||||
|
||||
OpenFile* fileDescriptorTable[20];
|
||||
|
||||
OpenFile *fileDescriptorTable[20];
|
||||
};
|
||||
|
||||
#else // FILESYS
|
||||
class FileSystem {
|
||||
public:
|
||||
FileSystem(bool format); // Initialize the file system.
|
||||
// Must be called *after* "synchDisk"
|
||||
// has been initialized.
|
||||
// If "format", there is nothing on
|
||||
// the disk, so initialize the directory
|
||||
// and the bitmap of free blocks.
|
||||
public:
|
||||
FileSystem(bool format); // Initialize the file system.
|
||||
// Must be called *after* "synchDisk"
|
||||
// has been initialized.
|
||||
// If "format", there is nothing on
|
||||
// the disk, so initialize the directory
|
||||
// and the bitmap of free blocks.
|
||||
|
||||
bool Create(char *name, int initialSize);
|
||||
// Create a file (UNIX creat)
|
||||
bool Create(char* name, int initialSize);
|
||||
// Create a file (UNIX creat)
|
||||
|
||||
OpenFile* Open(char *name); // Open a file (UNIX open)
|
||||
OpenFile* Open(char* name); // Open a file (UNIX open)
|
||||
|
||||
bool Remove(char *name); // Delete a file (UNIX unlink)
|
||||
bool Remove(char* name); // Delete a file (UNIX unlink)
|
||||
|
||||
void List(); // List all the files in the file system
|
||||
void List(); // List all the files in the file system
|
||||
|
||||
void Print(); // List all the files and their contents
|
||||
void Print(); // List all the files and their contents
|
||||
|
||||
private:
|
||||
OpenFile* freeMapFile; // Bit map of free disk blocks,
|
||||
// represented as a file
|
||||
OpenFile* directoryFile; // "Root" directory -- list of
|
||||
// file names, represented as a file
|
||||
private:
|
||||
OpenFile* freeMapFile; // Bit map of free disk blocks,
|
||||
// represented as a file
|
||||
OpenFile* directoryFile; // "Root" directory -- list of
|
||||
// file names, represented as a file
|
||||
};
|
||||
|
||||
#endif // FILESYS
|
||||
|
||||
@@ -27,32 +27,38 @@
|
||||
#ifdef FILESYS_STUB // Temporarily implement calls to
|
||||
// Nachos file system as calls to UNIX!
|
||||
// See definitions listed under #else
|
||||
typedef int OpenFileId;
|
||||
|
||||
class OpenFile {
|
||||
public:
|
||||
OpenFile(int f) { file = f; currentOffset = 0; } // open the file
|
||||
~OpenFile() { Close(file); } // close the file
|
||||
|
||||
int ReadAt(char *into, int numBytes, int position) {
|
||||
Lseek(file, position, 0);
|
||||
return ReadPartial(file, into, numBytes);
|
||||
Lseek(file, position, 0);
|
||||
return ReadPartial(file, into, numBytes);
|
||||
}
|
||||
int WriteAt(char *from, int numBytes, int position) {
|
||||
Lseek(file, position, 0);
|
||||
WriteFile(file, from, numBytes);
|
||||
return numBytes;
|
||||
Lseek(file, position, 0);
|
||||
WriteFile(file, from, numBytes);
|
||||
return numBytes;
|
||||
}
|
||||
int Read(char *into, int numBytes) {
|
||||
int numRead = ReadAt(into, numBytes, currentOffset);
|
||||
currentOffset += numRead;
|
||||
return numRead;
|
||||
}
|
||||
int numRead = ReadAt(into, numBytes, currentOffset);
|
||||
currentOffset += numRead;
|
||||
return numRead;
|
||||
}
|
||||
int Write(char *from, int numBytes) {
|
||||
int numWritten = WriteAt(from, numBytes, currentOffset);
|
||||
currentOffset += numWritten;
|
||||
return numWritten;
|
||||
int numWritten = WriteAt(from, numBytes, currentOffset);
|
||||
currentOffset += numWritten;
|
||||
return numWritten;
|
||||
}
|
||||
|
||||
int Length() { Lseek(file, 0, 2); return Tell(file); }
|
||||
|
||||
OpenFileId GetFileDescriptor() {
|
||||
return file;
|
||||
}
|
||||
|
||||
private:
|
||||
int file;
|
||||
|
||||
Reference in New Issue
Block a user