Linuxシステムプログラミング19ファイルシステム-ファイル統計の取得

男2の統計

NAME
stat、fstat、lstat、fstatat-ファイルステータスを取得し、ファイル属性情報を取得します

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

/ *
ファイルパス名のファイル属性が取り出され、struct stat * buf
* /
int stat(const char * pathname、struct stat * buf); //ファイル名で入力されます。

/ *
ファイルfdのファイル属性を取り出し、それをstruct stat * bufに入力します
* /
int fstat(int fd、struct stat * buf); //ファイル記述子を介して

戻り値
成功すると、ゼロが返されます。エラーの場合、-1が返され、errnoが適切に設定されます。

 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.統計を通じてファイルサイズ情報を取得します

#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