diff options
author | relan <relan@users.noreply.github.com> | 2011-04-22 19:24:08 +0000 |
---|---|---|
committer | relan <relan@users.noreply.github.com> | 2015-08-24 08:26:13 +0300 |
commit | 017ad988422ec1e75808b035b6e9679d44716188 (patch) | |
tree | 0dfc27e1aaa92b1960bd79af32ca31eb3b648414 /libexfat/io.c | |
parent | 752e8c65da7b867abcc423dcdab417374ece2dd7 (diff) | |
download | android_external_exfat-017ad988422ec1e75808b035b6e9679d44716188.tar.gz android_external_exfat-017ad988422ec1e75808b035b6e9679d44716188.tar.bz2 android_external_exfat-017ad988422ec1e75808b035b6e9679d44716188.zip |
Add exfat_open() function.
It opens the special file and ensures that this is either a block device
or a regular file.
Diffstat (limited to 'libexfat/io.c')
-rw-r--r-- | libexfat/io.c | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/libexfat/io.c b/libexfat/io.c index 65d8d74..3d63fa9 100644 --- a/libexfat/io.c +++ b/libexfat/io.c @@ -22,6 +22,8 @@ #include <inttypes.h> #include <sys/types.h> #include <sys/uio.h> +#include <sys/stat.h> +#include <fcntl.h> #define __USE_UNIX98 /* for pread() in Linux */ #include <unistd.h> @@ -29,6 +31,33 @@ #error You should define _FILE_OFFSET_BITS=64 #endif +int exfat_open(const char* spec, int ro) +{ + int fd; + struct stat stbuf; + + fd = open(spec, ro ? O_RDONLY : O_RDWR); + if (fd < 0) + { + exfat_error("failed to open `%s'", spec); + return -1; + } + if (fstat(fd, &stbuf) != 0) + { + close(fd); + exfat_error("failed to fstat `%s'", spec); + return -1; + } + if (!S_ISBLK(stbuf.st_mode) && !S_ISREG(stbuf.st_mode)) + { + close(fd); + exfat_error("`%s' is neither a block device, nor a regular file", + spec); + return -1; + } + return fd; +} + void exfat_read_raw(void* buffer, size_t size, off_t offset, int fd) { if (pread(fd, buffer, size, offset) != size) |