Linux get file attributes (API)

Source: Internet
Author: User
Tags file type file lstat posix

There are three file attributes to view Api:stat, Fstat, Lstat. #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> int stat (const char *pathname, struct Stat *buf); int fstat (int fd, struct stat *buf); int Lstat (const char *pathname, struct stat *buf); #include <fcntl.h> * Definition of At_* Constants */#include <sys/stat.h> int fstatat (int dirfd, const char *pathname, struct stat *buf,int flags); Stat does not open the file, Fstat need to open the file, Lstat is mainly a symbolic link file, query the link file itself. struct STAT is a kernel-defined struct, in # include <sys/stat.h> declaration, which adds up to the file attribute information. Each element inside is a variety of attributes. struct STAT {dev_t st_dev;/* ID of device containing file */ino_t St_ino;/* inode number */mode_t st_mode;/* Protect Ion */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 ID (if special file) */off_t st_size; /* Total size, in bytes */blksize_t st_blksize; /* BlockSize for filesystem I/O */blkcnt_t st_blocks; /* Number of 512B blocks allocated*/* Since Linux 2.6, the kernel supports nanosecond precision for the following timestamp fields. For the details before Linux 2.6, see NOTES. */struct TIMESPEC st_atim; /* Time of last access */struct TIMESPEC st_mtim; /* Time of last modification */struct TIMESPEC st_ctim; /* Time of last status change */#define ST_ATIME st_atim.tv_sec/* Backward compatibility */#define ST_MTIME ST_MTIM.TV_ SEC #define St_ctime St_ctim.tv_sec}; Enter stat XXX on ubantu16.04 command line: File: ' a.out ' Size: 8968 block: IO block: 1024 normal file device: 2ah/42dinode:5 331 Hard Links: 1 permissions: (0777/-RWXRWXRWX) Uid: (0/root) Gid: (0/root) last visited: 2018-08-12 12:29:37.000000000 +0800 Last modified: 2018-08-12 12:29 : 37.000000000 +0800 Last modified: 2018-08-12 12:29:37.000000000 +0800 creation time:-The appeal information is the structure inside the data. Here's a very simple test function # include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h > #include <string.h> #define FILENAME "1.txt" int main (void) {int ret = -1;struct stat buf;memset (&buf, 0, size Of (BUF)); ret = stat (FILENAME,&AMP;BUF); if (ret<0) {perror ("ret:"); _exit (-1);} printf ("St_ino =%d\n", Buf.st_ino);p rintf ("st_size =%d\n", buf.st_size);p rintf ("st_blksize =%d\n", buf.st_blksize); printf ("St_blocks =%d\n", buf.st_blocks); return 0;} The following is the application case: 1, determine the file type file type in St_mode, this inside is for the operation, similar to the CPSR register. There are many macros in Linux to test the operation bit. The following S_isreg (m) is it a regular file? S_isdir (m) directory? S_ISCHR (m) character device? S_isblk (m) block device? S_isfifo (M) FIFO (named pipe)? S_islnk (m) symbolic link? (not in posix.1-1996.) S_issock (m) socket? (not in posix.1-1996.) S_ISREG This macro returns 1, the normal file, or 0 if it is not. The result is that if the file returns 1, it does not return 0. Additionally, St_mode records the file permissions. The test method is similar to the file type, using a bitmask, but without a macro operation. Due to many, no longer listed, can be viewed through the Man 2 Stat. Simple test code: unsigned int result = Buf.st_mode & (s_irusr >> 8);p rintf ("file owner:%x\n", result); An important part is the rights management of the file. St_mode is essentially a 32-bit binary, and the type is almost unsigned int, anyway. Before the operation needs to check the file permission rules: 1, the file has 9 permission bits (owner,group,others) 2, if a.out as an example, is to see what executed the a.out, that is, the current process, which user process. Under Linux, you can use the Access function to determine if you have Execute permissions, and you can test to see if the current user's current environment has some kind of operation permission on the target file. #include <unistd.h> int access (const CHAR *pathname, int mode), can be viewed through Man 2 access. Chowd and Fchowd and Lchowd are viewed through the Man 2 chmod, which is the owner of the modified file. File Mask Umask is a global setting in Linux that sets up new permissions for our system. For catalog files, files that contain sub-files. Opendir with Readdir#include <sys/types.h> #include <dirent.h> DIR *opendir (const char *name); DIR *fdopendir (int fd); #include <dirent.h> struct dirent *readdir (dir *dirp); int Readdir_r (dir *dirp, struct dirent *entry, struct dirent * *result); struct Dirent {ino_t D_ino;/* inode number */off_t d_off;/* not ' an offset; see NOTES */unsigned short d_reclen;/* LE Ngth of this record */unsigned char d_type; /* type of file; Not supportedby all filesystem types */char d_name[256]; /* filename */}; Each call to Readdir can only read one at a time, to read out all the directory entries in the directory, you must read the Readdir function internally remember that the directory has been called, will not return the read directory entries, If Readdir returns NULL, it means that it has been read. The simple test functions are as follows: #include <stdio.h> #include <dirent.h> #include <sys/types.h> #include <dirent.h> int main (int argc,char **argv) {DIR *pdir = null;struct dirent *pent = null;if (argc!=2) {printf ("error"); return 0;} Pdir = Opendir (argv[1]); if (NULL = = Pdir) {perror ("opendir:\n"); return-1;} while (1) {pent = Readdir (Pdir), if (pent! = NULL) {printf ("name:%s\n", pent->d_name);} else {break;}} return 0;} reentrant function: The Readdir function is different from the previously contacted function, which returns a struct pointer directly. Because Readdir internal application of memory and gave us the address, multiple calls Readdir actually does not request memory repeatedly but uses the first call Readdir is allocated that memory, so the design method is readdir non-reentrant key. Library functions are non-reentrant, and later realized that this is unsafe, and later repackaged the C library function, generally the non-reentrant function after adding _r into a reentrant function.

Zhu Youpeng Teacher Video Learning Notes

Linux get file attributes (API)

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.