int stat(const char *path, struct stat *buf); int fstat(int fd, struct stat *buf); int lstat(const char *path, struct stat *buf);
三者差別只在「怎麼指定目標檔案」:stat 用路徑;fstat 用已開啟的檔案描述符;lstat 也走路徑,但遇到符號連結時它會去讀連結本身,而不是連結指向的目標。這個差異在處理 symlink 時至關重要——想查連結指向誰用 stat,想查連結自己的屬性用 lstat。
struct stat {
dev_t st_dev; /* ID of device containing file */
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) */
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for file system I/O */
blkcnt_t st_blocks; /* number of 512B blocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last status change */
};
幾個容易混淆的欄位:st_dev 是檔案所在裝置,st_rdev 只有裝置檔才有意義,記錄實際的裝置號;st_nlink 是硬連結數;st_blksize 是 I/O 建議區塊大小,st_blocks 才是真正佔用的 512B 區塊數(這兩者不一致時,就是稀疏檔案的訊號);三個時間分別是最後存取、最後修改、最後狀態變更。
示範程式(自製一個迷你 ls -l):
/*************************************************************************
> File Name: file_stat.c
> Author: Simba
> Mail: dameng34@163.com
> Created Time: Sat 23 Feb 2013 02:34:02 PM CST
************************************************************************/
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#define ERR_EXIT(m) \
do { \
perror(m); \
exit(EXIT_FAILURE); \
} while(0)
#define MAJOR(a) (int)((unsigned short)a >> 8) // 高 8 位,主裝置號
#define MINOR(a) (int)((unsigned short)a & 0xFF)
int filetype(struct stat *buf)
{
int flag = 0;
printf("Filetype:");
mode_t mode;
mode = buf->st_mode;
switch (mode & S_IFMT)
{
case S_IFSOCK:
printf("socket\n");
break;
case S_IFLNK:
printf("symbolic link\n");
break;
case S_IFREG:
printf("regular file\n");
break;
case S_IFBLK:
printf("block device\n");
flag = 1;
break;
case S_IFDIR:
printf("directory\n");
break;
case S_IFCHR:
printf("character device\n");
flag = 1;
break;
case S_IFIFO:
printf("FIFO\n");
break;
default:
printf("unknown file type\n");
break;
}
return flag;
}
void fileperm(struct stat *buf, char perm[])
{
strcpy(perm, "----------");
perm[0] = '?';
mode_t mode;
mode = buf->st_mode;
switch (mode & S_IFMT)
{
case S_IFSOCK:
perm[0] = 's';
break;
case S_IFLNK:
perm[0] = 'l';
break;
case S_IFREG:
perm[0] = '-';
break;
case S_IFBLK:
perm[0] = 'b';
break;
case S_IFDIR:
perm[0] = 'd';
break;
case S_IFCHR:
perm[0] = 'c';
break;
case S_IFIFO:
perm[0] = 'p';
break;
}
if (mode & S_IRUSR)
perm[1] = 'r';
if (mode & S_IWUSR)
perm[2] = 'w';
if (mode & S_IXUSR)
perm[3] = 'x';
if (mode & S_IRGRP)
perm[4] = 'r';
if (mode & S_IWGRP)
perm[5] = 'w';
if (mode & S_IXGRP)
perm[6] = 'x';
if (mode & S_IROTH)
perm[7] = 'r';
if (mode & S_IWOTH)
perm[8] = 'w';
if (mode & S_IXOTH)
perm[9] = 'x';
perm[10] = '\0';
}
int main(int argc, char *argv[])
{
if (argc != 2)
{
fprintf(stderr, "Usage %s file\n", argv[0]);
exit(EXIT_FAILURE);
}
printf("Filename:%s\n", argv[1]);
struct stat sbuf;
if (lstat(argv[1], &sbuf) == -1)
ERR_EXIT("stat error");
printf("file in Dev number:major %d, minor %d\n",
MAJOR(sbuf.st_dev), MINOR(sbuf.st_dev));
printf("File inode:%d\n", (int) sbuf.st_ino);
if (filetype(&sbuf))
{
printf("Device number:major %d, minor %d\n",
MAJOR(sbuf.st_rdev), MINOR(sbuf.st_rdev));
}
char perm[11] = {0};
fileperm(&sbuf, perm);
printf("File permission bits=%o %s\n", sbuf.st_mode & 07777, perm);
return 0;
}
測試:
simba@ubuntu:~/Documents/code/linux_programming/APUE/File_IO$ ./file_stat Makefile Filename:Makefile file in Dev number:major 8, minor 1 File inode:660022 Filetype:regular file File permission bits=664 -rw-rw-r--
這支程式的價值在於把 st_mode 這個「一欄多用」的欄位拆開來看:高幾位編碼檔案類型(用 S_IFMT 遮罩後比對 S_IFREG、S_IFDIR 等),低 12 位則是權限位(用 07777 遮罩)。ls -l 最左邊那個由 -、d、l、b、c、p、s 組成的字元,背後就是這段 switch 的邏輯。