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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
/*
* Copyright (c) [2012-2014] Novell, Inc.
*
* All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of version 2 of the GNU General Public License as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, contact Novell, Inc.
*
* To contact Novell about this file by physical or electronic mail, you may
* find current contact information at www.novell.com.
*/
#include <string.h>
#include <sys/types.h>
#include <boost/algorithm/string.hpp>
#include <snapper/Log.h>
#include <snapper/AppUtil.h>
#include <snapper/SnapperDefines.h>
#include "MetaSnapper.h"
MetaSnappers meta_snappers;
RefCounter::RefCounter()
: counter(0), last_used(monotonic_time())
{
}
int
RefCounter::inc_use_count()
{
boost::lock_guard<boost::mutex> lock(mutex);
return ++counter;
}
int
RefCounter::dec_use_count()
{
boost::lock_guard<boost::mutex> lock(mutex);
assert(counter > 0);
if (--counter == 0)
last_used = monotonic_time();
return counter;
}
void
RefCounter::update_use_time()
{
boost::lock_guard<boost::mutex> lock(mutex);
last_used = monotonic_time();
}
int
RefCounter::use_count() const
{
boost::lock_guard<boost::mutex> lock(mutex);
return counter;
}
int
RefCounter::unused_for() const
{
boost::lock_guard<boost::mutex> lock(mutex);
if (counter != 0)
return 0;
struct timespec tmp;
clock_gettime(CLOCK_MONOTONIC, &tmp);
return tmp.tv_sec - last_used;
}
time_t
RefCounter::monotonic_time()
{
struct timespec tmp;
clock_gettime(CLOCK_MONOTONIC, &tmp);
return tmp.tv_sec;
}
MetaSnapper::MetaSnapper(ConfigInfo& config_info)
: config_info(config_info), snapper(NULL)
{
set_permissions();
}
MetaSnapper::~MetaSnapper()
{
delete snapper;
}
void
MetaSnapper::setConfigInfo(const map<string, string>& raw)
{
for (map<string, string>::const_iterator it = raw.begin(); it != raw.end(); ++it)
config_info.setValue(it->first, it->second);
getSnapper()->setConfigInfo(raw);
if (raw.find(KEY_ALLOW_USERS) != raw.end() || raw.find(KEY_ALLOW_GROUPS) != raw.end())
set_permissions();
}
void
MetaSnapper::set_permissions()
{
uids.clear();
vector<string> users;
if (config_info.getValue(KEY_ALLOW_USERS, users))
{
for (vector<string>::const_iterator it = users.begin(); it != users.end(); ++it)
{
uid_t tmp;
if (get_user_uid(it->c_str(), tmp))
uids.push_back(tmp);
}
}
sort(uids.begin(), uids.end());
uids.erase(unique(uids.begin(), uids.end()), uids.end());
gids.clear();
vector<string> groups;
if (config_info.getValue(KEY_ALLOW_GROUPS, groups))
{
for (vector<string>::const_iterator it = groups.begin(); it != groups.end(); ++it)
{
gid_t tmp;
if (get_group_gid(it->c_str(), tmp))
gids.push_back(tmp);
}
}
sort(gids.begin(), gids.end());
gids.erase(unique(gids.begin(), gids.end()), gids.end());
}
Snapper*
MetaSnapper::getSnapper()
{
if (!snapper)
snapper = new Snapper(config_info.getConfigName());
update_use_time();
return snapper;
}
void
MetaSnapper::unload()
{
delete snapper;
snapper = NULL;
}
MetaSnappers::MetaSnappers()
{
}
MetaSnappers::~MetaSnappers()
{
}
void
MetaSnappers::init()
{
list<ConfigInfo> config_infos = Snapper::getConfigs();
for (list<ConfigInfo>::iterator it = config_infos.begin(); it != config_infos.end(); ++it)
{
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)
entries.emplace_back(*it);
#else
entries.push_back(*it);
#endif
}
}
MetaSnappers::iterator
MetaSnappers::find(const string& config_name)
{
for (iterator it = entries.begin(); it != entries.end(); ++it)
if (it->configName() == config_name)
return it;
throw UnknownConfig();
}
void
MetaSnappers::createConfig(const string& config_name, const string& subvolume,
const string& fstype, const string& template_name)
{
Snapper::createConfig(config_name, subvolume, fstype, template_name);
ConfigInfo config_info = Snapper::getConfig(config_name);
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)
entries.emplace_back(config_info);
#else
entries.push_back(config_info);
#endif
}
void
MetaSnappers::deleteConfig(iterator it)
{
Snapper::deleteConfig(it->configName());
entries.erase(it);
}
|