| 1 | /* |
| 2 | * This file is part of DisOrder |
| 3 | * Copyright (C) 2010 Richard Kettlewell |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | /** @file lib/hreader.c |
| 19 | * @brief Hands-off reader - read files without keeping them open |
| 20 | */ |
| 21 | #include <config.h> |
| 22 | #include "hreader.h" |
| 23 | #include "mem.h" |
| 24 | #include <string.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <errno.h> |
| 28 | #include <unistd.h> |
| 29 | |
| 30 | static int hreader_fill(struct hreader *h, off_t offset); |
| 31 | |
| 32 | int hreader_init(const char *path, struct hreader *h) { |
| 33 | struct stat sb; |
| 34 | if(stat(path, &sb) < 0) |
| 35 | return -1; |
| 36 | memset(h, 0, sizeof *h); |
| 37 | h->path = xstrdup(path); |
| 38 | h->size = sb.st_size; |
| 39 | h->bufsize = 65536; |
| 40 | h->buffer = xmalloc_noptr(h->bufsize); |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | void hreader_close(struct hreader *h) { |
| 45 | xfree(h->path); |
| 46 | xfree(h->buffer); |
| 47 | } |
| 48 | |
| 49 | int hreader_read(struct hreader *h, void *buffer, size_t n) { |
| 50 | int r = hreader_pread(h, buffer, n, h->read_offset); |
| 51 | if(r > 0) |
| 52 | h->read_offset += r; |
| 53 | return r; |
| 54 | } |
| 55 | |
| 56 | int hreader_pread(struct hreader *h, void *buffer, size_t n, off_t offset) { |
| 57 | size_t bytes_read = 0; |
| 58 | |
| 59 | while(bytes_read < n) { |
| 60 | // If the desired byte range is outside the file, fetch new contents |
| 61 | if(offset < h->buf_offset || offset >= h->buf_offset + (off_t)h->bytes) { |
| 62 | int r = hreader_fill(h, offset); |
| 63 | if(r < 0) |
| 64 | return -1; /* disaster! */ |
| 65 | else if(r == 0) |
| 66 | break; /* end of file */ |
| 67 | } |
| 68 | // Figure out how much we can read this time round |
| 69 | size_t left = h->bytes - (offset - h->buf_offset); |
| 70 | // Truncate the read if we don't want that much |
| 71 | if(left > (n - bytes_read)) |
| 72 | left = n - bytes_read; |
| 73 | memcpy((char *)buffer + bytes_read, |
| 74 | h->buffer + (offset - h->buf_offset), |
| 75 | left); |
| 76 | offset += left; |
| 77 | bytes_read += left; |
| 78 | } |
| 79 | return bytes_read; |
| 80 | } |
| 81 | |
| 82 | static int hreader_fill(struct hreader *h, off_t offset) { |
| 83 | int fd = open(h->path, O_RDONLY); |
| 84 | if(fd < 0) |
| 85 | return -1; |
| 86 | int n = pread(fd, h->buffer, h->bufsize, offset); |
| 87 | close(fd); |
| 88 | if(n < 0) |
| 89 | return -1; |
| 90 | h->buf_offset = offset; |
| 91 | h->bytes = n; |
| 92 | return n; |
| 93 | } |
| 94 | |
| 95 | off_t hreader_seek(struct hreader *h, off_t offset, int whence) { |
| 96 | switch(whence) { |
| 97 | case SEEK_SET: break; |
| 98 | case SEEK_CUR: offset += h->read_offset; break; |
| 99 | case SEEK_END: offset += h->size; break; |
| 100 | default: einval: errno = EINVAL; return -1; |
| 101 | } |
| 102 | if(offset < 0) goto einval; |
| 103 | h->read_offset = offset; |
| 104 | return offset; |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | Local Variables: |
| 109 | c-basic-offset:2 |
| 110 | comment-column:40 |
| 111 | fill-column:79 |
| 112 | indent-tabs-mode:nil |
| 113 | End: |
| 114 | */ |