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$ 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章