How to obtain direct access to raw HD data in C?

微笑、不失礼 提交于 2019-12-30 07:00:21

问题


I need to do a complete format on a USB stick (FAT16 or FAT32), put a file on the drive, then delete it and recover the file using a C program.

Could anyone give me a hint on what should I use to accomplish this?

I understand the layouts of the FAT16/32 filesystems, the problem is that I don't know how to access the raw HD data using C (since I can't use things like fopen or mmap because the file isn't there anymore).


回答1:


This is highly operating system specific.

For Linux, you would open the raw device /dev/sdxx. Note that there are privilege hoops to manage.

For Windows, you would use something like:

 HANDLE h = CreateFile ("\\\\.\\PhysicalDriveX", GENERIC_READ,
                    FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
                    OPEN_EXISTING,
                    FILE_FLAG_NO_BUFFERING | FILE_FLAG_RANDOM_ACCESS,
                    NULL);

where X depends on the device.




回答2:


Just use normal filesystem operations on the disk device.

This means you need to identify the disk device first, of course. But once opened, you can even mmap() it.



来源:https://stackoverflow.com/questions/8541848/how-to-obtain-direct-access-to-raw-hd-data-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!