linux内核的stat函数,Linux 基础(一)stat函数-Go语言中文社区

Header file:

#include

#include

#include

DEFINITION:

int stat(const char *pathname, struct stat *buf);

DESCRIPTION:

The functions return information about a file, in the buffer pointed to by buf.

No permissions are required on the file itself, but—in the case of stat(), fstatat(), and

lstat()—execute (search) permission is required on all of the directories in pathname

that lead to the file.

stat() and fstatat() retrieve information about the file pointed to by pathname;

stat() and fstatat() retrieve information about the file pointed to by pathname; the differences for fstatat() are described below.

lstat() is identical to stat(), except that if pathname is a symbolic link, then it returns information about the link itself, not the file that it refers to.

fstat() is identical to stat(), except that the file about which information is to be retrieved is specified by the file descriptor fd.

RETURN VALUE:

On success, zero is returned.  On error, -1 is returned, and errno is set appropriately.

ERRORS:

EACCES Search permission is denied for one of the directories in the path prefix of path‐name. (See also path_resolution(7).)

EBADF fd is bad.

EFAULT Bad address.

ELOOP Too many symbolic links encountered while traversing the path.

ENAMETOOLONG   pathname is too long.

ENOENT       A component of pathname does not exist, or pathname is an empty string.

ENOMEM   Out of memory (i.e., kernel memory).

ENOTDIR  A component of the path prefix of pathname is not a directory.

EOVERFLOW  pathname or fd refers to a file whose size, inode number, or number of blocks can‐not be represented in, respectively, the types off_t, ino_t, or blkcnt_t. This

error can occur when, for example, an application compiled on a 32-bit platform without -D_FILE_OFFSET_BITS=64 calls stat() on a file whose size exceeds (1<<31)-1 bytes.

#include #include#include

intmain() {structstat buf;

stat("HOME/C++/test", &buf);

printf("the file size = %dn", buf.st_size);

}

stat structure:

structstat {

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 filesystem I/O*/blkcnt_t st_blocks;/*number of 512B blocks allocated*/

/*Since Linux 2.6, the kernel supports nanosecond

precision for the following timestamp fields.

For the details before Linux 2.6, see NOTES.*/

struct timespec st_atim; /* time of last access */

struct timespec st_mtim; /* time of last modification */

struct timespec st_ctim; /* time of last status change */

#define st_atime st_atim.tv_sec /* Backward compatibility */

#define st_mtime st_mtim.tv_sec

#define st_ctime st_ctim.tv_sec};

};

fstat(int fd,struct stat *)接收的已open的文件描述符

stat(char *filename,struct stat *)接收的路径名, 需要注意的是 能处理符号链接,但处理的是符号链接指向的文件。

lstat(char *filename,struct stat *)接收的路径名  ,需要注意的是,也能能处理符号链接,但处理的是符号链接本身(自身)文件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值