File Management-File status

Source: Internet
Author: User
Tags lstat

The file status includes the file size, file owner, and file modification date. The information is stored separately from the file content.

Use the following function to get the file status.

# Include <sys/STAT. h>

Int Stat (const char * file_name, struct stat * BUF );
Function Description Stat () is used to copy the state of the file referred to by the parameter file_name to the structure referred to by the parameter Buf.
The following describes the parameters in struct stat.
Struct stat
{
Dev_t st_dev;/* Device */
Ino_t st_ino;/* inode */
Mode_t st_mode;/* Protection */
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 type */
Off_t st_size;/* Total size, in bytes */
Unsigned long st_blksize;/* blocksize for filesystem I/O */
Unsigned long st_blocks;/* number of blocks allocated */
Time_t st_atime;/* Time of lastaccess */
Time_t st_mtime;/* time of last modification */
Time_t st_ctime;/* time of last change */
};
Device ID of the st_dev File
I-node of the st_ino File
St_mode file type and access permission
The number of hard connections st_nlink connects to the file. The value of the created file is 1.
User ID of the st_uid file owner
Group ID of the st_gid file owner
St_rdev if this file is a device file, it is its device number
Size of the st_size file, in bytes
The size of the I/O buffer of the st_blksize file system.
The number of file blocks occupied by st_blcoks. The size of each block is 512 bytes.
The last time the st_atime file was accessed or executed, which is generally changed only when mknod, utime, read, write, and tructate are used.
The last modification time of the st_mtime file, which is generally changed only when mknod, utime, and write are used.
The last time st_ctime I-node was changed.
When a group or permission is changed, the st_mode described previously is updated, which defines the following conditions:
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
S_isuid 04000 file (Set User-ID on execution) Bit
S_isgid 02000 (set group-ID on execution) bit of the file
S_isvtx 01000 file Sticky Bit
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
The above file type defines in POSIX to check the macro definitions of these types
S_islnk (st_mode) determines whether it is a symbolic connection
S_isreg (st_mode) is a normal file
S_isdir (st_mode) is a directory
S_ischr (st_mode) is a character Device File
S_isblk (s3e) for FIFO
S_issock (st_mode) is socket
If a directory has a sticky bit (s_isvtx), it indicates that only
Can be deleted or renamed by the file owner, directory owner, or root.
If the return value is successfully executed, 0 is returned. If the return value is failed,-1 is returned. The error code is stored in errno.
Error Code: The file specified by the enoent parameter file_name does not exist.
The directory in the enotdir path exists but is not a real directory
The file to be opened by eloop has too many symbolic connections. The upper limit is 16.
The efault parameter Buf is an invalid pointer and points to the memory space that cannot exist.
The eaccess is denied when accessing the file.
Insufficient enomem core memory
The path name of the enametoolong parameter file_name is too long.

 

 

# Include <sys/STAT. h>
# Include <unistd. h>
Defines the int fstat (INT Fildes, struct stat * BUF) function );
Function Description: fstat () is used to copy the file status referred to by the Fildes parameter to the structure referred to by the Buf parameter (struct Stat ). Fstat () and Stat () Act exactly the same, different in
The input parameter is an opened file description word. For more information, see Stat ().
If the return value is successfully executed, 0 is returned. If the return value is failed,-1 is returned. The error code is stored in errno.

This function can obtain relevant information from an opened file descriptor.

 

 

 

# Include <sys/STAT. h>
# Include <unistd. h>
Define the int lstat (const char * file_name.struct stat * BUF) function );
Function Description: lstat () and Stat () Act exactly the same way and get the file status indicated by the file_name parameter. The difference is that when the file is a symbolic connection, lstat () returns the status of the link. For more information, see Stat ().
If the return value is successfully executed, 0 is returned. If the return value is failed,-1 is returned. The error code is stored in errno.

 

Example:

 

# Include <sys/STAT. h>
# Include <unistd. h>
Mian ()
{
Struct stat Buf;
Stat ("/etc/passwd", & BUF );
Printf ("/etc/passwd file size = % d/N", Buf. st_size );
}

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.