3 exFAT file system implementation library.
5 Free exFAT implementation.
6 Copyright (C) 2010-2013 Andrew Nayenko
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <sys/types.h>
27 #include <sys/mount.h>
44 off_t size; /* in bytes */
47 ublio_filehandle_t ufh;
51 static int open_ro(const char* spec)
53 return open(spec, O_RDONLY);
56 static int open_rw(const char* spec)
58 int fd = open(spec, O_RDWR);
63 This ioctl is needed because after "blockdev --setro" kernel still
64 allows to open the device in read-write mode but fails writes.
66 if (fd != -1 && ioctl(fd, BLKROGET, &ro) == 0 && ro)
75 struct exfat_dev* exfat_open(const char* spec, enum exfat_mode mode)
77 struct exfat_dev* dev;
80 struct ublio_param up;
83 dev = malloc(sizeof(struct exfat_dev));
86 exfat_error("failed to allocate memory for device structure");
93 dev->fd = open_ro(spec);
97 exfat_error("failed to open `%s' in read-only mode", spec);
100 dev->mode = EXFAT_MODE_RO;
103 dev->fd = open_rw(spec);
107 exfat_error("failed to open `%s' in read-write mode", spec);
110 dev->mode = EXFAT_MODE_RW;
113 dev->fd = open_rw(spec);
116 dev->mode = EXFAT_MODE_RW;
119 dev->fd = open_ro(spec);
122 dev->mode = EXFAT_MODE_RO;
123 exfat_warn("`%s' is write-protected, mounting read-only", spec);
127 exfat_error("failed to open `%s'", spec);
131 if (fstat(dev->fd, &stbuf) != 0)
135 exfat_error("failed to fstat `%s'", spec);
138 if (!S_ISBLK(stbuf.st_mode) &&
139 !S_ISCHR(stbuf.st_mode) &&
140 !S_ISREG(stbuf.st_mode))
144 exfat_error("`%s' is neither a device, nor a regular file", spec);
149 if (!S_ISREG(stbuf.st_mode))
151 uint32_t block_size = 0;
154 if (ioctl(dev->fd, DKIOCGETBLOCKSIZE, &block_size) != 0)
158 exfat_error("failed to get block size");
161 if (ioctl(dev->fd, DKIOCGETBLOCKCOUNT, &blocks) != 0)
165 exfat_error("failed to get blocks count");
168 dev->size = blocks * block_size;
173 /* works for Linux, FreeBSD, Solaris */
174 dev->size = exfat_seek(dev, 0, SEEK_END);
179 exfat_error("failed to get size of `%s'", spec);
182 if (exfat_seek(dev, 0, SEEK_SET) == -1)
186 exfat_error("failed to seek to the beginning of `%s'", spec);
192 memset(&up, 0, sizeof(struct ublio_param));
193 up.up_blocksize = 256 * 1024;
196 up.up_priv = &dev->fd;
199 dev->ufh = ublio_open(&up);
200 if (dev->ufh == NULL)
204 exfat_error("failed to initialize ublio");
212 int exfat_close(struct exfat_dev* dev)
217 if (ublio_close(dev->ufh) != 0)
219 exfat_error("failed to close ublio");
223 if (close(dev->fd) != 0)
225 exfat_error("failed to close device");
232 int exfat_fsync(struct exfat_dev* dev)
237 if (ublio_fsync(dev->ufh) != 0)
239 exfat_error("ublio fsync failed");
243 if (fsync(dev->fd) != 0)
245 exfat_error("fsync failed");
251 enum exfat_mode exfat_get_mode(const struct exfat_dev* dev)
256 off_t exfat_get_size(const struct exfat_dev* dev)
261 off_t exfat_seek(struct exfat_dev* dev, off_t offset, int whence)
264 /* XXX SEEK_CUR will be handled incorrectly */
265 return dev->pos = lseek(dev->fd, offset, whence);
267 return lseek(dev->fd, offset, whence);
271 ssize_t exfat_read(struct exfat_dev* dev, void* buffer, size_t size)
274 ssize_t result = ublio_pread(dev->ufh, buffer, size, dev->pos);
279 return read(dev->fd, buffer, size);
283 ssize_t exfat_write(struct exfat_dev* dev, const void* buffer, size_t size)
286 ssize_t result = ublio_pwrite(dev->ufh, buffer, size, dev->pos);
291 return write(dev->fd, buffer, size);
295 ssize_t exfat_pread(struct exfat_dev* dev, void* buffer, size_t size,
299 return ublio_pread(dev->ufh, buffer, size, offset);
301 return pread(dev->fd, buffer, size, offset);
305 ssize_t exfat_pwrite(struct exfat_dev* dev, const void* buffer, size_t size,
309 return ublio_pwrite(dev->ufh, buffer, size, offset);
311 return pwrite(dev->fd, buffer, size, offset);
315 ssize_t exfat_generic_pread(const struct exfat* ef, struct exfat_node* node,
316 void* buffer, size_t size, off_t offset)
320 off_t lsize, loffset, remainder;
322 if (offset >= node->size)
327 cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
328 if (CLUSTER_INVALID(cluster))
330 exfat_error("invalid cluster 0x%x while reading", cluster);
334 loffset = offset % CLUSTER_SIZE(*ef->sb);
335 remainder = MIN(size, node->size - offset);
336 while (remainder > 0)
338 if (CLUSTER_INVALID(cluster))
340 exfat_error("invalid cluster 0x%x while reading", cluster);
343 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
344 if (exfat_pread(ef->dev, bufp, lsize,
345 exfat_c2o(ef, cluster) + loffset) < 0)
347 exfat_error("failed to read cluster %#x", cluster);
353 cluster = exfat_next_cluster(ef, node, cluster);
355 if (!ef->ro && !ef->noatime)
356 exfat_update_atime(node);
357 return MIN(size, node->size - offset) - remainder;
360 ssize_t exfat_generic_pwrite(struct exfat* ef, struct exfat_node* node,
361 const void* buffer, size_t size, off_t offset)
364 const char* bufp = buffer;
365 off_t lsize, loffset, remainder;
367 if (offset > node->size)
368 if (exfat_truncate(ef, node, offset, true) != 0)
370 if (offset + size > node->size)
371 if (exfat_truncate(ef, node, offset + size, false) != 0)
376 cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
377 if (CLUSTER_INVALID(cluster))
379 exfat_error("invalid cluster 0x%x while writing", cluster);
383 loffset = offset % CLUSTER_SIZE(*ef->sb);
385 while (remainder > 0)
387 if (CLUSTER_INVALID(cluster))
389 exfat_error("invalid cluster 0x%x while writing", cluster);
392 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
393 if (exfat_pwrite(ef->dev, bufp, lsize,
394 exfat_c2o(ef, cluster) + loffset) < 0)
396 exfat_error("failed to write cluster %#x", cluster);
402 cluster = exfat_next_cluster(ef, node, cluster);
404 exfat_update_mtime(node);
405 return size - remainder;