summaryrefslogtreecommitdiff
path: root/netfs/daemon/daemonFile.cpp
blob: 3acd1ca038cd525afd4f31a6494cfee6e6215fab (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
76
77
#include <pch.hpp>
#include <Ice/ObjectAdapter.h>
#include <errno.h>
#include <map>
#include <fcntl.h>
#include <typeConvert.h>
#include <sys/stat.h>
#include "daemonFile.h"
#include <entCache.h>

FileServer::FileServer(int f) :
	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);
	}
	NetFS::Attr a;
	a << StatSource { s, boost::bind(&UserEntCache::getName, &UserEntCache::instance, _1, _2), boost::bind(&GroupEntCache::getName, &GroupEntCache::instance, _1, _2) };
	return a;
}

void
FileServer::close(const Ice::Current & ice)
{
	errno = 0;
	if (::close(fd) != 0) {
		throw NetFS::SystemError(errno);
	}
	ice.adapter->remove(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);
	}
}