3 exFAT file system implementation library.
5 Copyright (C) 2009, 2010 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>
27 #define __USE_UNIX98 /* for pread() in Linux */
30 #if _FILE_OFFSET_BITS != 64
31 #error You should define _FILE_OFFSET_BITS=64
34 int exfat_open(const char* spec, int ro)
39 fd = open(spec, ro ? O_RDONLY : O_RDWR);
42 exfat_error("failed to open `%s' in read-%s mode", spec,
43 ro ? "only" : "write");
46 if (fstat(fd, &stbuf) != 0)
49 exfat_error("failed to fstat `%s'", spec);
52 if (!S_ISBLK(stbuf.st_mode) && !S_ISREG(stbuf.st_mode))
55 exfat_error("`%s' is neither a block device, nor a regular file",
62 int exfat_close(int fd)
66 exfat_error("close failed");
72 void exfat_pread(int fd, void* buffer, size_t size, off_t offset)
74 if (pread(fd, buffer, size, offset) != size)
75 exfat_bug("failed to read %zu bytes from file at %"PRIu64, size,
79 void exfat_pwrite(int fd, const void* buffer, size_t size, off_t offset)
81 if (pwrite(fd, buffer, size, offset) != size)
82 exfat_bug("failed to write %zu bytes to file at %"PRIu64, size,
86 ssize_t exfat_generic_pread(const struct exfat* ef, struct exfat_node* node,
87 void* buffer, size_t size, off_t offset)
91 off_t lsize, loffset, remainder;
93 if (offset >= node->size)
98 cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
99 if (CLUSTER_INVALID(cluster))
101 exfat_error("got invalid cluster");
105 loffset = offset % CLUSTER_SIZE(*ef->sb);
106 remainder = MIN(size, node->size - offset);
107 while (remainder > 0)
109 if (CLUSTER_INVALID(cluster))
111 exfat_error("got invalid cluster");
114 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
115 exfat_pread(ef->fd, bufp, lsize, exfat_c2o(ef, cluster) + loffset);
119 cluster = exfat_next_cluster(ef, node, cluster);
121 if (!ef->ro && !ef->noatime)
122 exfat_update_atime(node);
123 return size - remainder;
126 ssize_t exfat_generic_pwrite(struct exfat* ef, struct exfat_node* node,
127 const void* buffer, size_t size, off_t offset)
130 const char* bufp = buffer;
131 off_t lsize, loffset, remainder;
133 if (offset + size > node->size)
135 int rc = exfat_truncate(ef, node, offset + size);
142 cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
143 if (CLUSTER_INVALID(cluster))
145 exfat_error("got invalid cluster");
149 loffset = offset % CLUSTER_SIZE(*ef->sb);
151 while (remainder > 0)
153 if (CLUSTER_INVALID(cluster))
155 exfat_error("got invalid cluster");
158 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
159 exfat_pwrite(ef->fd, bufp, lsize, exfat_c2o(ef, cluster) + loffset);
163 cluster = exfat_next_cluster(ef, node, cluster);
165 exfat_update_mtime(node);
166 return size - remainder;