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>
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)
80 struct exfat_dev* exfat_open(const char* spec, enum exfat_mode mode)
82 struct exfat_dev* dev;
85 struct ublio_param up;
88 dev = malloc(sizeof(struct exfat_dev));
91 exfat_error("failed to allocate memory for device structure");
98 dev->fd = open_ro(spec);
102 exfat_error("failed to open `%s' in read-only mode", spec);
105 dev->mode = EXFAT_MODE_RO;
108 dev->fd = open_rw(spec);
112 exfat_error("failed to open `%s' in read-write mode", spec);
115 dev->mode = EXFAT_MODE_RW;
118 dev->fd = open_rw(spec);
121 dev->mode = EXFAT_MODE_RW;
124 dev->fd = open_ro(spec);
127 dev->mode = EXFAT_MODE_RO;
128 exfat_warn("`%s' is write-protected, mounting read-only", spec);
132 exfat_error("failed to open `%s'", spec);
136 if (fstat(dev->fd, &stbuf) != 0)
140 exfat_error("failed to fstat `%s'", spec);
143 if (!S_ISBLK(stbuf.st_mode) &&
144 !S_ISCHR(stbuf.st_mode) &&
145 !S_ISREG(stbuf.st_mode))
149 exfat_error("`%s' is neither a device, nor a regular file", spec);
153 #if defined(__APPLE__)
154 if (!S_ISREG(stbuf.st_mode))
156 uint32_t block_size = 0;
159 if (ioctl(dev->fd, DKIOCGETBLOCKSIZE, &block_size) != 0)
163 exfat_error("failed to get block size");
166 if (ioctl(dev->fd, DKIOCGETBLOCKCOUNT, &blocks) != 0)
170 exfat_error("failed to get blocks count");
173 dev->size = blocks * block_size;
176 #elif defined(__OpenBSD__)
177 if (!S_ISREG(stbuf.st_mode))
179 struct disklabel lab;
180 struct partition* pp;
183 if (ioctl(dev->fd, DIOCGDINFO, &lab) == -1)
187 exfat_error("failed to get disklabel");
191 /* Don't need to check that partition letter is valid as we won't get
192 this far otherwise. */
193 partition = strchr(spec, '\0') - 1;
194 pp = &(lab.d_partitions[*partition - 'a']);
195 dev->size = DL_GETPSIZE(pp) * lab.d_secsize;
197 if (pp->p_fstype != FS_NTFS)
198 exfat_warn("partition type is not 0x07 (NTFS/exFAT); "
199 "you can fix this with fdisk(8)");
204 /* works for Linux, FreeBSD, Solaris */
205 dev->size = exfat_seek(dev, 0, SEEK_END);
210 exfat_error("failed to get size of `%s'", spec);
213 if (exfat_seek(dev, 0, SEEK_SET) == -1)
217 exfat_error("failed to seek to the beginning of `%s'", spec);
223 memset(&up, 0, sizeof(struct ublio_param));
224 up.up_blocksize = 256 * 1024;
227 up.up_priv = &dev->fd;
230 dev->ufh = ublio_open(&up);
231 if (dev->ufh == NULL)
235 exfat_error("failed to initialize ublio");
243 int exfat_close(struct exfat_dev* dev)
248 if (ublio_close(dev->ufh) != 0)
250 exfat_error("failed to close ublio");
254 if (close(dev->fd) != 0)
256 exfat_error("failed to close device");
263 int exfat_fsync(struct exfat_dev* dev)
268 if (ublio_fsync(dev->ufh) != 0)
270 exfat_error("ublio fsync failed");
274 if (fsync(dev->fd) != 0)
276 exfat_error("fsync failed");
282 enum exfat_mode exfat_get_mode(const struct exfat_dev* dev)
287 off_t exfat_get_size(const struct exfat_dev* dev)
292 off_t exfat_seek(struct exfat_dev* dev, off_t offset, int whence)
295 /* XXX SEEK_CUR will be handled incorrectly */
296 return dev->pos = lseek(dev->fd, offset, whence);
298 return lseek(dev->fd, offset, whence);
302 ssize_t exfat_read(struct exfat_dev* dev, void* buffer, size_t size)
305 ssize_t result = ublio_pread(dev->ufh, buffer, size, dev->pos);
310 return read(dev->fd, buffer, size);
314 ssize_t exfat_write(struct exfat_dev* dev, const void* buffer, size_t size)
317 ssize_t result = ublio_pwrite(dev->ufh, buffer, size, dev->pos);
322 return write(dev->fd, buffer, size);
326 ssize_t exfat_pread(struct exfat_dev* dev, void* buffer, size_t size,
330 return ublio_pread(dev->ufh, buffer, size, offset);
332 return pread(dev->fd, buffer, size, offset);
336 ssize_t exfat_pwrite(struct exfat_dev* dev, const void* buffer, size_t size,
340 return ublio_pwrite(dev->ufh, buffer, size, offset);
342 return pwrite(dev->fd, buffer, size, offset);
346 ssize_t exfat_generic_pread(const struct exfat* ef, struct exfat_node* node,
347 void* buffer, size_t size, off_t offset)
351 off_t lsize, loffset, remainder;
353 if (offset >= node->size)
358 cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
359 if (CLUSTER_INVALID(cluster))
361 exfat_error("invalid cluster 0x%x while reading", cluster);
365 loffset = offset % CLUSTER_SIZE(*ef->sb);
366 remainder = MIN(size, node->size - offset);
367 while (remainder > 0)
369 if (CLUSTER_INVALID(cluster))
371 exfat_error("invalid cluster 0x%x while reading", cluster);
374 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
375 if (exfat_pread(ef->dev, bufp, lsize,
376 exfat_c2o(ef, cluster) + loffset) < 0)
378 exfat_error("failed to read cluster %#x", cluster);
384 cluster = exfat_next_cluster(ef, node, cluster);
386 if (!ef->ro && !ef->noatime)
387 exfat_update_atime(node);
388 return MIN(size, node->size - offset) - remainder;
391 ssize_t exfat_generic_pwrite(struct exfat* ef, struct exfat_node* node,
392 const void* buffer, size_t size, off_t offset)
395 const char* bufp = buffer;
396 off_t lsize, loffset, remainder;
398 if (offset > node->size)
399 if (exfat_truncate(ef, node, offset, true) != 0)
401 if (offset + size > node->size)
402 if (exfat_truncate(ef, node, offset + size, false) != 0)
407 cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
408 if (CLUSTER_INVALID(cluster))
410 exfat_error("invalid cluster 0x%x while writing", cluster);
414 loffset = offset % CLUSTER_SIZE(*ef->sb);
416 while (remainder > 0)
418 if (CLUSTER_INVALID(cluster))
420 exfat_error("invalid cluster 0x%x while writing", cluster);
423 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
424 if (exfat_pwrite(ef->dev, bufp, lsize,
425 exfat_c2o(ef, cluster) + loffset) < 0)
427 exfat_error("failed to write cluster %#x", cluster);
433 cluster = exfat_next_cluster(ef, node, cluster);
435 exfat_update_mtime(node);
436 return size - remainder;