paul@93 | 1 | /* |
paul@93 | 2 | * A file accessor employing a "host" file provided via the C library. |
paul@93 | 3 | * |
paul@93 | 4 | * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk> |
paul@93 | 5 | * |
paul@93 | 6 | * This program is free software; you can redistribute it and/or |
paul@93 | 7 | * modify it under the terms of the GNU General Public License as |
paul@93 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@93 | 9 | * the License, or (at your option) any later version. |
paul@93 | 10 | * |
paul@93 | 11 | * This program is distributed in the hope that it will be useful, |
paul@93 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@93 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@93 | 14 | * GNU General Public License for more details. |
paul@93 | 15 | * |
paul@93 | 16 | * You should have received a copy of the GNU General Public License |
paul@93 | 17 | * along with this program; if not, write to the Free Software |
paul@93 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@93 | 19 | * Boston, MA 02110-1301, USA |
paul@93 | 20 | */ |
paul@93 | 21 | |
paul@12 | 22 | #include <stdio.h> |
paul@12 | 23 | #include <string.h> |
paul@12 | 24 | #include <sys/types.h> |
paul@12 | 25 | #include <sys/stat.h> |
paul@12 | 26 | |
paul@12 | 27 | #include "host_file_accessor.h" |
paul@12 | 28 | |
paul@12 | 29 | HostFileAccessor::HostFileAccessor(const char *path, fileid_t fileid) |
paul@12 | 30 | : Accessor(fileid), _path(path) |
paul@12 | 31 | { |
paul@12 | 32 | /* Initialise the size of the file. */ |
paul@12 | 33 | |
paul@12 | 34 | struct stat buf; |
paul@12 | 35 | |
paul@12 | 36 | if (!stat(_path, &buf)) |
paul@12 | 37 | _size = buf.st_size; |
paul@12 | 38 | else |
paul@12 | 39 | _size = 0; |
paul@12 | 40 | } |
paul@12 | 41 | |
paul@101 | 42 | /* Return the size of the file. */ |
paul@101 | 43 | |
paul@101 | 44 | offset_t HostFileAccessor::get_size() |
paul@101 | 45 | { |
paul@101 | 46 | return _size; |
paul@101 | 47 | } |
paul@101 | 48 | |
paul@101 | 49 | /* Update the size of the file. */ |
paul@101 | 50 | |
paul@101 | 51 | void HostFileAccessor::set_size(offset_t size) |
paul@101 | 52 | { |
paul@101 | 53 | _size = size; |
paul@101 | 54 | } |
paul@101 | 55 | |
paul@12 | 56 | /* Perform any closing operation on the file. */ |
paul@12 | 57 | |
paul@12 | 58 | void HostFileAccessor::close() |
paul@12 | 59 | { |
paul@12 | 60 | fclose(_fp); |
paul@12 | 61 | } |
paul@12 | 62 | |
paul@12 | 63 | /* Perform any opening operation on the file. */ |
paul@12 | 64 | |
paul@12 | 65 | void HostFileAccessor::open() |
paul@12 | 66 | { |
paul@12 | 67 | _fp = fopen(_path, "r"); |
paul@12 | 68 | } |
paul@12 | 69 | |
paul@56 | 70 | /* Data transfer helper methods. */ |
paul@12 | 71 | |
paul@56 | 72 | void HostFileAccessor::fill_populated(Flexpage *flexpage) |
paul@12 | 73 | { |
paul@56 | 74 | offset_t filepos = flexpage->base_offset; |
paul@56 | 75 | offset_t addr = flexpage->base_addr; |
paul@12 | 76 | |
paul@56 | 77 | /* Seek to the offset in the file. */ |
paul@12 | 78 | |
paul@12 | 79 | fseek(_fp, filepos, SEEK_SET); |
paul@12 | 80 | |
paul@12 | 81 | /* Tag the region with file state. */ |
paul@12 | 82 | |
paul@12 | 83 | flexpage->region->fill(fileid, filepos); |
paul@12 | 84 | |
paul@12 | 85 | /* Fill the region with file content. */ |
paul@12 | 86 | |
paul@12 | 87 | size_t nread = fread((void *) addr, sizeof(char), flexpage->size, _fp); |
paul@12 | 88 | |
paul@12 | 89 | /* Pad the flexpage with zero. */ |
paul@12 | 90 | |
paul@12 | 91 | memset((void *) (addr + nread), 0, flexpage->size - nread); |
paul@12 | 92 | } |
paul@12 | 93 | |
paul@56 | 94 | void HostFileAccessor::flush_populated(Flexpage *flexpage) |
paul@12 | 95 | { |
paul@12 | 96 | flexpage->region->flush(); |
paul@12 | 97 | } |
paul@12 | 98 | |
paul@12 | 99 | // vim: tabstop=4 expandtab shiftwidth=4 |