2011-05-09 17 views
1

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

+1

用什么编程语言? – 2011-05-09 10:27:15

+0

在Mac上使用C或客观C – anonymos 2011-05-09 10:31:05

+0

任何信息,..... – anonymos 2011-05-09 10:40:31

回答

3

我写了一个小程序(基于hfs-183.1),其中打印了一些在struct HFSPlusVolumeHeader中声明的信息。

#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:通过sudo(8)例如 - 该程序必须运行为root。请参阅此文件以获取HFS +卷标头中的完整字段列表。

+0

非常感谢您....您的代码段真的很有用 – anonymos 2011-05-10 04:12:37