Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int writeFile(char* fname, char* buffer, int sectors)
- {
- //Xwrite sectors*512 bytes of data from buffer into a file
- // indicated by fname
- //X if the file indicated by fname exists, overwrite it.
- //X max sectors is 26, only write 26 sectors if it is more
- //if file is successful, return number of sectors
- //X if there is no entry available return -1, don't write
- //if diskmap contains fewer than sectors free sectors, write as
- //many sectors as possible and return -2.
- //CHECK
- //ITERATE THROUGH DISKMAP
- //FIND A ZERO, CHANGE TO FF, TAKE INDEX
- //WRITE THAT AS FILELOCATION
- char sectorBuf[512];
- char mapBuf[512];
- char dirBuf[512];
- char currentName[7]; //Name is 6 bytes, ends with \0
- char fileLocation[27]; //Location is 26 bytes after name, ends with \0
- int returnVal;
- int looking = 1;
- int i; //Index
- int j = 0; //Index
- int k = 0; //Index
- int l = 0;
- int freeEntry = 0;
- int searchBreak = 0;
- int mapBufIndex = 0;
- int entryNumber= 0; //Number of the sector we're working with
- int found = 0; //Checks if file is found
- char entryNum[2];
- readSector(mapBuf, 1);
- readSector(dirBuf, 2);
- entryNum[1]=entryNumber;
- entryNum[2]='\0';
- //POPULATES fileName and fileLocation buffers from directory
- while(!found && searchBreak == 0)
- { //While we haven't found the file
- for(i = 0; i<32; i++)
- { //Search the entry (each entry is 32 bytes)
- if(i < 6) //While still checking the file name
- {
- currentName[i] = dirBuf[i+32*entryNumber]; //Apply ith letter of our entry's file name to index of currentName
- }
- else //If we've gone through all 6 bytes of the name
- {
- fileLocation[i-6] = dirBuf[i+32*entryNumber]; //Apply ith byte of our entry's location to index of fileLocation
- }
- }
- //COMPARES STRING OF FILENAME in entry TO CURRENTNAME
- for(i = 0; i < 6; i++) //For length of name
- {
- if(fname[i] == currentName[i]) //If i of fileName equal to i of our desired name
- {
- if(i == 5) //If the names match
- {
- found = 1; //It's found
- }
- }
- else //If it's not found
- {
- entryNum[0]=entryNumber;
- //printString("[wrong entry..] ");
- i = 7; //End comparison
- entryNumber++; //Go to next entry
- if(entryNumber == 16) //If we've searched all possible entries
- {
- printString(" [File not found!] "); //Then tell us we searched all possible entires
- searchBreak = 1;
- }
- }
- }
- }
- //FILENAME IS FOUND ENTRY MUST BE OVERWRITTEN
- if(found)
- {
- printString("[File Found]");
- deleteFile(fname);
- //ENTRY NUMBER
- //FIND AN EMPTY ENTRYNUMBER
- }
- else
- {
- entryNumber = 0;
- //BREAK OUT WHEN IT FINDS AN ENTRY
- for(i = 0; i<16; i++)
- {
- if(dirBuf[32*entryNumber] != 0x00)
- {
- entryNumber++;
- }
- else
- {
- //freeEntry = 1
- freeEntry = 1;
- i = 16;
- }
- if(i==15 && (freeEntry == 0))
- {
- //DIRECTORY IS FULL
- return -1;
- }
- }
- }
- k = 0; // # OF SECTORS SAVED
- //FOR EACH ENTRY IN THE DIRECTORY
- for(i = 0;i<32; i++)
- {
- if(i<6)
- {
- //COPY FILENAME
- dirBuf[i+32*entryNumber] = fname[i];
- }
- //FIND SPACE IN THE MAP
- //FOR GIVEN AMOUNT OF SECTORS
- else
- {
- if(k<sectors)
- {
- if(k>25)
- {
- //MORE SECTORS TO WRITE THAN ARE AVAILABLE
- return -2;
- }
- //FOR THE SIZE OF THE DISK MAP
- looking = 1;
- printString(" [LOOKING FOR FREE SECTOR] ");
- while(looking){
- // }
- //for(mapBufIndex = 0; mapBufIndex <512; mapBufIndex++)
- //{
- if(mapBufIndex == 512){
- printString("BREAK");
- looking = 0;
- }
- if(mapBuf[mapBufIndex] == 0x00)
- {
- //SPACE IS FREE!
- for(l = 0; l<mapBufIndex; l++){
- printString("*");
- }
- mapBuf[mapBufIndex] = 0xff; // FLAG THE DISK MAP
- printString("Flagged!");
- dirBuf[i+32*entryNumber] = mapBufIndex; //COPY SECTOR # TO DIRECTORY
- for(j = 0; j<512; j++)//COPY DATA FROM THE BUFFER PARAMETER
- {
- sectorBuf[j] = buffer[k*512+j];
- }
- printString("Sector Written!");
- writeSector(sectorBuf, mapBufIndex); //WRITE THE DATA BUFFER TO THE FOUND SECTOR
- k++; //SECTOR SAVED INCREMENT SECTOR COPIED VARIABLE
- mapBufIndex++; // INCREMENT THE INDEX
- looking = 0;
- }
- else
- {
- mapBufIndex++;
- //USED SPACE IN THE DISK MAP / INDEX INCREMENTS
- }
- }
- //CHECK THIS
- /*
- if(k < sectors)
- {
- return -2;
- }
- */
- }
- else
- {
- //ALL SECTORS WRITTEN
- dirBuf[32*entryNumber+i] = 0x00;
- }
- }
- }
- writeSector(dirBuf, 2);
- writeSector(mapBuf, 1);
- //SUCCESSFUL return k
- return k;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement