Linux System Programming 19 File System-Get File Stat

man 2 stat

NAME
stat, fstat, lstat, fstatat-get file status, get file attribute information

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);  //通过链接文件 获取文件属性信息

/*
The file attribute of the file pathname is taken out and filled into struct stat *buf
*/
int stat(const char *pathname, struct stat *buf); //by the file name

/*
Take out the file attribute of the file fd and fill it into struct stat *buf
*/
int fstat(int fd, struct stat *buf); //through the file descriptor

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
       };

Insert picture description here

Insert picture description here

stat command to get file attributes

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$ 

It can be seen that the stat command is encapsulated by the stat() function, and the various parameters of ls are obtained from stat.

Experiment 1. Obtain file size information through 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$ 

Guess you like

Origin blog.csdn.net/LinuxArmbiggod/article/details/105911091