Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/asmeurer/OS_lab1
Browse files Browse the repository at this point in the history
  • Loading branch information
slundqui committed Apr 26, 2012
2 parents 559a80f + b5f614f commit da4cfdd
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 27 deletions.
4 changes: 2 additions & 2 deletions filesystem/definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ enum rw {

/* Buffer struct */
typedef struct{
short addr;
unsigned short addr;
enum rw access_type;
byte init;
} buffer_slot;
Expand All @@ -113,7 +113,7 @@ struct fcb {
/*0 0 0 0 0 0 0 (Directory)*/
byte bits;
struct dir_queue_t* dirHead;
struct dir_queue_t* containing_dir;
struct dir_queue_t* parent_dir;
struct block_queue_t *block_queue;
struct fcb* next;
struct fcb* prev;
Expand Down
107 changes: 83 additions & 24 deletions filesystem/file_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ int format(int device_num, char fs_name, int blocksize){

error = delete_internal(device_num, format_me->root);

if (error < 0) {
return error;
}

/*Erase*/
for(i = 0; i < MEM_SIZE / 32; i++){
format_me->bitmap[i] = 0;
Expand All @@ -125,8 +129,8 @@ int format(int device_num, char fs_name, int blocksize){
format_me->root->bits = 0;
format_me->root->dirHead = null;
/* This is the reason we don't use create() to make the root directory.
* It has a special case for containing_dir, which is just null. */
format_me->root->containing_dir = null;
* It has a special case for parent_dir, which is just null. */
format_me->root->parent_dir = null;
format_me->root->block_queue = null;
format_me->root->next = null;
format_me->root->prev = null;
Expand Down Expand Up @@ -520,7 +524,7 @@ int create(char fs_name, struct path *file_path, int dir)
filename_copy(newfile->filename, next->string);
newfile->bits = dir;
newfile->dirHead = new_dirqueue;
newfile->containing_dir = current_dir;
newfile->parent_dir = current_dir;
newfile->block_queue = new_blockqueue;
newfile->device_num = dev;
newfile->error = 0;
Expand All @@ -536,7 +540,11 @@ int create(char fs_name, struct path *file_path, int dir)
* Directories are recursively deleted. All blocks for deleted files are
* files.
*
* @param fs_name The filesystem where
* @param fs_name The filesystem where the file to be deleted is.
*
* @param file_path The path data structure for the file to be deleted.
*
* @return Returns ERROR_SUCCESS on success and an error code on failure.
*/
int delete(char fs_name, struct path *file_path)
{
Expand All @@ -553,7 +561,9 @@ int delete(char fs_name, struct path *file_path)
return delete_internal(dev, file);
}


/**
* Internal function that does the heavy work for delete().
*/
int delete_internal(int dev, fcb *file)
{
int error;
Expand All @@ -563,7 +573,8 @@ int delete_internal(int dev, fcb *file)
/* If the file is a directory, delete it if and only if it is empty. */
if (file->bits | FCB_DIR_BITMASK) {
if (file->dirHead == null) {
file = dir_delete(file->containing_dir, file);
/* The directory is empty. Just delete it. */
file = dir_delete(file->parent_dir, file);
if (file->error < 0) {
return file->error;
}
Expand All @@ -579,13 +590,16 @@ int delete_internal(int dev, fcb *file)
}
}
} else {
file = dir_delete(file->containing_dir, file);
/* If the file is a file (not a directory), then delete all the data
* (blocks), in addition to the fcb. */
file = dir_delete(file->parent_dir, file);
if (file->error < 0) {
return file->error;
}
/* Clear all the blocks in the file. This means setting the addresses
* as free in the bitmap and actually clearing the "memory" of those
* blocks. */
* as free in the bitmap and actually clearing the "data" of those
* blocks by calling Free in the hardware simulator (which actually
* only frees the data structures associated with the blocks). */
file_block = block_dequeue(file->block_queue);
while (file_block != null) {
error = set_block_empty(dev, file_block->addr) < 0;
Expand All @@ -599,8 +613,18 @@ int delete_internal(int dev, fcb *file)
return ERROR_SUCCESS;
}


/* Convert a filesystem character name to a device number */
/**
* Convert a filesystem character name to a device number.
*
* Used internally. The public API uses the filesystem name, and the private
* API uses the device number. This function converts one to the other.
*
* The filesystem character must be an uppercase letter (A-Z).
*
* @param fs_name The filesystem name to be converted.
*
* @return Returns the device number of success and an error code on failure.
*/
int get_device(char fs_name)
{
int i;
Expand All @@ -618,21 +642,45 @@ int get_device(char fs_name)
return ERROR_FS_NAME_NOT_EXISTS;
}

/* Custom version of strcmp to compare filenames. Returns 0 if the names are
* unequal and 1 if they are equal. */
/**
* Custom version of strcmp to compare filenames.
*
* This is here because we cannot use the system call version. It has the
* bounds check for the size of a filename built-in.
*
* @param string1 The first string to be compared.
*
* @param string2 The second string to be compared.
*
* @return Returns 0 if the string1 and string2 are unequal and 1 if they are
* equal.
*/
int filename_eq(char *string1, char *string2)
{
int i;
for (i = 0; i < NAME_LIMIT; i++) {
if (string1[i] != string2[i]) {
return 0;
}
if (string1[i] == '\0' || string2[i] == '\0') {
return 1;
}
}
/* If we reach this, it's probably bad, because it means the strings
* don't end with null terminators. But we'll let someone else worry
* about that. */
return 1;
}

/* Custom version of strcpy for filenames. Copies the string from source into
* dest. Doesn't do any error checking. */
/**
* Custom version of strcpy for filenames. Copies the string from source into
* dest. Doesn't do any error checking. It has the filename length bounds
* check built-in.
*
* @param source The string to be copied into.
*
* @param dest The string to be copied.
*/
void filename_copy(char *source, char *dest)
{
int i;
Expand All @@ -644,9 +692,12 @@ void filename_copy(char *source, char *dest)

/**
* Find a free block slot in the device. This will correspond to a 0 bit in
* the blocks_free byte array.
* the bitmap byte array. This is the same function from the memory manager
* used to find empty slots in the backing store.
*
* @param dev Device number to search.
* @return Returns the index of the empty backing frame.
*
* @return Returns the address of the empty block.
*/
unsigned short find_empty_block(int dev) {
int prefix;
Expand Down Expand Up @@ -686,12 +737,16 @@ unsigned short find_empty_block(int dev) {


/**
* Set the block address addr to full. Returns errors if the block is
* already in the state it is being set to, or if the address is out of
* bounds.
* Set the block address addr to full. Returns errors if the block is already
* in the state it is being set to, or if the address is out of bounds. This
* is the same function from the memory manager to set a frame as empty in the
* backing store.
*
* @param dev The device to set.
*
* @param addr The address to be set as empty.
* @return Returns an error code.
*
* @return Returns ERROR_SUCCESS on success or an error code on failure.
*/
int set_block_empty(int dev, unsigned short addr) {
int prefix;
Expand All @@ -717,12 +772,16 @@ int set_block_empty(int dev, unsigned short addr) {
}

/**
* Set the address addr to empty. Returns errors if the memory is
* Set the block address addr to empty. Returns errors if the memory is
* already in the state it is being set to, or if the address is out of
* bounds.
* bounds. This is the same function from the memory manager to set a frame
* as full in the backing store.
*
* @param dev The device to set.
*
* @param addr The address to be set as full.
* @return Returns an error code.
*
* @return Returns ERROR_SUCCESS on success and an error code on failure.
*/
int set_block_full(int dev, unsigned short addr) {
int prefix;
Expand Down
2 changes: 1 addition & 1 deletion filesystem/file_queuemanager.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fcb error_file =
.filename = "\0",
.bits = 0,
.dirHead = null,
.containing_dir = null,
.parent_dir = null,
.block_queue = null,
.next = null,
.prev = null,
Expand Down

0 comments on commit da4cfdd

Please sign in to comment.