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/>.
21 #define _XOPEN_SOURCE 500 /* for pread() and pwrite() in Linux */
24 #include <sys/types.h>
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 int exfat_fsync(int fd)
76 exfat_error("fsync failed");
82 void exfat_pread(int fd, void* buffer, size_t size, off_t offset)
84 if (pread(fd, buffer, size, offset) != size)
85 exfat_bug("failed to read %zu bytes from file at %"PRIu64, size,
89 void exfat_pwrite(int fd, const void* buffer, size_t size, off_t offset)
91 if (pwrite(fd, buffer, size, offset) != size)
92 exfat_bug("failed to write %zu bytes to file at %"PRIu64, size,
96 ssize_t exfat_generic_pread(const struct exfat* ef, struct exfat_node* node,
97 void* buffer, size_t size, off_t offset)
101 off_t lsize, loffset, remainder;
103 if (offset >= node->size)
108 cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
109 if (CLUSTER_INVALID(cluster))
111 exfat_error("got invalid cluster");
115 loffset = offset % CLUSTER_SIZE(*ef->sb);
116 remainder = MIN(size, node->size - offset);
117 while (remainder > 0)
119 if (CLUSTER_INVALID(cluster))
121 exfat_error("got invalid cluster");
124 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
125 exfat_pread(ef->fd, bufp, lsize, exfat_c2o(ef, cluster) + loffset);
129 cluster = exfat_next_cluster(ef, node, cluster);
131 if (!ef->ro && !ef->noatime)
132 exfat_update_atime(node);
133 return size - remainder;
136 ssize_t exfat_generic_pwrite(struct exfat* ef, struct exfat_node* node,
137 const void* buffer, size_t size, off_t offset)
140 const char* bufp = buffer;
141 off_t lsize, loffset, remainder;
143 if (offset + size > node->size)
145 int rc = exfat_truncate(ef, node, offset + size);
152 cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
153 if (CLUSTER_INVALID(cluster))
155 exfat_error("got invalid cluster");
159 loffset = offset % CLUSTER_SIZE(*ef->sb);
161 while (remainder > 0)
163 if (CLUSTER_INVALID(cluster))
165 exfat_error("got invalid cluster");
168 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
169 exfat_pwrite(ef->fd, bufp, lsize, exfat_c2o(ef, cluster) + loffset);
173 cluster = exfat_next_cluster(ef, node, cluster);
175 exfat_update_mtime(node);
176 return size - remainder;