Question

I thought this would be an easy question, but I can't find the answer. I've mainly been reading these specs:

http://www.nongnu.org/ext2-doc/ext2.html

It doesn't seem to mention if a directory entry's name is supposed to be null-terminated. I'm thinking that it's undefined. There is a name_len field, so you can easily print the name anyway.

Are ext2 directory names guaranteed to be null-terminated?

I ask because in testing my ext2 driver on my hobby OS I noticed when I printed some directory names some garbage characters came out on the end. If I simply print name_len characters everything is fine, although it'd be nice if it was null-terminated.

Thanks!

Was it helpful?

Solution

I know this is an old question, but for anyone else who might find this:

No, it is not guaranteed to be null terminated. In fact, it should only be null terminated if the length of the name (name_len) is not a multiple of 4.

From Understanding the Linux Kernel, 3rd edition:

... the length of a directory entry is always a multiple of 4 and, therefore, null characters (\0) are added for padding at the end of the filename, if necessary. The name_len field stores the actual filename length

With an example below (from the book). Notice that the name "sbin" has no null termination, because the length (4) is a multiple of 4:

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top