1.1 --- a/files/block_file_accessor.cc Tue Apr 13 00:03:18 2021 +0200
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,127 +0,0 @@
1.4 -/*
1.5 - * A file accessor employing a rewritable memory area.
1.6 - *
1.7 - * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk>
1.8 - *
1.9 - * This program is free software; you can redistribute it and/or
1.10 - * modify it under the terms of the GNU General Public License as
1.11 - * published by the Free Software Foundation; either version 2 of
1.12 - * the License, or (at your option) any later version.
1.13 - *
1.14 - * This program is distributed in the hope that it will be useful,
1.15 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 - * GNU General Public License for more details.
1.18 - *
1.19 - * You should have received a copy of the GNU General Public License
1.20 - * along with this program; if not, write to the Free Software
1.21 - * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.22 - * Boston, MA 02110-1301, USA
1.23 - */
1.24 -
1.25 -#include <stdio.h>
1.26 -#include <string.h>
1.27 -#include <sys/types.h>
1.28 -#include <sys/stat.h>
1.29 -
1.30 -#include <algorithm>
1.31 -
1.32 -#include "block_file_accessor.h"
1.33 -
1.34 -
1.35 -
1.36 -BlockFileAccessor::BlockFileAccessor(const char *path, fileid_t fileid)
1.37 -: Accessor(fileid)
1.38 -{
1.39 - /* Obtain the size of the file. */
1.40 -
1.41 - struct stat buf;
1.42 -
1.43 - if (stat(path, &buf))
1.44 - {
1.45 - _size = 0;
1.46 - return;
1.47 - }
1.48 -
1.49 - /* Allocate memory. */
1.50 -
1.51 - _size = buf.st_size;
1.52 - _data = (char *) malloc(_size);
1.53 -
1.54 - if (_data == NULL)
1.55 - return;
1.56 -
1.57 - /* Load the file into memory and initialise the size. */
1.58 -
1.59 - FILE *fp = fopen(path, "r");
1.60 -
1.61 - if (fp == NULL)
1.62 - {
1.63 - free(_data);
1.64 - return;
1.65 - }
1.66 -
1.67 - _size = fread(_data, sizeof(char), _size, fp);
1.68 -
1.69 - fclose(fp);
1.70 -}
1.71 -
1.72 -/* Update the size of the file. */
1.73 -
1.74 -void BlockFileAccessor::set_size(offset_t size)
1.75 -{
1.76 - void *new_data = realloc(_data, size);
1.77 -
1.78 - if (new_data != NULL)
1.79 - {
1.80 - _data = (char *) new_data;
1.81 -
1.82 - if (size > _size)
1.83 - memset(_data + _size, 0, size - _size);
1.84 -
1.85 - Accessor::set_size(size);
1.86 - }
1.87 -}
1.88 -
1.89 -/* Data transfer helper methods. */
1.90 -
1.91 -/* Fill the populated portion of a flexpage. */
1.92 -
1.93 -void BlockFileAccessor::fill_populated(Flexpage *flexpage)
1.94 -{
1.95 - offset_t filepos = flexpage->base_offset;
1.96 - offset_t addr = flexpage->base_addr;
1.97 - offset_t populated_size = std::min(flexpage->size, _size - filepos);
1.98 -
1.99 - /* Tag the region with file state. */
1.100 -
1.101 - flexpage->region->fill(fileid, filepos);
1.102 -
1.103 - /* Fill the region with file content. */
1.104 -
1.105 - memcpy((void *) addr, _data + filepos, populated_size);
1.106 -
1.107 - /* Pad the flexpage with zero. */
1.108 -
1.109 - if (populated_size < flexpage->size)
1.110 - memset((void *) (addr + populated_size), 0, flexpage->size - populated_size);
1.111 -}
1.112 -
1.113 -/* Flush the populated portion of a flexpage. */
1.114 -
1.115 -void BlockFileAccessor::flush_populated(Flexpage *flexpage)
1.116 -{
1.117 - offset_t filepos = flexpage->base_offset;
1.118 - offset_t addr = flexpage->base_addr;
1.119 - offset_t populated_size = std::min(flexpage->size, _size - filepos);
1.120 -
1.121 - /* Remove the file state tag from the region. */
1.122 -
1.123 - flexpage->region->flush();
1.124 -
1.125 - /* Copy the populated region to the block memory. */
1.126 -
1.127 - memcpy((void *) (_data + filepos), (const void *) addr, populated_size);
1.128 -}
1.129 -
1.130 -// vim: tabstop=4 expandtab shiftwidth=4