1

I'm having some difficulty understanding the meaning behind all this parameters in the dirent structure.

struct dirent{
        ino_t           d_ino;      Inode number
        off_t           d_off;      Not an offset; see below
        unsigned short  d_reclen;   Length of this record
        unsigned chart  d_type;     Type of file; 
                                    not supported by all filesystem types

        char            d_name[256]; Null-Terminated filename
    }

I really need to understand this because I have to work on some files. Correct if I'm wrong, this struct is returned when you use opendir on a DIR* object right? This object is non-other than metadata of the file inside the directory, and every time I use it will return me the metadata of the next file, right?

The parameters that I don't understand are: d_off -> not an offset? What is it then? See below where? I checked the original page with all the info and I couldn't find where was I supposed to look. d_type -> what do they mean with "not supported by all filesystem types"? Which filesystem should I look out for? Okay, with this information how can I open the file on which I have to modify the data? Do I just use the d_name, or is there something way more comfortable I can rely on? Those are all my doubts, thank you in advance.

B.Castarunza
  • 135
  • 12
  • 2
    Did you read [this](http://man7.org/linux/man-pages/man3/readdir.3.html)? Anyway the only field you need to deal with is `d_name`, the others are useless in most cases. – Jabberwocky May 08 '20 at 15:49
  • Thank you for clarifying. Yeah, I read the beginning but I couldn't find the see below part, now I found it. – B.Castarunza May 08 '20 at 18:00

2 Answers2

1

The POSIX standard only defines the structure to have d_ino and d_name. The remaining structure fields are Linux specific.

The Linux manual page says that d_off is related to the telldir function, and that it should be considered an opaque value.

In short, you should never need to read or otherwise use this member.

As for the d_type member, its meaning and values is well-documented in the Linux manual page. On any normal filesystem it should be valid.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • If I'm exploring a directory with opendir, is it normal that the files are read in a random order? I have a directory with n files each named *File_i*, where *i* is a number between *0 and n*, but the order in which I print out their name is consistent but casual, what I mean is that the order is: "FIle1 FIle7 File6 File3 ecc . . ." and every time I run the program the same order is outputted. I only use one process to run it. – B.Castarunza May 08 '20 at 18:26
  • @B.Castarunza Could be ordered by inode number? If you want any specific ordering you need to collect all file-names, order them in the way you want, and then print in that order. – Some programmer dude Jan 05 '22 at 07:37
0

The only two members of that struct that are guaranteed to exist as per posix are d_name and d_ino.(Source)

In other words you should ignore the other struct members, and only use the following standard functions to manipulate dirent structs.

PiRocks
  • 1,708
  • 2
  • 18
  • 29