当前位置:Gxlcms > 数据库问题 > 文件系统之 stat与access

文件系统之 stat与access

时间:2021-07-01 10:21:17 帮助过:35人阅读

<sys/stat.h> #include <unistd.h> int stat(const char *path, struct stat *buf); int fstat(int fd, struct stat *buf); int lstat(const char *path, struct stat *buf); struct stat { dev_t st_dev; /* ID of device containing file 文件的设备编号*/ ino_t st_ino; /* inode number 文件索引号(Inode文件节点)*/ mode_t st_mode; /* protection 文件的类型和存取的权限*/ nlink_t st_nlink; /* number of hard links 连到该文件的硬连接数目, 刚建立的文件值为1.*/ 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 文件系统的I/O 缓冲区大小*/ blkcnt_t st_blocks; /* number of 512B blocks allocated 占用文件区块的个数, 每一区块大小为512 个字节*/ 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 最近一次被更改的时间*/ };

 

 

 

 

 

access函数

头文件:#include < unistd.h>

定义函数:int access(const char * pathname, int mode);

函数说明:access()会检查是否可以读/写某一已存在的文件。

参数mode 有几种情况组合:
1、R_OK, W_OK, X_OK和F_OK. R_OK, W_OK 与X_OK 用来检查文件是否具有读取写入和执行的权限。
2、F_OK 则是用来判断该文件是否存在。由于access()只作权限的核查, 并不理会文件形态或文件内容,因此,如果一目录表示为”可写入”,表示可以在该目录中建立新文件等操作,而非意味此目录可以被当做文件处理。例如:你会发现DOS 的文件都具有”可执行”权限,但用execve()执行时则会失败。

返回值:若所有欲查核的权限都通过了检查则返回0 值,表示成功,只要有一权限被禁止则返回-1。

错误代码:
1、EACCESS 参数pathname 所指定的文件不符合所要求测试的权限.
2、EROFS 欲测试写入权限的文件存在于只读文件系统内.
3、EFAULT 参数pathname 指针超出可存取内存空间.
4、EINVAL 参数mode 不正确.
5、ENAMETOOLONG 参数pathname 太长.
6、ENOTDIR 参数pathname 为一目录.
7、ENOMEM 核心内存不足
8、ELOOP 参数pathname 有过多符号连接问题.
9、EIO I/O 存取错误.

 

#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
    //R_OK 是否有读权限
    //W_OK 是否有写权限
    //X_OK 是否有执行权限
    //F_OK 测试一个文件是否存在
    if(access("abc",F_OK) <0)
    {
        perror("abc");
        exit(1);
    }

    printf("abc is ok\n");

    return 0;
}

 

文件系统之 stat与access

标签:就会   索引   符号   原因   信息   ima   访问时间   ctime   存在   

人气教程排行