]> git.sven.stormbind.net Git - sven/exfat-utils.git/blob - libexfat/io.c
releasing package exfat-utils version 1.3.0-2
[sven/exfat-utils.git] / libexfat / io.c
1 /*
2         io.c (02.09.09)
3         exFAT file system implementation library.
4
5         Free exFAT implementation.
6         Copyright (C) 2010-2018  Andrew Nayenko
7
8         This program is free software; you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation, either version 2 of the License, or
11         (at your option) any later version.
12
13         This program is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License along
19         with this program; if not, write to the Free Software Foundation, Inc.,
20         51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #include "exfat.h"
24 #include <inttypes.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <errno.h>
31 #if defined(__APPLE__)
32 #include <sys/disk.h>
33 #elif defined(__OpenBSD__)
34 #include <sys/param.h>
35 #include <sys/disklabel.h>
36 #include <sys/dkio.h>
37 #include <sys/ioctl.h>
38 #elif __linux__
39 #include <sys/mount.h>
40 #endif
41 #ifdef USE_UBLIO
42 #include <sys/uio.h>
43 #include <ublio.h>
44 #endif
45
46 struct exfat_dev
47 {
48         int fd;
49         enum exfat_mode mode;
50         off_t size; /* in bytes */
51 #ifdef USE_UBLIO
52         off_t pos;
53         ublio_filehandle_t ufh;
54 #endif
55 };
56
57 static bool is_open(int fd)
58 {
59         return fcntl(fd, F_GETFD) != -1;
60 }
61
62 static int open_ro(const char* spec)
63 {
64         return open(spec, O_RDONLY);
65 }
66
67 static int open_rw(const char* spec)
68 {
69         int fd = open(spec, O_RDWR);
70 #ifdef __linux__
71         int ro = 0;
72
73         /*
74            This ioctl is needed because after "blockdev --setro" kernel still
75            allows to open the device in read-write mode but fails writes.
76         */
77         if (fd != -1 && ioctl(fd, BLKROGET, &ro) == 0 && ro)
78         {
79                 close(fd);
80                 errno = EROFS;
81                 return -1;
82         }
83 #endif
84         return fd;
85 }
86
87 struct exfat_dev* exfat_open(const char* spec, enum exfat_mode mode)
88 {
89         struct exfat_dev* dev;
90         struct stat stbuf;
91 #ifdef USE_UBLIO
92         struct ublio_param up;
93 #endif
94
95         /* The system allocates file descriptors sequentially. If we have been
96            started with stdin (0), stdout (1) or stderr (2) closed, the system
97            will give us descriptor 0, 1 or 2 later when we open block device,
98            FUSE communication pipe, etc. As a result, functions using stdin,
99            stdout or stderr will actually work with a different thing and can
100            corrupt it. Protect descriptors 0, 1 and 2 from such misuse. */
101         while (!is_open(STDIN_FILENO)
102                 || !is_open(STDOUT_FILENO)
103                 || !is_open(STDERR_FILENO))
104         {
105                 /* we don't need those descriptors, let them leak */
106                 if (open("/dev/null", O_RDWR) == -1)
107                 {
108                         exfat_error("failed to open /dev/null");
109                         return NULL;
110                 }
111         }
112
113         dev = malloc(sizeof(struct exfat_dev));
114         if (dev == NULL)
115         {
116                 exfat_error("failed to allocate memory for device structure");
117                 return NULL;
118         }
119
120         switch (mode)
121         {
122         case EXFAT_MODE_RO:
123                 dev->fd = open_ro(spec);
124                 if (dev->fd == -1)
125                 {
126                         free(dev);
127                         exfat_error("failed to open '%s' in read-only mode: %s", spec,
128                                         strerror(errno));
129                         return NULL;
130                 }
131                 dev->mode = EXFAT_MODE_RO;
132                 break;
133         case EXFAT_MODE_RW:
134                 dev->fd = open_rw(spec);
135                 if (dev->fd == -1)
136                 {
137                         free(dev);
138                         exfat_error("failed to open '%s' in read-write mode: %s", spec,
139                                         strerror(errno));
140                         return NULL;
141                 }
142                 dev->mode = EXFAT_MODE_RW;
143                 break;
144         case EXFAT_MODE_ANY:
145                 dev->fd = open_rw(spec);
146                 if (dev->fd != -1)
147                 {
148                         dev->mode = EXFAT_MODE_RW;
149                         break;
150                 }
151                 dev->fd = open_ro(spec);
152                 if (dev->fd != -1)
153                 {
154                         dev->mode = EXFAT_MODE_RO;
155                         exfat_warn("'%s' is write-protected, mounting read-only", spec);
156                         break;
157                 }
158                 free(dev);
159                 exfat_error("failed to open '%s': %s", spec, strerror(errno));
160                 return NULL;
161         }
162
163         if (fstat(dev->fd, &stbuf) != 0)
164         {
165                 close(dev->fd);
166                 free(dev);
167                 exfat_error("failed to fstat '%s'", spec);
168                 return NULL;
169         }
170         if (!S_ISBLK(stbuf.st_mode) &&
171                 !S_ISCHR(stbuf.st_mode) &&
172                 !S_ISREG(stbuf.st_mode))
173         {
174                 close(dev->fd);
175                 free(dev);
176                 exfat_error("'%s' is neither a device, nor a regular file", spec);
177                 return NULL;
178         }
179
180 #if defined(__APPLE__)
181         if (!S_ISREG(stbuf.st_mode))
182         {
183                 uint32_t block_size = 0;
184                 uint64_t blocks = 0;
185
186                 if (ioctl(dev->fd, DKIOCGETBLOCKSIZE, &block_size) != 0)
187                 {
188                         close(dev->fd);
189                         free(dev);
190                         exfat_error("failed to get block size");
191                         return NULL;
192                 }
193                 if (ioctl(dev->fd, DKIOCGETBLOCKCOUNT, &blocks) != 0)
194                 {
195                         close(dev->fd);
196                         free(dev);
197                         exfat_error("failed to get blocks count");
198                         return NULL;
199                 }
200                 dev->size = blocks * block_size;
201         }
202         else
203 #elif defined(__OpenBSD__)
204         if (!S_ISREG(stbuf.st_mode))
205         {
206                 struct disklabel lab;
207                 struct partition* pp;
208                 char* partition;
209
210                 if (ioctl(dev->fd, DIOCGDINFO, &lab) == -1)
211                 {
212                         close(dev->fd);
213                         free(dev);
214                         exfat_error("failed to get disklabel");
215                         return NULL;
216                 }
217
218                 /* Don't need to check that partition letter is valid as we won't get
219                    this far otherwise. */
220                 partition = strchr(spec, '\0') - 1;
221                 pp = &(lab.d_partitions[*partition - 'a']);
222                 dev->size = DL_GETPSIZE(pp) * lab.d_secsize;
223
224                 if (pp->p_fstype != FS_NTFS)
225                         exfat_warn("partition type is not 0x07 (NTFS/exFAT); "
226                                         "you can fix this with fdisk(8)");
227         }
228         else
229 #endif
230         {
231                 /* works for Linux, FreeBSD, Solaris */
232                 dev->size = exfat_seek(dev, 0, SEEK_END);
233                 if (dev->size <= 0)
234                 {
235                         close(dev->fd);
236                         free(dev);
237                         exfat_error("failed to get size of '%s'", spec);
238                         return NULL;
239                 }
240                 if (exfat_seek(dev, 0, SEEK_SET) == -1)
241                 {
242                         close(dev->fd);
243                         free(dev);
244                         exfat_error("failed to seek to the beginning of '%s'", spec);
245                         return NULL;
246                 }
247         }
248
249 #ifdef USE_UBLIO
250         memset(&up, 0, sizeof(struct ublio_param));
251         up.up_blocksize = 256 * 1024;
252         up.up_items = 64;
253         up.up_grace = 32;
254         up.up_priv = &dev->fd;
255
256         dev->pos = 0;
257         dev->ufh = ublio_open(&up);
258         if (dev->ufh == NULL)
259         {
260                 close(dev->fd);
261                 free(dev);
262                 exfat_error("failed to initialize ublio");
263                 return NULL;
264         }
265 #endif
266
267         return dev;
268 }
269
270 int exfat_close(struct exfat_dev* dev)
271 {
272         int rc = 0;
273
274 #ifdef USE_UBLIO
275         if (ublio_close(dev->ufh) != 0)
276         {
277                 exfat_error("failed to close ublio");
278                 rc = -EIO;
279         }
280 #endif
281         if (close(dev->fd) != 0)
282         {
283                 exfat_error("failed to close device: %s", strerror(errno));
284                 rc = -EIO;
285         }
286         free(dev);
287         return rc;
288 }
289
290 int exfat_fsync(struct exfat_dev* dev)
291 {
292         int rc = 0;
293
294 #ifdef USE_UBLIO
295         if (ublio_fsync(dev->ufh) != 0)
296         {
297                 exfat_error("ublio fsync failed");
298                 rc = -EIO;
299         }
300 #endif
301         if (fsync(dev->fd) != 0)
302         {
303                 exfat_error("fsync failed: %s", strerror(errno));
304                 rc = -EIO;
305         }
306         return rc;
307 }
308
309 enum exfat_mode exfat_get_mode(const struct exfat_dev* dev)
310 {
311         return dev->mode;
312 }
313
314 off_t exfat_get_size(const struct exfat_dev* dev)
315 {
316         return dev->size;
317 }
318
319 off_t exfat_seek(struct exfat_dev* dev, off_t offset, int whence)
320 {
321 #ifdef USE_UBLIO
322         /* XXX SEEK_CUR will be handled incorrectly */
323         return dev->pos = lseek(dev->fd, offset, whence);
324 #else
325         return lseek(dev->fd, offset, whence);
326 #endif
327 }
328
329 ssize_t exfat_read(struct exfat_dev* dev, void* buffer, size_t size)
330 {
331 #ifdef USE_UBLIO
332         ssize_t result = ublio_pread(dev->ufh, buffer, size, dev->pos);
333         if (result >= 0)
334                 dev->pos += size;
335         return result;
336 #else
337         return read(dev->fd, buffer, size);
338 #endif
339 }
340
341 ssize_t exfat_write(struct exfat_dev* dev, const void* buffer, size_t size)
342 {
343 #ifdef USE_UBLIO
344         ssize_t result = ublio_pwrite(dev->ufh, buffer, size, dev->pos);
345         if (result >= 0)
346                 dev->pos += size;
347         return result;
348 #else
349         return write(dev->fd, buffer, size);
350 #endif
351 }
352
353 ssize_t exfat_pread(struct exfat_dev* dev, void* buffer, size_t size,
354                 off_t offset)
355 {
356 #ifdef USE_UBLIO
357         return ublio_pread(dev->ufh, buffer, size, offset);
358 #else
359         return pread(dev->fd, buffer, size, offset);
360 #endif
361 }
362
363 ssize_t exfat_pwrite(struct exfat_dev* dev, const void* buffer, size_t size,
364                 off_t offset)
365 {
366 #ifdef USE_UBLIO
367         return ublio_pwrite(dev->ufh, buffer, size, offset);
368 #else
369         return pwrite(dev->fd, buffer, size, offset);
370 #endif
371 }
372
373 ssize_t exfat_generic_pread(const struct exfat* ef, struct exfat_node* node,
374                 void* buffer, size_t size, off_t offset)
375 {
376         cluster_t cluster;
377         char* bufp = buffer;
378         off_t lsize, loffset, remainder;
379
380         if (offset >= node->size)
381                 return 0;
382         if (size == 0)
383                 return 0;
384
385         cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
386         if (CLUSTER_INVALID(*ef->sb, cluster))
387         {
388                 exfat_error("invalid cluster 0x%x while reading", cluster);
389                 return -EIO;
390         }
391
392         loffset = offset % CLUSTER_SIZE(*ef->sb);
393         remainder = MIN(size, node->size - offset);
394         while (remainder > 0)
395         {
396                 if (CLUSTER_INVALID(*ef->sb, cluster))
397                 {
398                         exfat_error("invalid cluster 0x%x while reading", cluster);
399                         return -EIO;
400                 }
401                 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
402                 if (exfat_pread(ef->dev, bufp, lsize,
403                                         exfat_c2o(ef, cluster) + loffset) < 0)
404                 {
405                         exfat_error("failed to read cluster %#x", cluster);
406                         return -EIO;
407                 }
408                 bufp += lsize;
409                 loffset = 0;
410                 remainder -= lsize;
411                 cluster = exfat_next_cluster(ef, node, cluster);
412         }
413         if (!(node->attrib & EXFAT_ATTRIB_DIR) && !ef->ro && !ef->noatime)
414                 exfat_update_atime(node);
415         return MIN(size, node->size - offset) - remainder;
416 }
417
418 ssize_t exfat_generic_pwrite(struct exfat* ef, struct exfat_node* node,
419                 const void* buffer, size_t size, off_t offset)
420 {
421         int rc;
422         cluster_t cluster;
423         const char* bufp = buffer;
424         off_t lsize, loffset, remainder;
425
426         if (offset > node->size)
427         {
428                 rc = exfat_truncate(ef, node, offset, true);
429                 if (rc != 0)
430                         return rc;
431         }
432         if (offset + size > node->size)
433         {
434                 rc = exfat_truncate(ef, node, offset + size, false);
435                 if (rc != 0)
436                         return rc;
437         }
438         if (size == 0)
439                 return 0;
440
441         cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
442         if (CLUSTER_INVALID(*ef->sb, cluster))
443         {
444                 exfat_error("invalid cluster 0x%x while writing", cluster);
445                 return -EIO;
446         }
447
448         loffset = offset % CLUSTER_SIZE(*ef->sb);
449         remainder = size;
450         while (remainder > 0)
451         {
452                 if (CLUSTER_INVALID(*ef->sb, cluster))
453                 {
454                         exfat_error("invalid cluster 0x%x while writing", cluster);
455                         return -EIO;
456                 }
457                 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
458                 if (exfat_pwrite(ef->dev, bufp, lsize,
459                                 exfat_c2o(ef, cluster) + loffset) < 0)
460                 {
461                         exfat_error("failed to write cluster %#x", cluster);
462                         return -EIO;
463                 }
464                 bufp += lsize;
465                 loffset = 0;
466                 remainder -= lsize;
467                 cluster = exfat_next_cluster(ef, node, cluster);
468         }
469         if (!(node->attrib & EXFAT_ATTRIB_DIR))
470                 /* directory's mtime should be updated by the caller only when it
471                    creates or removes something in this directory */
472                 exfat_update_mtime(node);
473         return size - remainder;
474 }