diff options
Diffstat (limited to 'lib/blkdev.c')
-rw-r--r-- | lib/blkdev.c | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/lib/blkdev.c b/lib/blkdev.c index 5f495822e..a57b3672b 100644 --- a/lib/blkdev.c +++ b/lib/blkdev.c @@ -25,9 +25,15 @@ # include <sys/disk.h> #endif +#ifndef EBADFD +# define EBADFD 77 /* File descriptor in bad state */ +#endif + #include "blkdev.h" #include "c.h" #include "linux_version.h" +#include "fileutils.h" +#include "nls.h" static long blkdev_valid_offset (int fd, off_t offset) { @@ -254,6 +260,24 @@ int blkdev_is_misaligned(int fd) #endif } +int open_blkdev_or_file(const struct stat *st, const char *name, const int oflag) +{ + int fd; + + if (S_ISBLK(st->st_mode)) { + fd = open(name, oflag | O_EXCL); + } else + fd = open(name, oflag); + if (-1 < fd && !is_same_inode(fd, st)) { + close(fd); + errno = EBADFD; + return -1; + } + if (-1 < fd && S_ISBLK(st->st_mode) && blkdev_is_misaligned(fd)) + warnx(_("warning: %s is misaligned"), name); + return fd; +} + int blkdev_is_cdrom(int fd) { #ifdef CDROM_GET_CAPABILITY |