linux 学习笔记之 Files and Directories
- file stat
int fstat(int fd, struct stat *info);
int lstat(const char *pathname, struct stat *info);
The fstat function obtains information about the file that is already open 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.
2. file access permissions
- The file rule is that whenever we want to open any type of file by name, we must have execute permission in each directory mentioned in the name, including the current directory, if it is implied. Read permission lets us read the directory(use ls command), write permission lets us create files below the directory and execute permission lets us pass through the directory when it is a component of a pathname what we are trying to access.
- We must have write permission for a file to specify the O_TRUNC flag in the open function.
- To delete an existing file, we need write permission and execute permission in the directory containing the file.
3. set-user-id, set-group-id, set-sticky-id
- set-user-id(S_ISUID) : used in some file like /etc/passwd and print queue, the S_ISUID tell kernel run program as file owner
- set-group-id(S_IGUID): tell kernel run program as file's group user
- set-sticky-id(S_ISVTX): if the bits is set for a directory, the file in the directory can be removed or renamed only if the user has the write permission for the directory and one of the fellowing: owns this file, owns the directory, is the superuser.
4. file truncation
int ftrruncate(int fd, off_t length);
- If the previous size of the file was greater than length, the data byond length is nolonger accessible.
- If the previous sizeof the file was less than length, the effect is system dependent , but XSI-confofrmation system will increase the file size. If the implementation does extend a file, data between the old end of file and the new end of file will read as 0.
int symlink(const char *actualpath, const char * sympath);
int unlink(const char *pathname);
int remove(const char *pathname);
- Many system disallow hard link to directory to prevent loops in the file system.
- When a file is closed, kernel first checks the count of the number of process that have the file open, If this count has reached 0, the kernel then checks the link count; if it is 0, file's contents are delete. If the set-sticky-id is set in this directory we must have enough permission(see above).
- If the pathname is a symbolic link unlink removes the symbolic link, not the references by the link. There is no function to remove the file referenced by a symbolic link given the name of the link.
- We can also unlink a file or a directory with the remove function. For a file, remove is identical to unlink. For directory, remove is identical rmdir.
- It is not required that actualpath exist when the symbolic link is created.
6. chdir, fchdir
int fchdir(int fd);
Each program if run is a separate process, so the current working directory of the shell is unaffected by the call to chdir in the program.
7. device special files
Each file system on the same disk drive would usually have the same number, but a different minor number.