一、structstat概述
structstat是一個C語言中的結構體,它包含了Unix和類Unix系統中文件狀態的所有信息。structstat用於查詢文件的各種屬性信息,比如大小、權限、時間戳等。在Unix和類Unix系統中,structstat結構體十分常見和有用。
Stat系統調用是Unix和類Unix系統中最常用的系統調用之一。它用於獲取文件的元數據,並將其存儲在structstat結構體中。元數據包括文件類型、大小、屬性、訪問時間、修改時間等。structstat結構體定義如下:
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* Inode number */
mode_t st_mode; /* File type and mode */
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; /* Block size for filesystem 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 */
};
我們可以通過Manipulating File Attributes和Stat() 兩篇官方文檔了解如何使用structstat獲取文件信息。
二、使用StructStatVfs獲取文件系統信息
structstat不僅可以用於獲取文件的屬性信息,還可以用於獲取文件系統的相關信息。可以使用下面列出的函數來獲取完全的stat結構,也可以使用較簡單的基本信息函數(如st_size,st_mtime等)來獲取部分信息。
1. statvfs()
statvfs()函數是一個Unix系統調用,用於檢索文件系統的狀態(特別是容量)。它返回一個structstatvfs結構體,其中包含文件系統的各種信息。structstatvfs定義如下:
struct statvfs {
unsigned long f_bsize; /* file system block size */
unsigned long f_frsize; /* fragment size */
fsblkcnt_t f_blocks; /* size of fs in f_frsize units */
fsblkcnt_t f_bfree; /* free blocks in fs */
fsblkcnt_t f_bavail; /* free blocks avail to non-superuser */
fsfilcnt_t f_files; /* total file nodes in file system */
fsfilcnt_t f_ffree; /* free file nodes in fs */
fsfilcnt_t f_favail; /* free file nodes avail to non-superuser */
unsigned long f_fsid; /* file system id */
unsigned long f_flag; /* mount flags */
unsigned long f_namemax; /* maximum filename length */
};
我們可以使用下面這個使用案例看一下如何使用statvfs()函數,使用該函數返回當前文件系統(root文件系統)的容量信息。
#include
#include
int main() {
struct statvfs stats;
statvfs("/", &stats);
printf("File system block size: %ld\n", stats.f_bsize);
printf("Fragment size: %ld\n", stats.f_frsize);
printf("Total blocks (in f_frsize units): %ld\n", stats.f_blocks);
printf("Free blocks available to non-superuser: %ld\n", stats.f_bavail);
printf("Total file nodes (in f_frsize units): %ld\n", stats.f_files);
printf("Free file nodes (in f_frsize units): %ld\n", stats.f_ffree);
printf("Max filename length: %ld\n", stats.f_namemax);
return 0;
}
2. fstatvfs()
fstatvfs()函數也是用於檢索文件系統的狀態(特別是容量)。它和statvfs()函數的用法幾乎一模一樣,只是它使用的是文件描述符(fd)來代替文件名。我們可以使用下面這個使用案例看一下如何使用fstatvfs()函數,使用該函數返回當前進程所在文件系統的容量信息。
#include
#include
#include
int main() {
int fd;
struct statvfs stats;
fd = open(".", O_RDONLY);
fstatvfs(fd, &stats);
close(fd);
printf("File system block size: %ld\n", stats.f_bsize);
printf("Fragment size: %ld\n", stats.f_frsize);
printf("Total blocks (in f_frsize units): %ld\n", stats.f_blocks);
printf("Free blocks available to non-superuser: %ld\n", stats.f_bavail);
printf("Total file nodes (in f_frsize units): %ld\n", stats.f_files);
printf("Free file nodes (in f_frsize units): %ld\n", stats.f_ffree);
printf("Max filename length: %ld\n", stats.f_namemax);
return 0;
}
三、使用StructStat獲取文件信息
我們可以使用下面列出的使用案例來獲取文件的信息:
#include
#include
int main() {
struct stat sb;
if (stat("/etc/passwd", &sb) == -1) {
perror("stat");
return 1;
}
printf("File type: ");
switch (sb.st_mode & S_IFMT) {
case S_IFBLK: printf("block device\n"); break;
case S_IFCHR: printf("character device\n"); break;
case S_IFDIR: printf("directory\n"); break;
case S_IFIFO: printf("FIFO/pipe\n"); break;
case S_IFLNK: printf("symlink\n"); break;
case S_IFREG: printf("regular file\n"); break;
case S_IFSOCK: printf("socket\n"); break;
default: printf("unknown?\n"); break;
}
printf("I-node number: %ld\n", (long) sb.st_ino);
printf("Mode: %lo (octal)\n",
(unsigned long) sb.st_mode);
printf("Link count: %ld\n", (long) sb.st_nlink);
printf("Ownership: UID=%ld GID=%ld\n",
(long) sb.st_uid, (long) sb.st_gid);
printf("Preferred I/O block size: %ld bytes\n",
(long) sb.st_blksize);
printf("File size: %lld bytes\n",
(long long) sb.st_size);
printf("Blocks allocated: %lld\n",
(long long) sb.st_blocks);
printf("Last status change: %s", ctime(&sb.st_ctime));
printf("Last file access: %s", ctime(&sb.st_atime));
printf("Last file modification: %s", ctime(&sb.st_mtime));
return 0;
}
四、使用StructStat在Windows系統中獲取文件信息
在Windows系統中,我們可以使用下面列出的使用案例來獲取文件的信息:
#include
#include
#include
int main() {
struct _stat buffer;
int status;
status = _stat("C:\\file.txt", &buffer);
if (status == 0) {
printf("File size: %ld bytes\n", buffer.st_size);
printf("Creation date: %s", ctime(&buffer.st_ctime));
printf("Modification date: %s", ctime(&buffer.st_mtime));
printf("Access date: %s", ctime(&buffer.st_atime));
} else {
printf("File not found");
}
return 0;
}
五、使用StructStat在MacOS系統中獲取文件信息
在MacOS系統中,我們可以使用下面的使用案例來獲取文件的信息:
#include
#include
#include
#include
int main() {
struct stat statbuf;
if (stat("/etc/passwd", &statbuf) == -1) {
perror("stat");
return -1;
}
printf("Size: %lld\n", statbuf.st_size);
printf("User ID: %d\n", statbuf.st_uid);
printf("Group ID: %d\n", statbuf.st_gid);
return 0;
}
六、小結
利用structstat結構體可以輕鬆的獲取到文件系統和文件的各種屬性信息,可以方便地實現類unix操作系統在使用過程中的代碼控制。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/239782.html
微信掃一掃
支付寶掃一掃