bug-hurd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] Explain how to access data returned from dir_readdir


From: Jessica Clarke
Subject: Re: [PATCH] Explain how to access data returned from dir_readdir
Date: Fri, 23 Apr 2021 12:17:20 +0100

On 23 Apr 2021, at 04:13, Andrew Eggenberger <andrew.eggenberger@gmail.com> 
wrote:
> 
> I wrote this patch for the website after struggling with dir_readdir. 
> Hopefully this will help the next person who needs it (probably me in a few 
> months).

This is a part of POSIX, the only Hurd-specific thing is the use of the
underlying RPC rather than the standard C function. I don’t see why we should
document things that are universally true rather than Hurd-specific. If you
want to know how to use dirent then go read a POSIX manual.

Jess

> ---
>  hurd/interface/fs/19.mdwn | 26 ++++++++++++++++++++++++++
>  1 file changed, 26 insertions(+)
> 
> diff --git a/hurd/interface/fs/19.mdwn b/hurd/interface/fs/19.mdwn
> index 86625d44..9ef9f8a4 100644
> --- a/hurd/interface/fs/19.mdwn
> +++ b/hurd/interface/fs/19.mdwn
> @@ -27,3 +27,29 @@ returning an array of struct directs in `data`.  The 
> number of entries
>  successfully read is returned in `amount`.  If `entry` is bigger than the 
> index
>  of the last entry, then 0 is returned in `amount`.  If `bufsize` is nonzero,
>  never return more than `bufsize` bytes of data regardless.
> +
> +Although the value returned in `data` is described as an "array of struct
> +directs" in fs.defs, typical array access (direct[4], etc.) will only work to
> +access the individual dirents in `data` if they all happen to be the size of
> +struct direct (an alias of struct dirent in glibc). This is unlikely because
> +the `d_name` member of struct dirent is a flexible `char` array to account 
> for
> +the variability of filename lengths. One way to access each member in turn is
> +demonstrated below.
> +
> +  data_t dirents;
> +  struct dirent* d;
> +  mach_msg_type_number_t dataCnt = 0;
> +  int amt = 0, i;
> +  
> +  err = dir_readdir(dirport, &dirents, &dataCnt, 0, -1, 0, &amt);
> +  if (err != KERN_SUCCESS)
> +    error(1, 0, "dir_readdir failed.\n");
> +
> +  while (i++ < amt){
> +    d = (struct dirent*)dirents;
> +
> +    /** ... do things with d ... **/
> +
> +    dirents += d->d_reclen;
> +
> +  }
> -- 
> 2.31.1
> 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]