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/>.
21 #define _XOPEN_SOURCE 500 /* for pread() and pwrite() in Linux */
24 #include <sys/types.h>
35 #if _FILE_OFFSET_BITS != 64
36 #error You should define _FILE_OFFSET_BITS=64
44 ublio_filehandle_t ufh;
48 struct exfat_dev* exfat_open(const char* spec, int ro)
50 struct exfat_dev* dev;
53 struct ublio_param up;
56 dev = malloc(sizeof(struct exfat_dev));
59 exfat_error("failed to allocate memory for device structure");
63 dev->fd = open(spec, ro ? O_RDONLY : O_RDWR);
67 exfat_error("failed to open `%s' in read-%s mode", spec,
68 ro ? "only" : "write");
71 if (fstat(dev->fd, &stbuf) != 0)
75 exfat_error("failed to fstat `%s'", spec);
78 if (!S_ISBLK(stbuf.st_mode) &&
79 !S_ISCHR(stbuf.st_mode) &&
80 !S_ISREG(stbuf.st_mode))
84 exfat_error("`%s' is neither a device, nor a regular file", spec);
89 memset(&up, 0, sizeof(struct ublio_param));
90 up.up_blocksize = 256 * 1024;
93 up.up_priv = &dev->fd;
96 dev->ufh = ublio_open(&up);
101 exfat_error("failed to initialize ublio");
109 int exfat_close(struct exfat_dev* dev)
112 if (ublio_close(dev->ufh) != 0)
113 exfat_error("failed to close ublio");
115 if (close(dev->fd) != 0)
118 exfat_error("failed to close device");
125 int exfat_fsync(struct exfat_dev* dev)
128 if (ublio_fsync(dev->ufh) != 0)
130 if (fsync(dev->fd) != 0)
133 exfat_error("fsync failed");
139 off_t exfat_seek(struct exfat_dev* dev, off_t offset, int whence)
142 /* XXX SEEK_CUR will be handled incorrectly */
143 return dev->pos = lseek(dev->fd, offset, whence);
145 return lseek(dev->fd, offset, whence);
149 ssize_t exfat_read(struct exfat_dev* dev, void* buffer, size_t size)
152 ssize_t result = ublio_pread(dev->ufh, buffer, size, dev->pos);
157 return read(dev->fd, buffer, size);
161 ssize_t exfat_write(struct exfat_dev* dev, const void* buffer, size_t size)
164 ssize_t result = ublio_pwrite(dev->ufh, buffer, size, dev->pos);
169 return write(dev->fd, buffer, size);
173 void exfat_pread(struct exfat_dev* dev, void* buffer, size_t size,
177 if (ublio_pread(dev->ufh, buffer, size, offset) != size)
179 if (pread(dev->fd, buffer, size, offset) != size)
181 exfat_bug("failed to read %zu bytes from file at %"PRIu64, size,
185 void exfat_pwrite(struct exfat_dev* dev, const void* buffer, size_t size,
189 if (ublio_pwrite(dev->ufh, buffer, size, offset) != size)
191 if (pwrite(dev->fd, buffer, size, offset) != size)
193 exfat_bug("failed to write %zu bytes to file at %"PRIu64, size,
197 ssize_t exfat_generic_pread(const struct exfat* ef, struct exfat_node* node,
198 void* buffer, size_t size, off_t offset)
202 off_t lsize, loffset, remainder;
204 if (offset >= node->size)
209 cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
210 if (CLUSTER_INVALID(cluster))
212 exfat_error("got invalid cluster");
216 loffset = offset % CLUSTER_SIZE(*ef->sb);
217 remainder = MIN(size, node->size - offset);
218 while (remainder > 0)
220 if (CLUSTER_INVALID(cluster))
222 exfat_error("got invalid cluster");
225 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
226 exfat_pread(ef->dev, bufp, lsize, exfat_c2o(ef, cluster) + loffset);
230 cluster = exfat_next_cluster(ef, node, cluster);
232 if (!ef->ro && !ef->noatime)
233 exfat_update_atime(node);
234 return size - remainder;
237 ssize_t exfat_generic_pwrite(struct exfat* ef, struct exfat_node* node,
238 const void* buffer, size_t size, off_t offset)
241 const char* bufp = buffer;
242 off_t lsize, loffset, remainder;
244 if (offset + size > node->size)
246 int rc = exfat_truncate(ef, node, offset + size);
253 cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
254 if (CLUSTER_INVALID(cluster))
256 exfat_error("got invalid cluster");
260 loffset = offset % CLUSTER_SIZE(*ef->sb);
262 while (remainder > 0)
264 if (CLUSTER_INVALID(cluster))
266 exfat_error("got invalid cluster");
269 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
270 exfat_pwrite(ef->dev, bufp, lsize, exfat_c2o(ef, cluster) + loffset);
274 cluster = exfat_next_cluster(ef, node, cluster);
276 exfat_update_mtime(node);
277 return size - remainder;