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>
26 #include <sys/mount.h>
35 #if !defined(_FILE_OFFSET_BITS) || (_FILE_OFFSET_BITS != 64)
36 #error You should define _FILE_OFFSET_BITS=64
45 ublio_filehandle_t ufh;
49 static int open_ro(const char* spec)
51 return open(spec, O_RDONLY);
54 static int open_rw(const char* spec)
56 int fd = open(spec, O_RDWR);
61 This ioctl is needed because after "blockdev --setro" kernel still
62 allows to open the device in read-write mode but fails writes.
64 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 struct ublio_param up;
81 dev = malloc(sizeof(struct exfat_dev));
84 exfat_error("failed to allocate memory for device structure");
91 dev->fd = open_ro(spec);
95 exfat_error("failed to open `%s' in read-only mode", spec);
98 dev->mode = EXFAT_MODE_RO;
101 dev->fd = open_rw(spec);
105 exfat_error("failed to open `%s' in read-write mode", spec);
108 dev->mode = EXFAT_MODE_RW;
111 dev->fd = open_rw(spec);
114 dev->mode = EXFAT_MODE_RW;
117 dev->fd = open_ro(spec);
120 dev->mode = EXFAT_MODE_RO;
121 exfat_warn("`%s' is write-protected, mounting read-only", spec);
125 exfat_error("failed to open `%s'", spec);
129 if (fstat(dev->fd, &stbuf) != 0)
133 exfat_error("failed to fstat `%s'", spec);
136 if (!S_ISBLK(stbuf.st_mode) &&
137 !S_ISCHR(stbuf.st_mode) &&
138 !S_ISREG(stbuf.st_mode))
142 exfat_error("`%s' is neither a device, nor a regular file", spec);
147 memset(&up, 0, sizeof(struct ublio_param));
148 up.up_blocksize = 256 * 1024;
151 up.up_priv = &dev->fd;
154 dev->ufh = ublio_open(&up);
155 if (dev->ufh == NULL)
159 exfat_error("failed to initialize ublio");
167 int exfat_close(struct exfat_dev* dev)
170 if (ublio_close(dev->ufh) != 0)
171 exfat_error("failed to close ublio");
173 if (close(dev->fd) != 0)
176 exfat_error("failed to close device");
183 int exfat_fsync(struct exfat_dev* dev)
186 if (ublio_fsync(dev->ufh) != 0)
188 if (fsync(dev->fd) != 0)
191 exfat_error("fsync failed");
197 enum exfat_mode exfat_mode(const struct exfat_dev* dev)
202 off_t exfat_seek(struct exfat_dev* dev, off_t offset, int whence)
205 /* XXX SEEK_CUR will be handled incorrectly */
206 return dev->pos = lseek(dev->fd, offset, whence);
208 return lseek(dev->fd, offset, whence);
212 ssize_t exfat_read(struct exfat_dev* dev, void* buffer, size_t size)
215 ssize_t result = ublio_pread(dev->ufh, buffer, size, dev->pos);
220 return read(dev->fd, buffer, size);
224 ssize_t exfat_write(struct exfat_dev* dev, const void* buffer, size_t size)
227 ssize_t result = ublio_pwrite(dev->ufh, buffer, size, dev->pos);
232 return write(dev->fd, buffer, size);
236 void exfat_pread(struct exfat_dev* dev, void* buffer, size_t size,
240 if (ublio_pread(dev->ufh, buffer, size, offset) != size)
242 if (pread(dev->fd, buffer, size, offset) != size)
244 exfat_bug("failed to read %zu bytes from file at %"PRIu64, size,
248 void exfat_pwrite(struct exfat_dev* dev, const void* buffer, size_t size,
252 if (ublio_pwrite(dev->ufh, buffer, size, offset) != size)
254 if (pwrite(dev->fd, buffer, size, offset) != size)
256 exfat_bug("failed to write %zu bytes to file at %"PRIu64, size,
260 ssize_t exfat_generic_pread(const struct exfat* ef, struct exfat_node* node,
261 void* buffer, size_t size, off_t offset)
265 off_t lsize, loffset, remainder;
267 if (offset >= node->size)
272 cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
273 if (CLUSTER_INVALID(cluster))
275 exfat_error("invalid cluster 0x%x while reading", cluster);
279 loffset = offset % CLUSTER_SIZE(*ef->sb);
280 remainder = MIN(size, node->size - offset);
281 while (remainder > 0)
283 if (CLUSTER_INVALID(cluster))
285 exfat_error("invalid cluster 0x%x while reading", cluster);
288 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
289 exfat_pread(ef->dev, bufp, lsize, exfat_c2o(ef, cluster) + loffset);
293 cluster = exfat_next_cluster(ef, node, cluster);
295 if (!ef->ro && !ef->noatime)
296 exfat_update_atime(node);
297 return size - remainder;
300 ssize_t exfat_generic_pwrite(struct exfat* ef, struct exfat_node* node,
301 const void* buffer, size_t size, off_t offset)
304 const char* bufp = buffer;
305 off_t lsize, loffset, remainder;
307 if (offset + size > node->size)
309 int rc = exfat_truncate(ef, node, offset + size);
316 cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
317 if (CLUSTER_INVALID(cluster))
319 exfat_error("invalid cluster 0x%x while writing", cluster);
323 loffset = offset % CLUSTER_SIZE(*ef->sb);
325 while (remainder > 0)
327 if (CLUSTER_INVALID(cluster))
329 exfat_error("invalid cluster 0x%x while writing", cluster);
332 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
333 exfat_pwrite(ef->dev, bufp, lsize, exfat_c2o(ef, cluster) + loffset);
337 cluster = exfat_next_cluster(ef, node, cluster);
339 exfat_update_mtime(node);
340 return size - remainder;