disk: Open whole block device with O_NONBLOCK

This would simplify handling of ENOMEDIUM error.
This commit is contained in:
Pali Rohár committed 2018-02-08 19:48:34 +01:00
1 parent e685787e67
commit a6c20b5ee4
1 file changed
+4 -6
+4 -6
View File
@@ -118,10 +118,9 @@ int disk_open_dev(int maj, int min, int partition, int readonly) {
blkdev[len] = 0; blkdev[len] = 0;
fd = open(blkdev, (readonly ? O_RDONLY : O_RDWR) | O_EXCL); fd = open(blkdev, (readonly ? O_RDONLY : O_RDWR) | O_EXCL | O_NONBLOCK);
if ( fd < 0 ) { if ( fd < 0 ) {
if ( errno != ENOMEDIUM ) ERROR_INFO("Cannot open block device %s", blkdev);
ERROR_INFO("Cannot open block device %s", blkdev);
return -1; return -1;
} else { } else {
if ( fstat(fd, &st) != 0 || ! S_ISBLK(st.st_mode) || makedev(maj, min) != st.st_rdev ) { if ( fstat(fd, &st) != 0 || ! S_ISBLK(st.st_mode) || makedev(maj, min) != st.st_rdev ) {
@@ -155,10 +154,9 @@ int disk_open_dev(int maj, int min, int partition, int readonly) {
} }
if ( fd < 0 && errno == ENOENT ) { if ( fd < 0 && errno == ENOENT ) {
blkdev[len] = 0; blkdev[len] = 0;
fd = open(blkdev, O_RDONLY); fd = open(blkdev, O_RDONLY | O_NONBLOCK);
if ( fd < 0 ) { if ( fd < 0 ) {
if ( errno != ENOMEDIUM ) ERROR_INFO("Cannot open block device %s", blkdev);
ERROR_INFO("Cannot open block device %s", blkdev);
} else { } else {
close(fd); close(fd);
ERROR("Block device %s does not have partitions", blkdev); ERROR("Block device %s does not have partitions", blkdev);