]> git.sven.stormbind.net Git - sven/exfat-utils.git/blob - libexfat/io.c
Merge branch 'master' of git.sven.stormbind.net:/home/sven/www/sven_htdocs/git/exfat...
[sven/exfat-utils.git] / libexfat / io.c
1 /*
2         io.c (02.09.09)
3         exFAT file system implementation library.
4
5         Copyright (C) 2009, 2010  Andrew Nayenko
6
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.
11
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.
16
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/>.
19 */
20
21 #include "exfat.h"
22 #include <inttypes.h>
23 #include <sys/types.h>
24 #include <sys/uio.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #define __USE_UNIX98 /* for pread() in Linux */
28 #include <unistd.h>
29
30 #if _FILE_OFFSET_BITS != 64
31         #error You should define _FILE_OFFSET_BITS=64
32 #endif
33
34 int exfat_open(const char* spec, int ro)
35 {
36         int fd;
37         struct stat stbuf;
38
39         fd = open(spec, ro ? O_RDONLY : O_RDWR);
40         if (fd < 0)
41         {
42                 exfat_error("failed to open `%s' in read-%s mode", spec,
43                                 ro ? "only" : "write");
44                 return -1;
45         }
46         if (fstat(fd, &stbuf) != 0)
47         {
48                 close(fd);
49                 exfat_error("failed to fstat `%s'", spec);
50                 return -1;
51         }
52         if (!S_ISBLK(stbuf.st_mode) && !S_ISREG(stbuf.st_mode))
53         {
54                 close(fd);
55                 exfat_error("`%s' is neither a block device, nor a regular file",
56                                 spec);
57                 return -1;
58         }
59         return fd;
60 }
61
62 void exfat_read_raw(void* buffer, size_t size, off_t offset, int fd)
63 {
64         if (pread(fd, buffer, size, offset) != size)
65                 exfat_bug("failed to read %zu bytes from file at %"PRIu64, size,
66                                 (uint64_t) offset);
67 }
68
69 void exfat_write_raw(const void* buffer, size_t size, off_t offset, int fd)
70 {
71         if (pwrite(fd, buffer, size, offset) != size)
72                 exfat_bug("failed to write %zu bytes to file at %"PRIu64, size,
73                                 (uint64_t) offset);
74 }
75
76 ssize_t exfat_read(const struct exfat* ef, struct exfat_node* node,
77                 void* buffer, size_t size, off_t offset)
78 {
79         cluster_t cluster;
80         char* bufp = buffer;
81         off_t lsize, loffset, remainder;
82
83         if (offset >= node->size)
84                 return 0;
85         if (size == 0)
86                 return 0;
87
88         cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
89         if (CLUSTER_INVALID(cluster))
90         {
91                 exfat_error("got invalid cluster");
92                 return -1;
93         }
94
95         loffset = offset % CLUSTER_SIZE(*ef->sb);
96         remainder = MIN(size, node->size - offset);
97         while (remainder > 0)
98         {
99                 if (CLUSTER_INVALID(cluster))
100                 {
101                         exfat_error("got invalid cluster");
102                         return -1;
103                 }
104                 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
105                 exfat_read_raw(bufp, lsize, exfat_c2o(ef, cluster) + loffset, ef->fd);
106                 bufp += lsize;
107                 loffset = 0;
108                 remainder -= lsize;
109                 cluster = exfat_next_cluster(ef, node, cluster);
110         }
111         if (!ef->ro && !ef->noatime)
112                 exfat_update_atime(node);
113         return size - remainder;
114 }
115
116 ssize_t exfat_write(struct exfat* ef, struct exfat_node* node,
117                 const void* buffer, size_t size, off_t offset)
118 {
119         cluster_t cluster;
120         const char* bufp = buffer;
121         off_t lsize, loffset, remainder;
122
123         if (offset + size > node->size)
124         {
125                 int rc = exfat_truncate(ef, node, offset + size);
126                 if (rc != 0)
127                         return rc;
128         }
129         if (size == 0)
130                 return 0;
131
132         cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
133         if (CLUSTER_INVALID(cluster))
134         {
135                 exfat_error("got invalid cluster");
136                 return -1;
137         }
138
139         loffset = offset % CLUSTER_SIZE(*ef->sb);
140         remainder = size;
141         while (remainder > 0)
142         {
143                 if (CLUSTER_INVALID(cluster))
144                 {
145                         exfat_error("got invalid cluster");
146                         return -1;
147                 }
148                 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
149                 exfat_write_raw(bufp, lsize, exfat_c2o(ef, cluster) + loffset, ef->fd);
150                 bufp += lsize;
151                 loffset = 0;
152                 remainder -= lsize;
153                 cluster = exfat_next_cluster(ef, node, cluster);
154         }
155         exfat_update_mtime(node);
156         return size - remainder;
157 }