Skip to content

Commit

Permalink
Makes RamDisk remember where EOF is. (#677)
Browse files Browse the repository at this point in the history
For a read-write RamDisk, this PR will remember what is the highest offset
that was ever written into that file. When reading beyond that offset, an EOF (zero length read) is returned.
Also adds support for O_TRUNC during open, which forgets about the data that was
written.
  • Loading branch information
balazsracz authored Nov 19, 2022
1 parent 3f124a2 commit 970f905
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions src/freertos_drivers/common/RamDisk.hxx
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@
#ifndef _FREERTOS_DRIVERS_COMMON_RAMDISK_HXX_
#define _FREERTOS_DRIVERS_COMMON_RAMDISK_HXX_

#include <fcntl.h>
#include "Devtab.hxx"

/// A simple device driver that reads/write data from a block of memory in RAM.
@@ -70,13 +71,24 @@ private:
void disable() OVERRIDE {}
void flush_buffers() OVERRIDE {}

/** Open method */
int open(File *file, const char * name, int flags, int mode) OVERRIDE
{
Node::open(file, name, flags, mode);
if ((flags & O_TRUNC) && !readOnly_)
{
actualSize_ = 0;
}
return 0;
}

ssize_t read(File *file, void *buf, size_t count) OVERRIDE
{
if (file->offset >= size_)
if (file->offset >= file_size())
{
return 0;
}
size_t left = size_ - file->offset;
size_t left = file_size() - file->offset;
count = std::min(count, left);
memcpy(buf, data_ + file->offset, count);
file->offset += count;
@@ -97,12 +109,33 @@ private:
count = std::min(count, left);
memcpy(data_ + file->offset, buf, count);
file->offset += count;
if (file->offset > (off_t)actualSize_)
{
actualSize_ = file->offset;
}
return count;
}

protected:
/// @return the file end offset. This is either the size presented at the
/// constructor for read-only files (that are filled with data when this
/// device gets instantiated), or the actual bytes written.
off_t file_size()
{
if (readOnly_)
{
return size_;
}
else
{
return actualSize_;
}
}

/// Pointer to data content.
uint8_t *data_;
/// What's the larget file offset that we received an actual write for.
unsigned actualSize_ = 0;
/// How many bytes we are exporting.
unsigned size_ : 30;
/// 1 ifreadonly file.

0 comments on commit 970f905

Please sign in to comment.