UNIX advanced environment programming (3) Files And Directories, unixdirectories
In the previous two articles, we learned some basic operation functions of IO operations, including open, read, and write.
In this article, we will learn about other features of the file system and the attributes of a file. The functions involved include:
- View all attributes of a file;
- Change the file owner;
- Change file permissions;
- Operation folder.
We will also learn about the data structures and symbolic links related to file systems ).
1. stat, fstat, fstatat, and lsat Functions
# Include <sys/stat. h> int stat (const char * restrict pathname, struct stat * restrict buf); int fstat (int fd, struct stat * buf); int lstat (const char * restrict pathname, struct stat * restrict buf); int fstatat (int fd, const char * restrict pathname, struct stat * restrict buf, int flag); // All four return: 0 if OK, −1 on error
Function:
Stat returns the information of the specified file with pathname;
Fstat obtains information about the file opened on the file descriptor fd;
The functions of lstat are similar to those of the stat function, but there is only one difference. When the file specified by pathname is a symbolic link, the file information obtained by the lstat function is the information of the symbolic link, instead of the information of the file to which the symbolic link points, we will learn later