I’m working to create a rudimentary file system in c++ and am having issues assigning names to files in my directory table. Here is the definition of a directoryEntry struct.
typedef struct{
char name[112];
unsigned int index;
unsigned int size;
unsigned int type;
unsigned int creation;
} directoryEntry;
Here is some code where I create a new blank file.
if(canFit){
for ( int i = 0; i < numClusters; ++i){
if (directoryTable[i].name[0] == 0x00){
//directoryTable[i].name = *newFile;
strcpy(directoryTable[i].name, newFile);
printf("%s %d ", directoryTable[i].name, i);
directoryTable[i].index = location;
directoryTable[i].size = 0;
directoryTable[i].type = 0x0000;
directoryTable[i].creation = time(NULL);
return 0;
}
}
What is happening here is I’m scanning the directory table for entries that contain a null byte as the first character of the filename. This tells me that that particular directory entry is not occupied. Then I use strcpy to assign the newFile (which is declared as a parameter as char newFile[112]).
The problem is the directory name prints properly in the above printf (printf(“%s %d “, directoryTable[i].name, i);) but it appears that the data contained in directoryTable[i].name is deleted. The above code is contained in a method called touch. When the code executes I get something like the following:
touch(“file1”);
file1 0
touch(“file2);
file2 0
which means that the value associated to index 0 is temporarily changed to file1, but is then changed back to several null bytes (Which is how it is originally allocated.) So my question is, why doesn’t the value of directoryTable[i] stay the same through multiple calls to touch? directoryTable is a global variable so if the value is assigned it shouldn’t disappear when out of scope.
It should also be noted that directoryTable is defined as directoryEntry* directoryTable if that has any bearing on my problem.
My professor will not allow us to use most of the standard libraries in C++. Strings, for example, are illegal. So if you are wondering, that is why I’m not using strings.
Thank you for your time.
EDIT:
Here is code where the directoryEntrys that make up the directoryTable are allocated and added to the directoryTable. They are also added to a file but I feel that is irrelevant to this particular question:
fseek(fp, clusterSize * root, SEEK_SET);
for (int i = 0; i < bootRecord[0] / 128 ; ++i){
fseek(fp, clusterSize * root + 128 * i, SEEK_SET);
directoryEntry * newEntry = (directoryEntry *)malloc(sizeof(directoryEntry));
//newEntry->name = (char *)malloc(112);
//memset(newEntry->name, 0x00, 112);
newEntry->size = 0;
newEntry->type = 0;
newEntry->creation = 0x0000;
newEntry->index = 0;
fwrite(newEntry->name, 112, 1, fp);
fwrite(&newEntry->size, sizeof(int), 1, fp);
fwrite(&newEntry->type, sizeof(int), 1, fp);
fwrite(&newEntry->creation, sizeof(int), 1, fp);
fwrite(&newEntry->index, sizeof(int), 1, fp);
directoryTable[i] = *newEntry;
}
You’ll need some sort of actual storage defined for your directory table:
The pointer you’ve defined just points to random memory. I’m surprised it doesn’t segfault.
Edit
You malloc the DirectoryEntries, but you’re not storing them anywhere valid. You have no storage for your pointers, so you’re losing them as soon as you assign them to the directoryTable You’ll either need to create an array of DirectoryEntry*:
Where you can put your malloc’d pointers or, just create the whole table at runtime: