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>
39 #include <sys/mount.h>
50 off_t size; /* in bytes */
53 ublio_filehandle_t ufh;
57 static bool is_open(int fd)
59 return fcntl(fd, F_GETFD) != -1;
62 static int open_ro(const char* spec)
64 return open(spec, O_RDONLY);
67 static int open_rw(const char* spec)
69 int fd = open(spec, O_RDWR);
74 This ioctl is needed because after "blockdev --setro" kernel still
75 allows to open the device in read-write mode but fails writes.
77 if (fd != -1 && ioctl(fd, BLKROGET, &ro) == 0 && ro)
87 struct exfat_dev* exfat_open(const char* spec, enum exfat_mode mode)
89 struct exfat_dev* dev;
92 struct ublio_param up;
95 /* The system allocates file descriptors sequentially. If we have been
96 started with stdin (0), stdout (1) or stderr (2) closed, the system
97 will give us descriptor 0, 1 or 2 later when we open block device,
98 FUSE communication pipe, etc. As a result, functions using stdin,
99 stdout or stderr will actually work with a different thing and can
100 corrupt it. Protect descriptors 0, 1 and 2 from such misuse. */
101 while (!is_open(STDIN_FILENO)
102 || !is_open(STDOUT_FILENO)
103 || !is_open(STDERR_FILENO))
105 /* we don't need those descriptors, let them leak */
106 if (open("/dev/null", O_RDWR) == -1)
108 exfat_error("failed to open /dev/null");
113 dev = malloc(sizeof(struct exfat_dev));
116 exfat_error("failed to allocate memory for device structure");
123 dev->fd = open_ro(spec);
127 exfat_error("failed to open '%s' in read-only mode: %s", spec,
131 dev->mode = EXFAT_MODE_RO;
134 dev->fd = open_rw(spec);
138 exfat_error("failed to open '%s' in read-write mode: %s", spec,
142 dev->mode = EXFAT_MODE_RW;
145 dev->fd = open_rw(spec);
148 dev->mode = EXFAT_MODE_RW;
151 dev->fd = open_ro(spec);
154 dev->mode = EXFAT_MODE_RO;
155 exfat_warn("'%s' is write-protected, mounting read-only", spec);
159 exfat_error("failed to open '%s': %s", spec, strerror(errno));
163 if (fstat(dev->fd, &stbuf) != 0)
167 exfat_error("failed to fstat '%s'", spec);
170 if (!S_ISBLK(stbuf.st_mode) &&
171 !S_ISCHR(stbuf.st_mode) &&
172 !S_ISREG(stbuf.st_mode))
176 exfat_error("'%s' is neither a device, nor a regular file", spec);
180 #if defined(__APPLE__)
181 if (!S_ISREG(stbuf.st_mode))
183 uint32_t block_size = 0;
186 if (ioctl(dev->fd, DKIOCGETBLOCKSIZE, &block_size) != 0)
190 exfat_error("failed to get block size");
193 if (ioctl(dev->fd, DKIOCGETBLOCKCOUNT, &blocks) != 0)
197 exfat_error("failed to get blocks count");
200 dev->size = blocks * block_size;
203 #elif defined(__OpenBSD__)
204 if (!S_ISREG(stbuf.st_mode))
206 struct disklabel lab;
207 struct partition* pp;
210 if (ioctl(dev->fd, DIOCGDINFO, &lab) == -1)
214 exfat_error("failed to get disklabel");
218 /* Don't need to check that partition letter is valid as we won't get
219 this far otherwise. */
220 partition = strchr(spec, '\0') - 1;
221 pp = &(lab.d_partitions[*partition - 'a']);
222 dev->size = DL_GETPSIZE(pp) * lab.d_secsize;
224 if (pp->p_fstype != FS_NTFS)
225 exfat_warn("partition type is not 0x07 (NTFS/exFAT); "
226 "you can fix this with fdisk(8)");
231 /* works for Linux, FreeBSD, Solaris */
232 dev->size = exfat_seek(dev, 0, SEEK_END);
237 exfat_error("failed to get size of '%s'", spec);
240 if (exfat_seek(dev, 0, SEEK_SET) == -1)
244 exfat_error("failed to seek to the beginning of '%s'", spec);
250 memset(&up, 0, sizeof(struct ublio_param));
251 up.up_blocksize = 256 * 1024;
254 up.up_priv = &dev->fd;
257 dev->ufh = ublio_open(&up);
258 if (dev->ufh == NULL)
262 exfat_error("failed to initialize ublio");
270 int exfat_close(struct exfat_dev* dev)
275 if (ublio_close(dev->ufh) != 0)
277 exfat_error("failed to close ublio");
281 if (close(dev->fd) != 0)
283 exfat_error("failed to close device: %s", strerror(errno));
290 int exfat_fsync(struct exfat_dev* dev)
295 if (ublio_fsync(dev->ufh) != 0)
297 exfat_error("ublio fsync failed");
301 if (fsync(dev->fd) != 0)
303 exfat_error("fsync failed: %s", strerror(errno));
309 enum exfat_mode exfat_get_mode(const struct exfat_dev* dev)
314 off_t exfat_get_size(const struct exfat_dev* dev)
319 off_t exfat_seek(struct exfat_dev* dev, off_t offset, int whence)
322 /* XXX SEEK_CUR will be handled incorrectly */
323 return dev->pos = lseek(dev->fd, offset, whence);
325 return lseek(dev->fd, offset, whence);
329 ssize_t exfat_read(struct exfat_dev* dev, void* buffer, size_t size)
332 ssize_t result = ublio_pread(dev->ufh, buffer, size, dev->pos);
337 return read(dev->fd, buffer, size);
341 ssize_t exfat_write(struct exfat_dev* dev, const void* buffer, size_t size)
344 ssize_t result = ublio_pwrite(dev->ufh, buffer, size, dev->pos);
349 return write(dev->fd, buffer, size);
353 ssize_t exfat_pread(struct exfat_dev* dev, void* buffer, size_t size,
357 return ublio_pread(dev->ufh, buffer, size, offset);
359 return pread(dev->fd, buffer, size, offset);
363 ssize_t exfat_pwrite(struct exfat_dev* dev, const void* buffer, size_t size,
367 return ublio_pwrite(dev->ufh, buffer, size, offset);
369 return pwrite(dev->fd, buffer, size, offset);
373 ssize_t exfat_generic_pread(const struct exfat* ef, struct exfat_node* node,
374 void* buffer, size_t size, off_t offset)
378 off_t lsize, loffset, remainder;
380 if (offset >= node->size)
385 cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
386 if (CLUSTER_INVALID(*ef->sb, cluster))
388 exfat_error("invalid cluster 0x%x while reading", cluster);
392 loffset = offset % CLUSTER_SIZE(*ef->sb);
393 remainder = MIN(size, node->size - offset);
394 while (remainder > 0)
396 if (CLUSTER_INVALID(*ef->sb, cluster))
398 exfat_error("invalid cluster 0x%x while reading", cluster);
401 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
402 if (exfat_pread(ef->dev, bufp, lsize,
403 exfat_c2o(ef, cluster) + loffset) < 0)
405 exfat_error("failed to read cluster %#x", cluster);
411 cluster = exfat_next_cluster(ef, node, cluster);
413 if (!(node->attrib & EXFAT_ATTRIB_DIR) && !ef->ro && !ef->noatime)
414 exfat_update_atime(node);
415 return MIN(size, node->size - offset) - remainder;
418 ssize_t exfat_generic_pwrite(struct exfat* ef, struct exfat_node* node,
419 const void* buffer, size_t size, off_t offset)
423 const char* bufp = buffer;
424 off_t lsize, loffset, remainder;
426 if (offset > node->size)
428 rc = exfat_truncate(ef, node, offset, true);
432 if (offset + size > node->size)
434 rc = exfat_truncate(ef, node, offset + size, false);
441 cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
442 if (CLUSTER_INVALID(*ef->sb, cluster))
444 exfat_error("invalid cluster 0x%x while writing", cluster);
448 loffset = offset % CLUSTER_SIZE(*ef->sb);
450 while (remainder > 0)
452 if (CLUSTER_INVALID(*ef->sb, cluster))
454 exfat_error("invalid cluster 0x%x while writing", cluster);
457 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
458 if (exfat_pwrite(ef->dev, bufp, lsize,
459 exfat_c2o(ef, cluster) + loffset) < 0)
461 exfat_error("failed to write cluster %#x", cluster);
467 cluster = exfat_next_cluster(ef, node, cluster);
469 if (!(node->attrib & EXFAT_ATTRIB_DIR))
470 /* directory's mtime should be updated by the caller only when it
471 creates or removes something in this directory */
472 exfat_update_mtime(node);
473 return size - remainder;