00001 // directory.h 00002 // Data structures to manage a UNIX-like directory of file names. 00003 // 00004 // A directory is a table of pairs: <file name, sector #>, 00005 // giving the name of each file in the directory, and 00006 // where to find its file header (the data structure describing 00007 // where to find the file's data blocks) on disk. 00008 // 00009 // We assume mutual exclusion is provided by the caller. 00010 // 00011 // Copyright (c) 1992-1993 The Regents of the University of California. 00012 // All rights reserved. See copyright.h for copyright notice and limitation 00013 // of liability and disclaimer of warranty provisions. 00014 00015 #include "copyright.h" 00016 00017 #ifndef DIRECTORY_H 00018 #define DIRECTORY_H 00019 00020 #include "openfile.h" 00021 00022 #define FileNameMaxLen 9 // for simplicity, we assume 00023 // file names are <= 9 characters long 00024 00025 // The following class defines a "directory entry", representing a file 00026 // in the directory. Each entry gives the name of the file, and where 00027 // the file's header is to be found on disk. 00028 // 00029 // Internal data structures kept public so that Directory operations can 00030 // access them directly. 00031 00032 class DirectoryEntry { 00033 public: 00034 bool inUse; // Is this directory entry in use? 00035 int sector; // Location on disk to find the 00036 // FileHeader for this file 00037 char name[FileNameMaxLen + 1]; // Text name for file, with +1 for 00038 // the trailing '\0' 00039 }; 00040 00041 // The following class defines a UNIX-like "directory". Each entry in 00042 // the directory describes a file, and where to find it on disk. 00043 // 00044 // The directory data structure can be stored in memory, or on disk. 00045 // When it is on disk, it is stored as a regular Nachos file. 00046 // 00047 // The constructor initializes a directory structure in memory; the 00048 // FetchFrom/WriteBack operations shuffle the directory information 00049 // from/to disk. 00050 00051 class Directory { 00052 public: 00053 Directory(int size); // Initialize an empty directory 00054 // with space for "size" files 00055 ~Directory(); // De-allocate the directory 00056 00057 void FetchFrom(OpenFile *file); // Init directory contents from disk 00058 void WriteBack(OpenFile *file); // Write modifications to 00059 // directory contents back to disk 00060 00061 int Find(char *name); // Find the sector number of the 00062 // FileHeader for file: "name" 00063 00064 bool Add(char *name, int newSector); // Add a file name into the directory 00065 00066 bool Remove(char *name); // Remove a file from the directory 00067 00068 void List(); // Print the names of all the files 00069 // in the directory 00070 void Print(); // Verbose print of the contents 00071 // of the directory -- all the file 00072 // names and their contents. 00073 00074 private: 00075 int tableSize; // Number of directory entries 00076 DirectoryEntry *table; // Table of pairs: 00077 // <file name, file header location> 00078 00079 int FindIndex(char *name); // Find the index into the directory 00080 // table corresponding to "name" 00081 }; 00082 00083 #endif // DIRECTORY_H