paul@93 | 1 | /* |
paul@93 | 2 | * A file accessor employing a rewritable memory area. |
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@50 | 22 | #include <stdio.h> |
paul@50 | 23 | #include <string.h> |
paul@50 | 24 | #include <sys/types.h> |
paul@50 | 25 | #include <sys/stat.h> |
paul@50 | 26 | |
paul@50 | 27 | #include <algorithm> |
paul@50 | 28 | |
paul@50 | 29 | #include "block_file_accessor.h" |
paul@50 | 30 | |
paul@50 | 31 | |
paul@50 | 32 | |
paul@143 | 33 | BlockFileAccessor::BlockFileAccessor(fileid_t fileid) |
paul@143 | 34 | : Accessor(fileid), _fp(NULL) |
paul@143 | 35 | { |
paul@143 | 36 | _size = 0; |
paul@143 | 37 | } |
paul@143 | 38 | |
paul@106 | 39 | BlockFileAccessor::BlockFileAccessor(FILE *fp, fileid_t fileid) |
paul@106 | 40 | : Accessor(fileid), _fp(fp) |
paul@50 | 41 | { |
paul@50 | 42 | /* Obtain the size of the file. */ |
paul@50 | 43 | |
paul@50 | 44 | struct stat buf; |
paul@50 | 45 | |
paul@106 | 46 | if (fstat(fileno(fp), &buf)) |
paul@50 | 47 | { |
paul@50 | 48 | _size = 0; |
paul@50 | 49 | return; |
paul@50 | 50 | } |
paul@50 | 51 | |
paul@50 | 52 | /* Allocate memory. */ |
paul@50 | 53 | |
paul@50 | 54 | _size = buf.st_size; |
paul@50 | 55 | _data = (char *) malloc(_size); |
paul@50 | 56 | |
paul@50 | 57 | if (_data == NULL) |
paul@50 | 58 | return; |
paul@50 | 59 | |
paul@50 | 60 | /* Load the file into memory and initialise the size. */ |
paul@50 | 61 | |
paul@50 | 62 | _size = fread(_data, sizeof(char), _size, fp); |
paul@50 | 63 | } |
paul@50 | 64 | |
paul@101 | 65 | /* Return the size of the file. */ |
paul@101 | 66 | |
paul@101 | 67 | offset_t BlockFileAccessor::get_size() |
paul@101 | 68 | { |
paul@101 | 69 | return _size; |
paul@101 | 70 | } |
paul@101 | 71 | |
paul@55 | 72 | /* Update the size of the file. */ |
paul@55 | 73 | |
paul@55 | 74 | void BlockFileAccessor::set_size(offset_t size) |
paul@55 | 75 | { |
paul@55 | 76 | void *new_data = realloc(_data, size); |
paul@55 | 77 | |
paul@55 | 78 | if (new_data != NULL) |
paul@55 | 79 | { |
paul@55 | 80 | _data = (char *) new_data; |
paul@55 | 81 | |
paul@55 | 82 | if (size > _size) |
paul@55 | 83 | memset(_data + _size, 0, size - _size); |
paul@55 | 84 | |
paul@101 | 85 | _size = size; |
paul@55 | 86 | } |
paul@55 | 87 | } |
paul@55 | 88 | |
paul@56 | 89 | /* Data transfer helper methods. */ |
paul@50 | 90 | |
paul@56 | 91 | /* Fill the populated portion of a flexpage. */ |
paul@56 | 92 | |
paul@56 | 93 | void BlockFileAccessor::fill_populated(Flexpage *flexpage) |
paul@50 | 94 | { |
paul@50 | 95 | offset_t filepos = flexpage->base_offset; |
paul@50 | 96 | offset_t addr = flexpage->base_addr; |
paul@56 | 97 | offset_t populated_size = std::min(flexpage->size, _size - filepos); |
paul@50 | 98 | |
paul@50 | 99 | /* Tag the region with file state. */ |
paul@50 | 100 | |
paul@50 | 101 | flexpage->region->fill(fileid, filepos); |
paul@50 | 102 | |
paul@50 | 103 | /* Fill the region with file content. */ |
paul@50 | 104 | |
paul@50 | 105 | memcpy((void *) addr, _data + filepos, populated_size); |
paul@50 | 106 | |
paul@50 | 107 | /* Pad the flexpage with zero. */ |
paul@50 | 108 | |
paul@50 | 109 | if (populated_size < flexpage->size) |
paul@50 | 110 | memset((void *) (addr + populated_size), 0, flexpage->size - populated_size); |
paul@50 | 111 | } |
paul@50 | 112 | |
paul@56 | 113 | /* Flush the populated portion of a flexpage. */ |
paul@55 | 114 | |
paul@56 | 115 | void BlockFileAccessor::flush_populated(Flexpage *flexpage) |
paul@56 | 116 | { |
paul@56 | 117 | offset_t filepos = flexpage->base_offset; |
paul@56 | 118 | offset_t addr = flexpage->base_addr; |
paul@56 | 119 | offset_t populated_size = std::min(flexpage->size, _size - filepos); |
paul@50 | 120 | |
paul@50 | 121 | /* Remove the file state tag from the region. */ |
paul@50 | 122 | |
paul@50 | 123 | flexpage->region->flush(); |
paul@50 | 124 | |
paul@50 | 125 | /* Copy the populated region to the block memory. */ |
paul@50 | 126 | |
paul@50 | 127 | memcpy((void *) (_data + filepos), (const void *) addr, populated_size); |
paul@50 | 128 | } |
paul@50 | 129 | |
paul@50 | 130 | // vim: tabstop=4 expandtab shiftwidth=4 |