struct grub_util_biosdisk_data
{
char *dev;
+ int access_mode;
int fd;
};
disk->id = drive;
disk->data = data = xmalloc (sizeof (struct grub_util_biosdisk_data));
data->dev = NULL;
+ data->access_mode = 0;
data->fd = -1;
/* Get the size. */
&& strncmp (map[disk->id].device, "/dev/", 5) == 0)
is_partition = linux_find_partition (dev, disk->partition->start);
- if (data->dev && strcmp (data->dev, dev) == 0)
+ if (data->dev && strcmp (data->dev, dev) == 0 &&
+ data->access_mode == (flags & O_ACCMODE))
{
grub_dprintf ("hostdisk", "reusing open device `%s'\n", dev);
fd = data->fd;
}
else
{
+ free (data->dev);
+ if (data->fd != -1)
+ close (data->fd);
+
/* Open the partition. */
grub_dprintf ("hostdisk", "opening the device `%s' in open_device()\n", dev);
fd = open (dev, flags);
XXX: This also empties the buffer cache. */
ioctl (fd, BLKFLSBUF, 0);
- free (data->dev);
data->dev = xstrdup (dev);
+ data->access_mode = (flags & O_ACCMODE);
data->fd = fd;
}
}
#endif
- if (data->dev && strcmp (data->dev, map[disk->id].device) == 0)
+ if (data->dev && strcmp (data->dev, map[disk->id].device) == 0 &&
+ data->access_mode == (flags & O_ACCMODE))
{
grub_dprintf ("hostdisk", "reusing open device `%s'\n", data->dev);
fd = data->fd;
}
else
{
+ free (data->dev);
+ if (data->fd != -1)
+ close (data->fd);
+
fd = open (map[disk->id].device, flags);
if (fd >= 0)
{
- free (data->dev);
data->dev = xstrdup (map[disk->id].device);
+ data->access_mode = (flags & O_ACCMODE);
data->fd = fd;
}
}