1 /* 2 * A file accessor employing a rewritable memory area. 3 * 4 * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <stdio.h> 23 #include <string.h> 24 #include <sys/types.h> 25 #include <sys/stat.h> 26 27 #include <algorithm> 28 29 #include "block_file_accessor.h" 30 31 32 33 BlockFileAccessor::BlockFileAccessor(fileid_t fileid) 34 : Accessor(fileid), _fp(NULL) 35 { 36 _size = 0; 37 } 38 39 BlockFileAccessor::BlockFileAccessor(FILE *fp, fileid_t fileid) 40 : Accessor(fileid), _fp(fp) 41 { 42 /* Obtain the size of the file. */ 43 44 struct stat buf; 45 46 if (fstat(fileno(fp), &buf)) 47 { 48 _size = 0; 49 return; 50 } 51 52 /* Allocate memory. */ 53 54 _size = buf.st_size; 55 _data = (char *) malloc(_size); 56 57 if (_data == NULL) 58 return; 59 60 /* Load the file into memory and initialise the size. */ 61 62 _size = fread(_data, sizeof(char), _size, fp); 63 } 64 65 /* Return the size of the file. */ 66 67 offset_t BlockFileAccessor::get_size() 68 { 69 return _size; 70 } 71 72 /* Update the size of the file. */ 73 74 void BlockFileAccessor::set_size(offset_t size) 75 { 76 void *new_data = realloc(_data, size); 77 78 if (new_data != NULL) 79 { 80 _data = (char *) new_data; 81 82 if (size > _size) 83 memset(_data + _size, 0, size - _size); 84 85 _size = size; 86 } 87 } 88 89 /* Data transfer helper methods. */ 90 91 /* Fill the populated portion of a flexpage. */ 92 93 void BlockFileAccessor::fill_populated(Flexpage *flexpage) 94 { 95 offset_t filepos = flexpage->base_offset; 96 offset_t addr = flexpage->base_addr; 97 offset_t populated_size = std::min(flexpage->size, _size - filepos); 98 99 /* Tag the region with file state. */ 100 101 flexpage->region->fill(fileid, filepos); 102 103 /* Fill the region with file content. */ 104 105 memcpy((void *) addr, _data + filepos, populated_size); 106 107 /* Pad the flexpage with zero. */ 108 109 if (populated_size < flexpage->size) 110 memset((void *) (addr + populated_size), 0, flexpage->size - populated_size); 111 } 112 113 /* Flush the populated portion of a flexpage. */ 114 115 void BlockFileAccessor::flush_populated(Flexpage *flexpage) 116 { 117 offset_t filepos = flexpage->base_offset; 118 offset_t addr = flexpage->base_addr; 119 offset_t populated_size = std::min(flexpage->size, _size - filepos); 120 121 /* Remove the file state tag from the region. */ 122 123 flexpage->region->flush(); 124 125 /* Copy the populated region to the block memory. */ 126 127 memcpy((void *) (_data + filepos), (const void *) addr, populated_size); 128 } 129 130 // vim: tabstop=4 expandtab shiftwidth=4