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 /* Update the size of the file. */ 70 71 void BlockFileAccessor::set_size(offset_t size) 72 { 73 void *new_data = realloc(_data, size); 74 75 if (new_data != NULL) 76 { 77 _data = (char *) new_data; 78 79 if (size > _size) 80 memset(_data + _size, 0, size - _size); 81 82 Accessor::set_size(size); 83 } 84 } 85 86 /* Data transfer helper methods. */ 87 88 /* Fill the populated portion of a flexpage. */ 89 90 void BlockFileAccessor::fill_populated(Flexpage *flexpage) 91 { 92 offset_t filepos = flexpage->base_offset; 93 offset_t addr = flexpage->base_addr; 94 offset_t populated_size = std::min(flexpage->size, _size - filepos); 95 96 /* Tag the region with file state. */ 97 98 flexpage->region->fill(fileid, filepos); 99 100 /* Fill the region with file content. */ 101 102 memcpy((void *) addr, _data + filepos, populated_size); 103 104 /* Pad the flexpage with zero. */ 105 106 if (populated_size < flexpage->size) 107 memset((void *) (addr + populated_size), 0, flexpage->size - populated_size); 108 } 109 110 /* Flush the populated portion of a flexpage. */ 111 112 void BlockFileAccessor::flush_populated(Flexpage *flexpage) 113 { 114 offset_t filepos = flexpage->base_offset; 115 offset_t addr = flexpage->base_addr; 116 offset_t populated_size = std::min(flexpage->size, _size - filepos); 117 118 /* Remove the file state tag from the region. */ 119 120 flexpage->region->flush(); 121 122 /* Copy the populated region to the block memory. */ 123 124 memcpy((void *) (_data + filepos), (const void *) addr, populated_size); 125 } 126 127 // vim: tabstop=4 expandtab shiftwidth=4