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