c/c++語言函式 stat, fstat, lstat, fstatat - get file status

悟透發表於2024-06-26

前言全域性說明

c/c++語言函式 stat, fstat, lstat, fstatat - get file status


一、說明



二、函式原型

2.1

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int stat(const char *pathname, struct stat *statbuf);
int fstat(int fd, struct stat *statbuf);
int lstat(const char *pathname, struct stat *statbuf);

2.2

#include <fcntl.h>           /* Definition of AT_* constants */
#include <sys/stat.h>

int fstatat(int dirfd, const char *pathname, struct stat *statbuf, int flags);

2.3

struct stat {
    dev_t     st_dev;         /* ID of device containing file */
    ino_t     st_ino;         /* Inode number */
    mode_t    st_mode;        /* File type and mode */
    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;     /* Block size 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
};

三、使用示例

3.1 獲取檔案大小

fd = open(filename, O_RDONLY, S_IREAD);
if( fd < 0 ) {
	printf("get_file_size: Unable to open file\n");
	return -1;
}

fstat(fd, &istat);
printf("File size: %d", istat.st_size);

3.2

檔名:



四、引數,速查表格

4.1 struct stat結構體

常量 許可權 中文說明 原文說明 備註
st_dev This field describes the device on which this file resides. (The major(3) and minor(3) macros may be useful to decompose the device ID in this field.)
st_ino This field contains the file's inode number.
st_mode This field contains the file type and mode. See inode(7) for further information.
st_nlink This field contains the number of hard links to the file.
st_uid This field contains the user ID of the owner of the file.
st_gid This field contains the ID of the group owner of the file.
st_rdev This field describes the device that this file (inode) represents.
st_size 獲取檔案大小 This field gives the size of the file (if it is a regular file or a symbolic link) in bytes. The size of a symbolic link is the length of the pathname it contains, without a terminating null byte.
st_blksize This field gives the "preferred" block size for efficient filesystem I/O.
st_blocks This field indicates the number of blocks allocated to the file, in 512-byte units. (This may be smaller than st_size/512 when the file has holes.)
st_atime This is the file's last access timestamp.
st_mtime This is the file's last modification timestamp.
st_ctime This is the file's last status change timestamp.

4.2 錯誤返回值

常量 許可權 中文說明 原文說明 備註
EACCES Search permission is denied for one of the directories in the path prefix of pathname. (See also path_resolution(7).)
EBADF fd is not a valid open file descriptor.
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 and AT_EMPTY_PATH was not specified in flags.
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 cannot 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_OFF‐SET_BITS=64 calls stat() on a file whose size exceeds (1<<31)-1 bytes. The following additional errors can occur for fstatat():
EBADF dirfd is not a valid file descriptor.
EINVAL Invalid flag specified in flags.
ENOTDIR pathname is relative and dirfd is a file descriptor referring to a file other than a directory.


免責宣告:本號所涉及內容僅供安全研究與教學使用,如出現其他風險,後果自負。




參考、來源:
man 2 fstat
https://blog.csdn.net/weixin_37926485/article/details/122804385



相關文章