3 exFAT file system implementation library.
5 Free exFAT implementation.
6 Copyright (C) 2010-2016 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>
45 off_t size; /* in bytes */
48 static int open_ro(const char* spec)
50 return open(spec, O_RDONLY);
53 static int open_rw(const char* spec)
55 int fd = open(spec, O_RDWR);
60 This ioctl is needed because after "blockdev --setro" kernel still
61 allows to open the device in read-write mode but fails writes.
63 if (fd != -1 && ioctl(fd, BLKROGET, &ro) == 0 && ro)
73 struct exfat_dev* exfat_open(const char* spec, enum exfat_mode mode)
75 struct exfat_dev* dev;
78 dev = malloc(sizeof(struct exfat_dev));
81 exfat_error("failed to allocate memory for device structure");
88 dev->fd = open_ro(spec);
92 exfat_error("failed to open '%s' in read-only mode: %s", spec,
96 dev->mode = EXFAT_MODE_RO;
99 dev->fd = open_rw(spec);
103 exfat_error("failed to open '%s' in read-write mode: %s", spec,
107 dev->mode = EXFAT_MODE_RW;
110 dev->fd = open_rw(spec);
113 dev->mode = EXFAT_MODE_RW;
116 dev->fd = open_ro(spec);
119 dev->mode = EXFAT_MODE_RO;
120 exfat_warn("'%s' is write-protected, mounting read-only", spec);
124 exfat_error("failed to open '%s': %s", spec, strerror(errno));
128 if (fstat(dev->fd, &stbuf) != 0)
132 exfat_error("failed to fstat '%s'", spec);
135 if (!S_ISBLK(stbuf.st_mode) &&
136 !S_ISCHR(stbuf.st_mode) &&
137 !S_ISREG(stbuf.st_mode))
141 exfat_error("'%s' is neither a device, nor a regular file", spec);
145 #if defined(__APPLE__)
146 if (!S_ISREG(stbuf.st_mode))
148 uint32_t block_size = 0;
151 if (ioctl(dev->fd, DKIOCGETBLOCKSIZE, &block_size) != 0)
155 exfat_error("failed to get block size");
158 if (ioctl(dev->fd, DKIOCGETBLOCKCOUNT, &blocks) != 0)
162 exfat_error("failed to get blocks count");
165 dev->size = blocks * block_size;
168 #elif defined(__OpenBSD__)
169 if (!S_ISREG(stbuf.st_mode))
171 struct disklabel lab;
172 struct partition* pp;
175 if (ioctl(dev->fd, DIOCGDINFO, &lab) == -1)
179 exfat_error("failed to get disklabel");
183 /* Don't need to check that partition letter is valid as we won't get
184 this far otherwise. */
185 partition = strchr(spec, '\0') - 1;
186 pp = &(lab.d_partitions[*partition - 'a']);
187 dev->size = DL_GETPSIZE(pp) * lab.d_secsize;
189 if (pp->p_fstype != FS_NTFS)
190 exfat_warn("partition type is not 0x07 (NTFS/exFAT); "
191 "you can fix this with fdisk(8)");
196 /* works for Linux, FreeBSD, Solaris */
197 dev->size = exfat_seek(dev, 0, SEEK_END);
202 exfat_error("failed to get size of '%s'", spec);
205 if (exfat_seek(dev, 0, SEEK_SET) == -1)
209 exfat_error("failed to seek to the beginning of '%s'", spec);
217 int exfat_close(struct exfat_dev* dev)
221 if (close(dev->fd) != 0)
223 exfat_error("failed to close device: %s", strerror(errno));
230 int exfat_fsync(struct exfat_dev* dev)
234 if (fsync(dev->fd) != 0)
236 exfat_error("fsync failed: %s", strerror(errno));
242 enum exfat_mode exfat_get_mode(const struct exfat_dev* dev)
247 off_t exfat_get_size(const struct exfat_dev* dev)
252 off_t exfat_seek(struct exfat_dev* dev, off_t offset, int whence)
254 return lseek(dev->fd, offset, whence);
257 ssize_t exfat_read(struct exfat_dev* dev, void* buffer, size_t size)
259 return read(dev->fd, buffer, size);
262 ssize_t exfat_write(struct exfat_dev* dev, const void* buffer, size_t size)
264 return write(dev->fd, buffer, size);
267 ssize_t exfat_pread(struct exfat_dev* dev, void* buffer, size_t size,
270 return pread(dev->fd, buffer, size, offset);
273 ssize_t exfat_pwrite(struct exfat_dev* dev, const void* buffer, size_t size,
276 return pwrite(dev->fd, buffer, size, offset);
279 ssize_t exfat_generic_pread(const struct exfat* ef, struct exfat_node* node,
280 void* buffer, size_t size, off_t offset)
284 off_t lsize, loffset, remainder;
286 if (offset >= node->size)
291 cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
292 if (CLUSTER_INVALID(cluster))
294 exfat_error("invalid cluster 0x%x while reading", cluster);
298 loffset = offset % CLUSTER_SIZE(*ef->sb);
299 remainder = MIN(size, node->size - offset);
300 while (remainder > 0)
302 if (CLUSTER_INVALID(cluster))
304 exfat_error("invalid cluster 0x%x while reading", cluster);
307 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
308 if (exfat_pread(ef->dev, bufp, lsize,
309 exfat_c2o(ef, cluster) + loffset) < 0)
311 exfat_error("failed to read cluster %#x", cluster);
317 cluster = exfat_next_cluster(ef, node, cluster);
319 if (!ef->ro && !ef->noatime)
320 exfat_update_atime(node);
321 return MIN(size, node->size - offset) - remainder;
324 ssize_t exfat_generic_pwrite(struct exfat* ef, struct exfat_node* node,
325 const void* buffer, size_t size, off_t offset)
328 const char* bufp = buffer;
329 off_t lsize, loffset, remainder;
331 if (offset > node->size)
332 if (exfat_truncate(ef, node, offset, true) != 0)
334 if (offset + size > node->size)
335 if (exfat_truncate(ef, node, offset + size, false) != 0)
340 cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
341 if (CLUSTER_INVALID(cluster))
343 exfat_error("invalid cluster 0x%x while writing", cluster);
347 loffset = offset % CLUSTER_SIZE(*ef->sb);
349 while (remainder > 0)
351 if (CLUSTER_INVALID(cluster))
353 exfat_error("invalid cluster 0x%x while writing", cluster);
356 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
357 if (exfat_pwrite(ef->dev, bufp, lsize,
358 exfat_c2o(ef, cluster) + loffset) < 0)
360 exfat_error("failed to write cluster %#x", cluster);
366 cluster = exfat_next_cluster(ef, node, cluster);
368 exfat_update_mtime(node);
369 return size - remainder;