diff --git a/ChangeLog b/ChangeLog index bd44964ca..a89fb3494 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2013-10-14 Vladimir Serbinenko + + Define GRUB_UTIL_FD_O_* and always use them with grub_util_fd_open. + 2013-10-14 Vladimir Serbinenko * include/grub/osdep/hostfile_windows.h (grub_util_utf8_to_tchar): Add diff --git a/grub-core/disk/cryptodisk.c b/grub-core/disk/cryptodisk.c index dc714f641..3ca24f979 100644 --- a/grub-core/disk/cryptodisk.c +++ b/grub-core/disk/cryptodisk.c @@ -505,7 +505,7 @@ grub_cryptodisk_open (const char *name, grub_disk_t disk) if (dev->cheat) { if (!GRUB_UTIL_FD_IS_VALID (dev->cheat_fd)) - dev->cheat_fd = grub_util_fd_open (dev->cheat, O_RDONLY); + dev->cheat_fd = grub_util_fd_open (dev->cheat, GRUB_UTIL_FD_O_RDONLY); if (!GRUB_UTIL_FD_IS_VALID (dev->cheat_fd)) return grub_error (GRUB_ERR_IO, N_("cannot open `%s': %s"), dev->cheat, strerror (errno)); diff --git a/grub-core/disk/geli.c b/grub-core/disk/geli.c index ce6bb3911..6cf9a1eec 100644 --- a/grub-core/disk/geli.c +++ b/grub-core/disk/geli.c @@ -210,7 +210,7 @@ make_uuid (const struct grub_geli_phdr *header, char * grub_util_get_geli_uuid (const char *dev) { - grub_util_fd_t fd = grub_util_fd_open (dev, O_RDONLY); + grub_util_fd_t fd = grub_util_fd_open (dev, GRUB_UTIL_FD_O_RDONLY); grub_uint64_t s; unsigned log_secsize; grub_uint8_t hdr[512]; diff --git a/grub-core/kern/emu/hostdisk.c b/grub-core/kern/emu/hostdisk.c index 6097803cd..2b7f37e86 100644 --- a/grub-core/kern/emu/hostdisk.c +++ b/grub-core/kern/emu/hostdisk.c @@ -151,7 +151,7 @@ grub_util_biosdisk_open (const char *name, grub_disk_t disk) { grub_util_fd_t fd; - fd = grub_util_fd_open (map[drive].device, O_RDONLY); + fd = grub_util_fd_open (map[drive].device, GRUB_UTIL_FD_O_RDONLY); if (!GRUB_UTIL_FD_IS_VALID(fd)) return grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("cannot open `%s': %s"), @@ -232,12 +232,7 @@ grub_util_fd_open_device (const grub_disk_t disk, grub_disk_addr_t sector, int f *max = ~0ULL; -#ifdef O_SYNC - flags |= O_SYNC; -#endif -#ifdef O_FSYNC - flags |= O_FSYNC; -#endif + flags |= GRUB_UTIL_FD_O_SYNC; if (data->dev && strcmp (data->dev, map[disk->id].device) == 0 && data->access_mode == (flags & O_ACCMODE)) @@ -293,7 +288,7 @@ grub_util_biosdisk_read (grub_disk_t disk, grub_disk_addr_t sector, { grub_util_fd_t fd; grub_disk_addr_t max = ~0ULL; - fd = grub_util_fd_open_device (disk, sector, O_RDONLY, &max); + fd = grub_util_fd_open_device (disk, sector, GRUB_UTIL_FD_O_RDONLY, &max); if (!GRUB_UTIL_FD_IS_VALID (fd)) return grub_errno; @@ -328,7 +323,7 @@ grub_util_biosdisk_write (grub_disk_t disk, grub_disk_addr_t sector, { grub_util_fd_t fd; grub_disk_addr_t max = ~0ULL; - fd = grub_util_fd_open_device (disk, sector, O_WRONLY, &max); + fd = grub_util_fd_open_device (disk, sector, GRUB_UTIL_FD_O_WRONLY, &max); if (!GRUB_UTIL_FD_IS_VALID (fd)) return grub_errno; @@ -364,7 +359,7 @@ grub_util_biosdisk_flush (struct grub_disk *disk) if (!GRUB_UTIL_FD_IS_VALID (data->fd)) { grub_disk_addr_t max; - data->fd = grub_util_fd_open_device (disk, 0, O_RDONLY, &max); + data->fd = grub_util_fd_open_device (disk, 0, GRUB_UTIL_FD_O_RDONLY, &max); if (!GRUB_UTIL_FD_IS_VALID (data->fd)) return grub_errno; } @@ -408,7 +403,7 @@ grub_util_check_file_presence (const char *p) { #if defined (__MINGW32__) || defined(__CYGWIN__) HANDLE h; - h = grub_util_fd_open (p, O_RDONLY); + h = grub_util_fd_open (p, GRUB_UTIL_FD_O_RDONLY); if (!GRUB_UTIL_FD_IS_VALID(h)) return 0; CloseHandle (h); diff --git a/grub-core/osdep/windows/hostdisk.c b/grub-core/osdep/windows/hostdisk.c index 8ebde1a6d..245f91ca9 100644 --- a/grub-core/osdep/windows/hostdisk.c +++ b/grub-core/osdep/windows/hostdisk.c @@ -194,19 +194,23 @@ grub_util_fd_seek (grub_util_fd_t fd, const char *name, grub_uint64_t off) grub_util_fd_t grub_util_fd_open (const char *os_dev, int flags) { - DWORD flg = 0; + DWORD flg = 0, crt; LPTSTR dev = grub_util_get_windows_path (os_dev); grub_util_fd_t ret; - if (flags & O_WRONLY) + if (flags & GRUB_UTIL_FD_O_WRONLY) flg |= GENERIC_WRITE; - if (flags & O_RDONLY) + if (flags & GRUB_UTIL_FD_O_RDONLY) flg |= GENERIC_READ; - flg = GENERIC_READ; + + if (flags & GRUB_UTIL_FD_O_CREATTRUNC) + crt = CREATE_ALWAYS; + else + crt = OPEN_EXISTING; + ret = CreateFile (dev, flg, FILE_SHARE_READ | FILE_SHARE_WRITE, - 0, OPEN_EXISTING, 0, 0); + 0, crt, 0, 0); free (dev); - grub_util_info ("handle = %p", ret); return ret; } diff --git a/include/grub/osdep/hostfile_aros.h b/include/grub/osdep/hostfile_aros.h index 1a0cd3b3a..c1ee49e3a 100644 --- a/include/grub/osdep/hostfile_aros.h +++ b/include/grub/osdep/hostfile_aros.h @@ -39,6 +39,22 @@ struct grub_util_fd }; typedef struct grub_util_fd *grub_util_fd_t; +enum grub_util_fd_open_flags_t + { + GRUB_UTIL_FD_O_RDONLY = O_RDONLY, + GRUB_UTIL_FD_O_WRONLY = O_WRONLY, + GRUB_UTIL_FD_O_RDWR = O_RDWR, + GRUB_UTIL_FD_O_CREAT = O_CREAT, + GRUB_UTIL_FD_O_SYNC = (0 +#ifdef O_SYNC + | O_SYNC +#endif +#ifdef O_FSYNC + | O_FSYNC +#endif + ) + }; + #define GRUB_UTIL_FD_INVALID NULL #define GRUB_UTIL_FD_IS_VALID(x) ((x) != GRUB_UTIL_FD_INVALID) #define GRUB_UTIL_FD_STAT_IS_FUNCTIONAL 0 diff --git a/include/grub/osdep/hostfile_unix.h b/include/grub/osdep/hostfile_unix.h index e99509fc8..cdd98f703 100644 --- a/include/grub/osdep/hostfile_unix.h +++ b/include/grub/osdep/hostfile_unix.h @@ -25,6 +25,10 @@ #include #include +#include +#include +#include + #if defined (__NetBSD__) /* NetBSD uses /boot for its boot block. */ # define DEFAULT_DIRECTORY "/"GRUB_DIR_NAME @@ -32,6 +36,22 @@ # define DEFAULT_DIRECTORY "/"GRUB_BOOT_DIR_NAME"/"GRUB_DIR_NAME #endif +enum grub_util_fd_open_flags_t + { + GRUB_UTIL_FD_O_RDONLY = O_RDONLY, + GRUB_UTIL_FD_O_WRONLY = O_WRONLY, + GRUB_UTIL_FD_O_RDWR = O_RDWR, + GRUB_UTIL_FD_O_CREATTRUNC = O_CREAT | O_TRUNC, + GRUB_UTIL_FD_O_SYNC = (0 +#ifdef O_SYNC + | O_SYNC +#endif +#ifdef O_FSYNC + | O_FSYNC +#endif + ) + }; + #define DEFAULT_DEVICE_MAP DEFAULT_DIRECTORY "/device.map" typedef int grub_util_fd_t; diff --git a/include/grub/osdep/hostfile_windows.h b/include/grub/osdep/hostfile_windows.h index 276b87a09..e04dfbeb0 100644 --- a/include/grub/osdep/hostfile_windows.h +++ b/include/grub/osdep/hostfile_windows.h @@ -31,6 +31,15 @@ typedef HANDLE grub_util_fd_t; #define DEFAULT_DIRECTORY "C:\\"GRUB_BOOT_DIR_NAME"\\"GRUB_DIR_NAME #define DEFAULT_DEVICE_MAP DEFAULT_DIRECTORY "/device.map" +enum grub_util_fd_open_flags_t + { + GRUB_UTIL_FD_O_RDONLY = 1, + GRUB_UTIL_FD_O_WRONLY = 2, + GRUB_UTIL_FD_O_RDWR = 3, + GRUB_UTIL_FD_O_CREATTRUNC = 4, + GRUB_UTIL_FD_O_SYNC = 0, + }; + LPTSTR grub_util_utf8_to_tchar (const char *in); char *