Why directory record length is variable in ext2 filesystem?
Hi,
The declaration for directory record length in ext2 filesystem is as follows: Code:
#define EXT2_NAME_LEN 255 THANKS! |
Quote:
The struct is a convenient way for the C programmer to look at the data. If the final element of a struct is an array, it is often not necessary in real life for the number of elements in the array to be a true indication of how large the array is. If the struct is itself in an array, then it is indeed necessary. One needs to know that the length of each element of the array of structs is constant, so one can index into that array correctly. But this is different. You use the length of each item (as stored in the rec_len element) to calculate where the next entry begins. Why do they do things this way? Because most names as stored in directory entries are much, much shorter tha the maximum, so this way we can store more directory entries within a directory block. |
Quote:
Code:
struct ext_dir_entry |
Quote:
Quote:
Quote:
Code:
sizeof()s: 8 1024 Code:
#include <stdio.h> Quote:
|
Quote:
And in my small code example, using sizeof got the same results no matter how short the `name' array was. If the lengths of the structure `ext_dir_entry' instances could be variable, then does it mean the sizeof operator returned wrong size? Or the sizeof operator should not be used to determine the size a structure of this kind? |
Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
|
wje_lq, thank you so much for the detailed answers.
I think many of my questions were answered. But I still can't figure why people do not put such kind of information into the standard. I mean, putting a constant in the square brackets instead of empty, you still get variable-length array here in the structure. Quite confused. |
Quote:
Suppose you have the following struct, designed to handle up to twelve children: Code:
/* Disclaimer: This ugly code has not been tested, or even compiled. */ We could write a function which calculates the number of bytes required to store a particular family's information: Code:
/* Disclaimer: This ugly code has not been tested, or even compiled. */ Code:
/* Disclaimer: This ugly code has not been tested, or even compiled. */ Code:
/* Disclaimer: This ugly code has not been tested, or even compiled. */ But programmers do it all the time with flexible array members. You can put anything between the square brackets ([], [1], [255], anything) and it will work. And the only real reason to put any mention in the standard is that [] is allowed. If, in a different world, [] were not allowed, programmers could put any non-negative integer between the square brackets, and it would be just as legal as the ugly code I wrote above. Allowing [], with no integer between the square brackets, is the only reason this is mentioned in the standard at all. It's a good way of documenting that the array length is flexible (determined at runtime), and one could argue that the implementers of ext2 should have done that. |
All times are GMT -5. The time now is 05:54 PM. |