How to remove an entry in a fixed length array in C?

242 Views Asked by At

I am working on a program that resembles the Unix File System. I am working in C. I am stuck on the following problem:

  • The function in question is a function that removes a specific file entry from a directory.
  • The directory has an iNode and a data block. This data block is the array of fixed size in which I want to delete an entry.
  • The array in which I am working on is an array of fixed length of 14 entries of structs
  • The struct looks like this
typedef struct {
    int16_t iNodeNumber;
    char Filename[14];
} Entry;

Note that the memory of the array is on the stack, not the heap.

What I want : Remove a specific entry from the array while keeping the length of the array

What I have done : I replace the current position (the one which I have to delete the info) by the content of the next index until I reach the end of the array. Thus, I can make sure that upon deletion, every entry that has an empty block in the next array position is actually the last one. (No fragmentation)

However this does not work if I want to remove the last entry of the array (position 13). Also I thought about setting the Filename to an empty string but what about the iNodeNumber? It is a number that ranges from 2 to 23; 0 and 1 are specific iNode numbers reserved for other usage.

Thanks!

EDIT : Here are more explanations to the problem : Say we have a directory, let's take this directory.

/example

In UFS, as each file is represented by an Inode and not a name, this directory would be represented by this struct:

typedef struct {
stat iNodestat;
int16_t dataBlock[14]
} iNodeEntry

The stat struct can be omitted it's simply a struct which contains informations on the inode like the number, the mode, the file size, etc.

This dataBlock is an array of integers. Those integers simply reference a memory block. A regular file iNodeEntry may have 14 blocks of data. In the case of a directory, it only has one block of data. This sole block number references a memory block which contains the Array that is the subject of this post.

This array is composed of the Entry struct shown above. It contains exactly 14 of these structs.

To conclude, here's a concrete example. Consider :

  • /example directory has no file
  • its iNode number is 2

It would then have two entries filled with information out of the 14 available in the array :

  • filename : '.' , iNode number : 2
  • filename : '..', iNode number : whatever the parent directory iNode number is
0

There are 0 best solutions below