summaryrefslogtreecommitdiff
path: root/netfs/daemon/daemonFile.cpp
blob: cf34b7d749c4a0b78ed233ae93fb704e6a040178 (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
78
79
80
81
82
83
84
85
86
87
88
#include "daemonFile.h"
#include <Ice/ObjectAdapter.h>
#include <cerrno>
#include <entCache.h>
#include <fcntl.h>
#include <map>
#include <numeric.h>
#include <sys/stat.h>
#include <typeConverter.h>
#include <unistd.h>

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

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

NetFS::Attr
FileServer::fgetattr(const Ice::Current &)
{
	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;
	if (size) {
		buf.resize(safe {size});
		errno = 0;
		auto r = pread(fd, buf.data(), safe {size}, offset);
		if (r == -1) {
			throw NetFS::SystemError(errno);
		}
		else if (std::cmp_not_equal(r, size)) {
			buf.resize(safe {r});
		}
	}
	return buf;
}

void
FileServer::write(Ice::Long offset, Ice::Long size, const ::std::pair<const ::Ice::Byte *, const ::Ice::Byte *> data,
		const Ice::Current &)
{
	errno = 0;
	if (pwrite(fd, data.first, safe {size}, offset) != size) {
		throw NetFS::SystemError(errno);
	}
}

Ice::Long
FileServer::copyrange(NetFS::FilePrxPtr to, Ice::Long offsrc, Ice::Long offdst, Ice::Long size, Ice::Int flags,
		const Ice::Current & ice)
{
	if (auto obj = ice.adapter->findByProxy(to); auto file = std::dynamic_pointer_cast<FileServer>(obj)) {
		errno = 0;
		off_t src = offsrc, dst = offdst;
		if (auto rtn = copy_file_range(fd, &src, file->fd, &dst, safe {size}, safe {flags}); rtn != -1) {
			return rtn;
		}
		throw NetFS::SystemError(errno);
	}
	else {
		const auto bytes = read(offsrc, size, ice);
		to->write(offdst, size, {bytes.data(), bytes.data() + size});
		return size;
	}
}