Apue Study Notes-Chapter 4. Files And Directories

Source: Internet
Author: User
Tags lstat

1. Stat, fstat, lstat

Int Stat (const char * restrict pathname, struct stat * restrict BUF ); Int fstat (INT filedes, struct stat * restrict BUF );

Int lstat (const char * restrict pathname, struct stat * restrict BUF ); All the above three functions can obtain information about the file and return it in the stat structure. Among them, stat uses the file name for retrieval, and tracks the link, while lstat displays the information of the linked file and does not trace. Fstat obtains the relevant file information through the opened file descriptor.
 Struct stat { 
Mode_t st_mode;/* file type & mode (permissions )*/
Ino_t st_ino;/* I-node number (serial number )*/
Dev_t st_dev;/* Device number (File System )*/
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 */
Blksize_t st_blksize;/* Best I/O BLOCK SIZE */
Blkcnt_t st_blocks;/* Number of disk blocks allocated */
};
 
2. Each process has six user IDs:

real user id
real group ID

who we really are

valid tive user id
valid tive group ID
Supplementary group IDs

used for file access permission checks

saved set-user-id
saved set-group-id

saved by exec functions, which is a copy of valid user ID and valid group ID.



 
3. File Access Permissions

S_irusr

User-read

S_iwusr

User-write

S_ixusr

User-Execute

S_irgrp

Group-read

S_iwgrp

Group-write

S_ixgrp

Group-Execute

S_iroth

Other-read

S_iwoth

Other-write

S_ixoth

Other-Execute

 
When accessing related files, we must have the running permission for all the folders on the path. If we have the read permission, we can open the file in read-only mode,
 
If you have the write permission, you can open the file as a write. To create or delete a file under a directory, you must have the write and run permissions on the directory.
 
4. Access check
 
Int access (const char * pathname, int mode );
 
5. Sticky Bit
 
By setting stick bit, after the file is run for the first time, the text content is copied to the swap area, and the next time can be quickly run.
 
6. File System

 
Because each I-node is numbered inside the same file system, ln (1) cannot be linked in different file systems.
 
In the same file system, MV and RM operations do not need to carry data.
Each cylinder group can be expressed as follows:

 

 

 
Open the file. If the link count of the file is 0, delete the file.
 
Each inode contains the following information:
 
File type, File Permission, file size, file Data Pointer
Most of the information in stat is stored in inode.
 
7. Links
 
Hard links all point to the same I-node
 
Symbolic Links are implemented by storing the directed path name, which can be cross-file systems and direct to directories.

Function

Does not follow symbolic link

Follows symbolic link

Access

 

Chdir

 

Chmod

 

Chown

Creat

 

Exec

 

Lchown

 

Link

 

Lstat

 

Open

 

Opendir

 

Pathconf

 

Readlink

 

Remove

 

Rename

 

Stat

 

Truncate

 

Unlink

 
 

 
8. File time:

Field

Description

Example

Ls(1) Option

St_atime

Last-access time of file data

Read

-U

St_mtime

Last-modification time of file data

Write

Default

St_ctime

Last-Change Time of I-node status

Chmod, chown

-C

 
You can use the following functions to modify access time and modification time:
Int utime (const char * pathname, const struct utimbuf * times );
 

 

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.