structstat詳解

一、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-tw/n/239782.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-12 12:18
下一篇 2024-12-12 12:18

相關推薦

  • Linux sync詳解

    一、sync概述 sync是Linux中一個非常重要的命令,它可以將文件系統緩存中的內容,強制寫入磁碟中。在執行sync之前,所有的文件系統更新將不會立即寫入磁碟,而是先緩存在內存…

    編程 2025-04-25
  • 神經網路代碼詳解

    神經網路作為一種人工智慧技術,被廣泛應用於語音識別、圖像識別、自然語言處理等領域。而神經網路的模型編寫,離不開代碼。本文將從多個方面詳細闡述神經網路模型編寫的代碼技術。 一、神經網…

    編程 2025-04-25
  • nginx與apache應用開發詳解

    一、概述 nginx和apache都是常見的web伺服器。nginx是一個高性能的反向代理web伺服器,將負載均衡和緩存集成在了一起,可以動靜分離。apache是一個可擴展的web…

    編程 2025-04-25
  • MPU6050工作原理詳解

    一、什麼是MPU6050 MPU6050是一種六軸慣性感測器,能夠同時測量加速度和角速度。它由三個感測器組成:一個三軸加速度計和一個三軸陀螺儀。這個組合提供了非常精細的姿態解算,其…

    編程 2025-04-25
  • git config user.name的詳解

    一、為什麼要使用git config user.name? git是一個非常流行的分散式版本控制系統,很多程序員都會用到它。在使用git commit提交代碼時,需要記錄commi…

    編程 2025-04-25
  • C語言貪吃蛇詳解

    一、數據結構和演算法 C語言貪吃蛇主要運用了以下數據結構和演算法: 1. 鏈表 typedef struct body { int x; int y; struct body *nex…

    編程 2025-04-25
  • 詳解eclipse設置

    一、安裝與基礎設置 1、下載eclipse並進行安裝。 2、打開eclipse,選擇對應的工作空間路徑。 File -> Switch Workspace -> [選擇…

    編程 2025-04-25
  • Python安裝OS庫詳解

    一、OS簡介 OS庫是Python標準庫的一部分,它提供了跨平台的操作系統功能,使得Python可以進行文件操作、進程管理、環境變數讀取等系統級操作。 OS庫中包含了大量的文件和目…

    編程 2025-04-25
  • Java BigDecimal 精度詳解

    一、基礎概念 Java BigDecimal 是一個用於高精度計算的類。普通的 double 或 float 類型只能精確表示有限的數字,而對於需要高精度計算的場景,BigDeci…

    編程 2025-04-25
  • Linux修改文件名命令詳解

    在Linux系統中,修改文件名是一個很常見的操作。Linux提供了多種方式來修改文件名,這篇文章將介紹Linux修改文件名的詳細操作。 一、mv命令 mv命令是Linux下的常用命…

    編程 2025-04-25

發表回復

登錄後才能評論