3 exFAT file system implementation library.
5 Copyright (C) 2009, 2010 Andrew Nayenko
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <sys/types.h>
27 #define __USE_UNIX98 /* for pread() in Linux */
30 #if _FILE_OFFSET_BITS != 64
31 #error You should define _FILE_OFFSET_BITS=64
34 int exfat_open(const char* spec, int ro)
39 fd = open(spec, ro ? O_RDONLY : O_RDWR);
42 exfat_error("failed to open `%s' in read-%s mode", spec,
43 ro ? "only" : "write");
46 if (fstat(fd, &stbuf) != 0)
49 exfat_error("failed to fstat `%s'", spec);
52 if (!S_ISBLK(stbuf.st_mode) && !S_ISREG(stbuf.st_mode))
55 exfat_error("`%s' is neither a block device, nor a regular file",
62 void exfat_read_raw(void* buffer, size_t size, off_t offset, int fd)
64 if (pread(fd, buffer, size, offset) != size)
65 exfat_bug("failed to read %zu bytes from file at %"PRIu64, size,
69 void exfat_write_raw(const void* buffer, size_t size, off_t offset, int fd)
71 if (pwrite(fd, buffer, size, offset) != size)
72 exfat_bug("failed to write %zu bytes to file at %"PRIu64, size,
76 ssize_t exfat_read(const struct exfat* ef, struct exfat_node* node,
77 void* buffer, size_t size, off_t offset)
81 off_t lsize, loffset, remainder;
83 if (offset >= node->size)
88 cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
89 if (CLUSTER_INVALID(cluster))
91 exfat_error("got invalid cluster");
95 loffset = offset % CLUSTER_SIZE(*ef->sb);
96 remainder = MIN(size, node->size - offset);
99 if (CLUSTER_INVALID(cluster))
101 exfat_error("got invalid cluster");
104 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
105 exfat_read_raw(bufp, lsize, exfat_c2o(ef, cluster) + loffset, ef->fd);
109 cluster = exfat_next_cluster(ef, node, cluster);
111 if (!ef->ro && !ef->noatime)
112 exfat_update_atime(node);
113 return size - remainder;
116 ssize_t exfat_write(struct exfat* ef, struct exfat_node* node,
117 const void* buffer, size_t size, off_t offset)
120 const char* bufp = buffer;
121 off_t lsize, loffset, remainder;
123 if (offset + size > node->size)
125 int rc = exfat_truncate(ef, node, offset + size);
132 cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
133 if (CLUSTER_INVALID(cluster))
135 exfat_error("got invalid cluster");
139 loffset = offset % CLUSTER_SIZE(*ef->sb);
141 while (remainder > 0)
143 if (CLUSTER_INVALID(cluster))
145 exfat_error("got invalid cluster");
148 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
149 exfat_write_raw(bufp, lsize, exfat_c2o(ef, cluster) + loffset, ef->fd);
153 cluster = exfat_next_cluster(ef, node, cluster);
155 exfat_update_mtime(node);
156 return size - remainder;