Creating a Directory Entry

25 Mar

Creating a Directory Entry
After finding an empty entry, a host can store information about a new file or subdirectory. The PopulateEntries function accepts pointers to a FILE structure (fo), a file name (name), and the number of the file’s entry in its directory (fHandle). The function stores the file name and other information in the entry and returns a status code.

The function calls the Cache_File_Entry and Write_File_Entry functions from Chapter 9. In the passed file structure, the dirclus member must contain the number of the first cluster in the entry’s directory.

#define ATTR_ARCHIVE 0×20
byte PopulateEntries(FILEOBJ fo, char *name , word *fHandle)
byte csum;
byte error = CE_GOOD;
byte index;
byte nameptr;
byte temp;
// Get the file’s directory entry.
// The FILE structure’s dirclus member is the first cluster
// of the directory containing the entry to read.
dir = Cache_File_Entry( fo, fHandle, TRUE);
// Copy information into the entry.
strncpy(dir -> DIR_Name, name, DIR_NAMECOMP);
dir -> DIR_Attr = ATTR_ARCHIVE;
dir -> DIR_NTRes = 0×00;

// A system with a real-time clock would retrieve these values from the clock
// instead of using these fixed values.
dir -> DIR_CrtTimeTenth =0×64; // creation time, hundredths of a second (1 sec.)
dir -> DIR_CrtTime = 0×43C5; // creation time (8:30:10)
dir -> DIR_CrtDate = 0×34B0; // creation date (5/16/2006)
dir -> DIR_LstAccDate = 0×34B0; // last access date
dir -> DIR_FstClusHI = 0×0000; // high word of this enty’s first cluster number
dir -> DIR_WrtTime = 0×43C6; // last modified time (8:30:12)
dir -> DIR_WrtDate = 0×34B0; // last modified date
dir->DIR_FstClusLO = 0×0000; // low word of this entry’s first cluster number
dir->DIR_FileSize = 0×0; // file size
// Save information in the file structure.
fo -> size = dir -> DIR_FileSize;
fo -> time = dir -> DIR_CrtTime;
fo -> date = dir -> DIR_CrtDate;
fo -> attributes = dir -> DIR_Attr;
fo -> entry = *fHandle;
// Write the entry to the directory.

Finding an Available Entry
To create a file, the host must find an available entry in the directory the file will reside in. The FindEmpyEntries function performs this task. The function accepts a pointer to a FILE structure (fo) that contains the number of a directory’s first cluster and a pointer to a variable that will hold the number of the found entry within the directory (fHandle). To start at the beginning of the directory, fHandle should equal zero. In the passed file structure, the dirclus member must contain the number of the first cluster in the directory to search. The function returns a status code. The function calls the Cache_File_Entry function from Chapter 9 and the FILEAllocate_New_Cluster function above. (The FILEfind function in this chapter can also find an available directory entry.)

byte FindEmptyEntries(FILEOBJ fo, word *fHandle)
byte a;
byte amountfound;
word bHandle;
byte status = NOT_FOUND;
// Call Cache_File_Entry with the ForceRead parameter = TRUE
// to read the directory’s sector from the media into fo -> dsk -> buffer.
// fHandle contains the number of the entry to read in the directory.
if ((dir = Cache_File_Entry(fo, fHandle, TRUE)) == NULL)
while (status == NOT_FOUND)
amountfound = 0;
bHandle = *fHandle;
// Look for a deleted or empty entry.
// Get an entry.
// Set the ForceRead parameter FALSE so the function reads from the media
// only when necessary (when starting a new sector).
dir = Cache_File_Entry(fo, fHandle, FALSE);
// Read the first character of the file name.
a = dir -> DIR_Name[0];
// Increment the entry number.
// Stop looking on finding a deleted or empty entry
// or on reaching the end of the cluster.
} while ((a == DIR_DEL || a == DIR_EMPTY) && (dir != (DIRENTRY)NULL)
&& (++amountfound < 1));
if (dir == NULL)
// It was the cluster’s last entry.
// Get the cluster number of the directory.
a = fo -> dirccls;
if (a == 0)
// It’s the root directory.
// The root directory is full and can’t be expanded in FAT16.
status = NO_MORE;
// It’s not the root directory. Save the current cluster number.
fo -> ccls = a;
// Allocate a new cluster to the directory.
if (FILEallocate_new_cluster(fo) == CE_DISK_FULL)
status = NO_MORE;
// The first entry (and all entries) in a new cluster are empty.
status = FOUND;
} // End: It’s the cluster’s last entry.
if (amountfound == 1)
// An empty cluster was found.
status = FOUND;
} // End: while (status == NOT_FOUND)
// Save the number of the found entry.
*fHandle = bHandle;
} // End: search for an entry.
if (status == FOUND)

Random Posts

No comments yet

Leave a Reply

You must be logged in to post a comment.