Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 85 additions & 78 deletions proj3-clean.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ int firstClusterSector(struct BS_BPB *bs) {
return Partition_LBA_Begin + bs->BPB_RsvdSecCnt + (bs->BPB_NumFATs * bs->BPB_SecPerClus);
}



//----------------OPEN FILE/DIRECTORY TABLES-----------------
/* create a new file to be put in the file descriptor table
Expand Down Expand Up @@ -637,6 +638,18 @@ uint32_t byteOffsetofDirectoryEntry(struct BS_BPB * bs, uint32_t clusterNum, int
return (dataAddress + offset);
}


/* description: feed this a cluster that's a member of a chain and this
* sets your environment variable "io_writeCluster" to the last in that
* chain. The thinking is this is where you'll write in order to grow
* that file. before writing to cluster check with FAT_findNextOpenEntry()
* to see if there's space or you need to extend the chain using FAT_extendClusterChain()
*/
int FAT_setIoWriteCluster(struct BS_BPB * bs, uint32_t clusterChainMember) {
environment.io_writeCluster = getLastClusterInChain(bs, clusterChainMember);
return 0;
}

/* This is the workhorse. It is used for ls, cd, filesearching.
* Directory Functionality:
* It is used to perform cd, which you use by setting <cd>. if <goingUp> is set it uses
Expand Down Expand Up @@ -897,77 +910,26 @@ int FAT_freeClusterChain(struct BS_BPB * bs, uint32_t firstClusterOfChain){

}

/* description: feed this a cluster that's a member of a chain and this
* sets your environment variable "io_writeCluster" to the last in that
* chain. The thinking is this is where you'll write in order to grow
* that file. before writing to cluster check with FAT_findNextOpenEntry()
* to see if there's space or you need to extend the chain using FAT_extendClusterChain()
*/
int FAT_setIoWriteCluster(struct BS_BPB * bs, uint32_t clusterChainMember) {
environment.io_writeCluster = getLastClusterInChain(bs, clusterChainMember);
return 0;
}

/* description: this will write a new entry to <destinationCluster>. if that cluster
* is full it grows the cluster chain and write the entry in the first spot
* of the new cluster. if <isDotEntries> is set this automatically writes both
* '.' and '..' to offsets 0 and 1 of <destinationCluster>
*
*/
int writeFileEntry(struct BS_BPB * bs, struct DIR_ENTRY * entry, uint32_t destinationCluster, bool isDotEntries) {
int dataAddress;
int freshCluster;
FILE* f = fopen(environment.imageName, "r+");
checkForFileError(f);
if(isDotEntries == FALSE) {
if((dataAddress = FAT_findNextOpenEntry(bs, destinationCluster)) != -1) {//-1 means current cluster is at capacity
fseek(f, dataAddress, 0);
fwrite (entry , 1 , sizeof(struct DIR_ENTRY) , f );
} else {
freshCluster = FAT_extendClusterChain(bs, destinationCluster);
dataAddress = FAT_findNextOpenEntry(bs, freshCluster);
fseek(f, dataAddress, 0);
fwrite (entry , 1 , sizeof(struct DIR_ENTRY) , f );
}
} else {
struct DIR_ENTRY dotEntry;
struct DIR_ENTRY dotDotEntry;
makeSpecialDirEntries(&dotEntry, &dotDotEntry, destinationCluster, environment.pwd_cluster);
//seek to first spot in new dir cluster chin and write the '.' entry
dataAddress = byteOffsetofDirectoryEntry(bs, destinationCluster, 0);
fseek(f, dataAddress, 0);
fwrite (&dotEntry , 1 , sizeof(struct DIR_ENTRY) , f );
//seek to second spot in new dir cluster chin and write the '..' entry
dataAddress = byteOffsetofDirectoryEntry(bs, destinationCluster, 1);
fseek(f, dataAddress, 0);
fwrite (&dotDotEntry , 1 , sizeof(struct DIR_ENTRY) , f );
}
fclose(f);
return 0;
}



/* description: takes a directory entry and all the necesary info
and populates the entry with the info in a correct format for
insertion into a disk.
and populates the entry with the info in a correct format for
insertion into a disk.
*/
int createEntry(struct DIR_ENTRY * entry,
const char * filename,
const char * ext,
int isDir,
uint32_t firstCluster,
uint32_t filesize,
const char * filename,
const char * ext,
int isDir,
uint32_t firstCluster,
uint32_t filesize,
bool emptyAfterThis,
bool emptyDirectory) {
//set the same no matter the entry
entry->r1 = 0;
entry->r2 = 0;
entry->crtTime = 0;
entry->crtDate = 0;
entry->accessDate = 0;
entry->lastWrTime = 0;
entry->lastWrDate = 0;
entry->r2 = 0;
entry->crtTime = 0;
entry->crtDate = 0;
entry->accessDate = 0;
entry->lastWrTime = 0;
entry->lastWrDate = 0;

if(emptyAfterThis == FALSE && emptyDirectory == FALSE) { //if both are false
int x;
Expand All @@ -990,10 +952,10 @@ int createEntry(struct DIR_ENTRY * entry,
if(isDir == TRUE) {
entry->fileSize = 0;
entry->attributes = ATTR_DIRECTORY;
} else {
} else {
entry->fileSize = filesize;
entry->attributes = ATTR_ARCHIVE;
}
}
return 0; //stops execution so we don't flow out into empty entry config code below

} else if(emptyAfterThis == TRUE) { //if this isn't true, then the other must be
Expand All @@ -1010,29 +972,74 @@ int createEntry(struct DIR_ENTRY * entry,
for(x = 1; x < 11; x++)
entry->filename[x] = 0x00;

entry->loCluster[0] = 0x00;
entry->loCluster[0] = 0x00;
entry->loCluster[1] = 0x00;
entry->hiCluster[0] = 0x00;
entry->hiCluster[1] = 0x00;
entry->attributes = 0x00;
entry->fileSize = 0;
entry->attributes = 0x00;
entry->fileSize = 0;
return 0;
}

/* description: take two entries and cluster info and populates them with
* '.' and '..' entriy information. The entries are ready for writing to


/* description: take two entries and cluster info and populates them with
* '.' and '..' entriy information. The entries are ready for writing to
* the disk after this. helper function for mkdir()
*/
int makeSpecialDirEntries(struct DIR_ENTRY * dot,
struct DIR_ENTRY * dotDot,
uint32_t newlyAllocatedCluster,
uint32_t pwdCluster ) {
createEntry(dot, ".", "", TRUE, newlyAllocatedCluster, 0, FALSE, FALSE);
createEntry(dotDot, "..", "", TRUE, pwdCluster, 0, FALSE, FALSE);
return 0;
struct DIR_ENTRY * dotDot,
uint32_t newlyAllocatedCluster,
uint32_t pwdCluster ) {
createEntry(dot, ".", "", TRUE, newlyAllocatedCluster, 0, FALSE, FALSE);
createEntry(dotDot, "..", "", TRUE, pwdCluster, 0, FALSE, FALSE);
return 0;
}

/* description: this will write a new entry to <destinationCluster>. if that cluster
* is full it grows the cluster chain and write the entry in the first spot
* of the new cluster. if <isDotEntries> is set this automatically writes both
* '.' and '..' to offsets 0 and 1 of <destinationCluster>
*
*/
int writeFileEntry(struct BS_BPB * bs, struct DIR_ENTRY * entry, uint32_t destinationCluster, bool isDotEntries) {
int dataAddress;
int freshCluster;
FILE* f = fopen(environment.imageName, "r+");
checkForFileError(f);
if(isDotEntries == FALSE) {
if((dataAddress = FAT_findNextOpenEntry(bs, destinationCluster)) != -1) {//-1 means current cluster is at capacity
fseek(f, dataAddress, 0);
fwrite (entry , 1 , sizeof(struct DIR_ENTRY) , f );
} else {
freshCluster = FAT_extendClusterChain(bs, destinationCluster);
dataAddress = FAT_findNextOpenEntry(bs, freshCluster);
fseek(f, dataAddress, 0);
fwrite (entry , 1 , sizeof(struct DIR_ENTRY) , f );
}
} else {
struct DIR_ENTRY dotEntry;
struct DIR_ENTRY dotDotEntry;
makeSpecialDirEntries(&dotEntry, &dotDotEntry, destinationCluster, environment.pwd_cluster);
//seek to first spot in new dir cluster chin and write the '.' entry
dataAddress = byteOffsetofDirectoryEntry(bs, destinationCluster, 0);
fseek(f, dataAddress, 0);
fwrite (&dotEntry , 1 , sizeof(struct DIR_ENTRY) , f );
//seek to second spot in new dir cluster chin and write the '..' entry
dataAddress = byteOffsetofDirectoryEntry(bs, destinationCluster, 1);
fseek(f, dataAddress, 0);
fwrite (&dotDotEntry , 1 , sizeof(struct DIR_ENTRY) , f );
}
fclose(f);
return 0;
}






/* returns offset of the entry, <searchName> in the pwd
* if found return the offset, if not return -1
*/
Expand Down