Linux系统编程19 文件系统 - 获取文件属性 stat

man 2 stat

NAME
stat, fstat, lstat, fstatat - get file status,获取文件属性信息

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

   int stat(const char *pathname, struct stat *buf); //通过文件名 获取文件属性信息
   int fstat(int fd, struct stat *buf); //通过文件描述符 获取文件属性信息
   int lstat(const char *pathname, struct stat *buf);  //通过链接文件 获取文件属性信息

/*
将文件pathname 的文件属性 取出 填充到 struct stat *buf
*/
int stat(const char *pathname, struct stat *buf); //通过文件名

/*
将文件 fd 的文件属性 取出 填充到 struct stat *buf
*/
int fstat(int fd, struct stat *buf); //通过文件描述符

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

 struct stat {
           dev_t     st_dev;         /* ID of device containing file */文件所在设备的ID
           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) */设备ID号,针对设备文件
           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
       };

在这里插入图片描述

在这里插入图片描述

stat命令 获取文件属性

mhr@ubuntu:~/work/linux/wenjianxitong/19$ 
mhr@ubuntu:~/work/linux/wenjianxitong/19$ stat test
  File: 'test'
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 801h/2049d	Inode: 12847384    Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/     mhr)   Gid: ( 1000/     mhr)
Access: 2020-05-03 09:01:33.072012148 -0700
Modify: 2020-05-03 09:01:29.500013664 -0700
Change: 2020-05-03 09:01:31.380012438 -0700
 Birth: -
mhr@ubuntu:~/work/linux/wenjianxitong/19$ 
mhr@ubuntu:~/work/linux/wenjianxitong/19$ 

可以看出 stat 命令使用 stat()函数封装出来的,ls 各种参数的 都是从stat 中获取信息。

实验1, 通过 stat 获取文件大小信息

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

/*
	注意,st_size的类型是 off_t , 不是int 
	off_t类型具体是多少位是不清楚的,只是在一些体系中,会把 off_t 定义成32位的,
*/
static off_t flen(const char *fname)
{
	struct stat statres;
	if(stat(fname,&statres) < 0)
	{
		perror("stat()");
		exit(1);
	}
	return statres.st_size;
}

int main(int argc, char *argv[])
{

	if(argc < 2)
	{
		fprintf(stderr,"Usage......\n");
		exit(1);
	}
	
	printf("%lld\n",(long long)flen(argv[1]));
	
	exit(0);
}


mhr@ubuntu:~/work/linux/wenjianxitong/19$ gcc fslen.c 
mhr@ubuntu:~/work/linux/wenjianxitong/19$ ./a.out test 
0
mhr@ubuntu:~/work/linux/wenjianxitong/19$
mhr@ubuntu:~/work/linux/wenjianxitong/19$ stat test
  File: 'test'
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 801h/2049d	Inode: 12847384    Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/     mhr)   Gid: ( 1000/     mhr)
Access: 2020-05-03 09:01:33.072012148 -0700
Modify: 2020-05-03 09:01:29.500013664 -0700
Change: 2020-05-03 09:01:31.380012438 -0700
 Birth: -
mhr@ubuntu:~/work/linux/wenjianxitong/19$ 

猜你喜欢

转载自blog.csdn.net/LinuxArmbiggod/article/details/105911091