In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
What is the usage of stat function in Linux system? for this question, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Header file: # include
# include definition function: int stat (const char * file_name, struct stat * buf)
Function description: obtain the file information through the file name filename and save it in the structure stat referred to by buf
Return value: 0 for successful execution and-1 for failure. The error code is stored in errno
Error codes: copying cod
The file specified by the ENOENT parameter file_name does not exist in the directory in the ENOTDIR path, but it is not a real directory. The file to be opened by ELOOP has too many symbolic connection problems. The upper limit is 16. The EFAULT parameter buf is an invalid pointer. File access to non-existent memory space EACCESS is rejected ENOMEM core out of memory ENAMETOOLONG parameter file_name path name too long copy code copy code
# include#include#include
Int main () {struct stat buf; stat ("/ etc/hosts", & buf); printf ("/ etc/hosts file size =% d\ n", buf.st_size);}
/ * / struct stat {device number of the dev_t st_dev; / / file ino_t st_ino; / / Node mode_t st_mode; / / File type and access permissions nlink_t st_nlink; / / number of hard connections to the file. The newly created file value 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 (the size of the unsigned long st_blksize; O buffer of the file system) 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 attributes)}
Copy the code
The st_mode described earlier defines the following situations: copy the code
S_IFMT 0170000 file type bit mask S_IFSOCK 0140000 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 file (set group-id on execution) bit S_ISVTX 01000 Sticky bit of file S_IRUSR (S_IREAD) 00400 File owner has read access S_IWUSR (S_IWRITE) 00200 File owner has writeable access S_IXUSR (S_IEXEC) 00100 File owner has executable permission S_IRGRP 00040 user group has read access S_IWGRP 00020 user group has writable access S_IXGRP 00010 User group has executable permission S_IROTH 00004 other users have read access S_IWOTH 00002 other users have writeable access S_IXOTH 00001 other users have executable permission to copy code
The above file types define macro definitions in POSIX to check these types: copy code
S_ISLNK (st_mode) determines whether symbolic link S_ISREG (st_mode) is a general file S_ISDIR (st_mode) is a directory S_ISCHR (st_mode) is a character device file S_ISBLK (s3e) is a first-in-first-out (st_mode) whether it is a socket copy code
If a directory has the sticky bit (S_ISVTX), it means that files in this directory can only be deleted or renamed by the file owner, the directory owner, or root. Copy the code
Struct statfs {long fancitype; / / filesystem type long fancibsize; / / block size long flocketed blocks; / / how many blocks are long fancibfrees; / / free blocks long flocks; / / free blocks long flocks; / / total file node long fanciffree; / / free file node fsid_t frankfsidy; / / file system id long fame name; / / maximum length of file names [6] / / spare for later}
Copy the code
Stat, fstat, and lstat functions (UNIX)
# include#include
Int stat (const char * restrict pathname, struct stat * restrict buf)
Stat provides the name of the file and gets the corresponding attributes of the file. It seems that this is generally done when the file is not opened.
Int fstat (int filedes, struct stat * buf)
Fstat acquires the corresponding attributes of the file through the file descriptor. Do this after the file is opened
Int lstat (const char * restrict pathname, struct stat * restrict buf)
Lstat connection file
The return of the three functions: 0 for success and-1 for error.
Given that a pathname,stat function returns an information structure related to this named file, the fstat function gets information about the file that has been opened on the descriptor filedes. The lstat function is similar to stat, but when the named file is a symbolic link, lstat returns information about the symbolic link, not the file referenced by the symbolic link.
The second parameter is a pointer, which points to a structure that we should provide. These functions fill in the structure pointed to by buf. The actual definition of the structure may vary from implementation to implementation, but its basic form is to copy the code
Struct stat {mode_t st_mode; / file tpye & mode (permissions) / ino_t st_ino; / i=node number (serial number) / dev_t st_rdev; / device number for special files/ nlink_t st_nlink; / number of links/ uid_t st_uid; / user id of owner/ gid_t st_gid; / group ID of owner/ off_t st_size / size in bytes for regular files/ time_t st_atime; / time of last access/ time_t st_mtime; / time of last modification/ time_t st_ctime; / time of last file status change/ long st_blksize; / * best I block size O block size / long st_blocks; / number of 512-byte blocks allocated*/}
Copy the code
Note that with the exception of the last two, the other members are basic system data types. We will describe each member of this structure to understand the file properties.
The most frequently used stat function is probably the ls-l command, which allows you to get all the information about a file.
The function is to get the attributes of files (normal files, directories, pipes, socket, characters, blocks).
Function prototype:
# include
Int stat (const char * restrict pathname, struct stat * restrict buf)
Provide the file name and get the corresponding attributes of the file.
Int fstat (int filedes, struct stat * buf)
Get the corresponding attributes of the file through the file descriptor.
Int lstat (const char * restrict pathname, struct stat * restrict buf)
Connect the file description life to get the file properties.
Copy the code for the attributes corresponding to the file
Struct stat {
Mode_t st_mode; / / file corresponding mode, file, directory, etc.
Ino_t st_ino; / / inode Node number
Dev_t st_dev; / / device number
Dev_t st_rdev; / / Special equipment number
Number of connections to nlink_t st_nlink; / / files
Uid_t st_uid; / / File owner
Gid_t st_gid; / / the group corresponding to the file owner
Off_t st_size; / / normal file, corresponding to the number of file bytes
The time when the time_t st_atime; / / file was last accessed
The time when the content of the time_t st_mtime; / / file was last modified
Time_t st_ctime; / / File status change time
Block size corresponding to blksize_t st_blksize; / / file content
Number of blocks corresponding to blkcnt_t st_blocks; / / file content}
This is the answer to the question about the usage of the stat function in the Linux system. I hope the above content can be of some help to everyone. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.