Fstat, stat, and Lstat usage

Source: Internet
Author: User
Tags lstat

The STAT system call series includes Fstat, Stat, and Lstat, all of which are used to return "related file state information", except that the source file is set differently. The first introduction is a very important type of struct, named struct Stat. It can be said that without this struct stat support, the above three system calls will be unable to move. There is a small difference in the definition of this struct stat struct in different unix/linux systems, but you do not have to worry about it, it does not affect our use.

The domains that we commonly use in struct stat structures and which each platform must have are:
St_mode file permissions and file type information
St_ino the inode associated with the file
St_dev the device that saved the file
St_uid file owner's UID number
St_gid file belongs to the master GID number
The time that the St_atime file was last visited
St_ctime file permissions, owner, group, or content last modified time
The time that the contents of the St_mtime file were last modified.
St_nlink the number of hard connections on this file


struct STAT {
dev_t St_dev; Device number of the file
ino_t St_ino; Node
mode_t St_mode; Type of file and permissions to access
nlink_t St_nlink; The number of hard connections to the file, the file value that was just created is 1
uid_t St_uid; User ID
gid_t St_gid; Group ID
dev_t St_rdev; (device type) If this file is a device file, its device number
off_t st_size; Number of File bytes (file size)
unsigned long st_blksize; Block Size (file system I/o buffer size)
unsigned long st_blocks; Number of blocks
time_t St_atime; Last Access time
time_t St_mtime; Last modification time
time_t St_ctime; Last change time (refers to attribute)
};

The original definition of a struct stat structure that can be extracted separately from Solaris (UNIX) and Fedora (Linux) can be found to be different, but the primary domain is exactly the same. St_mode This field is not as easy to use as other domains, the values of the other fields are obvious, and the St_mode domain requires some macros to work with. In fact, popularly said, these macros are some of the 1 binary number of the nickname, we use them and St_mode for "&" operation, so we can get some specific information.

File type flags include:

S_ifmt 0170000 file Type bit mask
S_ifsock 0140000 Scoket
S_iflnk 0120000 Symbolic Connection
S_ifreg 1,000,001-like file
S_IFBLK 0060000 block Unit
S_ifdir 0040000 Catalogue
S_IFCHR 0020000-character device
S_ififo 0010000 FIFO pipeline


Other pattern flags include:
S_isuid: File set suid bit
S_isgid: File Set Sgid bit
S_ISVTX: File set sticky bit
The masks used to interpret the St_mode flags include:

   s_irusr (s_iread) 00400     file owner with readable permissions
     s_iwusr (s_iwrite) 00200     file owner with writable permissions
    s_ixusr (s_iexec) 00100     file owner with executable permissions

    s_irgrp 00040              user group with readable permissions
    s_iwgrp 00020              user group with writable permissions
    s_ixgrp 00010              user group with executable permissions

    s_iroth 00004              other users have Read permissions
    S_iwoth 00002              other users with writable permissions
    S_ Ixoth 00001             other users with executable permissions
S_IFMT: File type
S_irwxu: Master's read/write/execute permission, can be divided into s_ixusr, S_IRUSR, S_IWUSR
S_IRWXG: Read/write/execute permissions for group, can be divided into S_ixgrp, S_irgrp, S_IWGRP
S_irwxo: Other user's read/write/execute permissions, can be divided into S_ixoth, S_iroth, S_iwoth



There are also macro definitions to help determine the file types, which are different from the macros above, which are macros with parameters, similar to how the function is used:
S_isblk (st_mode) : test for special block device files
S_ISCHR (st_mode) : Test If the device file is a special character
S_isdir (st_mode) : Test Whether it is a directory
S_isfifo (st_mode): Test if it is a FIFO device
S_isreg (st_mode): Test whether it is a normal file
S_islnk (st_mode) : Test is a symbolic link
S_issock (st_mode): Test Whether it is a socket


The prototypes are:
int fstat (int filedes, struct stat *buf);
int stat (const char *path, struct stat *buf);
int Lstat (const char *path, struct stat *buf);

Fstat differs from the other two system invocations in that the FSTAT system call accepts a " file descriptor ", while the other two directly accept " file full path ". File descriptors are required after we call the open system to get the file, and the entire path is written directly on it.
The difference between stat and Lstat: When a file is a symbolic link, Lstat returns information about the symbolic link itself, and Stat returns the information for the file that the link points to.



the difference between stat and Lstat: When a file is a symbolic link, Lstat returns information about the symbolic link itself, and Stat returns the information for the file that the link points to. (It seems a bit dizzy, so remember, lstat than stat a l, so it is capable of processing symbolic link files, so when the symbolic link file is encountered, Lstat of course will not let go. and the stat system call does not have this ability, it can only open one eye to the symbolic link file, directly to deal with the link refers to the file .
2 Properties for the file
struct STAT {
mode_t St_mode; File corresponding to the mode, file, directory, etc.
ino_t St_ino; Inode Node Number
dev_t St_dev; Device number
dev_t St_rdev; Special Equipment Number
nlink_t St_nlink; Number of connections to the file
uid_t St_uid; File owner
gid_t St_gid; The group that the file owner corresponds to
off_t st_size; Normal file, corresponding file byte number
time_t St_atime; Time the file was last visited
time_t St_mtime; time when the contents of the file were last modified
time_t St_ctime; File status Change time
blksize_t st_blksize; Block size for file contents
blkcnt_t st_blocks; The number of blocks corresponding to the Wei Jian content
};
You can return a struct by using the function provided above, which holds the information of the file.

3 getting the file attributes of several encapsulated functions
///////////////////////////////////////////////////////////////////////////////
Function Description: Determine if the link file
Input parameters:
Output parameter: VOID
return value: Ycpp_bool
Instructions for use:
///////////////////////////////////////////////////////////////////////////////
Ycpp_bool islink (const char * szdirname)
{
struct STAT st;

Return (Lstat (szdirname, &st) = = 0 && s_islnk (st.st_mode));
}

///////////////////////////////////////////////////////////////////////////////
Function Description: Determine if the block file
Input parameters:
Output parameter: VOID
return value: Ycpp_bool
Instructions for use:
///////////////////////////////////////////////////////////////////////////////
Ycpp_bool isblk (const char * szdirname)
{
struct STAT st;

Return (Lstat (szdirname, &st) = = 0 && s_isblk (st.st_mode));
}

///////////////////////////////////////////////////////////////////////////////
Function Description: Determine if FIFO file
Input parameters:
Output parameter: VOID
return value: Ycpp_bool
Instructions for use:
///////////////////////////////////////////////////////////////////////////////
Ycpp_bool Isfifo (const char * szdirname)
{
struct STAT st;

Return (Lstat (szdirname, &st) = = 0 && s_isfifo (st.st_mode));
}

///////////////////////////////////////////////////////////////////////////////
Function Description: Determine if the directory file
Input parameters:
Output parameter: VOID
return value: Ycpp_bool
Instructions for use:
///////////////////////////////////////////////////////////////////////////////
Ycpp_bool isdir (const char * szdirname)
{
struct STAT st;

Return (Lstat (szdirname, &st) = = 0 && s_isdir (st.st_mode));
}

///////////////////////////////////////////////////////////////////////////////
Function Description: Determine if the device file
Input parameters:
Output parameter: VOID
return value: Ycpp_bool
Instructions for use:
///////////////////////////////////////////////////////////////////////////////
Ycpp_bool isdevice (const char * szdirname)
{
struct STAT st;

Return (Lstat (szdirname, &st) = = 0 && s_ischr (st.st_mode));
}

///////////////////////////////////////////////////////////////////////////////
Function Description: Determine whether ordinary files
Input parameters:
Output parameter: VOID
return value: Ycpp_bool
Instructions for use:
///////////////////////////////////////////////////////////////////////////////
Ycpp_bool isfile (const char * szdirname)
{
struct STAT st;

Return (Lstat (szdirname, &st) = = 0 && s_isreg (st.st_mode));
}

///////////////////////////////////////////////////////////////////////////////
Function Description: Determine whether it is readable
Input parameters:
Output parameter: VOID
return value: Ycpp_bool
Instructions for use:
///////////////////////////////////////////////////////////////////////////////
Ycpp_bool canaccess (const char *szfilename)
{
if (Access (szFileName, r_ok) = = 0)
return ycpp_true;

return ycpp_false;
}

///////////////////////////////////////////////////////////////////////////////
Function Description: Determine whether it can be modified
Input parameters:
Output parameter: VOID
return value: Ycpp_bool
Instructions for use:
///////////////////////////////////////////////////////////////////////////////
Ycpp_bool canmodify (const char *szfilename)
{
if (Access (szFileName, W_OK | R_OK) = = 0)
return ycpp_true;

return ycpp_false;
}

///////////////////////////////////////////////////////////////////////////////
Feature Description: Get the file last modified time
Input parameters:
Output parameter: VOID
return value: Ycpp_ret
Instructions for use:
///////////////////////////////////////////////////////////////////////////////
Ycpp_ret filelastmtime (const char *szfilename)
{
struct STAT st;

memset (&st, 0, sizeof (struct stat));
if (Lstat (szFileName, &st) = = 0)
return st.st_mtime;

printf ("%s filelastmtime error%s\n", szFileName, Strerror (errno));
return-1;
}

///////////////////////////////////////////////////////////////////////////////
Function Description: Gets the file size, in bytes
Input parameters:
Output parameter: VOID
return value: Ycpp_ret
Instructions for use:
///////////////////////////////////////////////////////////////////////////////
Ycpp_ret FileSize (const char *szfilename)
{
struct STAT st;

memset (&st, 0, sizeof (struct stat));
if (Lstat (szFileName, &st) = = 0)
return st.st_size;

printf ("%s FileSize error%s\n", szFileName, Strerror (errno));
return-1;
}




Fstat, stat, and Lstat usage

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.