sbuild  1.7.2
userdata.h
1 /* Copyright © 2005-2013 Roger Leigh <rleigh@debian.org>
2  *
3  * schroot is free software: you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * schroot is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  *********************************************************************/
18 
19 #ifndef SBUILD_CHROOT_FACET_USERDATA_H
20 #define SBUILD_CHROOT_FACET_USERDATA_H
21 
22 #include <sbuild/chroot/facet/facet.h>
23 #include <sbuild/custom-error.h>
24 #include <sbuild/types.h>
25 
26 namespace sbuild
27 {
28  namespace chroot
29  {
30  namespace facet
31  {
32 
39  class userdata : public facet
40  {
41  public:
44  {
50  };
51 
54 
56  typedef std::shared_ptr<userdata> ptr;
57 
59  typedef std::shared_ptr<const userdata> const_ptr;
60 
61  private:
63  userdata ();
64 
65  public:
67  virtual ~userdata ();
68 
74  static ptr
75  create ();
76 
77  virtual facet::ptr
78  clone () const;
79 
80  virtual std::string const&
81  get_name () const;
82 
83  virtual void
84  setup_env (environment& env) const;
85 
86  virtual void
87  get_details (format_detail& detail) const;
88 
89  virtual void
90  get_used_keys (string_list& used_keys) const;
91 
92  virtual void
93  get_keyfile (keyfile& keyfile) const;
94 
95  virtual void
96  set_keyfile (const keyfile& keyfile);
97 
98 
99  template <typename T>
100  bool
101  get_value (const std::string& key,
102  T& value) const
103  {
104  log_debug(DEBUG_INFO) << "Getting userdata key=" << key << std::endl;
105  const string_map::const_iterator found_item = this->data.find(key);
106  if (found_item != this->data.end())
107  {
108  try
109  {
110  parse_value(found_item->second, value);
111  return true;
112  }
113  catch (const parse_value_error& e)
114  {
115  error ep(key, PARSE_ERROR, e);
117  return false;
118  }
119  }
120  return false;
121  }
122 
128  string_map const&
129  get_data () const;
130 
139  bool
140  get_data (const std::string& key,
141  std::string& value) const;
142 
149  void
150  set_data (const string_map& data);
151 
159  void
160  set_data (const std::string& key,
161  const std::string& value);
162 
170  void
171  set_system_data (const std::string& key,
172  const std::string& value);
173 
179  void
180  remove_data (const std::string& key);
181 
187  string_set const&
188  get_user_modifiable_keys () const;
189 
195  void
196  set_user_modifiable_keys (const string_set& keys);
197 
203  string_set const&
204  get_root_modifiable_keys () const;
205 
211  void
212  set_root_modifiable_keys (const string_set& keys);
213 
221  void
222  set_user_data(const string_map& data);
223 
231  void
232  set_root_data(const string_map& data);
233 
239  void
241 
242  private:
250  void
251  set_data(const string_map& data,
252  const string_set& allowed_keys,
253  bool root);
254 
263  };
264 
265  }
266  }
267 }
268 
269 #endif /* SBUILD_CHROOT_FACET_USERDATA_H */
270 
271 /*
272  * Local Variables:
273  * mode:C++
274  * End:
275  */
virtual ~userdata()
The destructor.
Definition: userdata.cc:114
void remove_data(const std::string &key)
Remove a single key.
Definition: userdata.cc:231
string_set const & get_root_modifiable_keys() const
Get the set of keys allowed to be modified by root.
Definition: userdata.cc:331
string_set const & get_user_modifiable_keys() const
Get the set of keys allowed to be modified by a user.
Definition: userdata.cc:319
Debian source builder components.
Definition: ctty.cc:31
void set_root_modifiable_keys(const string_set &keys)
Set the set of keys allowed to be modified by root.
Definition: userdata.cc:337
virtual void setup_env(environment &env) const
Set environment.
Definition: userdata.cc:137
string_map data
Mapping between user keys and values.
Definition: userdata.h:256
virtual facet::ptr clone() const
Copy the chroot facet.
Definition: userdata.cc:125
string_map const & get_data() const
Get user data as a map of key-value pairs.
Definition: userdata.cc:189
std::shared_ptr< const userdata > const_ptr
A shared_ptr to a const chroot facet object.
Definition: userdata.h:59
std::ostream & log_debug(debug_level level)
Log a debug message.
Definition: log.cc:110
std::map< std::string, std::string > string_map
A string map.
Definition: types.h:44
Container of environment variables.
Definition: environment.h:38
std::set< std::string > string_set
A string set.
Definition: types.h:41
virtual void get_used_keys(string_list &used_keys) const
Get a list of the keys used during keyfile parsing.
Definition: userdata.cc:343
string_set user_modifiable_keys
Keys modifiable by users.
Definition: userdata.h:260
error_code
Error codes.
Definition: userdata.h:43
virtual std::string const & get_name() const
Get the name of the chroot facet.
Definition: userdata.cc:131
Chroot support for extensible user metadata.
Definition: userdata.h:39
void log_exception_warning(const std::exception &e)
Log an exception as a warning.
Definition: log.cc:143
virtual void get_details(format_detail &detail) const
Get detailed information about the chroot for output.
Definition: userdata.cc:157
Configuration key is not allowed to be modified.
Definition: userdata.h:47
Base class for all facets.
Definition: facet.h:50
Invalid name for configuration key.
Definition: userdata.h:48
virtual void get_keyfile(keyfile &keyfile) const
Copy the chroot properties into a keyfile.
Definition: userdata.cc:350
void set_data(const string_map &data)
Set user data from a string map.
Definition: userdata.cc:247
std::vector< std::string > string_list
A string vector.
Definition: types.h:38
Parse error.
Definition: parse-error.h:34
Custom error.
Definition: custom-error.h:31
void set_user_modifiable_keys(const string_set &keys)
Set the set of keys allowed to be modified by a user.
Definition: userdata.cc:325
Environment variable name is ambiguous.
Definition: userdata.h:45
string_set env
Environment checking.
Definition: userdata.h:258
userdata()
The constructor.
Definition: userdata.cc:105
void set_system_data(const std::string &key, const std::string &value)
Set a single key-value pair.
Definition: userdata.cc:206
string_set root_modifiable_keys
Keys modifiable by root.
Definition: userdata.h:262
Format names and values for output.
Definition: format-detail.h:38
Informational messages.
Definition: log.h:32
custom_error< error_code > error
Exception type.
Definition: userdata.h:53
static ptr create()
Create a chroot facet.
Definition: userdata.cc:119
Configuration key name is ambiguous.
Definition: userdata.h:46
Configuration file parser.
Definition: keyfile.h:46
void set_user_data(const string_map &data)
Set data for the current user.
Definition: userdata.cc:254
std::shared_ptr< facet > ptr
A shared_ptr to a chroot facet object.
Definition: facet.h:63
void parse_value(const std::string &value, bool &parsed_value)
Parse a boolean value.
Definition: parse-value.cc:35
std::shared_ptr< userdata > ptr
A shared_ptr to a chroot facet object.
Definition: userdata.h:56
Error parsing value.
Definition: userdata.h:49
virtual void set_keyfile(const keyfile &keyfile)
Set the chroot properties from a keyfile.
Definition: userdata.cc:371
void set_root_data(const string_map &data)
Set data for root.
Definition: userdata.cc:260