* grub-core/kern/emu/hostdisk.c (grub_util_biosdisk_open): Use

grub_util_fd_strerror when using grub_util_fd_*.
	(grub_util_fd_open_device): Likewise.
	(grub_util_biosdisk_read): Likewise.
	(grub_util_biosdisk_write): Likewise.
	* grub-core/kern/emu/hostdisk_unix.c (grub_util_fd_open): New function.
	(grub_util_fd_strerror): Likewise.
	(grub_util_fd_sync): Likewise.
	(grub_util_fd_close): Likewise.
	* grub-core/kern/emu/hostdisk_windows.c (grub_util_fd_sync): Likewise.
	(grub_util_fd_close): Likewise.
	(grub_util_fd_strerror): Likewise.
	* include/grub/emu/hostdisk.h (grub_util_fd_close): Make into real
	function proto rather than macro.
	(grub_util_fd_sync): Likewise.
	(grub_util_fd_open): Likewise.
	(grub_util_fd_strerror): New proto.
This commit is contained in:
Vladimir 'phcoder' Serbinenko 2013-09-24 19:08:34 +02:00
parent e8fd80bc3d
commit 43b1c99d53
5 changed files with 92 additions and 11 deletions

View file

@ -1,3 +1,23 @@
2013-09-24 Vladimir Serbinenko <phcoder@gmail.com>
* grub-core/kern/emu/hostdisk.c (grub_util_biosdisk_open): Use
grub_util_fd_strerror when using grub_util_fd_*.
(grub_util_fd_open_device): Likewise.
(grub_util_biosdisk_read): Likewise.
(grub_util_biosdisk_write): Likewise.
* grub-core/kern/emu/hostdisk_unix.c (grub_util_fd_open): New function.
(grub_util_fd_strerror): Likewise.
(grub_util_fd_sync): Likewise.
(grub_util_fd_close): Likewise.
* grub-core/kern/emu/hostdisk_windows.c (grub_util_fd_sync): Likewise.
(grub_util_fd_close): Likewise.
(grub_util_fd_strerror): Likewise.
* include/grub/emu/hostdisk.h (grub_util_fd_close): Make into real
function proto rather than macro.
(grub_util_fd_sync): Likewise.
(grub_util_fd_open): Likewise.
(grub_util_fd_strerror): New proto.
2013-09-24 Vladimir Serbinenko <phcoder@gmail.com>
* util/getroot.c (grub_util_biosdisk_is_present): Don't do stat on

View file

@ -155,7 +155,7 @@ grub_util_biosdisk_open (const char *name, grub_disk_t disk)
if (!GRUB_UTIL_FD_IS_VALID(fd))
return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("cannot open `%s': %s"),
map[drive].device, strerror (errno));
map[drive].device, grub_util_fd_strerror ());
disk->total_sectors = grub_util_get_fd_size (fd, map[drive].device,
&disk->log_sector_size);
@ -308,7 +308,7 @@ grub_util_fd_open_device (const grub_disk_t disk, grub_disk_addr_t sector, int f
if (!GRUB_UTIL_FD_IS_VALID(data->fd))
{
grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot open `%s': %s"),
map[disk->id].device, strerror (errno));
map[disk->id].device, grub_util_fd_strerror ());
return GRUB_UTIL_FD_INVALID;
}
@ -353,7 +353,7 @@ grub_util_biosdisk_read (grub_disk_t disk, grub_disk_addr_t sector,
if (grub_util_fd_read (fd, buf, max << disk->log_sector_size)
!= (ssize_t) (max << disk->log_sector_size))
return grub_error (GRUB_ERR_READ_ERROR, N_("cannot read `%s': %s"),
map[disk->id].device, strerror (errno));
map[disk->id].device, grub_util_fd_strerror ());
size -= max;
buf += (max << disk->log_sector_size);
sector += max;
@ -388,7 +388,7 @@ grub_util_biosdisk_write (grub_disk_t disk, grub_disk_addr_t sector,
if (grub_util_fd_write (fd, buf, max << disk->log_sector_size)
!= (ssize_t) (max << disk->log_sector_size))
return grub_error (GRUB_ERR_WRITE_ERROR, N_("cannot write to `%s': %s"),
map[disk->id].device, strerror (errno));
map[disk->id].device, grub_util_fd_strerror ());
size -= max;
buf += (max << disk->log_sector_size);
}

View file

@ -166,4 +166,28 @@ grub_util_fd_write (grub_util_fd_t fd, const char *buf, size_t len)
return size;
}
grub_util_fd_t
grub_util_fd_open (const char *os_dev, int flags)
{
return open (os_dev, flags);
}
const char *
grub_util_fd_strerror (void)
{
return strerror (errno);
}
void
grub_util_fd_sync (grub_util_fd_t fd)
{
fsync (fd);
}
void
grub_util_fd_close (grub_util_fd_t fd)
{
close (fd);
}
#endif

View file

@ -204,3 +204,38 @@ grub_util_fd_write (grub_util_fd_t fd, const char *buf, size_t len)
grub_util_info ("successful write");
return real_read;
}
void
grub_util_fd_sync (grub_util_fd_t fd)
{
FlushFileBuffers (fd);
}
void
grub_util_fd_close (grub_util_fd_t fd)
{
CloseHandle (fd);
}
const char *
grub_util_fd_strerror (void)
{
DWORD err;
static TCHAR tbuf[1024];
err = GetLastError ();
FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, err,
MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
tbuf, ARRAY_SIZE (tbuf), NULL);
#if SIZEOF_TCHAR == 1
return (char *) tbuf;
#elif SIZEOF_TCHAR == 2
static grub_uint8_t buf[ARRAY_SIZE (tbuf) * GRUB_MAX_UTF8_PER_UTF16 + 1];
*grub_utf16_to_utf8 (buf, tbuf, ARRAY_SIZE (tbuf)) = '\0';
return (char *) buf;
#else
#error "Unsupported TCHAR size"
#endif
}

View file

@ -29,19 +29,21 @@
typedef HANDLE grub_util_fd_t;
#define GRUB_UTIL_FD_INVALID INVALID_HANDLE_VALUE
#define GRUB_UTIL_FD_IS_VALID(x) ((x) != GRUB_UTIL_FD_INVALID)
#define grub_util_fd_close(x) CloseHandle(x)
#define grub_util_fd_sync(x) FlushFileBuffers(x)
grub_util_fd_t
grub_util_fd_open (const char *os_dev, int flags);
#else
typedef int grub_util_fd_t;
#define GRUB_UTIL_FD_INVALID -1
#define GRUB_UTIL_FD_IS_VALID(x) ((x) >= 0)
#define grub_util_fd_close(x) close(x)
#define grub_util_fd_sync(x) fsync(x)
#define grub_util_fd_open(x,y) open(x,y)
#endif
grub_util_fd_t
grub_util_fd_open (const char *os_dev, int flags);
const char *
grub_util_fd_strerror (void);
void
grub_util_fd_sync (grub_util_fd_t fd);
void
grub_util_fd_close (grub_util_fd_t fd);
grub_util_fd_t
grub_util_fd_open_device (const grub_disk_t disk, grub_disk_addr_t sector, int flags,
grub_disk_addr_t *max);