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