We are trying to read disk sectors raw Data. Sample code can be found at the bottom.
For external drive, it is working fine. We are able to see raw data which is not zero. For internal ssd APFS drive, We only get data filled with zeroes.
We have tried with System Integrity Protection enabling and disabling.
Please let us know Why same API failing for internal APFS drive?
Is there any specific API available for reading raw data of internal APFS drive?
#include <fcntl.h>
#include <unistd.h>
#include <iomanip>
int main() {
// Adjust this to your disk device on macOS
const char* diskPath = "/dev/rdisk1"; //internal SSD device on macOS
// Size of a sector (usually 4096 bytes for most disks on macOS)
const int sectorSize = 4096;
// Number of sectors you want to read
const int numSectors = 8;
// Starting sector number
off_t startSector = 0;
// Open the disk device using low-level file I/O
int diskFile = open(diskPath, O_RDONLY);
if (diskFile == -1) {
std::cerr << "Error opening disk file." << std::endl;
return 1;
}
// Read multiple sectors into a buffer
char buffer[numSectors * sectorSize];
ssize_t bytesRead = pread(diskFile, buffer, numSectors * sectorSize, startSector * sectorSize);
// Close the disk file
close(diskFile);
if (bytesRead != numSectors * sectorSize) {
std::cerr << "Error reading sectors." << std::endl;
return 1;
}
// Display the contents of the sectors in hex
for (int i = 0; i < numSectors * sectorSize; ++i) {
std::cout << std::hex << std::setw(2) << std::setfill('0') << (int)(unsigned char)buffer[i] << " ";
if ((i + 1) % 16 == 0) {
std::cout << std::endl;
}
}
return 0;
}