Re: [PATCH] Add getdents32t syscall

From: Linus Torvalds
Date: Thu Feb 26 2004 - 18:23:36 EST




On Thu, 26 Feb 2004, Ulrich Drepper wrote:
> Linus Torvalds wrote:
>
> > - pre-fill the dirent area with 0xff or something
>
> fill whole temporary buffer allocated by opendir() for every call to
> getdents(2)?

No no.

You only need to do this _once_. Once you know that the kernel is ok, you
never ever need to do it again.

It's not even "once per file descriptor" or anything like that. It's
literally _once_.

And you don't need to fill the whole buffer even that first time. You
only need to fill enough to guarantee that the _first_ entry is filled
up.

In fact, if you're willing to have an algorithm that always works, but
might under some circumstances be a bit conservative, you can avoid
filling entirely, and just have a static flag that says "newformat", you
can do the following:

- assume old format
- if you ever see a reclen that is "too big" for the name length, you
know you have a new-format case (this will happen with any name that is
of length 1 modulo 4 on a 32-bit architecture).

For old-format stuff, you return DT_UNKNOWN, or you do your old existing
song and dance. For new-format stuff you do the trivial thing.

And guess what? The entry "." will give you the information abotu whether
it is old-format or not. On an old-format thing, "." will look like this:

offset
0: 32-bit d_ino
4: 32-bit d_offset = 12
8: 16-bit d_namelen = 1
10: string ".\0"

12: 32-bit d_ino for the next entry
...

while with a new-format readdir you will get

offset
0: 32-bit d_ino
4: 32-bit d_offset = 16
8: 16-bit d_namelen = 1
10: string ".\0"
12-14: three bytes of garbage
15: 8-bit d_type

16: 32-bit d_ino for the next entry..
...

Notice? You are guaranteed to find out really quickly whether it's old- or
new-format unless the user is doing something really really strange, and
even if the user is doing something strange, returning D_UNKNOWN is always
"correct".

So not only is my solution simple in kernel space, it allows you to
simplify glibc too, if you are willing to make the old case go slower.

Linus
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/