Linux under Dir,dirent,stat and other structural body detailed

Source: Internet
Author: User

Excerpt from: http://www.liweifan.com/2012/05/13/linux-system-function-files-operation/

Recently in the Linux file operation related chapters, encountered a few structure, was engaged in dizzy, today is free, carefully studied a bit, benefited.

First of all, the dir is a struct, the following is the definition of the DIR structure:

  1. struct __dirstream
  2. {
  3. void *__fd;
  4. Char *__data;
  5. int __entry_data;
  6. Char *__ptr;
  7. int __entry_ptr;
  8. size_t __allocation;
  9. size_t __size;
  10. __libc_lock_define (, __lock)
  11. };
  12. typedef struct __dirstream DIR;

The dir struct is similar to file and is an internal structure, and the following functions use this internal structure to hold information about the directory that is currently being read (excerpt from advanced Programming in the UNIX Environment (second edition)). The function dir *opendir (const char *pathname), which is the open file directory, returns a pointer to the DIR struct, which is used by the following functions:

    1. struct Dirent *readdir (DIR *DP);
    2. void Rewinddir (DIR *dp);
    3. int Closedir (DIR *DP);
    4. Long Telldir (DIR *DP);
    5. void Seekdir (DIR *dp,long Loc);

With regard to the DIR structure, we know so much that we don't have to go into the structural members.

Then there is the dirent structure, first of all we need to figure out the concept of directory file: This file contains the names of other files and pointers to information related to those files (excerpt from Advanced Programming for UNIX Environment (second edition)). As can be seen from the definition,dirent not only points to the directory, but also to the specific files in the directory, the Readdir function also reads the file under the directory, this is the evidence. The following is the definition of the dirent structure:

  1. struct dirent
  2. {
  3.   Long D_ino; / * Inode number Index Node # * /
  4. off_t D_off; / * Offset to this dirent in the directory file * /
  5. unsigned short d_reclen; / * Length of this d_name filename * /
  6. unsigned char d_type; /* The type of d_name file type * /
  7. Char d_name [name_max+1]; /* File name (null-terminated) file name, maximum 255 characters */
  8. }

From the above definition can also be seen, dirent structure stored in the information about the file is very few, so Dirent also plays an index role, if you want to get similar to ls-l the effect of the file information, must rely on the stat function.

The file name read through the Readdir function is stored in the D_name member of the struct dirent, and the function

int stat (const char *file_name, struct stat *buf);

is to get the details of the file named D_name, which is stored in the stat structure. The following is the definition of the stat structure:

  1. struct STAT {
  2. mode_t St_mode; //File access rights
  3. ino_t St_ino; //Index node number
  4. dev_t St_dev; //The device number used by the file
  5. dev_t St_rdev; //device number of the device file
  6. nlink_t St_nlink; number of hard connections to//files
  7. uid_t St_uid; //Owner user identification number
  8. gid_t St_gid; //Group identification number
  9. off_t st_size; //File capacity in bytes
  10. time_t st_atime; //Last time the file was accessed
  11. time_t st_mtime; //Last time the file was modified
  12. time_t st_ctime; //The last time the file state was changed
  13. blksize_t st_blksize; //The size of the disk block that contains the file
  14. blkcnt_t st_blocks; //disk block occupied by this file
  15. };

This record of information is very detailed, huh, huh.

Finally, to summarize, what should we do to get detailed information about the B file under a directory (e.g. a)?

First, we use the Opendir function to open Directory A and return to dir struct C, which points to directory a.

Next, we call the Readdir (c) function to read all the files (including directories) under Directory A, and return to the dirent struct D, which points to all the files under directory A.

Then we traverse D, call stat (D->name,stat *e) to get the details of each file, stored in the stat struct E.

As a whole, it is a process of gradual refinement, in which three kinds of structures play different roles.

Linux under Dir,dirent,stat and other structural body detailed

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.