Linux stat function and stat command

Keywords: Linux VirtualBox socket vim

Stat function and stat command

In the linux file, [inode = index node] explains: to understand inode, you must understand disk and [directory item], inode is actually the intermediate material connecting [directory item] and disk.

  • The big circle in the picture represents the hard disk, and the small circle in the picture represents a file stored on the disk.
  • The node of [inode = index node] contains:
    • file size
    • Last Modification Time of Documents
    • The user to which the file belongs
    • Permissions of files
    • Hard Link Count (ls-l Number)
    • Block location: Specifies the specific location where files are stored on disk.
  • The hello in the figure below is a common file, and hello.hard is a hard link to hello.
  • The contents of each file are shown below. The contents of each file are as follows:
    • file name
    • Size of the directory item
    • Type of file
    • inode

  • How to view the file [inode]? Use the [-i] option

    Ls-li file name

    Implementation results:

    ys@ys-VirtualBox:~/lianxi1$ ls -li hello hello.hard 
    3801352 -rw-rw-r-- 2 ys ys 0 4 Month 2411:01 hello
    3801352 -rw-rw-r-- 2 ys ys 0 4 Month 2411:01 hello.hard

    The discovery that hello and hello.hard's inode (3801352) are the same shows that only one copy is stored on disk.

  • How do I view directory entries? Open the directory (lianxi1) with emacs or vim, as shown below. But you can't see the file [inode].

1. stat function: Get the file attributes of the specified file, and store the file attributes in the structure stat.

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int stat(const char *pathname, struct stat *statbuf);
int fstat(int fd, struct stat *statbuf);
int lstat(const char *pathname, struct stat *statbuf);

Strct stat structure:

struct stat {
               dev_t     st_dev;         /* ID of device containing file */
               ino_t     st_ino;         /* Inode number */
               mode_t    st_mode;        /* File type and mode */
               nlink_t   st_nlink;       /* Number of hard links */
               uid_t     st_uid;         /* User ID of owner */
               gid_t     st_gid;         /* Group ID of owner */
               dev_t     st_rdev;        /* Device ID (if special file) */
               off_t     st_size;        /* Total size, in bytes */
               blksize_t st_blksize;     /* Block size for filesystem I/O */
               blkcnt_t  st_blocks;      /* Number of 512B blocks allocated */

               /* Since Linux 2.6, the kernel supports nanosecond
                  precision for the following timestamp fields.
                  For the details before Linux 2.6, see NOTES. */

               struct timespec st_atim;  /* Time of last access */
               struct timespec st_mtim;  /* Time of last modification */
               struct timespec st_ctim;  /* Time of last status change */

           #define st_atime st_atim.tv_sec      /* Backward compatibility */
           #define st_mtime st_mtim.tv_sec
           #define st_ctime st_ctim.tv_sec
           };
  • st_dev: Device ID, not very common

  • st_ino: [inode], [inode]? I don't know, just look at the explanation above about inode.

  • st_mode: Types and permissions of files, 16 bits in total, as shown below.

    • Permissions for 0-11-bit control files

    • Types of 12-15-bit control files

    0-2 bits: other user rights

    3-5 Bit: Group User Rights

    6-8 bits: local user rights

    9-11 bits: special permissions

    12-15 bits: File type (since the file type is only 7, 12-14 bits is enough).

The macros of the file type are as follows (the following digits are octal):

  • S_IFSOCK 0140000 socket
  • S_IFLNK 0120000 symbolic link
  • S_IFREG 0100000 regular file (ordinary file)
  • S_IFBLK 0060000 block device (block device file)
  • S_IFDIR 0040000 directory (directory)
  • S_IFCHR 0020000 character device (character device file)
  • S_IFIFO 0010000 FIFO (Pipeline)
Another file type macro:       
       S_ISREG(m)  is it a regular file?

       S_ISDIR(m)  directory?

       S_ISCHR(m)  character device?

       S_ISBLK(m)  block device?

       S_ISFIFO(m) FIFO (named pipe)?

       S_ISLNK(m)  symbolic link?  (Not in POSIX.1-1996.)

       S_ISSOCK(m) socket?  (Not in POSIX.1-1996.)

The macros for file permissions are as follows:

       S_ISUID     04000   set-user-ID bit
       S_ISGID     02000   set-group-ID bit (see below)
       S_ISVTX     01000   sticky bit (see below)

       S_IRWXU     00700   owner has read, write, and execute permission
       S_IRUSR     00400   owner has read permission
       S_IWUSR     00200   owner has write permission
       S_IXUSR     00100   owner has execute permission

       S_IRWXG     00070   group has read, write, and execute permission
       S_IRGRP     00040   group has read permission
       S_IWGRP     00020   group has write permission
       S_IXGRP     00010   group has execute permission

       S_IRWXO     00007   others (not in group) have read,  write,  and
                           execute permission
       S_IROTH     00004   others have read permission
       S_IWOTH     00002   others have write permission
       S_IXOTH     00001   others have execute permission
  • st_nlink: Hard Connection Count

  • st_uid: The ID of the user to which this file belongs

  • st_gid: The group ID of the user to which this file belongs

  • st_rdev: ID for special devices, not very common

  • st_size: File size

  • st_blksize: I don't know what it is.

  • st_blocks: I don't know what it is.

  • struct timespec st_atim: last access time

  • struct timespec st_mtim: the last modification time

  • struct timespec st_ctim: the time at which the final state changes

    struct timespec {
      __kernel_time_t tv_sec;  /* seconds */Current time to 1970.1.1 00:00:00 Seconds
      long        tv_nsec;    /* nanoseconds *//Number of nanoseconds (I don't know where to go)
    };
    1s  second   = 1000ms Millisecond
    1ms Millisecond = 1000us Microsecond
    1us Microsecond = 1000ns nanosecond

pathname: File name

Return value: 0 represents success; - 1 represents failure and sets error

Example: statbuf is the structured stat. It can be seen that st_mode is a decimal number.

  • st_mode

    Using GDB to display st_mode, it is found that the returned st_mode is a decimal number. Using the command of gdb, [p/o] (o stands for 8-digit representation), the decimal 33204 is converted into 8-digit [0100664]. The first 0-digit pen is 8-digit, and the last three [100] represent the file type. It can be seen from the above description that [100] represents the ordinary file, and the last three [664] represent the text. The permission of the component (this user: rw-, group user: rw-, other users: r-). So you can know the file type and permission settings from st_mode (using only 16 bits, really save space, bull-punch!)

  • st_uid

  • st_gid

    It is found that st_uid and st_gid are 1000, but how does this 1000 correspond to users? Look at the / etc/passwd file and find that both uid and GID for ys are 1000, so they correspond.

The stat command, which corresponds to the stat function, is executed as follows:

ys@ys-VirtualBox:~/lianxi1$ stat hello
  File: hello
  Size: 11          Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d  Inode: 3801352     Links: 2
Access: (0764/-rwxrw-r--)  Uid: ( 1000/      ys)   Gid: ( 1000/      ys)
Access: 2019-04-24 17:02:39.199461489 +0800
Modify: 2019-04-24 16:54:16.407461489 +0800
Change: 2019-04-24 17:03:44.927461489 +0800

Posted by nesargha on Wed, 24 Apr 2019 14:21:34 -0700