Print

Print


On Wed, Apr 25, 2007 at 06:09:56PM +0400, Eygene Ryabinkin wrote:

> Maarten, good day.
> 
> Wed, Apr 25, 2007 at 03:35:09PM +0200, [log in to unmask] wrote:
> > I meant to say the max. length of the d_name field may be larger.
> 
> Precisely. And there is no good way to determine it at the
> compilation time.
> 
> > > You need to allocate space for the struct with something like:
> > >   struct dirent *buffer;
> > >   size = dirent_buf_size(gridmapdirstream);
> > >   buffer = (struct dirent *)malloc(size);
> > > you aren't supposed to use the struct without allocating enough space
> > > for it.
> > > 
> > > Have a look at:
> > > http://seclists.org/fulldisclosure/2005/Nov/0001.html
> > > for the dirent_buf_size function and the reason why you can not just
> > > use something like:
> > >   pathmax = pathconf("foo", _PC_NAME_MAX);
> > >   buffer = (struct dirent *)malloc(offsetof(struct dirent, d_name) + pathmax + 1);
> > 
> > Since we have plenty of memory, what do you think of this:
> > 
> >     char buf[sizeof(struct dirent) + PATH_MAX + 1];
> 
> And if the maximal length of dirent->d_name at the NIKHEF machine
> (or some other host) will be longer than 2*PATH_MAX? This can help
> in the particular case, but it is not the general way to fix this
> issue if it lies in the size of the 'struct dirent'.

dirent->d_name is usually quite small, It's valid to difine it as
char d_name[1] (linux uses 255 which is the same as NAME_MAX)

> Note, that the usage of the 'pathconf' function makes us safe for
> any filesystem that correctly reports its maximal number of bytes
> in the pathlen. And this is done in the run-time.  You can use
> something like 'sizeof(struct dirent) + pathconf(...) + 1', but use
> malloc, since we need the run-time memory allocation.

offsetof(struct dirent, d_name) + .... is better you don't have to waste
memory with whatever size the d_name is using.

> I assume that the root of the problem is that NFS names can be
> longer than the local ones? No? For the test I am attaching the
> very simple C program that prints the results of the pathconf() for
> various directories and files. I am getting the same results for
> the local files and NFS mounts on our cluster. Ronald, can you try
> the program with the names of your grid-mapfile and some local file?

Or you can use getconf {NAME_MAX,PATH_MAX,_POSIX_PATH_MAX} .... :)

Kostas

PS> Of course it could be something else that is causing the crash and
not the bad usage of readdir/readdir_r.