summaryrefslogtreecommitdiff
path: root/netfs/daemonFiles.cpp
blob: 826f7be86ad59a566c7c96aaceb62a51111840f2 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <errno.h>
#include <map>
#include <fcntl.h>
#include <sys/stat.h>
#include "daemonFiles.h"

FileServer::FileServer(DaemonGlobalStatePtr dgs) :
	DaemonModule(dgs)
{
}

void
FileServer::truncate(Ice::Long tok, const std::string & path, Ice::Long size, const Ice::Current&)
{
	SessionPtr sess(dgs->getSession(tok));
	errno = 0;
	if (::truncate((sess->exportCfg->root / path).string().c_str(), size) != 0) {
		throw NetFSComms::SystemError(errno);
	}
	// s.replicatedRequest = true;
}

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

NetFSComms::Attr
FileServer::fgetattr(Ice::Long tok, Ice::Int id, const Ice::Current &)
{
	SessionPtr sess(dgs->getSession(tok));
	if (sess->files.find(id) == sess->files.end()) {
		throw NetFSComms::SystemError(EBADF);
	}
	struct stat s;
	if (::fstat(sess->files[id], &s) != 0) {
		throw NetFSComms::SystemError(errno);
	}
	NetFSComms::Attr a;
	a.dev = s.st_dev;
	a.inode = s.st_ino;
	a.mode = s.st_mode;
	a.links = s.st_nlink;
	a.uid = s.st_uid;
	a.gid = s.st_gid;
	a.rdev = s.st_rdev;
	a.size = s.st_size;
	a.blockSize = s.st_blksize;
	a.blocks = s.st_blocks;
	a.atime = s.st_atime;
	a.mtime = s.st_mtime;
	a.ctime = s.st_ctime;
	return a;
}

void
FileServer::unlink(Ice::Long tok, const std::string & path, const Ice::Current&)
{
	SessionPtr sess(dgs->getSession(tok));
	errno = 0;
	if (::unlink((sess->exportCfg->root / path).string().c_str()) != 0) {
		throw NetFSComms::SystemError(errno);
	}
	// s.replicatedRequest = true;
}

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

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

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

NetFSComms::Buffer
FileServer::read(Ice::Long tok, Ice::Int id, Ice::Long offset, Ice::Long size, const Ice::Current&)
{
	SessionPtr sess(dgs->getSession(tok));
	if (sess->files.find(id) == sess->files.end()) {
		throw NetFSComms::SystemError(EBADF);
	}
	NetFSComms::Buffer buf;
	buf.resize(size);
	errno = 0;
	int r = pread(sess->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::Long tok, Ice::Int id, Ice::Long offset, Ice::Long size, const NetFSComms::Buffer & data, const Ice::Current&)
{
	SessionPtr sess(dgs->getSession(tok));
	if (sess->files.find(id) == sess->files.end()) {
		throw NetFSComms::SystemError(EBADF);
	}
	errno = 0;
	if (pwrite(sess->files[id], &data[0], size, offset) != size) {
		throw NetFSComms::SystemError(errno);
	}
	// s.replicatedRequest = true;
}