Fstat, stat, and lstat usage, fstatstatlstat

Source: Internet
Author: User
Tags bit set lstat

Fstat, stat, and lstat usage, fstatstatlstat

The stat System Call series include fstat, stat, and lstat, which are used to return "Related File status information". The difference between the three is that the source file setting method is different. First, we will introduce a very important struct type called struct stat. It can be said that without the support of this struct stat, the above three system calls will be unable to proceed. The definition of this struct stat struct in different UNIX/Linux systems is slightly different, but you don't have to worry about it, which won't affect our use.

In the struct stat struct, we commonly use and each platform must have the following domains:
St_mode File Permission and file type information
Inode associated with the file
Device Where st_dev saves files
UID of the owner of the st_uid File
GID Number of the owner of the st_gid File
Last time st_atime file was accessed
The last modification time of the permissions, owner, group, or content of the st_ctime File
The last modification time of the content of the st_mtime file.
St_nlink: Number of hard connections on the file


Struct stat {
Dev_t st_dev; // device ID of the file
Ino_t st_ino; // Node
Mode_t st_mode; // file type and access permission
Nlink_t st_nlink; // number of hard connections to the file. The value of the created file 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, it is 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; // the last change time (attribute)
};

You can compare the original definition of the struct stat struct of solaris (UNIX) and fedora (Linux) to find that the two are indeed different, but the main fields are identical. The st_mode field is not as easy to use as other fields. The values of other fields are obvious, while the st_mode field requires some macros for use. In fact, in general, these macros are some external numbers of binary numbers with a specific position of 1. We use them and st_mode to perform the "&" operation to get some specific information.

The file type flag includes:

S_IFMT 0170000 bit masks for file types
S_ifsock0140000 scoket
S_IFLNK 0120000 symbolic connection
S_IFREG 0100000 general file
S_IFBLK 0060000 block Device
S_IFDIR 0040000 directory
S_IFCHR 0020000 character device
S_IFIFO 0010000 first-in-first-out Pipeline


Other mode labels include:
S_ISUID: SUID bit set in the file
S_ISGID: the SGID bit is set in the file.
S_ISVTX: the sticky bit is set in the file.
The mask used to explain the st_mode flag includes:

S_IRUSR (S_IREAD) 00400 the file owner has the read permission
S_IWUSR (S_IWRITE) 00200 the file owner has the write permission
S_IXUSR (S_IEXEC) 00100 the file owner has executable permissions

S_IRGRP 00040 user group with read permission
S_IWGRP 00020 user group with write permission
S_IXGRP 00010 user group with executable permissions

S_IROTH 00004 other users have read permission
S_IWOTH 00002 other users have write permission
S_IXOTH 00001 other users have executable permissions
S_IFMT: file type
S_IRWXU: read, write, and execute permissions of the owner, which can be divided into S_IXUSR, S_IRUSR, S_IWUSR
S_IRWXG: read/write/execute permissions of the group, which can be divided into S_IXGRP, S_IRGRP, S_IWGRP
S_IRWXO: read/write/execute permissions of other users, which can be divided into S_IXOTH, S_IROTH, S_IWOTH



There are also some macro definitions used to help determine the file type, which are different from the above macros. These are macros with parameters, similar to the usage of functions:
S_ISBLK (st_mode): test whether it is a special block device file.
S_ISCHR (st_mode): test whether it is a special character Device File
S_ISDIR (st_mode): test whether it is a directory.
S_ISFIFO (st_mode): test whether the device is a FIFO device.
S_ISREG (st_mode): test whether it is a common file
S_ISLNK (st_mode): test whether it is a symbolic link.
S_ISSOCK (st_mode): test whether it is a socket


The prototype is:
Int fstat (int filedes, struct stat * buf );
Int stat (const char * path, struct stat * buf );
Int lstat (const char * path, struct stat * buf );

The difference between fstat and the other two systems is that fstat system calls accept a "file descriptor", while the other two directly accept "full file path ". The file descriptor can be obtained only after being called by the open system, and the file path can be directly written.
The difference between stat and lstat: When a file is a symbolic link, lstat returns the information of the symbolic link itself, and stat returns the information of the file to which the link points.



The difference between stat and lstat: When a file is a symbolic link, lstat returns the information of the symbolic link itself, while stat returns the information of the file to which the link points. (It seems a bit dizzy. In this case, lstat has an l more than stat, so it has the ability to process symbolic link files. Therefore, when encountering symbolic link files, lstat certainly won't let it go. The stat system does not have this capability. It can only close one eye of the symbolic link file and directly process the file indicated by the link)
2. File Attributes
Struct stat {
Mode_t st_mode; // file mode, file, directory, etc.
Ino_t st_ino; // inode node number
Dev_t st_dev; // device number
Dev_t st_rdev; // special device number
Nlink_t st_nlink; // number of file connections
Uid_t st_uid; // file owner
Gid_t st_gid; // group corresponding to the file owner
Off_t st_size; // common file, corresponding to the number of bytes
Time_t st_atime; // time when the object was last accessed
Time_t st_mtime; // The last modification time of the file content
Time_t st_ctime; // File status change time
Blksize_t st_blksize; // block size corresponding to the file content
Blkcnt_t st_blocks; // Number of chunks corresponding to Wei Jian content
};
You can use the function provided above to return a struct that stores the file information.

3Several encapsulation functions for getting file attributes
//////////////////////////////////////// ///////////////////////////////////////
// Function Description: Determine whether to link the 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 whether to block a 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 whether a file is FIFO
// 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: determines whether a directory file is used.
// 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: determines whether a device file is used.
// 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: determines whether a common file is used.
// 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: determines whether the object 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: determines whether the modification is allowed
// 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;
}

//////////////////////////////////////// ///////////////////////////////////////
// Function Description: gets the last modification time of the object.
// 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: obtains 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;
}




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.