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>
43 off_t size; /* in bytes */
46 ublio_filehandle_t ufh;
50 static int open_ro(const char* spec)
52 return open(spec, O_RDONLY);
55 static int open_rw(const char* spec)
57 int fd = open(spec, O_RDWR);
62 This ioctl is needed because after "blockdev --setro" kernel still
63 allows to open the device in read-write mode but fails writes.
65 if (fd != -1 && ioctl(fd, BLKROGET, &ro) == 0 && ro)
74 struct exfat_dev* exfat_open(const char* spec, enum exfat_mode mode)
76 struct exfat_dev* dev;
79 struct ublio_param up;
82 dev = malloc(sizeof(struct exfat_dev));
85 exfat_error("failed to allocate memory for device structure");
92 dev->fd = open_ro(spec);
96 exfat_error("failed to open `%s' in read-only mode", spec);
99 dev->mode = EXFAT_MODE_RO;
102 dev->fd = open_rw(spec);
106 exfat_error("failed to open `%s' in read-write mode", spec);
109 dev->mode = EXFAT_MODE_RW;
112 dev->fd = open_rw(spec);
115 dev->mode = EXFAT_MODE_RW;
118 dev->fd = open_ro(spec);
121 dev->mode = EXFAT_MODE_RO;
122 exfat_warn("`%s' is write-protected, mounting read-only", spec);
126 exfat_error("failed to open `%s'", spec);
130 if (fstat(dev->fd, &stbuf) != 0)
134 exfat_error("failed to fstat `%s'", spec);
137 if (!S_ISBLK(stbuf.st_mode) &&
138 !S_ISCHR(stbuf.st_mode) &&
139 !S_ISREG(stbuf.st_mode))
143 exfat_error("`%s' is neither a device, nor a regular file", spec);
148 if (!S_ISREG(stbuf.st_mode))
150 uint32_t block_size = 0;
153 if (ioctl(dev->fd, DKIOCGETBLOCKSIZE, &block_size) != 0)
157 exfat_error("failed to get block size");
160 if (ioctl(dev->fd, DKIOCGETBLOCKCOUNT, &blocks) != 0)
164 exfat_error("failed to get blocks count");
167 dev->size = blocks * block_size;
172 /* works for Linux, FreeBSD, Solaris */
173 dev->size = exfat_seek(dev, 0, SEEK_END);
178 exfat_error("failed to get size of `%s'", spec);
181 if (exfat_seek(dev, 0, SEEK_SET) == -1)
185 exfat_error("failed to seek to the beginning of `%s'", spec);
191 memset(&up, 0, sizeof(struct ublio_param));
192 up.up_blocksize = 256 * 1024;
195 up.up_priv = &dev->fd;
198 dev->ufh = ublio_open(&up);
199 if (dev->ufh == NULL)
203 exfat_error("failed to initialize ublio");
211 int exfat_close(struct exfat_dev* dev)
214 if (ublio_close(dev->ufh) != 0)
215 exfat_error("failed to close ublio");
217 if (close(dev->fd) != 0)
220 exfat_error("failed to close device");
227 int exfat_fsync(struct exfat_dev* dev)
230 if (ublio_fsync(dev->ufh) != 0)
232 if (fsync(dev->fd) != 0)
235 exfat_error("fsync failed");
241 enum exfat_mode exfat_get_mode(const struct exfat_dev* dev)
246 off_t exfat_get_size(const struct exfat_dev* dev)
251 off_t exfat_seek(struct exfat_dev* dev, off_t offset, int whence)
254 /* XXX SEEK_CUR will be handled incorrectly */
255 return dev->pos = lseek(dev->fd, offset, whence);
257 return lseek(dev->fd, offset, whence);
261 ssize_t exfat_read(struct exfat_dev* dev, void* buffer, size_t size)
264 ssize_t result = ublio_pread(dev->ufh, buffer, size, dev->pos);
269 return read(dev->fd, buffer, size);
273 ssize_t exfat_write(struct exfat_dev* dev, const void* buffer, size_t size)
276 ssize_t result = ublio_pwrite(dev->ufh, buffer, size, dev->pos);
281 return write(dev->fd, buffer, size);
285 void exfat_pread(struct exfat_dev* dev, void* buffer, size_t size,
289 if (ublio_pread(dev->ufh, buffer, size, offset) != size)
291 if (pread(dev->fd, buffer, size, offset) != size)
293 exfat_bug("failed to read %zu bytes from file at %"PRIu64, size,
297 void exfat_pwrite(struct exfat_dev* dev, const void* buffer, size_t size,
301 if (ublio_pwrite(dev->ufh, buffer, size, offset) != size)
303 if (pwrite(dev->fd, buffer, size, offset) != size)
305 exfat_bug("failed to write %zu bytes to file at %"PRIu64, size,
309 ssize_t exfat_generic_pread(const struct exfat* ef, struct exfat_node* node,
310 void* buffer, size_t size, off_t offset)
314 off_t lsize, loffset, remainder;
316 if (offset >= node->size)
321 cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
322 if (CLUSTER_INVALID(cluster))
324 exfat_error("invalid cluster 0x%x while reading", cluster);
328 loffset = offset % CLUSTER_SIZE(*ef->sb);
329 remainder = MIN(size, node->size - offset);
330 while (remainder > 0)
332 if (CLUSTER_INVALID(cluster))
334 exfat_error("invalid cluster 0x%x while reading", cluster);
337 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
338 exfat_pread(ef->dev, bufp, lsize, exfat_c2o(ef, cluster) + loffset);
342 cluster = exfat_next_cluster(ef, node, cluster);
344 if (!ef->ro && !ef->noatime)
345 exfat_update_atime(node);
346 return MIN(size, node->size - offset) - remainder;
349 ssize_t exfat_generic_pwrite(struct exfat* ef, struct exfat_node* node,
350 const void* buffer, size_t size, off_t offset)
353 const char* bufp = buffer;
354 off_t lsize, loffset, remainder;
356 if (offset > node->size)
357 if (exfat_truncate(ef, node, offset, true) != 0)
359 if (offset + size > node->size)
360 if (exfat_truncate(ef, node, offset + size, false) != 0)
365 cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
366 if (CLUSTER_INVALID(cluster))
368 exfat_error("invalid cluster 0x%x while writing", cluster);
372 loffset = offset % CLUSTER_SIZE(*ef->sb);
374 while (remainder > 0)
376 if (CLUSTER_INVALID(cluster))
378 exfat_error("invalid cluster 0x%x while writing", cluster);
381 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
382 exfat_pwrite(ef->dev, bufp, lsize, exfat_c2o(ef, cluster) + loffset);
386 cluster = exfat_next_cluster(ef, node, cluster);
388 exfat_update_mtime(node);
389 return size - remainder;