OSDN Git Service

Set errno to EROFS on failure to open the device in read-write mode.
[android-x86/external-exfat.git] / libexfat / io.c
index 2bdfc43..ad71036 100644 (file)
@@ -65,12 +65,20 @@ static int open_rw(const char* spec)
        int ro = 0;
 
        /*
-          This ioctl is needed because after "blockdev --setro" kernel still
+          This code is needed because after "blockdev --setro" kernel still
           allows to open the device in read-write mode but fails writes.
        */
-       if (fd != -1 && ioctl(fd, BLKROGET, &ro) == 0 && ro)
+       if (fd == -1)
+               return -1;
+       if (ioctl(fd, BLKROGET, &ro) != 0)
+       {
+               close(fd);
+               return -1;
+       }
+       if (ro)
        {
                close(fd);
+               errno = EROFS;
                return -1;
        }
 #endif