1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/host_file_accessor.cc Wed Feb 03 00:15:16 2021 +0100
1.3 @@ -0,0 +1,68 @@
1.4 +#include <stdio.h>
1.5 +#include <string.h>
1.6 +#include <sys/types.h>
1.7 +#include <sys/stat.h>
1.8 +
1.9 +#include "host_file_accessor.h"
1.10 +
1.11 +HostFileAccessor::HostFileAccessor(const char *path, fileid_t fileid)
1.12 +: Accessor(fileid), _path(path)
1.13 +{
1.14 + /* Initialise the size of the file. */
1.15 +
1.16 + struct stat buf;
1.17 +
1.18 + if (!stat(_path, &buf))
1.19 + _size = buf.st_size;
1.20 + else
1.21 + _size = 0;
1.22 +}
1.23 +
1.24 +/* Perform any closing operation on the file. */
1.25 +
1.26 +void HostFileAccessor::close()
1.27 +{
1.28 + fclose(_fp);
1.29 +}
1.30 +
1.31 +/* Perform any opening operation on the file. */
1.32 +
1.33 +void HostFileAccessor::open()
1.34 +{
1.35 + _fp = fopen(_path, "r");
1.36 +}
1.37 +
1.38 +/* Data transfer methods. */
1.39 +
1.40 +void HostFileAccessor::fill(Flexpage *flexpage)
1.41 +{
1.42 + /* Obtain the offset and seek in the file. */
1.43 +
1.44 + offset_t filepos = flexpage->base_offset;
1.45 +
1.46 + fseek(_fp, filepos, SEEK_SET);
1.47 +
1.48 + /* Tag the region with file state. */
1.49 +
1.50 + flexpage->region->fill(fileid, filepos);
1.51 +
1.52 + /* Fill the region with file content. */
1.53 +
1.54 + offset_t addr = flexpage->base_addr;
1.55 +
1.56 + size_t nread = fread((void *) addr, sizeof(char), flexpage->size, _fp);
1.57 +
1.58 + /* Pad the flexpage with zero. */
1.59 +
1.60 + memset((void *) (addr + nread), 0, flexpage->size - nread);
1.61 +}
1.62 +
1.63 +void HostFileAccessor::flush(Flexpage *flexpage)
1.64 +{
1.65 + /* NOTE: A real flush operation would store the memory content and update
1.66 + the file size, if appropriate. */
1.67 +
1.68 + flexpage->region->flush();
1.69 +}
1.70 +
1.71 +// vim: tabstop=4 expandtab shiftwidth=4