Linux Server development: Stat (), Fstat (), Lstat () detailed introduction + Case presentation

Source: Internet
Author: User
Tags function definition lstat posix



1. Dependent header Files

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

2. Function definition:

By passing in the file path, the struct stat struct pointer

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

Gets the properties of the file with the file descriptor. This action when the file is opened
int fstat (int fd, struct stat *buf);

Gets the properties of the file with the file descriptor. This action when the file is opened
int Lstat (const char *path, struct stat *buf);

Description: Execution successfully returned 0, failure returned-1, error code stored in errno

Attention:

Given a path

The A:stat function returns a structure of information related to this named file

The B:fstat function obtains the file information that has been opened on the descriptor Filedes

The C:lstat function is similar to stat, but when a named file is a symbolic connection, LSTAT returns information about the symbolic connection, not the file referenced by this symbolic link

3. The function of these functions is to return information about a file;

A:stat has both the command and the same name function, which is used to obtain the main information in the inode of the file, stat-Trace symbolic link

B:lstat do not track symbolic links

4.stat Inside Time discrimination

Atime (last accessed time)

Mtime (last modified): Refers to the time when the contents of the file were last changed

CTime (Last Modified time): Refers to the recent change Innode time, to note that mtime information is one of the innode, so once it is modified, this item will also be modified

5. All of these system calls return a stat structure that includes the following:

struct STAT {
dev_t St_dev; /* ID of device containing file device number */
ino_t St_ino; /* Inode Number Node # */
mode_t St_mode; /* Protection file type and access permissions */
nlink_t St_nlink; /* Number of hard links attached to the file, the file value is just 1*/
uid_t St_uid; /* User ID of owner users id*/
gid_t St_gid; /* Group ID of the owner group id*/
dev_t St_rdev; /* Device ID (if special file) (device type) If this file tail is a device file, its device number */
off_t st_size; /* Total size, in bytes file bytes (file size) */
blksize_t st_blksize; /* BlockSize for filesystem I/O block size (file system I/o buffer size), type unsigned long type */
blkcnt_t st_blocks; /* Number of 512B blocks allocated, allocated 512 bytes of block, type unsigned long type */
time_t St_atime; /* Date Last access time */
time_t St_mtime; /* Time of last modification change */
time_t St_ctime; /* Time of last status change inode changed */
};

6. Using the following macro (macros) to define the file type, the following St_mode fields include the following sections:

S_isreg (M) is it a regular file? Whether it is a regular file

S_isdir (m) directory? Whether it is a directory

S_ISCHR (m) character device? Whether it is a character device

S_isblk (m) block device? Whether it is a block device

S_isfifo (M) FIFO (named pipe)? Whether it is an input/output (pipe)

S_islnk (m) symbolic link?      (not in posix.1-1996.) Whether it is a symbolic link

S_issock (m) socket?              (not in posix.1-1996.) Whether it is a socket

7. The following flags are defined for this St_mode domain

S_IFMT 0170000 bit mask for the file type bit field defines a bitmask for a bit domain
S_ifsock 0140000 Socket Socket
S_iflnk 0120000 Symbolic Link symbolic connection
S_ifreg 0100000 Regular file regular files
S_IFBLK 0060000 block Device
S_ifdir 0040000 Directory Catalog
S_IFCHR 0020000 character Device character devices
S_ififo 0010000 FIFO Advanced first-out


           s_isuid    0004000    Set-user-id bit                                   File (set User-id on execution) bit
           s_isgid     0002000   Set-group-id bit (see below)              File (set Group-id on execution) bit
           s_isvtx     0001000   sticky bit (see below)           The sticky of the              file is


           s_irwxu    00700      Mask for file owner permissions     
            s_irusr    00400     owner has read permission              owner has Read permissions
            s_iwusr    00200     Owner has write permission to the owner of the permission            
           s_ixusr    00100      Owner has execute permission         the owner has execute permissions


S_irwxg Mask of 00070 mask for group permissions group permissions
S_IRGRP 00040 Group has read access to permission groups

S_IWGRP 00020 Group has write permission groups some permissions
S_IXGRP 00010 Group has EXECUTE permission groups have Execute permissions


S_irwxo 00007 Mask for permissions to others (not in group) permission mask
S_iroth 00004 Others have Read permission other people have access
S_iwoth 00002 Others have write permission others have writing permissions
S_ixoth 00001 Others have execute permission others have execution permissions

8. About Sticky position

If a directory has a sticky bit (s_isvtx), then the files in this directory can only be deleted or modified by the owner of the file, the owner of the secondary directory, or root.

9. In addition

struct STATFS {
Long F_type; File system type
Long f_bsize; Block size
Long f_blocks; How many blocks?
Long F_bfree; Free Blocks
Long F_bavail; Available blocks
Long F_files; Total File node
Long F_ffree; Free File node
fsid_t F_fsid; File System ID
Long F_namelen; Maximum length of file name
Long f_spare[6]; Spare for later
};

10.stat () function invocation case (called stat () programmatically and renders the information in the stat struct)

Operation Result:

Linux Server development: Stat (), Fstat (), Lstat () detailed introduction + Case presentation

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.