]> git.sven.stormbind.net Git - sven/exfat-utils.git/blob - libexfat/io.c
Remove boilarplate from debian/rules
[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'", spec);
43                 return -1;
44         }
45         if (fstat(fd, &stbuf) != 0)
46         {
47                 close(fd);
48                 exfat_error("failed to fstat `%s'", spec);
49                 return -1;
50         }
51         if (!S_ISBLK(stbuf.st_mode) && !S_ISREG(stbuf.st_mode))
52         {
53                 close(fd);
54                 exfat_error("`%s' is neither a block device, nor a regular file",
55                                 spec);
56                 return -1;
57         }
58         return fd;
59 }
60
61 void exfat_read_raw(void* buffer, size_t size, off_t offset, int fd)
62 {
63         if (pread(fd, buffer, size, offset) != size)
64                 exfat_bug("failed to read %zu bytes from file at %"PRIu64, size,
65                                 (uint64_t) offset);
66 }
67
68 void exfat_write_raw(const void* buffer, size_t size, off_t offset, int fd)
69 {
70         if (pwrite(fd, buffer, size, offset) != size)
71                 exfat_bug("failed to write %zu bytes to file at %"PRIu64, size,
72                                 (uint64_t) offset);
73 }
74
75 ssize_t exfat_read(const struct exfat* ef, struct exfat_node* node,
76                 void* buffer, size_t size, off_t offset)
77 {
78         cluster_t cluster;
79         char* bufp = buffer;
80         off_t lsize, loffset, remainder;
81
82         if (offset >= node->size)
83                 return 0;
84         if (size == 0)
85                 return 0;
86
87         cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
88         if (CLUSTER_INVALID(cluster))
89         {
90                 exfat_error("got invalid cluster");
91                 return -1;
92         }
93
94         loffset = offset % CLUSTER_SIZE(*ef->sb);
95         remainder = MIN(size, node->size - offset);
96         while (remainder > 0)
97         {
98                 if (CLUSTER_INVALID(cluster))
99                 {
100                         exfat_error("got invalid cluster");
101                         return -1;
102                 }
103                 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
104                 exfat_read_raw(bufp, lsize, exfat_c2o(ef, cluster) + loffset, ef->fd);
105                 bufp += lsize;
106                 loffset = 0;
107                 remainder -= lsize;
108                 cluster = exfat_next_cluster(ef, node, cluster);
109         }
110         if (!ef->ro && !ef->noatime)
111                 exfat_update_atime(node);
112         return size - remainder;
113 }
114
115 ssize_t exfat_write(struct exfat* ef, struct exfat_node* node,
116                 const void* buffer, size_t size, off_t offset)
117 {
118         cluster_t cluster;
119         const char* bufp = buffer;
120         off_t lsize, loffset, remainder;
121
122         if (offset + size > node->size)
123         {
124                 int rc = exfat_truncate(ef, node, offset + size);
125                 if (rc != 0)
126                         return rc;
127         }
128         if (size == 0)
129                 return 0;
130
131         cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
132         if (CLUSTER_INVALID(cluster))
133         {
134                 exfat_error("got invalid cluster");
135                 return -1;
136         }
137
138         loffset = offset % CLUSTER_SIZE(*ef->sb);
139         remainder = size;
140         while (remainder > 0)
141         {
142                 if (CLUSTER_INVALID(cluster))
143                 {
144                         exfat_error("got invalid cluster");
145                         return -1;
146                 }
147                 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
148                 exfat_write_raw(bufp, lsize, exfat_c2o(ef, cluster) + loffset, ef->fd);
149                 bufp += lsize;
150                 loffset = 0;
151                 remainder -= lsize;
152                 cluster = exfat_next_cluster(ef, node, cluster);
153         }
154         exfat_update_mtime(node);
155         return size - remainder;
156 }