Unix-Linux 编程实践教程 第三章 小结

  • Linux下一切皆文件,所以目录也只是一种特殊的文件,可以同文件一样open,read,close,只是函数换成了opendir(),readdir(),closedir()
  • 用于读取目录文件的数据结构---struct dirent
    #include <dirent.h>
    
    struct dirent {
           ino_t          d_ino;       /* Inode number */
           off_t          d_off;       /* Not an offset; see below */
           unsigned short d_reclen;    /* Length of this record */
           unsigned char  d_type;      /* Type of file; not supported
                                          by all filesystem types */
           char           d_name[256]; /* Null-terminated filename */
    };

           

  • 用于存储文件状态的数据结构---struct stat

    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    
    int stat(const char *path, struct stat *buf);
    
    struct stat {
        dev_t     st_dev;     /* ID of device containing file */
        ino_t     st_ino;     /* inode number */
        mode_t    st_mode;    /* protection */
        nlink_t   st_nlink;   /* number of hard links */
        uid_t     st_uid;     /* user ID of owner */
        gid_t     st_gid;     /* group ID of owner */
        dev_t     st_rdev;    /* device ID (if special file) */
        off_t     st_size;    /* total size, in bytes */
        blksize_t st_blksize; /* blocksize for file system I/O */
        blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
        time_t    st_atime;   /* time of last access */
        time_t    st_mtime;   /* time of last modification */
        time_t    st_ctime;   /* time of last status change */
    };

       

  • 用于存储用户信息的数据结构---struct passwd

    #include < sys / types.h >
    #include < pwd.h >
    
    struct passwd * getpwnam(const char * name ); //通过用户名获取用户信息
    
    struct passwd * getpwuid(uid_t  uid );        //通过用户ID获取用户信息
    
    struct passwd {
        char   *pw_name;       /* username */
        char   *pw_passwd;     /* user password */
        uid_t   pw_uid;        /* user ID */
        gid_t   pw_gid;        /* group ID */
        char   *pw_gecos;      /* user information */
        char   *pw_dir;        /* home directory */
        char   *pw_shell;      /* shell program */
    }
    
    

      

  • 用于存储用户组信息的数据结构---struct group

    #include <sys/types.h>
    #include <grp.h>
    
    struct group *getgrnam(const char *name);  //通过用户组名来获取用户组信息
    
    struct group *getgrgid(gid_t gid);         //通过用户组ID来获取用户组信息
    
    struct group {
        char   *gr_name;       /* group name */
        char   *gr_passwd;     /* group password */
        gid_t   gr_gid;        /* group ID */
        char  **gr_mem;        /* group members */
    };
  • 三个文件特殊属性:

    1. SUID:一般作用于二进制文件上;执行者将暂时拥有文件拥有者的所有权限

    2. SGID:对于二进制文件来说,SGID作用同SUID,使得执行者暂时拥有文件所属用户组的所有权限;对于目录来说,在该目录下创建的文件属组于目录属组相同

    3. SBIT:仅对目录有效,使得该目录下创建的文件仅有文件拥有者和root可删除,其他用户不可删除该目录下的文件

  • struct dirent.d_ino==struct stat.st_ino,都表示文件的i-node

猜你喜欢

转载自my.oschina.net/u/3281747/blog/2874390