OSDN Git Service

Added exfat_open() function that opens the special file and ensures that this is...
authorresver <resver@60bc1c72-a15a-11de-b98f-4500b42dc123>
Fri, 22 Apr 2011 19:24:08 +0000 (19:24 +0000)
committerresver <resver@60bc1c72-a15a-11de-b98f-4500b42dc123>
Fri, 22 Apr 2011 19:24:08 +0000 (19:24 +0000)
git-svn-id: http://exfat.googlecode.com/svn/trunk@221 60bc1c72-a15a-11de-b98f-4500b42dc123

libexfat/exfat.h
libexfat/io.c

index bcdfb93..84673dc 100644 (file)
@@ -119,6 +119,7 @@ void exfat_warn(const char* format, ...)
 void exfat_debug(const char* format, ...)
        __attribute__((format(printf, 1, 2)));
 
+int exfat_open(const char* spec, int ro);
 void exfat_read_raw(void* buffer, size_t size, off_t offset, int fd);
 void exfat_write_raw(const void* buffer, size_t size, off_t offset, int fd);
 ssize_t exfat_read(const struct exfat* ef, struct exfat_node* node,
index 65d8d74..3d63fa9 100644 (file)
@@ -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>
 
        #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)