File Structure stat--
The following three functions can obtain status information for a file:
#include <sys/stat.h>intstat*pathstat*buf);int fstat(intstat*buf);intlstat*pathstat*buf);
The stat function specifies the file path, Fstat Specifies the file descriptor, Lstat is similar to stat, but for a symbolic link file, Lstat obtains the status information of the symbolic link file itself rather than the state information of the referenced file, stat is a struct, as follows:
struct STAT {dev_t st_dev; /* ID ofDevice containingfile*/ino_t St_ino; /* Inode Number*/mode_t St_mode; /* Protection */nlink_t St_nlink; /* Number ofHard links */uid_t st_uid; /* User ID ofOwner */gid_t St_gid; /* Group ID ofOwner */dev_t St_rdev; /* Device ID (ifSpecialfile) */off_t st_size; /* Total size,inchBytes */blksize_t st_blksize; /* BlockSize for fileSystem I/O */blkcnt_t st_blocks; /* Number of +B Blocks Allocated */time_t st_atime; /* Time of LastAccess */time_t st_mtime; /* Time of LastModification */time_t st_ctime; /* Time of LastStatus change */};
File Type--
Unix System file types include ordinary files, directory files, block special files, character special files, FIFO (Named pipes), sockets, symbolic links, the most important file type is ordinary files.
File Permissions--
Each file has 9 access bits, which are users, groups, and other read, write, and Execute permissions, respectively. There are many IDs associated with each process, namely the actual user ID, the actual group ID, the valid user ID, the valid group ID, the additional group ID, the saved Settings user ID, the saved Settings group ID. When you create a new file, the ID of the new file is related to the ID of the current process. The following access functions are tested for access rights by actual user ID and actual group ID:
#include <unistd.h>int access(constcharint mode);
When you create a file, you can specify a file mode, which is related to this file mode, and the Umask function can specify a mask word:
#include <sys/stat.h>mode_t umask(mode_t cmask);
The following two functions can be used to change access permissions for an existing file:
#include <sys/stat.h>int chmod(constchar *path, mode_t mode);int fchmod(int fd, mode_t mode);
The following functions can be used to change the user ID and group ID of an existing file:
#include <unistd.h>int chown(constchargroup);int fchown(intgroup);int lchown(constchargroup);
File Truncated--
When you create a file, you can specify O_TRUNC to truncate the file, and the following function can do the same:
#include <unistd.h>inttruncate*pathlength);int ftruncate(intlength);
Stick Bit--
The S_ISVTX bit is called the stick bit or holds the text bit, if this bit of an executable program file is set, then the first time the program is executed and ended, a copy of its program body part is still saved in the swap area, which allows it to be loaded into the memory area more quickly the next time the program executes. The reason is that the swap area occupies a contiguous disk space, it can be considered as a continuous file, and the body of a program in the swap area is also continuous storage, and in the general UNIX file system, the file's data blocks are likely to be stored randomly, for common applications such as text editor and C editor, We often set the stick bit of the file where they are located. Most of the newer UNIX systems are now configured with virtual storage systems and fast file systems, so this technology is no longer needed.
File System--
Using the traditional BSD-based UNIX file system UFS as an example, we can divide a disk into one or more partitions, each of which can contain a file system. An important concept is the I node, which contains most of the file-related information: The file type, the file access permission bit, the length of the file, and pointers to the data blocks that the file occupies, and so on. Most of the information in the stat structure is taken from the I node. Only two items of data are stored in the catalog entry: The file name and the I node number.
File Links--
Links are divided into symbolic links and hard links, which are indirect pointers to a file, which directly points to the I node of the file. The reason for introducing symbolic links is to avoid some of the limitations of hard links: Hard links often require that links and files reside on the same file system; Only Superuser can create hard links to directories. Since symbolic links can point to the table of contents, it is possible to introduce loops, which you need to be aware of.
Any one file can have multiple directory entries pointing to the I node, and creating a link to an existing file is using the link function:
#include <unistd.h>int link(constcharconstchar *newpath);
In order to delete an existing catalog entry, you can call the unlink function:
#include <unistd.h>int unlink(constchar *pathname);
You can also use the Remove function to remove a link to a file or directory:
#include <stdio.h>int remove(constchar *pathname);
The file name or directory is renamed with the rename function:
#include <stdio.h>int rename(constcharconstchar *newname);
The Symlink function creates a symbolic link:
#include <unistd.h>int symlink(constcharconstchar *newpath);
The Readlink function opens the symbolic link and reads the name in the link:
#include <unistd.h>ssize_t readlink(constcharchar *buf, size_t bufsize);
File Time--
There are three time periods for each file, respectively, the last access time of the file data St_atime, the last modified time of the file data St_mtime, and the last change time of the I node state st_ctime. The access and modification times of a file can be changed with the Utime function:
#include <utime.h>int utime(constcharconststruct utimbuf *times);
The UTIMBUF structure is as follows:
struct utimbuf { time_t actime; /* access time */ time_t modtime; /* modification time */ };
Directory Operations--
The mkdir function creates the directory, and the RmDir function deletes the empty directory:
#include <syt/stat.h>int mkdir(constchar *pathname, mode_t mde);#include <unistd.h>int rmdir(constchar *pathname);
The following functions can be used to read a directory:
#include <dirent.h>opendir*name);DIR* fdopendir(intreaddir*dirprewinddir*dirp);int*dirp);inttelldir*dirpseekdir*dirp, long offset);
The dirent structure is as follows:
struct dirent { ino_t d_ino; number */ off_t d_off; offsettothe next dirent */ unsigned short d_reclen; lengthofrecord */ unsigned char d_type; offilenot supported byfile system types */ char d_name[256]; /* filename */ };
The following two functions can change the current working directory:
#include <unistd.h>int chdir(constchar *path);int fchdir(int fd);
GETCWD get current working directory:
#include <unistd.h>char* getcwd(char *buf, size_t size);
Files and directories for the UNIX environment