首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何打印HFS卷头

如何打印HFS卷头
EN

Stack Overflow用户
提问于 2011-05-09 18:22:59
回答 2查看 776关注 0票数 1

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

EN

回答 2

Stack Overflow用户

发布于 2011-05-09 19:49:34

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

代码语言:javascript
复制
#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+卷头中字段的完整列表,请参阅此文件。

票数 3
EN

Stack Overflow用户

发布于 2011-05-09 18:58:19

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5935509

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档