在MacOs上使用statfs获取块文件大小。

问题描述 投票:0回答:1

我想知道什么是MacOs下的块文件大小。在我的例子中,我试图确定devdisk0。

diskutil info /dev/disk0
   Device Identifier:         disk0
   Device Node:               /dev/disk0
   Whole:                     Yes
   Part of Whole:             disk0
   Device / Media Name:       APPLE SSD SD0128F

   Volume Name:               Not applicable (no file system)
   Mounted:                   Not applicable (no file system)
   File System:               None

   Content (IOContent):       GUID_partition_scheme
   OS Can Be Installed:       No
   Media Type:                Generic
   Protocol:                  PCI
   SMART Status:              Verified

   Disk Size:                 121.3 GB (121332826112 Bytes) (exactly 236978176 512-Byte-Units)

Diskutil给出了正确的大小 "123.3 GB"。现在用 statfs:(https:/developer.apple.comlibraryarchivedocumentationSystemConceptualManPages_iPhoneOSman2statfs.2.html#/apple_refdocman2statfs。):

#include <iostream>

#include <sys/param.h>
#include <sys/mount.h>

int main()
{
        struct statfs s;
        statfs("/dev/disk0", &s);
        std::cout << s.f_bsize * s.f_blocks << " B\n";
}

产出: 196096 B

c++ macos filesystems
1个回答
2
投票

文件中的 statfs 是这样的。

Statfs()返回挂载文件系统的信息。 Path是挂载文件系统中任何文件的路径名。 Buf 是指向 statfs 或 statfs64 结构的指针,定义如下......

您正在传递 /dev/disk0 作为路径,这将为你提供任何文件系统代表的大小。/dev,这很可能是某种devtmpfs,你的路径应该是文件系统中的文件名或路径。你的路径应该是文件系统中的一个文件名或路径,它在 /dev/disk0而不是块设备本身。

对于一个任意的块设备。

发出IOCTLs 据载:

int fd = open("/dev/whatever", O_RDONLY);
if(fd  < 0) {
    // error handling
}
uint64_t count;
if(ioctl(fd, DKIOCGETBLOCKCOUNT, &count) < 0) {
    // error handling
}
uint32_t bsize;
if(ioctl(fd, DKIOCGETBLOCKSIZE, &bsize) < 0) {
    // error handling
}
return count * bsize;
© www.soinside.com 2019 - 2024. All rights reserved.