Commit d37e8885 authored by Alessandro Rubini's avatar Alessandro Rubini

sdbfs/lib: add write method; misc cleanups

Signed-off-by: Alessandro Rubini's avatarAlessandro Rubini <rubini@gnudd.com>
parent 54db5f20
/*
* Copyright (C) 2012 CERN (www.cern.ch)
* Copyright (C) 2012,2013 CERN (www.cern.ch)
* Author: Alessandro Rubini <rubini@gnudd.com>
*
* Released according to the GNU GPL, version 2 or any later version.
......@@ -19,8 +19,10 @@ int sdbfs_fstat(struct sdbfs *fs, struct sdb_device *record_return)
return 0;
}
int sdbfs_fread(struct sdbfs *fs, int offset, char *buf, int count)
int sdbfs_fread(struct sdbfs *fs, int offset, void *buf, int count)
{
int ret = count;
if (!fs->currentp)
return -ENOENT;
if (offset < 0)
......@@ -30,12 +32,27 @@ int sdbfs_fread(struct sdbfs *fs, int offset, char *buf, int count)
if (fs->data)
memcpy(buf, fs->data + fs->f_offset + offset, count);
else
fs->read(fs, fs->f_offset + offset, buf, count);
fs->read_offset = offset + count;
return count;
ret = fs->read(fs, fs->f_offset + offset, buf, count);
if (ret > 0)
fs->read_offset = offset + ret;
return ret;
}
int sdbfs_fwrite(struct sdbfs *fs, int offset, char *buf, int count)
int sdbfs_fwrite(struct sdbfs *fs, int offset, void *buf, int count)
{
return -ENOSYS;
int ret = count;
if (!fs->currentp)
return -ENOENT;
if (offset < 0)
offset = fs->read_offset;
if (offset + count > fs->f_len)
count = fs->f_len - offset;
if (fs->data)
memcpy(buf, fs->data + fs->f_offset + offset, count);
else
ret = fs->write(fs, fs->f_offset + offset, buf, count);
if (ret > 0)
fs->read_offset = offset + ret;
return ret;
}
......@@ -74,6 +74,8 @@ static struct sdb_device *sdbfs_readentry(struct sdbfs *fs,
*/
if (fs->data)
return (struct sdb_device *)(fs->data + offset);
if (!fs->read)
return NULL;
fs->read(fs, offset, &fs->current_record, sizeof(fs->current_record));
return &fs->current_record;
}
......
......@@ -59,8 +59,8 @@ struct sdb_device *sdbfs_scan(struct sdbfs *fs, int newscan);
/* Defined in access.c */
int sdbfs_fstat(struct sdbfs *fs, struct sdb_device *record_return);
int sdbfs_fread(struct sdbfs *fs, int offset, char *buf, int count);
int sdbfs_fwrite(struct sdbfs *fs, int offset, char *buf, int count);
int sdbfs_fread(struct sdbfs *fs, int offset, void *buf, int count);
int sdbfs_fwrite(struct sdbfs *fs, int offset, void *buf, int count);
/* This is needed to convert endianness. Hoping it is not defined elsewhere */
static inline uint64_t htonll(uint64_t ll)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment