Thursday 15 October 2015

Linux文件属性及系统结构

stat结构体
-Struct stat is a system call that returns files attributes about an inode. 

-Inode is a data structure used to represent a file system object, echo inode stores the attributes and disk block locations of the file system object's data.  And stat() return a file's  information in the inode and retrieves the file's inode number.

-Filesystem object attributes may include manipulation metadata (e.g. change,  access, modify time), as well as owner and permission data (e.g. group-id, user-id, permissions).
-Directories are lists of names assigned to inodes. The directory contains an entry for itself, its parent, and each of its children.
struct stat() is defined in <sys/stat.h> header file:
struct stat()
{
    mode_t    st_mode; /*file type & permission*/
    ino_t   st_ino; /*i-node number*/
    dev_t   st_dev /*device number*/
    nlink_t st_nlink /*number of links*/
    uid_t   st_uid /*user ID of owner -user id*/
    gid_t   st_gid /*group ID of owner*/
    off_t   st_size /*total size, in bytes unit*/
    time_t  st_atime /*time of last access*/
    time_t  st_mtime /*time of last modification*/
    ....
}


There are 9 types authority of visit a file(t_mode):
S_IRUSR, S_IWUSR, S_IXUSR
S_IRGRP, S_IWGRP, S_IXGRP
S_IROTH, S_IWOTH, S_IXOTH

We can use "t_mode  S_IXXXX" to juge the file, or we can use also the function "int access=(char *filepath, int mode)" to compare, if the result is positive,it return 0.

Let's understand how the system calculate the file's authority.
In Linux, the file's default authority is 666, and mask(掩码) is 
002 ==> 000 000 010(in binary) and we take the inverse number:
111 111 101 then it caculate 111 111 101 & 110 110 110 = 110 110 100 (664 in decimal)



File descriptor 文件描述符:
A file descriptor is an abstract indicator used to access a file or other input/output resource.
There three standard POSIX file descriptors, corresponding the three streams(define in head file <unistd.h>), which presumably every process should expect to have:
  1. 0 input STDIN_FILENO stdin
  2. 1 output STDOUT_FILENO stout
  3. 2 error STDERR_FILENO stderr



No comments:

Post a Comment