3 exFAT file system implementation library.
5 Copyright (C) 2010-2012 Andrew Nayenko
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <sys/types.h>
25 #include <sys/mount.h>
41 off_t size; /* in bytes */
44 ublio_filehandle_t ufh;
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)
72 struct exfat_dev* exfat_open(const char* spec, enum exfat_mode mode)
74 struct exfat_dev* dev;
77 struct ublio_param up;
80 dev = malloc(sizeof(struct exfat_dev));
83 exfat_error("failed to allocate memory for device structure");
90 dev->fd = open_ro(spec);
94 exfat_error("failed to open `%s' in read-only mode", spec);
97 dev->mode = EXFAT_MODE_RO;
100 dev->fd = open_rw(spec);
104 exfat_error("failed to open `%s' in read-write mode", 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'", spec);
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);
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;
170 /* works for Linux, FreeBSD, Solaris */
171 dev->size = exfat_seek(dev, 0, SEEK_END);
176 exfat_error("failed to get size of `%s'", spec);
182 memset(&up, 0, sizeof(struct ublio_param));
183 up.up_blocksize = 256 * 1024;
186 up.up_priv = &dev->fd;
189 dev->ufh = ublio_open(&up);
190 if (dev->ufh == NULL)
194 exfat_error("failed to initialize ublio");
202 int exfat_close(struct exfat_dev* dev)
205 if (ublio_close(dev->ufh) != 0)
206 exfat_error("failed to close ublio");
208 if (close(dev->fd) != 0)
211 exfat_error("failed to close device");
218 int exfat_fsync(struct exfat_dev* dev)
221 if (ublio_fsync(dev->ufh) != 0)
223 if (fsync(dev->fd) != 0)
226 exfat_error("fsync failed");
232 enum exfat_mode exfat_get_mode(const struct exfat_dev* dev)
237 off_t exfat_get_size(const struct exfat_dev* dev)
242 off_t exfat_seek(struct exfat_dev* dev, off_t offset, int whence)
245 /* XXX SEEK_CUR will be handled incorrectly */
246 return dev->pos = lseek(dev->fd, offset, whence);
248 return lseek(dev->fd, offset, whence);
252 ssize_t exfat_read(struct exfat_dev* dev, void* buffer, size_t size)
255 ssize_t result = ublio_pread(dev->ufh, buffer, size, dev->pos);
260 return read(dev->fd, buffer, size);
264 ssize_t exfat_write(struct exfat_dev* dev, const void* buffer, size_t size)
267 ssize_t result = ublio_pwrite(dev->ufh, buffer, size, dev->pos);
272 return write(dev->fd, buffer, size);
276 void exfat_pread(struct exfat_dev* dev, void* buffer, size_t size,
280 if (ublio_pread(dev->ufh, buffer, size, offset) != size)
282 if (pread(dev->fd, buffer, size, offset) != size)
284 exfat_bug("failed to read %zu bytes from file at %"PRIu64, size,
288 void exfat_pwrite(struct exfat_dev* dev, const void* buffer, size_t size,
292 if (ublio_pwrite(dev->ufh, buffer, size, offset) != size)
294 if (pwrite(dev->fd, buffer, size, offset) != size)
296 exfat_bug("failed to write %zu bytes to file at %"PRIu64, size,
300 ssize_t exfat_generic_pread(const struct exfat* ef, struct exfat_node* node,
301 void* buffer, size_t size, off_t offset)
305 off_t lsize, loffset, remainder;
307 if (offset >= node->size)
312 cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
313 if (CLUSTER_INVALID(cluster))
315 exfat_error("invalid cluster 0x%x while reading", cluster);
319 loffset = offset % CLUSTER_SIZE(*ef->sb);
320 remainder = MIN(size, node->size - offset);
321 while (remainder > 0)
323 if (CLUSTER_INVALID(cluster))
325 exfat_error("invalid cluster 0x%x while reading", cluster);
328 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
329 exfat_pread(ef->dev, bufp, lsize, exfat_c2o(ef, cluster) + loffset);
333 cluster = exfat_next_cluster(ef, node, cluster);
335 if (!ef->ro && !ef->noatime)
336 exfat_update_atime(node);
337 return size - remainder;
340 ssize_t exfat_generic_pwrite(struct exfat* ef, struct exfat_node* node,
341 const void* buffer, size_t size, off_t offset)
344 const char* bufp = buffer;
345 off_t lsize, loffset, remainder;
347 if (offset + size > node->size)
348 if (exfat_truncate(ef, node, offset + size) != 0)
353 cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
354 if (CLUSTER_INVALID(cluster))
356 exfat_error("invalid cluster 0x%x while writing", cluster);
360 loffset = offset % CLUSTER_SIZE(*ef->sb);
362 while (remainder > 0)
364 if (CLUSTER_INVALID(cluster))
366 exfat_error("invalid cluster 0x%x while writing", cluster);
369 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
370 exfat_pwrite(ef->dev, bufp, lsize, exfat_c2o(ef, cluster) + loffset);
374 cluster = exfat_next_cluster(ef, node, cluster);
376 exfat_update_mtime(node);
377 return size - remainder;