summaryrefslogtreecommitdiff
path: root/netfs/daemon/daemonFile.cpp
blob: 0df3c2a3a40108d535ca4616816b5769bcee24f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <Ice/ObjectAdapter.h>
#include <errno.h>
#include <map>
#include <fcntl.h>
#include <typeConverter.h>
#include <sys/stat.h>
#include "daemonFile.h"
#include <entCache.h>

FileServer::FileServer(int f, EntryTypeConverter & t) :
	EntryTypeConverter(t),
	fd(f)
{
}

FileServer::~FileServer()
{
}

void
FileServer::ftruncate(const NetFS::ReqEnv re, Ice::Long size, const Ice::Current&)
{
	(void)re;
	errno = 0;
	if (::ftruncate(fd, size) != 0) {
		throw NetFS::SystemError(errno);
	}
}

NetFS::Attr
FileServer::fgetattr(const NetFS::ReqEnv re, const Ice::Current &)
{
	(void)re;
	struct stat s;
	if (::fstat(fd, &s) != 0) {
		throw NetFS::SystemError(errno);
	}
	return convert(s);
}

void
FileServer::close(const Ice::Current & ice)
{
	errno = 0;
	if (::close(fd) != 0) {
		throw NetFS::SystemError(errno);
	}
	ice.adapter->removeAllFacets(ice.id);
}

NetFS::Buffer
FileServer::read(Ice::Long offset, Ice::Long size, const Ice::Current&)
{
	NetFS::Buffer buf;
	buf.resize(size);
	errno = 0;
	int r = pread(fd, &buf[0], size, offset);
	if (r == -1) {
		throw NetFS::SystemError(errno);
	}
	else if (r != size) {
		buf.resize(r);
	}
	return buf;
}

void
FileServer::write(Ice::Long offset, Ice::Long size, const NetFS::Buffer data, const Ice::Current&)
{
	errno = 0;
	if (pwrite(fd, &data.front(), size, offset) != size) {
		throw NetFS::SystemError(errno);
	}
}