打开原始磁盘并获取大小 OS X

发布于 2025-01-01 10:24:55 字数 1039 浏览 2 评论 0原文

使用以下代码,我能够在我的计算机上成功打开原始磁盘,但是当我获取磁盘长度时,我每次都会得到 0...

// Where "Path" is /dev/rdisk1 -- is rdisk1 versus disk1 the proper way to open a raw disk?
Device = open(Path, O_RDWR);
if (Device == -1)
{
    throw xException("Error opening device");
}

并且使用这两种方法获取大小都会返回 0:

struct stat st;

if (stat(Path, &st) == 0)
    _Length = st.st_size;

/

_Length = (INT64)lseek(Device, 0, SEEK_END);
        lseek(Device, 0, SEEK_SET);

我不是完全熟悉非 Windows 平台上的编程,所以请原谅任何看起来奇怪的地方。我的问题是:

  1. 这是在 OS X 下打开原始磁盘的正确方法吗?
  2. 什么可能导致磁盘大小返回为 0?

有问题的磁盘是未格式化的磁盘,但对于那些想要从磁盘实用程序获取信息的人(删除了不重要的内容):

Name :  ST920217 AS Media
Type :  Disk

Partition Map Scheme :  Unformatted
Disk Identifier      :  disk1
Media Name           :  ST920217 AS Media
Media Type           :  Generic
Writable             :  Yes
Total Capacity       :  20 GB (20,003,880,960 Bytes)
Disk Number          :  1
Partition Number     :  0

Using the following code, I'm able to successfully open a raw disk on my machine, but when I get the disk length I get 0 each time...

// Where "Path" is /dev/rdisk1 -- is rdisk1 versus disk1 the proper way to open a raw disk?
Device = open(Path, O_RDWR);
if (Device == -1)
{
    throw xException("Error opening device");
}

And getting size with both of these methods returns 0:

struct stat st;

if (stat(Path, &st) == 0)
    _Length = st.st_size;

/

_Length = (INT64)lseek(Device, 0, SEEK_END);
        lseek(Device, 0, SEEK_SET);

I'm not totally familiar with programming on non-Windows platforms, so please forgive anything that seems odd. My questions here are:

  1. Is this the proper way to open a raw disk under OS X?
  2. What might be causing the disk size to be returned as 0?

The disk in question is an unformatted disk, but for those wanting the info from Disk Utility (with non-important stuff removed):

Name :  ST920217 AS Media
Type :  Disk

Partition Map Scheme :  Unformatted
Disk Identifier      :  disk1
Media Name           :  ST920217 AS Media
Media Type           :  Generic
Writable             :  Yes
Total Capacity       :  20 GB (20,003,880,960 Bytes)
Disk Number          :  1
Partition Number     :  0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等风来 2025-01-08 10:24:55

在对 ioctl 请求代码进行了一些搜索之后,我发现了一些确实有效的东西。

#include <sys/disk.h>
#include <sys/ioctl.h>
#include <fcntl.h>

int main()
{
    // Open disk
    uint32_t dev = open("/dev/disk1", O_RDONLY);

    if (dev == -1) {
        perror("Failed to open disk");
        return -1;
    }

    uint64_t sector_count = 0;
    // Query the number of sectors on the disk
    ioctl(dev, DKIOCGETBLOCKCOUNT, §or_count);

    uint32_t sector_size = 0;
    // Query the size of each sector
    ioctl(dev, DKIOCGETBLOCKSIZE, §or_size);

    uint64_t disk_size = sector_count * sector_size;
    printf("%ld", disk_size);
    return 0;
}

像这样的东西应该可以解决问题。我只是将我的代码复制到其中,所以我不确定它是否可以正常编译,但应该可以。

After a little bit of searching through ioctl request codes, I found something that actually works.

#include <sys/disk.h>
#include <sys/ioctl.h>
#include <fcntl.h>

int main()
{
    // Open disk
    uint32_t dev = open("/dev/disk1", O_RDONLY);

    if (dev == -1) {
        perror("Failed to open disk");
        return -1;
    }

    uint64_t sector_count = 0;
    // Query the number of sectors on the disk
    ioctl(dev, DKIOCGETBLOCKCOUNT, §or_count);

    uint32_t sector_size = 0;
    // Query the size of each sector
    ioctl(dev, DKIOCGETBLOCKSIZE, §or_size);

    uint64_t disk_size = sector_count * sector_size;
    printf("%ld", disk_size);
    return 0;
}

Something like that should do the trick. I just copied the code I had into that, so I'm not sure if it would compile alright but it should.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文