• linux 文件属性


    关于属性的结构

    在linux下文件和文件夹都被认为是文件, 所以以下的这个属性对文件和文件夹通用
    获取属性的函数有stat/fstat/lstat/fstat

    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; //文件拥有者的ID
    gid_t    st_gid; //文件拥所在的用户组
    offt_t    st_size; //文件大小
    struct timespec    st_atime;
    struct timespec    st_mtime;
    struct timespec    st_ctime;
    blksize_t    st_blksize;
    blkcnt_t    st_blocks;
    }
    
    struct stat buf;
    char *pathname="./test.txt";
    if(lstat(pathname, &buf) < 0){
        printf("lstat error");
        exit(1);
    }
    

    文件类型

    linux下文件分为以下几种:
    1.普通文件(regular file), 判断函数为S_ISREG();
    2.目录文件(directory file), 判断函数为S_ISDIR();
    3.块特殊文件(block special file), S_ISBLK();
    3.字符特殊文件(character special file), S_ISCHR();
    5.进程通信管道文件(FIFO), S_ISFIFO();
    6.套接字(socket), S_ISSOCK();
    7.符号链接(symbolic link), S_ISLNK();

    struct stat buf;
    char *pathname="./test.txt";
    if(lstat(pathname, &buf) < 0){
        printf("lstat error");
        exit(1);
    }
    
    if(S_ISREG(buf.st_mode))
        printf("this is a regular file");
    else if(S_ISDIR(buf.st_mode))
        printf("this is a directory");
    else if(..)
    ...
    else
        printf("unknown file type");
    

    测试访问权限

    文件有读/写/执行三种权限, 文件的拥有者对文件可能有读/写/执行的权限, 同组的可能有读/执行的权限, 而不同组的可能连读的权限都没有
    所以在我们对已存在的文件进行读写操作时, 可以先进行访问权限判断
    int access(const char *pathname, int mode), mode有三个供选择参数:R_OK, W_OK, X_OK, 分别是读/写/执行

    char *pathname="./test.txt";
    if(access(pathname,R_OK) < 0){
        perror("access error");
        exit(1);
    }else
        printf("read access");
    
    int fd;
    if((fd=open(pathname,O_RDONLY))<0){
        printf("open error");
        exit(1);
    }else
        printf("open for reading");
    

    更改访问权限

    更改文件的访问权限条件需至少需满足一项:1.超级用户进程进行更改; 2.文件拥有者的进程进行更改
    文件权限的设置分成三部分, 分别作用于:
    1.文件的拥有者,S_IRUSR,S_IWUSR,S_IXUSR,三合一的写法为S_IRWXU
    2.同组的用户,S_IRGRP,S_IWGRP,S_IXGRP,三合一的写法为S_IRWXG
    3.其它组的用户,S_IROTH,S_IWOTH,S_IXOTH,三合一的写法为S_IRWXO

    以上权限依次为读, 写, 执行, 三合一的包含三个权限

    更改的函数有chmod/fchmod/fchmodat

    struct stat buf;
    char *pathname="./test.txt";
    if(stat(pathname,&buf)<0){
        printf("stat error");
        exit(1);
    }
    
    //只关掉S_IXGRP
    if(chmod(pathname,buf.st_mode & ~S_IXGRP)<0)
        printf("chmod error");
    
    //不管当前的权限, 以绝对的方式设置
    if(chmod(pathname,S_IRUSR|S_IWUSR|S_IRGRP)<0)
        printf("chmod error");
    

    更改文件拥有者

    该操作在大多数linux系统中需要root来执行
    chown/fchown/fchownat/lchown
    int chown (const char *pathname, uid_t owner, gid_t group);

    //假设当前系统有个普通用户组的用户,ID和组ID都为1000
    //以root身份运行, sudo root
    char *pathname="./test.txt";
    if(chown(pathname,1000,1000)<0)
        printf("chown failed");
    

    文件大小

    struct stat buf;
    char *pathname="./test.txt";
    if(lstat(pathname,&buf)<0){
        printf("lstat error");
        exit(1);
    }
    printf("file size: %lu",buf.st_size);
    

    另外可以用truncate(pathname,0)将文件大小设置成0,也就是擦除文件内容

    其它

    创建文件: int creat(const char *path, mode_t mode) mode为访问权限,用open也可以创建文件
    文件重命名: int rename(const char *oldname, const char *newname)
    删除文件: int remove(const char *pathname)

  • 相关阅读:
    Java8新特性2 lambda表达式
    Java8新特性1
    多线程与高并发(2)Volatile+CAS
    多线程与高并发 Synchronize
    《重构:改善既有代码的设计》读书笔记5 简化函数调用
    《重构:改善既有代码的设计》读书笔记4 简化条件表达式
    mysql语法大全使用篇
    《重构:改善既有代码的设计》读书笔记3 重新组织数据
    《重构:改善既有代码的设计》读书笔记2 在对象之间搬移特性
    《重构:改善既有代码的设计》读书笔记1——重新组织函数
  • 原文地址:https://www.cnblogs.com/cfans1993/p/5624413.html
Copyright © 2020-2023  润新知