summaryrefslogtreecommitdiff
path: root/netfs/daemonFiles.cpp
blob: 31a48cf35b90ae3e169d11cd67aa71120d6f39f1 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <errno.h>
#include <map>
#include <fcntl.h>
#include "daemonFiles.h"

int fileNo = 0;
std::map<Ice::Int, int> files;

void
FileServer::truncate(const std::string & path, Ice::Long size, const Ice::Current&)
{
	errno = 0;
	if (::truncate(path.c_str(), size) != 0) {
		throw NetFSComms::SystemError(errno);
	}
	// s.replicatedRequest = true;
}

void
FileServer::ftruncate(Ice::Int id, Ice::Long size, const Ice::Current&)
{
	if (files.find(id) == files.end()) {
		throw NetFSComms::SystemError(EBADF);
	}
	errno = 0;
	if (::ftruncate(files[id], size) != 0) {
		throw NetFSComms::SystemError(errno);
	}
	// s.replicatedRequest = true;
}

void
FileServer::unlink(const std::string & path, const Ice::Current&)
{
	errno = 0;
	if (::unlink(path.c_str()) != 0) {
		throw NetFSComms::SystemError(errno);
	}
	// s.replicatedRequest = true;
}

Ice::Int
FileServer::open(const std::string & path, Ice::Int flags, const Ice::Current&)
{
	errno = 0;
	int fd = ::open(path.c_str(), flags);
	if (fd == -1) {
		throw NetFSComms::SystemError(errno);
	}
	files[++fileNo] = fd;
	//s.replicatedRequest = true;
	return fileNo;
}

Ice::Int
FileServer::create(const std::string & path, Ice::Int flags, Ice::Int mode, const Ice::Current&)
{
	errno = 0;
	int fd = ::open(path.c_str(), O_CREAT | flags, mode);
	if (fd == -1) {
		throw NetFSComms::SystemError(errno);
	}
	files[++fileNo] = fd;
	//s.replicatedRequest = true;
	return fileNo;
}

void
FileServer::close(Ice::Int id, const Ice::Current&)
{
	if (files.find(id) == files.end()) {
		throw NetFSComms::SystemError(EBADF);
	}
	errno = 0;
	if (::close(files[id]) != 0) {
		throw NetFSComms::SystemError(errno);
	}
	files.erase(id);
	// s.replicatedRequest = true;
}

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

void
FileServer::write(Ice::Int id, Ice::Long offset, Ice::Long size, const NetFSComms::Buffer & data, const Ice::Current&)
{
	if (files.find(id) == files.end()) {
		throw NetFSComms::SystemError(EBADF);
	}
	errno = 0;
	if (pwrite(files[id], &data[0], size, offset) != size) {
		throw NetFSComms::SystemError(errno);
	}
	// s.replicatedRequest = true;
}