zoukankan      html  css  js  c++  java
  • [04]APUE:文件与目录

    [a] stat / lstat / fstat

    #include <sys/stat.h>
    int stat(const char *restrict pathname, struct stat *restrict buf)
    int lstat(const char *restrict pathname, struct stat *restrict buf)
    int fstat(int fd, struct stat *buf)
    struct stat {
        mode_t    st_mode;
        ino_t    st_ino;
        dev_t    st_dev;
        dev_t    st_rdev;
        nlink_t    st_nlink;
        uid_t    st_uid;
        gid_t    st_gid;
        off_t    st_size;
        struct timespec    st_atime;
        struct timespec    st_mtime;
        struct timesepc    st_ctime;
        blksize_t    st_blksize;
        blkcnt_t    st_blocks;
    }
    /* st_mode 二进制位对应关系, 显示的数字为 8 进制*/
         #define S_IFMT   0170000  /* type of file mask */
         #define S_IFIFO  0010000  /* named pipe (fifo) */
         #define S_IFCHR  0020000  /* character special */
         #define S_IFDIR  0040000  /* directory */
         #define S_IFBLK  0060000  /* block special */
         #define S_IFREG  0100000  /* regular */
         #define S_IFLNK  0120000  /* symbolic link */
         #define S_IFSOCK 0140000  /* socket */
         #define S_IFWHT  0160000  /* whiteout */
         #define S_ISUID  0004000  /* set user id on execution */
         #define S_ISGID  0002000  /* set group id on execution */
         #define S_ISVTX  0001000  /* save swapped text even after use */
         #define S_IRWXU  0000700  /* RWX mask for owner */
         #define S_IRUSR  0000400  /* read permission, owner */
         #define S_IWUSR  0000200  /* write permission, owner */
         #define S_IXUSR  0000100  /* execute/search permission, owner */
         #define S_IRWXG  0000070  /* RWX mask for group */
         #define S_IRGRP  0000040  /* read permission, group */
         #define S_IWGRP  0000020  /* write permission, group */
         #define S_IXGRP  0000010  /* execute/search permission, group */
         #define S_IRWXO  0000007  /* RWX mask for other */
         #define S_IROTH  0000004  /* read permission, other */
         #define S_IWOTH  0000002  /* write permission, other */
         #define S_IXOTH  0000001  /* execute/search permission, other */
    struct timespec {
        time_t    tv_sec;
        long    tv_nsec;
    }
    /* 判断文件类型的預定义宏, 类型匹配返回非 0, 否则返回 0 */
        S_ISREG(buf.st_mode)
        S_ISDIR(buf.st_mode)
        S_ISCHR(buf.st_mode)
        S_ISBLK(buf.st_mode)
        S_ISFIFO(buf.st_mode)
        S_ISLNK(buf.st_mode)
        S_ISSOCK(buf.st_mode)
    • 成功返回 0, 出錯返回 -1
    • lstat 不追踪软链接目标文件, 可获得软链接本身的状态信息, 软链接的文件大小等于其所指向的目标文件名称的字符数量
    • 文件的状态信息将写入事先定义的 stat 結构体中
    • 文件类型的判断, 由于每种文件类型并不独占相应的二进制位, 故需使用預定义的 S_ISREG ... 等宏, 或者取 buf.st_mode & S_IFMT 的結果与对应的文件类型(S_IFDIR ...)进行相等性测试
    • 文件的每类权限均独占相应的二进制位, 故可直接使用 buf.st_mode 与各权限宏逐一按位 '&'

    [b] umask

    #include <sys/stat.h>
    mode_t    umask(mode_t cmask) 
    • 总是成功, 返回之前的屏蔽字, 没有出錯返回值
    • 仅作用于当前进程环境, 不更改 shell 的 umask 值

    [c] chmod / lchmod / fchmod

    #include <sys/stat.h>
    int chmod(const char *path, mode_t mode)
    int lchmod(const char *path, mode_t mode)
    int fchmod(int fd, mode_t mode) 
    • 成功返回 0, 出錯返回 -1
    • lchmod 不追踪软链接
    • mode 可使用八进制数字表示, 如 0644 等

    [d] chown / lchown / fchown

    #include <unistd.h>
    int chown(const char *path, uid_t owner, gid_t group)
    int lchown(const char *path, uid_t owner, gid_t group)
    int fchown(int fd, uid_t owner, gid_t group) 
    • 成功返回 0, 出錯返回 -1

    [e] truncate / ftruncate

    #include <unistd.h>
    int truncate(const char *path, off_t length)
    int ftruncate(int fd, off_t length)
    • 成功返回 0, 出錯返回 -1
    • 将指定文件截断为 length 字节, 其值若大于原文件大小, 将在文件末尾创建空洞

    [f] link / unlink / mkdir / rmdir / remove

    #include <unistd.h>
    int link(const char *path, const char *path2)
    int unlink(const char *path)
    int mkdir(const char *path, mode_t mode)
    int rmdir(const char *path)
    #include <stdio.h>
    int remove(const char *path) 
    • 成功返回 0, 出錯返回 -1
    • link 创建硬链接, unlink 删除硬链接, mkdir 创建目录, rmdir 删除空目录
    • 标准库函数 remove 可对文件或目录执行 "删除"

    [g] rename

    #include <stdio.h>
    int rename(const char *path, const char *path2) 
    • 成功返回 0, 出錯返回 -1

    [h] symlink

    #include <unistd.h>
    int symlink(const char *path, const char *path2) 
    • 成功返回 0, 出錯返回 -1

    [i] readlink

    #include <unistd.h>
    ssize_t readlink(const char *restrict path, char *restrict buf, size_t bufsize) 
    • 成功返回读取到的字节数, 出錯返回 -1
    • 用于读取软链接指向的目标文件或目录名称

    [j] futimens

    #include <sys/stat.h>
    int futimens(int fd, const struct timespec times[2]) 
    • 成功返回 0, 出錯返回 -1
    • 用于更改文件或目录的 atime 或 mtime, 不能更改 ctime, 結构体数組的第一个元素指 atime
    • 若 times 参数指定为 NULL, 则更改为当前时间
    • 任意数組元素的 tv_nsec 字段为 UTIME_NOW, 则对应时间设置为当前时间; 若为 UTIME_OMIT, 则对应时间保持不变

    [k] opendir / fdopendir / readdir / rewinddir / closedir / telldir / seekdir

    #include <dirent.h>
    DIR *opendir(const char *path)
    DIR *fdopendir(int fd)
    struct dirent *readdir(DIR *dp)
    void rewinddir(DIR *dp)
    int closedir(DIR *dp)
    long telldir(DIR *dp)
    void seekdir(DIR *dp, long loc) 
    struct dirent {
        ino_t d_ino;
        char d_name[];
    } 

    [l] chdir / fchdir / getcwd

    #include <unistd.h>
    int chdir(const char *path)
    int fchdir(int fd)
    char *getcwd(char *buf, size_t size) 

    ... 

  • 相关阅读:
    Video视频播放中断问题排查记录
    下一站:手机安全
    数据之美 之一
    数据之美 之二
    数据之美 之三
    Groovy入门
    Java8新特性(Lambda表达式、Stream流、Optional类)等
    websocket和ajax的区别(和http的区别)
    java泛型<? extends E>和<? super E>的区别和适用场景
    JAVA反射
  • 原文地址:https://www.cnblogs.com/hadex/p/6128421.html
Copyright © 2011-2022 走看看