summaryrefslogtreecommitdiff
path: root/netfs/entCache.cpp
blob: d79de2958e6d16c91703fdc6c5c96260bdc2ab7b (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
#include "pchCommon.hpp"
#include "entCache.h"
#include <netfsComms.h>

#define LOCK boost::unique_lock<boost::shared_mutex> _lck(lock)
#define SLOCK boost::shared_lock<boost::shared_mutex> _lck(lock)

template<class id_t, class name_t>
EntCache<id_t, name_t>::EntCache()
{
}

template<class id_t, class name_t>
EntCache<id_t, name_t>::~EntCache()
{
}

template<class id_t, class name_t>
const id_t &
EntCache<id_t, name_t>::getID(const name_t & u) const
{
	{
		SLOCK;
		typename IDs::right_map::const_iterator cu = idcache.right.find(u);
		if (cu != idcache.right.end()) {
			return cu->second;
		}
	}
	fillCache();
	SLOCK;
	typename IDs::right_map::const_iterator cu = idcache.right.find(u);
	if (cu != idcache.right.end()) {
		return cu->second;
	}
	throw NetFSComms::SystemError(EPERM);
}

template<class id_t, class name_t>
const name_t &
EntCache<id_t, name_t>::getName(const id_t & u) const
{
	{
		SLOCK;
		typename IDs::left_map::const_iterator cu = idcache.left.find(u);
		if (cu != idcache.left.end()) {
			return cu->second;
		}
	}
	fillCache();
	SLOCK;
	typename IDs::left_map::const_iterator cu = idcache.left.find(u);
	if (cu != idcache.left.end()) {
		return cu->second;
	}
	throw NetFSComms::SystemError(EPERM);
}

template class EntCache<uid_t, std::string>;
void
UserEntCache::fillCache() const
{
	LOCK;
	setpwent();
	idcache.clear();
	while (struct passwd * pwp = getpwent()) {
		idcache.insert(boost::bimap<uid_t, std::string>::value_type(pwp->pw_uid, pwp->pw_name));
	}
	endpwent();
}

void
GroupEntCache::fillCache() const
{
	LOCK;
	setgrent();
	idcache.clear();
	while (struct group * grpp = getgrent()) {
		idcache.insert(boost::bimap<gid_t, std::string>::value_type(grpp->gr_gid, grpp->gr_name));
	}
	endgrent();
}