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/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

发表回复

登录后才能评论