3 exFAT file system implementation library.
5 Free exFAT implementation.
6 Copyright (C) 2010-2018 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>
38 #elif defined(__NetBSD__)
39 #include <sys/ioctl.h>
41 #include <sys/mount.h>
52 off_t size; /* in bytes */
55 ublio_filehandle_t ufh;
59 static bool is_open(int fd)
61 return fcntl(fd, F_GETFD) != -1;
64 static int open_ro(const char* spec)
66 return open(spec, O_RDONLY);
69 static int open_rw(const char* spec)
71 int fd = open(spec, O_RDWR);
76 This ioctl is needed because after "blockdev --setro" kernel still
77 allows to open the device in read-write mode but fails writes.
79 if (fd != -1 && ioctl(fd, BLKROGET, &ro) == 0 && ro)
89 struct exfat_dev* exfat_open(const char* spec, enum exfat_mode mode)
91 struct exfat_dev* dev;
94 struct ublio_param up;
97 /* The system allocates file descriptors sequentially. If we have been
98 started with stdin (0), stdout (1) or stderr (2) closed, the system
99 will give us descriptor 0, 1 or 2 later when we open block device,
100 FUSE communication pipe, etc. As a result, functions using stdin,
101 stdout or stderr will actually work with a different thing and can
102 corrupt it. Protect descriptors 0, 1 and 2 from such misuse. */
103 while (!is_open(STDIN_FILENO)
104 || !is_open(STDOUT_FILENO)
105 || !is_open(STDERR_FILENO))
107 /* we don't need those descriptors, let them leak */
108 if (open("/dev/null", O_RDWR) == -1)
110 exfat_error("failed to open /dev/null");
115 dev = malloc(sizeof(struct exfat_dev));
118 exfat_error("failed to allocate memory for device structure");
125 dev->fd = open_ro(spec);
129 exfat_error("failed to open '%s' in read-only mode: %s", spec,
133 dev->mode = EXFAT_MODE_RO;
136 dev->fd = open_rw(spec);
140 exfat_error("failed to open '%s' in read-write mode: %s", spec,
144 dev->mode = EXFAT_MODE_RW;
147 dev->fd = open_rw(spec);
150 dev->mode = EXFAT_MODE_RW;
153 dev->fd = open_ro(spec);
156 dev->mode = EXFAT_MODE_RO;
157 exfat_warn("'%s' is write-protected, mounting read-only", spec);
161 exfat_error("failed to open '%s': %s", spec, strerror(errno));
165 if (fstat(dev->fd, &stbuf) != 0)
169 exfat_error("failed to fstat '%s'", spec);
172 if (!S_ISBLK(stbuf.st_mode) &&
173 !S_ISCHR(stbuf.st_mode) &&
174 !S_ISREG(stbuf.st_mode))
178 exfat_error("'%s' is neither a device, nor a regular file", spec);
182 #if defined(__APPLE__)
183 if (!S_ISREG(stbuf.st_mode))
185 uint32_t block_size = 0;
188 if (ioctl(dev->fd, DKIOCGETBLOCKSIZE, &block_size) != 0)
192 exfat_error("failed to get block size");
195 if (ioctl(dev->fd, DKIOCGETBLOCKCOUNT, &blocks) != 0)
199 exfat_error("failed to get blocks count");
202 dev->size = blocks * block_size;
205 #elif defined(__OpenBSD__)
206 if (!S_ISREG(stbuf.st_mode))
208 struct disklabel lab;
209 struct partition* pp;
212 if (ioctl(dev->fd, DIOCGDINFO, &lab) == -1)
216 exfat_error("failed to get disklabel");
220 /* Don't need to check that partition letter is valid as we won't get
221 this far otherwise. */
222 partition = strchr(spec, '\0') - 1;
223 pp = &(lab.d_partitions[*partition - 'a']);
224 dev->size = DL_GETPSIZE(pp) * lab.d_secsize;
226 if (pp->p_fstype != FS_NTFS)
227 exfat_warn("partition type is not 0x07 (NTFS/exFAT); "
228 "you can fix this with fdisk(8)");
231 #elif defined(__NetBSD__)
232 if (!S_ISREG(stbuf.st_mode))
236 if (ioctl(dev->fd, DIOCGMEDIASIZE, &size) == -1)
240 exfat_error("failed to get media size");
248 /* works for Linux, FreeBSD, Solaris */
249 dev->size = exfat_seek(dev, 0, SEEK_END);
254 exfat_error("failed to get size of '%s'", spec);
257 if (exfat_seek(dev, 0, SEEK_SET) == -1)
261 exfat_error("failed to seek to the beginning of '%s'", spec);
267 memset(&up, 0, sizeof(struct ublio_param));
268 up.up_blocksize = 256 * 1024;
271 up.up_priv = &dev->fd;
274 dev->ufh = ublio_open(&up);
275 if (dev->ufh == NULL)
279 exfat_error("failed to initialize ublio");
287 int exfat_close(struct exfat_dev* dev)
292 if (ublio_close(dev->ufh) != 0)
294 exfat_error("failed to close ublio");
298 if (close(dev->fd) != 0)
300 exfat_error("failed to close device: %s", strerror(errno));
307 int exfat_fsync(struct exfat_dev* dev)
312 if (ublio_fsync(dev->ufh) != 0)
314 exfat_error("ublio fsync failed");
318 if (fsync(dev->fd) != 0)
320 exfat_error("fsync failed: %s", strerror(errno));
326 enum exfat_mode exfat_get_mode(const struct exfat_dev* dev)
331 off_t exfat_get_size(const struct exfat_dev* dev)
336 off_t exfat_seek(struct exfat_dev* dev, off_t offset, int whence)
339 /* XXX SEEK_CUR will be handled incorrectly */
340 return dev->pos = lseek(dev->fd, offset, whence);
342 return lseek(dev->fd, offset, whence);
346 ssize_t exfat_read(struct exfat_dev* dev, void* buffer, size_t size)
349 ssize_t result = ublio_pread(dev->ufh, buffer, size, dev->pos);
354 return read(dev->fd, buffer, size);
358 ssize_t exfat_write(struct exfat_dev* dev, const void* buffer, size_t size)
361 ssize_t result = ublio_pwrite(dev->ufh, (void*) buffer, size, dev->pos);
366 return write(dev->fd, buffer, size);
370 ssize_t exfat_pread(struct exfat_dev* dev, void* buffer, size_t size,
374 return ublio_pread(dev->ufh, buffer, size, offset);
376 return pread(dev->fd, buffer, size, offset);
380 ssize_t exfat_pwrite(struct exfat_dev* dev, const void* buffer, size_t size,
384 return ublio_pwrite(dev->ufh, (void*) buffer, size, offset);
386 return pwrite(dev->fd, buffer, size, offset);
390 ssize_t exfat_generic_pread(const struct exfat* ef, struct exfat_node* node,
391 void* buffer, size_t size, off_t offset)
393 uint64_t newsize = offset;
396 off_t lsize, loffset, remainder;
400 if (newsize >= node->size)
405 cluster = exfat_advance_cluster(ef, node, newsize / CLUSTER_SIZE(*ef->sb));
406 if (CLUSTER_INVALID(*ef->sb, cluster))
408 exfat_error("invalid cluster 0x%x while reading", cluster);
412 loffset = newsize % CLUSTER_SIZE(*ef->sb);
413 remainder = MIN(size, node->size - newsize);
414 while (remainder > 0)
416 if (CLUSTER_INVALID(*ef->sb, cluster))
418 exfat_error("invalid cluster 0x%x while reading", cluster);
421 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
422 if (exfat_pread(ef->dev, bufp, lsize,
423 exfat_c2o(ef, cluster) + loffset) < 0)
425 exfat_error("failed to read cluster %#x", cluster);
431 cluster = exfat_next_cluster(ef, node, cluster);
433 if (!(node->attrib & EXFAT_ATTRIB_DIR) && !ef->ro && !ef->noatime)
434 exfat_update_atime(node);
435 return MIN(size, node->size - newsize) - remainder;
438 ssize_t exfat_generic_pwrite(struct exfat* ef, struct exfat_node* node,
439 const void* buffer, size_t size, off_t offset)
441 uint64_t newsize = offset;
444 const char* bufp = buffer;
445 off_t lsize, loffset, remainder;
449 if (newsize > node->size)
451 rc = exfat_truncate(ef, node, newsize, true);
455 if (newsize + size > node->size)
457 rc = exfat_truncate(ef, node, newsize + size, false);
464 cluster = exfat_advance_cluster(ef, node, newsize / CLUSTER_SIZE(*ef->sb));
465 if (CLUSTER_INVALID(*ef->sb, cluster))
467 exfat_error("invalid cluster 0x%x while writing", cluster);
471 loffset = newsize % CLUSTER_SIZE(*ef->sb);
473 while (remainder > 0)
475 if (CLUSTER_INVALID(*ef->sb, cluster))
477 exfat_error("invalid cluster 0x%x while writing", cluster);
480 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
481 if (exfat_pwrite(ef->dev, bufp, lsize,
482 exfat_c2o(ef, cluster) + loffset) < 0)
484 exfat_error("failed to write cluster %#x", cluster);
490 cluster = exfat_next_cluster(ef, node, cluster);
492 if (!(node->attrib & EXFAT_ATTRIB_DIR))
493 /* directory's mtime should be updated by the caller only when it
494 creates or removes something in this directory */
495 exfat_update_mtime(node);
496 return size - remainder;