access.c 1.85 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * 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.
 *
 * This work is part of the White Rabbit project, a research effort led
 * by CERN, the European Institute for Nuclear Research.
 */

/* To avoid many #ifdef and associated mess, all headers are included there */
#include "libsdbfs.h"

int sdbfs_fstat(struct sdbfs *fs, struct sdb_device *record_return)
{
	if (!fs->currentp)
		return -ENOENT;
	memcpy(record_return, fs->currentp, sizeof(*record_return));
	return 0;
}

int sdbfs_fread(struct sdbfs *fs, int offset, void *buf, int count)
{
24
	int ret;
25 26 27 28 29 30 31

	if (!fs->currentp)
		return -ENOENT;
	if (offset < 0)
		offset = fs->read_offset;
	if (offset + count > fs->f_len)
		count = fs->f_len - offset;
32
	ret = count;
33 34 35 36 37 38 39 40 41 42 43
	if (fs->data)
		memcpy(buf, fs->data + fs->f_offset + offset, count);
	else
		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, void *buf, int count)
{
44
	int ret;
45 46 47 48 49 50 51

	if (!fs->currentp)
		return -ENOENT;
	if (offset < 0)
		offset = fs->read_offset;
	if (offset + count > fs->f_len)
		count = fs->f_len - offset;
52
	ret = count;
53
	if (fs->data)
54
		memcpy(fs->data + fs->f_offset + offset, buf, count);
55 56 57 58 59 60
	else
		ret = fs->write(fs, fs->f_offset + offset, buf, count);
	if (ret > 0)
		fs->read_offset = offset + ret;
	return ret;
}
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

int sdbfs_ferase(struct sdbfs *fs, int offset, int count)
{
	int ret;

	if (!fs->currentp)
		return -ENOENT;
	if (offset < 0)
		offset = fs->read_offset;
	if (offset + count > fs->f_len)
		count = fs->f_len - offset;
	ret = count;
	if (fs->data)
		memset(fs->data + fs->f_offset + offset, 0xFF, count);
	else
		ret = fs->erase(fs, fs->f_offset + offset, count);
	if (ret > 0)
		fs->read_offset = offset + ret;
	return ret;
}