Switch to POSIX open() in useable_device() (!46)

For every device that GParted scans, it determines if it can be used by
attempting to read the first sector.  This is the sequence of events,
as reported in the previous two commit messages:

  gparted    |set_devices_thread(pdevices)  pdevices->["/dev/sdb"]
  ...
  gparted    |  useable_device(lp_device)  lp_device->path="/dev/sdb"
  gparted    |    ped_device_open()
  libparted  |      open("/dev/sdb", O_RDWR) = 8
  gparted    |    ped_device_read()
  gparted    |    ped_device_close()
  libparted  |      close(8)
  udev(async)|        KERNEL change /devices/.../sdb (block)
  ...
  udev(async)|        UDEV   change /devices/.../sdb (block)

Note that these udev block device change events occur before the ones
waited for in the first commit "Wait for udev change on /dev/DISK when
querying whole device FS (!46)" so these were already waited for.

So because libparted only opens devices read-write this triggers a set
of udev change events for the whole block device, and if the device is
partitioned, also remove and re-add events for all the partitions.

Switch to using POSIX open(), read() and close() calls so read-only
access can be specified to avoid the unnecessary udev change events.

Closes !46 - Whole device FAT32 file system reports device busy warning
             from mlabel
This commit is contained in:
Mike Fleetwood 2019-07-10 10:49:43 +01:00 committed by Curtis Gedak
parent f170a1e542
commit e5898e5813
1 changed files with 6 additions and 5 deletions

View File

@ -60,6 +60,7 @@
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <glibmm/miscutils.h>
#include <glibmm/fileutils.h>
#include <glibmm/shell.h>
@ -4051,12 +4052,12 @@ bool GParted_Core::useable_device( PedDevice * lp_device )
// Must be able to read from the first sector before the disk device is considered
// useable in GParted.
bool success = false;
if ( ped_device_open( lp_device ) &&
ped_device_read( lp_device, buf, 0, 1 ) )
int fd = open(lp_device->path, O_RDONLY|O_NONBLOCK);
if (fd >= 0)
{
success = true;
ped_device_close( lp_device );
ssize_t bytes_read = read(fd, buf, lp_device->sector_size);
success = (bytes_read == lp_device->sector_size);
close(fd);
}
free( buf );