CernVM-FS  2.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
uid_map.h
Go to the documentation of this file.
1 
5 #ifndef CVMFS_UID_MAP_H_
6 #define CVMFS_UID_MAP_H_
7 
8 #include <sys/types.h>
9 
10 #include <cerrno>
11 #include <map>
12 #include <string>
13 #include <vector>
14 
15 #include "sanitizer.h"
16 #include "util/logging.h"
17 #include "util/string.h"
18 
45 template <typename T>
46 class IntegerMap {
47  public:
48  typedef T key_type;
49  typedef T value_type;
50  typedef typename std::map<key_type, value_type> map_type;
51 
52  public:
53  IntegerMap()
54  : valid_(true)
55  , has_default_value_(false)
56  , default_value_(T(0)) {}
57 
63  void Set(const T k, const T v) { map_[k] = v; }
64 
70  void SetDefault(const T v) {
71  has_default_value_ = true;
72  default_value_ = v;
73  }
74 
84  bool Read(const std::string &path) {
85  valid_ = ReadFromFile(path);
86  return IsValid();
87  }
88 
94  bool Contains(const T k) const {
95  assert(IsValid());
96  return map_.find(k) != map_.end();
97  }
98 
104  T Map(const T k) const {
105  assert(IsValid());
106  typename map_type::const_iterator i = map_.find(k);
107  if (i != map_.end()) {
108  return i->second;
109  }
110 
111  return (HasDefault())
112  ? default_value_
113  : k;
114  }
115 
116  bool HasEffect() const {
117  return (map_.size() != 0) || has_default_value_;
118  }
119 
120  bool IsEmpty() const { return map_.size() == 0; }
121  bool IsValid() const { return valid_; }
122  bool HasDefault() const { return has_default_value_; }
123  size_t RuleCount() const { return map_.size(); }
124 
125  T GetDefault() const { assert(has_default_value_); return default_value_; }
126  const map_type& GetRuleMap() const { return map_; }
127 
128  protected:
129  bool ReadFromFile(const std::string &path) {
130  FILE *fmap = fopen(path.c_str(), "r");
131  if (!fmap) {
132  LogCvmfs(kLogUtility, kLogDebug, "failed to open %s (errno: %d)",
133  path.c_str(), errno);
134  return false;
135  }
136 
137  sanitizer::IntegerSanitizer int_sanitizer;
138 
139  std::string line;
140  unsigned int line_number = 0;
141  while (GetLineFile(fmap, &line)) {
142  ++line_number;
143  line = Trim(line);
144  if (line.empty() || line[0] == '#') {
145  continue;
146  }
147 
148  std::vector<std::string> components = SplitString(line, ' ');
149  FilterEmptyStrings(&components);
150  if (components.size() != 2 ||
151  !int_sanitizer.IsValid(components[1]) ||
152  (components[0] != "*" && !int_sanitizer.IsValid(components[0]))) {
153  fclose(fmap);
154  LogCvmfs(kLogUtility, kLogDebug, "failed to read line %d in %s",
155  line_number, path.c_str());
156  return false;
157  }
158 
159  value_type to = String2Uint64(components[1]);
160  if (components[0] == "*") {
161  SetDefault(to);
162  continue;
163  }
164 
165  key_type from = String2Uint64(components[0]);
166  Set(from, to);
167  }
168 
169  fclose(fmap);
170  return true;
171  }
172 
173  void FilterEmptyStrings(std::vector<std::string> *vec) const {
174  std::vector<std::string>::iterator i = vec->begin();
175  for (; i != vec->end(); ) {
176  i = (i->empty()) ? vec->erase(i) : i + 1;
177  }
178  }
179 
180  private:
181  bool valid_;
182  map_type map_;
183 
184  bool has_default_value_;
185  T default_value_;
186 };
187 
188 typedef IntegerMap<uid_t> UidMap;
189 typedef IntegerMap<gid_t> GidMap;
190 
191 #endif // CVMFS_UID_MAP_H_
string Trim(const string &raw, bool trim_newline)
Definition: string.cc:428
static void Read(void *buf, size_t nbyte)
Definition: helper_util.cc:94
assert((mem||(size==0))&&"Out Of Memory")
bool IsValid(const std::string &input) const
Definition: sanitizer.cc:114
bool GetLineFile(FILE *f, std::string *line)
Definition: string.cc:386
vector< string > SplitString(const string &str, char delim)
Definition: string.cc:290
uint64_t String2Uint64(const string &value)
Definition: string.cc:228
CVMFS_EXPORT void LogCvmfs(const LogSource source, const int mask, const char *format,...)
Definition: logging.cc:528