3 exFAT file system implementation library.
5 Free exFAT implementation.
6 Copyright (C) 2010-2017 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 bool is_open(int fd)
50 return fcntl(fd, F_GETFD) != -1;
53 static int open_ro(const char* spec)
55 return open(spec, O_RDONLY);
58 static int open_rw(const char* spec)
60 int fd = open(spec, O_RDWR);
65 This ioctl is needed because after "blockdev --setro" kernel still
66 allows to open the device in read-write mode but fails writes.
68 if (fd != -1 && ioctl(fd, BLKROGET, &ro) == 0 && ro)
78 struct exfat_dev* exfat_open(const char* spec, enum exfat_mode mode)
80 struct exfat_dev* dev;
83 /* The system allocates file descriptors sequentially. If we have been
84 started with stdin (0), stdout (1) or stderr (2) closed, the system
85 will give us descriptor 0, 1 or 2 later when we open block device,
86 FUSE communication pipe, etc. As a result, functions using stdin,
87 stdout or stderr will actualy work with a different thing and can
88 corrupt it. Protect descriptors 0, 1 and 2 from such misuse. */
89 while (!is_open(STDIN_FILENO)
90 || !is_open(STDOUT_FILENO)
91 || !is_open(STDERR_FILENO))
93 /* we don't need those descriptors, let them leak */
94 if (open("/dev/null", O_RDWR) == -1)
96 exfat_error("failed to open /dev/null");
101 dev = malloc(sizeof(struct exfat_dev));
104 exfat_error("failed to allocate memory for device structure");
111 dev->fd = open_ro(spec);
115 exfat_error("failed to open '%s' in read-only mode: %s", spec,
119 dev->mode = EXFAT_MODE_RO;
122 dev->fd = open_rw(spec);
126 exfat_error("failed to open '%s' in read-write mode: %s", spec,
130 dev->mode = EXFAT_MODE_RW;
133 dev->fd = open_rw(spec);
136 dev->mode = EXFAT_MODE_RW;
139 dev->fd = open_ro(spec);
142 dev->mode = EXFAT_MODE_RO;
143 exfat_warn("'%s' is write-protected, mounting read-only", spec);
147 exfat_error("failed to open '%s': %s", spec, strerror(errno));
151 if (fstat(dev->fd, &stbuf) != 0)
155 exfat_error("failed to fstat '%s'", spec);
158 if (!S_ISBLK(stbuf.st_mode) &&
159 !S_ISCHR(stbuf.st_mode) &&
160 !S_ISREG(stbuf.st_mode))
164 exfat_error("'%s' is neither a device, nor a regular file", spec);
168 #if defined(__APPLE__)
169 if (!S_ISREG(stbuf.st_mode))
171 uint32_t block_size = 0;
174 if (ioctl(dev->fd, DKIOCGETBLOCKSIZE, &block_size) != 0)
178 exfat_error("failed to get block size");
181 if (ioctl(dev->fd, DKIOCGETBLOCKCOUNT, &blocks) != 0)
185 exfat_error("failed to get blocks count");
188 dev->size = blocks * block_size;
191 #elif defined(__OpenBSD__)
192 if (!S_ISREG(stbuf.st_mode))
194 struct disklabel lab;
195 struct partition* pp;
198 if (ioctl(dev->fd, DIOCGDINFO, &lab) == -1)
202 exfat_error("failed to get disklabel");
206 /* Don't need to check that partition letter is valid as we won't get
207 this far otherwise. */
208 partition = strchr(spec, '\0') - 1;
209 pp = &(lab.d_partitions[*partition - 'a']);
210 dev->size = DL_GETPSIZE(pp) * lab.d_secsize;
212 if (pp->p_fstype != FS_NTFS)
213 exfat_warn("partition type is not 0x07 (NTFS/exFAT); "
214 "you can fix this with fdisk(8)");
219 /* works for Linux, FreeBSD, Solaris */
220 dev->size = exfat_seek(dev, 0, SEEK_END);
225 exfat_error("failed to get size of '%s'", spec);
228 if (exfat_seek(dev, 0, SEEK_SET) == -1)
232 exfat_error("failed to seek to the beginning of '%s'", spec);
240 int exfat_close(struct exfat_dev* dev)
244 if (close(dev->fd) != 0)
246 exfat_error("failed to close device: %s", strerror(errno));
253 int exfat_fsync(struct exfat_dev* dev)
257 if (fsync(dev->fd) != 0)
259 exfat_error("fsync failed: %s", strerror(errno));
265 enum exfat_mode exfat_get_mode(const struct exfat_dev* dev)
270 off_t exfat_get_size(const struct exfat_dev* dev)
275 off_t exfat_seek(struct exfat_dev* dev, off_t offset, int whence)
277 return lseek(dev->fd, offset, whence);
280 ssize_t exfat_read(struct exfat_dev* dev, void* buffer, size_t size)
282 return read(dev->fd, buffer, size);
285 ssize_t exfat_write(struct exfat_dev* dev, const void* buffer, size_t size)
287 return write(dev->fd, buffer, size);
290 ssize_t exfat_pread(struct exfat_dev* dev, void* buffer, size_t size,
293 return pread(dev->fd, buffer, size, offset);
296 ssize_t exfat_pwrite(struct exfat_dev* dev, const void* buffer, size_t size,
299 return pwrite(dev->fd, buffer, size, offset);
302 ssize_t exfat_generic_pread(const struct exfat* ef, struct exfat_node* node,
303 void* buffer, size_t size, off_t offset)
307 off_t lsize, loffset, remainder;
309 if (offset >= node->size)
314 cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
315 if (CLUSTER_INVALID(*ef->sb, cluster))
317 exfat_error("invalid cluster 0x%x while reading", cluster);
321 loffset = offset % CLUSTER_SIZE(*ef->sb);
322 remainder = MIN(size, node->size - offset);
323 while (remainder > 0)
325 if (CLUSTER_INVALID(*ef->sb, cluster))
327 exfat_error("invalid cluster 0x%x while reading", cluster);
330 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
331 if (exfat_pread(ef->dev, bufp, lsize,
332 exfat_c2o(ef, cluster) + loffset) < 0)
334 exfat_error("failed to read cluster %#x", cluster);
340 cluster = exfat_next_cluster(ef, node, cluster);
342 if (!(node->attrib & EXFAT_ATTRIB_DIR) && !ef->ro && !ef->noatime)
343 exfat_update_atime(node);
344 return MIN(size, node->size - offset) - remainder;
347 ssize_t exfat_generic_pwrite(struct exfat* ef, struct exfat_node* node,
348 const void* buffer, size_t size, off_t offset)
352 const char* bufp = buffer;
353 off_t lsize, loffset, remainder;
355 if (offset > node->size)
357 rc = exfat_truncate(ef, node, offset, true);
361 if (offset + size > node->size)
363 rc = exfat_truncate(ef, node, offset + size, false);
370 cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
371 if (CLUSTER_INVALID(*ef->sb, cluster))
373 exfat_error("invalid cluster 0x%x while writing", cluster);
377 loffset = offset % CLUSTER_SIZE(*ef->sb);
379 while (remainder > 0)
381 if (CLUSTER_INVALID(*ef->sb, cluster))
383 exfat_error("invalid cluster 0x%x while writing", cluster);
386 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
387 if (exfat_pwrite(ef->dev, bufp, lsize,
388 exfat_c2o(ef, cluster) + loffset) < 0)
390 exfat_error("failed to write cluster %#x", cluster);
396 cluster = exfat_next_cluster(ef, node, cluster);
398 if (!(node->attrib & EXFAT_ATTRIB_DIR))
399 /* directory's mtime should be updated by the caller only when it
400 creates or removes something in this directory */
401 exfat_update_mtime(node);
402 return size - remainder;