Project Home
Project Home
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - Getting file descriptor from a DIR structure: (4 Items)
   
Getting file descriptor from a DIR structure  
I'm currently porting the latest version of DBUS (1.2.3) from Linux to QNX 6.3.2. DBUS has a platform specific wrapper 
function that takes a DIR pointer (e.g. dirp) and retrieves the _PC_NAME_MAX from the file descriptor associated with 
the DIR structure. Under Linux, the file descriptor associated with the DIR structure can be retrieved as follows:

dirp->dd_fd (or) dir->__d_fd depending on the Linux/Unix release.

This file descriptor is then used in the call to fpathconf() to get the maximum path length, e.g.

long name_max = fpathconf(dirp->dd_fd, _PC_NAME_MAX)

Unfortunately, QNX treats the DIR structure has a completely opaque structure from the user's perspective (e.g. 
apparently there is no file descriptor to access).

Can someone suggest a work-around that would give me access to the file descriptor associated with the DIR structure? 
For now, I've hard-coded name_max to 1024 but I know that doesn't hold true for all file systems (notably DOS) supported
 under QNX.

Re: Getting file descriptor from a DIR structure  
Glenn Schmottlach wrote:
>  I'm currently porting the latest version of DBUS (1.2.3) from Linux
>  to QNX 6.3.2. DBUS has a platform specific wrapper function that
>  takes a DIR pointer (e.g. dirp) and retrieves the _PC_NAME_MAX from
>  the file descriptor associated with the DIR structure. Under Linux,
>  the file descriptor associated with the DIR structure can be
>  retrieved as follows:
>
>  dirp->dd_fd (or) dir->__d_fd depending on the Linux/Unix release.
>
>  This file descriptor is then used in the call to fpathconf() to get
>  the maximum path length, e.g.
>
>  long name_max = fpathconf(dirp->dd_fd, _PC_NAME_MAX)
>
>  Unfortunately, QNX treats the DIR structure has a completely opaque
>  structure from the user's perspective (e.g. apparently there is no
>  file descriptor to access).

Actually, the problem is that a directory under QNX is represented by
an array of file descriptors that connect the client to all of the servers
that provide filesystems along that path (which are transparently unioned
together to provide a single cohesive view).

>  Can someone suggest a work-around that would give me access to the
>  file descriptor associated with the DIR structure? For now, I've
>  hard-coded name_max to 1024 but I know that doesn't hold true for all
>  file systems (notably DOS) supported under QNX.

What would you do if you had many different filesystems with different
capabilities?  I guess you'd take the lesser of them all?  One thing that
you could do is to wrap the DIR structure into a custom structure that
contains a DIR * and stores the original path.

The source for the directory code is all in lib/c/1/iodir.c in the C library
source.

Thomas
Re: Getting file descriptor from a DIR structure  
 
> What would you do if you had many different filesystems with different
> capabilities?  I guess you'd take the lesser of them all?

In the general case, if I go with a hard-coded solution, I'd have to return the minimum path length of all supported 
file systems. Obviously this is not an ideal solution. For my specific project, assuming I know the file system that 
will be used, this is a feasible, albeit, a non-general solution.

> One thing that
> you could do is to wrap the DIR structure into a custom structure that
> contains a DIR * and stores the original path.

I am constrained by the DBUS API with regards to mucking around with the DIR structure and embedding it inside a wrapper
 structure the contains the original path. Plus, I don't want to introduce any more modifications than necessary into 
the DBUS source since it will make future updates much more problematic.

The only alternative I could think of is if there was some way to extract the *full* path from the DIR structure. I know
 I could use readdir(dirp) to get the dirent structure which contains a directory entry (and the d_name field). 
Unfortunately, that does not include the path leading to the directory entry (I guess it assumes you would've used 
opendir() to get the dirp in the first place). Is there any way to deduce the full path to the named directory entry 
given only the dirp?



Re: Getting file descriptor from a DIR structure  
What you're describing is dirfd() which last I heard
was on some extended api posix draft.  Looking
at the code for fchdir() might be useful.

-seanb