如何打印HFS卷标头

时间:2011-05-09 10:22:59

标签: objective-c macos hfs+

任何人都请给出如何打印HFS +磁盘卷标题的代码片段。

2 个答案:

答案 0 :(得分:3)

我写了一个小程序(基于hfs-183.1),它打印了struct HFSPlusVolumeHeader中声明的一些信息。该计划必须以root运行 - 例如,通过sudo(8)

#include <fcntl.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <hfs/hfs_format.h>
#include <libkern/OSByteOrder.h>

int main(void) {
    int fd;
    struct stat stat_buf;
    struct HFSPlusVolumeHeader vheader;

    const char *vname = "/dev/rdisk0s2";

    if (lstat(vname, &stat_buf) == -1) {
        fprintf(stderr, "Couldn't stat %s\n", vname);
        perror(NULL);
        exit(1);
    }

    if ((stat_buf.st_mode & S_IFMT) != S_IFCHR) {
        fprintf(stderr, "%s is not a raw char device\n", vname);
        perror(NULL);
        exit(2);
    }

    fd = open(vname, O_RDONLY);
    if (fd == -1) {
        fprintf(stderr, "%s couldn't be opened for reading\n", vname);
        perror(NULL);
        exit(3);
    }

    // The volume header starts at offset 1024

    if (pread(fd, &vheader, sizeof vheader, 1024) != sizeof vheader) {
        fprintf(stderr, "couldn't read %s's volume header\n", vname);
        perror(NULL);
        exit(4);
    }

    printf("fileCount   = %u\n"
           "folderCount = %u\n"
           "blockSize   = %u\n"
           "totalBlocks = %u\n"
           "freeBlocks  = %u\n",
           OSSwapBigToHostInt32(vheader.fileCount),
           OSSwapBigToHostInt32(vheader.folderCount),
           OSSwapBigToHostInt32(vheader.blockSize),
           OSSwapBigToHostInt32(vheader.totalBlocks),
           OSSwapBigToHostInt32(vheader.freeBlocks));

    close(fd);

    return 0;
}

标头文件<hfs/hfs_format.h>声明struct HFSPlusVolumeHeader。有关HFS +卷标题内的完整字段列表,请参阅此文件。

答案 1 :(得分:1)

系统调用getattrlist()可能会为您提供所需的信息。