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>
46 off_t size; /* in bytes */
49 static bool is_open(int fd)
51 return fcntl(fd, F_GETFD) != -1;
54 static int open_ro(const char* spec)
56 return open(spec, O_RDONLY);
59 static int open_rw(const char* spec)
61 int fd = open(spec, O_RDWR);
66 This ioctl is needed because after "blockdev --setro" kernel still
67 allows to open the device in read-write mode but fails writes.
69 if (fd != -1 && ioctl(fd, BLKROGET, &ro) == 0 && ro)
79 struct exfat_dev* exfat_open(const char* spec, enum exfat_mode mode)
81 struct exfat_dev* dev;
84 /* The system allocates file descriptors sequentially. If we have been
85 started with stdin (0), stdout (1) or stderr (2) closed, the system
86 will give us descriptor 0, 1 or 2 later when we open block device,
87 FUSE communication pipe, etc. As a result, functions using stdin,
88 stdout or stderr will actually work with a different thing and can
89 corrupt it. Protect descriptors 0, 1 and 2 from such misuse. */
90 while (!is_open(STDIN_FILENO)
91 || !is_open(STDOUT_FILENO)
92 || !is_open(STDERR_FILENO))
94 /* we don't need those descriptors, let them leak */
95 if (open("/dev/null", O_RDWR) == -1)
97 exfat_error("failed to open /dev/null");
102 dev = malloc(sizeof(struct exfat_dev));
105 exfat_error("failed to allocate memory for device structure");
112 dev->fd = open_ro(spec);
116 exfat_error("failed to open '%s' in read-only mode: %s", spec,
120 dev->mode = EXFAT_MODE_RO;
123 dev->fd = open_rw(spec);
127 exfat_error("failed to open '%s' in read-write mode: %s", spec,
131 dev->mode = EXFAT_MODE_RW;
134 dev->fd = open_rw(spec);
137 dev->mode = EXFAT_MODE_RW;
140 dev->fd = open_ro(spec);
143 dev->mode = EXFAT_MODE_RO;
144 exfat_warn("'%s' is write-protected, mounting read-only", spec);
148 exfat_error("failed to open '%s': %s", spec, strerror(errno));
152 if (fstat(dev->fd, &stbuf) != 0)
156 exfat_error("failed to fstat '%s'", spec);
159 if (!S_ISBLK(stbuf.st_mode) &&
160 !S_ISCHR(stbuf.st_mode) &&
161 !S_ISREG(stbuf.st_mode))
165 exfat_error("'%s' is neither a device, nor a regular file", spec);
169 #if defined(__APPLE__)
170 if (!S_ISREG(stbuf.st_mode))
172 uint32_t block_size = 0;
175 if (ioctl(dev->fd, DKIOCGETBLOCKSIZE, &block_size) != 0)
179 exfat_error("failed to get block size");
182 if (ioctl(dev->fd, DKIOCGETBLOCKCOUNT, &blocks) != 0)
186 exfat_error("failed to get blocks count");
189 dev->size = blocks * block_size;
192 #elif defined(__OpenBSD__)
193 if (!S_ISREG(stbuf.st_mode))
195 struct disklabel lab;
196 struct partition* pp;
199 if (ioctl(dev->fd, DIOCGDINFO, &lab) == -1)
203 exfat_error("failed to get disklabel");
207 /* Don't need to check that partition letter is valid as we won't get
208 this far otherwise. */
209 partition = strchr(spec, '\0') - 1;
210 pp = &(lab.d_partitions[*partition - 'a']);
211 dev->size = DL_GETPSIZE(pp) * lab.d_secsize;
213 if (pp->p_fstype != FS_NTFS)
214 exfat_warn("partition type is not 0x07 (NTFS/exFAT); "
215 "you can fix this with fdisk(8)");
220 /* works for Linux, FreeBSD, Solaris */
221 dev->size = exfat_seek(dev, 0, SEEK_END);
226 exfat_error("failed to get size of '%s'", spec);
229 if (exfat_seek(dev, 0, SEEK_SET) == -1)
233 exfat_error("failed to seek to the beginning of '%s'", spec);
241 int exfat_close(struct exfat_dev* dev)
245 if (close(dev->fd) != 0)
247 exfat_error("failed to close device: %s", strerror(errno));
254 int exfat_fsync(struct exfat_dev* dev)
258 if (fsync(dev->fd) != 0)
260 exfat_error("fsync failed: %s", strerror(errno));
266 enum exfat_mode exfat_get_mode(const struct exfat_dev* dev)
271 off_t exfat_get_size(const struct exfat_dev* dev)
276 off_t exfat_seek(struct exfat_dev* dev, off_t offset, int whence)
278 return lseek(dev->fd, offset, whence);
281 ssize_t exfat_read(struct exfat_dev* dev, void* buffer, size_t size)
283 return read(dev->fd, buffer, size);
286 ssize_t exfat_write(struct exfat_dev* dev, const void* buffer, size_t size)
288 return write(dev->fd, buffer, size);
291 ssize_t exfat_pread(struct exfat_dev* dev, void* buffer, size_t size,
294 return pread(dev->fd, buffer, size, offset);
297 ssize_t exfat_pwrite(struct exfat_dev* dev, const void* buffer, size_t size,
300 return pwrite(dev->fd, buffer, size, offset);
303 ssize_t exfat_generic_pread(const struct exfat* ef, struct exfat_node* node,
304 void* buffer, size_t size, off_t offset)
308 off_t lsize, loffset, remainder;
310 if (offset >= node->size)
315 cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
316 if (CLUSTER_INVALID(*ef->sb, cluster))
318 exfat_error("invalid cluster 0x%x while reading", cluster);
322 loffset = offset % CLUSTER_SIZE(*ef->sb);
323 remainder = MIN(size, node->size - offset);
324 while (remainder > 0)
326 if (CLUSTER_INVALID(*ef->sb, cluster))
328 exfat_error("invalid cluster 0x%x while reading", cluster);
331 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
332 if (exfat_pread(ef->dev, bufp, lsize,
333 exfat_c2o(ef, cluster) + loffset) < 0)
335 exfat_error("failed to read cluster %#x", cluster);
341 cluster = exfat_next_cluster(ef, node, cluster);
343 if (!(node->attrib & EXFAT_ATTRIB_DIR) && !ef->ro && !ef->noatime)
344 exfat_update_atime(node);
345 return MIN(size, node->size - offset) - remainder;
348 ssize_t exfat_generic_pwrite(struct exfat* ef, struct exfat_node* node,
349 const void* buffer, size_t size, off_t offset)
353 const char* bufp = buffer;
354 off_t lsize, loffset, remainder;
356 if (offset > node->size)
358 rc = exfat_truncate(ef, node, offset, true);
362 if (offset + size > node->size)
364 rc = exfat_truncate(ef, node, offset + size, false);
371 cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
372 if (CLUSTER_INVALID(*ef->sb, cluster))
374 exfat_error("invalid cluster 0x%x while writing", cluster);
378 loffset = offset % CLUSTER_SIZE(*ef->sb);
380 while (remainder > 0)
382 if (CLUSTER_INVALID(*ef->sb, cluster))
384 exfat_error("invalid cluster 0x%x while writing", cluster);
387 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
388 if (exfat_pwrite(ef->dev, bufp, lsize,
389 exfat_c2o(ef, cluster) + loffset) < 0)
391 exfat_error("failed to write cluster %#x", cluster);
397 cluster = exfat_next_cluster(ef, node, cluster);
399 if (!(node->attrib & EXFAT_ATTRIB_DIR))
400 /* directory's mtime should be updated by the caller only when it
401 creates or removes something in this directory */
402 exfat_update_mtime(node);
403 return size - remainder;