summaryrefslogtreecommitdiff
path: root/netfs/daemonMisc.cpp
blob: d5ccbbaa79938dcb82276a8773cd3f8bf5177e19 (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
#include <errno.h>
#include <map>
#include <unistd.h>
#include <sys/stat.h>
#include <limits.h>
#include <fcntl.h>
#include "daemonFileSystem.h"

extern std::map<Ice::Int, int> files;

Ice::Int
FileSystemServer::access(const NetFSComms::ReqEnv & re, const std::string & path, Ice::Int mode, const Ice::Current &)
{
	SessionPtr sess(dgs->getSession(re.tok));
	TempPrivs tp(re, &uentries, &gentries);
	return ::access((sess->exportCfg->root / path).string().c_str(), mode);
}

NetFSComms::Attr
FileSystemServer::getattr(const NetFSComms::ReqEnv & re, const std::string & path, const Ice::Current &)
{
	SessionPtr sess(dgs->getSession(re.tok));
	TempPrivs tp(re, &uentries, &gentries);
	struct stat s;
	if (::lstat((sess->exportCfg->root / path).string().c_str(), &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 = uentries.getName(s.st_uid);
	a.gid = gentries.getName(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
FileSystemServer::symlink(const NetFSComms::ReqEnv & re, const std::string & path1, const std::string & path2, const Ice::Current &)
{
	SessionPtr sess(dgs->getSession(re.tok));
	TempPrivs tp(re, &uentries, &gentries);
	errno = 0;
	if (::symlink((sess->exportCfg->root / path1).string().c_str(), (sess->exportCfg->root / path2).string().c_str()) != 0) {
		throw NetFSComms::SystemError(errno);
	}
	// s.replicatedRequest = true;
}

void
FileSystemServer::link(const NetFSComms::ReqEnv & re, const std::string & path1, const std::string & path2, const Ice::Current &)
{
	SessionPtr sess(dgs->getSession(re.tok));
	TempPrivs tp(re, &uentries, &gentries);
	errno = 0;
	if (::link((sess->exportCfg->root / path1).string().c_str(), (sess->exportCfg->root / path2).string().c_str()) != 0) {
		throw NetFSComms::SystemError(errno);
	}
	// s.replicatedRequest = true;
}

void
FileSystemServer::rename(const NetFSComms::ReqEnv & re, const std::string & from, const std::string & to, const Ice::Current &)
{
	SessionPtr sess(dgs->getSession(re.tok));
	TempPrivs tp(re, &uentries, &gentries);
	errno = 0;
	if (::rename((sess->exportCfg->root / from).string().c_str(), (sess->exportCfg->root / to).string().c_str()) != 0) {
		throw NetFSComms::SystemError(errno);
	}
	// s.replicatedRequest = true;
}

std::string
FileSystemServer::readlink(const NetFSComms::ReqEnv & re, const std::string & path, const Ice::Current &)
{
	SessionPtr sess(dgs->getSession(re.tok));
	TempPrivs tp(re, &uentries, &gentries);
	errno = 0;
	char buf[PATH_MAX];
	ssize_t rc = ::readlink((sess->exportCfg->root / path).string().c_str(), buf, PATH_MAX);
	if (rc == -1) {
		throw NetFSComms::SystemError(errno);
	}
	return std::string(buf, rc);
}

void
FileSystemServer::chmod(const NetFSComms::ReqEnv & re, const std::string & path, Ice::Int mode, const Ice::Current &)
{
	SessionPtr sess(dgs->getSession(re.tok));
	TempPrivs tp(re, &uentries, &gentries);
	errno = 0;
	if (::chmod((sess->exportCfg->root / path).string().c_str(), mode) != 0) {
		throw NetFSComms::SystemError(errno);
	}
	// s.replicatedRequest = true;
}

void
FileSystemServer::chown(const NetFSComms::ReqEnv & re, const std::string & path, Ice::Int uid, Ice::Int gid, const Ice::Current &)
{
	SessionPtr sess(dgs->getSession(re.tok));
	TempPrivs tp(re, &uentries, &gentries);
	errno = 0;
	if (::chown((sess->exportCfg->root / path).string().c_str(), uid, gid) != 0) {
		throw NetFSComms::SystemError(errno);
	}
	// s.replicatedRequest = true;
}

void
FileSystemServer::utimens(const NetFSComms::ReqEnv & re, const std::string & path,
		Ice::Long s0, Ice::Long ns0, Ice::Long s1, Ice::Long ns1, const Ice::Current&)
{
	SessionPtr sess(dgs->getSession(re.tok));
	TempPrivs tp(re, &uentries, &gentries);
	errno = 0;
	struct timespec times[2];
	times[0].tv_sec = s0;
	times[0].tv_nsec = ns0;
	times[1].tv_sec = s1;
	times[1].tv_nsec = ns1;
	if (::utimensat(0, (sess->exportCfg->root / path).string().c_str(), times, AT_SYMLINK_NOFOLLOW) != 0) {
		throw NetFSComms::SystemError(errno);
	}
	// s.replicatedRequest = true;
}