3 exFAT file system implementation library.
5 Free exFAT implementation.
6 Copyright (C) 2010-2015 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>
31 #if defined(__APPLE__)
33 #elif defined(__OpenBSD__)
34 #include <sys/param.h>
35 #include <sys/disklabel.h>
37 #include <sys/ioctl.h>
39 #include <sys/mount.h>
49 off_t size; /* in bytes */
52 ublio_filehandle_t ufh;
56 static int open_ro(const char* spec)
58 return open(spec, O_RDONLY);
61 static int open_rw(const char* spec)
63 int fd = open(spec, O_RDWR);
68 This ioctl is needed because after "blockdev --setro" kernel still
69 allows to open the device in read-write mode but fails writes.
71 if (fd != -1 && ioctl(fd, BLKROGET, &ro) == 0 && ro)
81 struct exfat_dev* exfat_open(const char* spec, enum exfat_mode mode)
83 struct exfat_dev* dev;
86 struct ublio_param up;
89 dev = malloc(sizeof(struct exfat_dev));
92 exfat_error("failed to allocate memory for device structure");
99 dev->fd = open_ro(spec);
103 exfat_error("failed to open '%s' in read-only mode: %s", spec,
107 dev->mode = EXFAT_MODE_RO;
110 dev->fd = open_rw(spec);
114 exfat_error("failed to open '%s' in read-write mode: %s", spec,
118 dev->mode = EXFAT_MODE_RW;
121 dev->fd = open_rw(spec);
124 dev->mode = EXFAT_MODE_RW;
127 dev->fd = open_ro(spec);
130 dev->mode = EXFAT_MODE_RO;
131 exfat_warn("'%s' is write-protected, mounting read-only", spec);
135 exfat_error("failed to open '%s': %s", spec, strerror(errno));
139 if (fstat(dev->fd, &stbuf) != 0)
143 exfat_error("failed to fstat '%s'", spec);
146 if (!S_ISBLK(stbuf.st_mode) &&
147 !S_ISCHR(stbuf.st_mode) &&
148 !S_ISREG(stbuf.st_mode))
152 exfat_error("'%s' is neither a device, nor a regular file", spec);
156 #if defined(__APPLE__)
157 if (!S_ISREG(stbuf.st_mode))
159 uint32_t block_size = 0;
162 if (ioctl(dev->fd, DKIOCGETBLOCKSIZE, &block_size) != 0)
166 exfat_error("failed to get block size");
169 if (ioctl(dev->fd, DKIOCGETBLOCKCOUNT, &blocks) != 0)
173 exfat_error("failed to get blocks count");
176 dev->size = blocks * block_size;
179 #elif defined(__OpenBSD__)
180 if (!S_ISREG(stbuf.st_mode))
182 struct disklabel lab;
183 struct partition* pp;
186 if (ioctl(dev->fd, DIOCGDINFO, &lab) == -1)
190 exfat_error("failed to get disklabel");
194 /* Don't need to check that partition letter is valid as we won't get
195 this far otherwise. */
196 partition = strchr(spec, '\0') - 1;
197 pp = &(lab.d_partitions[*partition - 'a']);
198 dev->size = DL_GETPSIZE(pp) * lab.d_secsize;
200 if (pp->p_fstype != FS_NTFS)
201 exfat_warn("partition type is not 0x07 (NTFS/exFAT); "
202 "you can fix this with fdisk(8)");
207 /* works for Linux, FreeBSD, Solaris */
208 dev->size = exfat_seek(dev, 0, SEEK_END);
213 exfat_error("failed to get size of '%s'", spec);
216 if (exfat_seek(dev, 0, SEEK_SET) == -1)
220 exfat_error("failed to seek to the beginning of '%s'", spec);
226 memset(&up, 0, sizeof(struct ublio_param));
227 up.up_blocksize = 256 * 1024;
230 up.up_priv = &dev->fd;
233 dev->ufh = ublio_open(&up);
234 if (dev->ufh == NULL)
238 exfat_error("failed to initialize ublio");
246 int exfat_close(struct exfat_dev* dev)
251 if (ublio_close(dev->ufh) != 0)
253 exfat_error("failed to close ublio");
257 if (close(dev->fd) != 0)
259 exfat_error("failed to close device: %s", strerror(errno));
266 int exfat_fsync(struct exfat_dev* dev)
271 if (ublio_fsync(dev->ufh) != 0)
273 exfat_error("ublio fsync failed");
277 if (fsync(dev->fd) != 0)
279 exfat_error("fsync failed: %s", strerror(errno));
285 enum exfat_mode exfat_get_mode(const struct exfat_dev* dev)
290 off_t exfat_get_size(const struct exfat_dev* dev)
295 off_t exfat_seek(struct exfat_dev* dev, off_t offset, int whence)
298 /* XXX SEEK_CUR will be handled incorrectly */
299 return dev->pos = lseek(dev->fd, offset, whence);
301 return lseek(dev->fd, offset, whence);
305 ssize_t exfat_read(struct exfat_dev* dev, void* buffer, size_t size)
308 ssize_t result = ublio_pread(dev->ufh, buffer, size, dev->pos);
313 return read(dev->fd, buffer, size);
317 ssize_t exfat_write(struct exfat_dev* dev, const void* buffer, size_t size)
320 ssize_t result = ublio_pwrite(dev->ufh, buffer, size, dev->pos);
325 return write(dev->fd, buffer, size);
329 ssize_t exfat_pread(struct exfat_dev* dev, void* buffer, size_t size,
333 return ublio_pread(dev->ufh, buffer, size, offset);
335 return pread(dev->fd, buffer, size, offset);
339 ssize_t exfat_pwrite(struct exfat_dev* dev, const void* buffer, size_t size,
343 return ublio_pwrite(dev->ufh, buffer, size, offset);
345 return pwrite(dev->fd, buffer, size, offset);
349 ssize_t exfat_generic_pread(const struct exfat* ef, struct exfat_node* node,
350 void* buffer, size_t size, off_t offset)
354 off_t lsize, loffset, remainder;
356 if (offset >= node->size)
361 cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
362 if (CLUSTER_INVALID(cluster))
364 exfat_error("invalid cluster 0x%x while reading", cluster);
368 loffset = offset % CLUSTER_SIZE(*ef->sb);
369 remainder = MIN(size, node->size - offset);
370 while (remainder > 0)
372 if (CLUSTER_INVALID(cluster))
374 exfat_error("invalid cluster 0x%x while reading", cluster);
377 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
378 if (exfat_pread(ef->dev, bufp, lsize,
379 exfat_c2o(ef, cluster) + loffset) < 0)
381 exfat_error("failed to read cluster %#x", cluster);
387 cluster = exfat_next_cluster(ef, node, cluster);
389 if (!ef->ro && !ef->noatime)
390 exfat_update_atime(node);
391 return MIN(size, node->size - offset) - remainder;
394 ssize_t exfat_generic_pwrite(struct exfat* ef, struct exfat_node* node,
395 const void* buffer, size_t size, off_t offset)
398 const char* bufp = buffer;
399 off_t lsize, loffset, remainder;
401 if (offset > node->size)
402 if (exfat_truncate(ef, node, offset, true) != 0)
404 if (offset + size > node->size)
405 if (exfat_truncate(ef, node, offset + size, false) != 0)
410 cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
411 if (CLUSTER_INVALID(cluster))
413 exfat_error("invalid cluster 0x%x while writing", cluster);
417 loffset = offset % CLUSTER_SIZE(*ef->sb);
419 while (remainder > 0)
421 if (CLUSTER_INVALID(cluster))
423 exfat_error("invalid cluster 0x%x while writing", cluster);
426 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
427 if (exfat_pwrite(ef->dev, bufp, lsize,
428 exfat_c2o(ef, cluster) + loffset) < 0)
430 exfat_error("failed to write cluster %#x", cluster);
436 cluster = exfat_next_cluster(ef, node, cluster);
438 exfat_update_mtime(node);
439 return size - remainder;