Opening device files on OS X

66 Views Asked by At

I'm building a file recovery tool and I've encountered a strange problem I can't figure out.

The following code does not fail, but the resulting file size reported by fstat is 0 bytes.

    int f = -1;
    f = open("/dev/disk2", O_RDONLY);

     if (f < 0) {
        printf("Unable to open file\n");
        return 1;
     }

     struct stat s = {0};

     if (fstat(f, &s) < 0) {
        printf("Unable to stat\n");
        return 2;
     }

     printf("Size: %lli\n",s.st_size);

(I pointed the code to another file to test it and it returned the size correctly.)

However, if I type the following in Terminal it displays the contents of the drive with no problems:

cat /dev/disk2

I'm running both as root to avoid any permissions issues when testing. The drive is an external HFS volume that is unmounted.

0

There are 0 best solutions below