CernVM-FS  2.13.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), has_default_value_(false), default_value_(T(0)) { }
55 
61  void Set(const T k, const T v) { map_[k] = v; }
62 
68  void SetDefault(const T v) {
69  has_default_value_ = true;
70  default_value_ = v;
71  }
72 
82  bool Read(const std::string &path) {
83  valid_ = ReadFromFile(path);
84  return IsValid();
85  }
86 
92  bool Contains(const T k) const {
93  assert(IsValid());
94  return map_.find(k) != map_.end();
95  }
96 
102  T Map(const T k) const {
103  assert(IsValid());
104  const typename map_type::const_iterator i = map_.find(k);
105  if (i != map_.end()) {
106  return i->second;
107  }
108 
109  return (HasDefault()) ? default_value_ : k;
110  }
111 
112  bool HasEffect() const { return (map_.size() != 0) || has_default_value_; }
113 
114  bool IsEmpty() const { return map_.size() == 0; }
115  bool IsValid() const { return valid_; }
116  bool HasDefault() const { return has_default_value_; }
117  size_t RuleCount() const { return map_.size(); }
118 
119  T GetDefault() const {
120  assert(has_default_value_);
121  return default_value_;
122  }
123  const map_type &GetRuleMap() const { return map_; }
124 
125  protected:
126  bool ReadFromFile(const std::string &path) {
127  FILE *fmap = fopen(path.c_str(), "r");
128  if (!fmap) {
129  LogCvmfs(kLogUtility, kLogDebug, "failed to open %s (errno: %d)",
130  path.c_str(), errno);
131  return false;
132  }
133 
134  const sanitizer::IntegerSanitizer int_sanitizer;
135 
136  std::string line;
137  unsigned int line_number = 0;
138  while (GetLineFile(fmap, &line)) {
139  ++line_number;
140  line = Trim(line);
141  if (line.empty() || line[0] == '#') {
142  continue;
143  }
144 
145  std::vector<std::string> components = SplitString(line, ' ');
146  FilterEmptyStrings(&components);
147  if (components.size() != 2 || !int_sanitizer.IsValid(components[1])
148  || (components[0] != "*" && !int_sanitizer.IsValid(components[0]))) {
149  fclose(fmap);
150  LogCvmfs(kLogUtility, kLogDebug, "failed to read line %d in %s",
151  line_number, path.c_str());
152  return false;
153  }
154 
155  value_type to = String2Uint64(components[1]);
156  if (components[0] == "*") {
157  SetDefault(to);
158  continue;
159  }
160 
161  key_type from = String2Uint64(components[0]);
162  Set(from, to);
163  }
164 
165  fclose(fmap);
166  return true;
167  }
168 
169  void FilterEmptyStrings(std::vector<std::string> *vec) const {
170  std::vector<std::string>::iterator i = vec->begin();
171  for (; i != vec->end();) {
172  i = (i->empty()) ? vec->erase(i) : i + 1;
173  }
174  }
175 
176  private:
177  bool valid_;
178  map_type map_;
179 
180  bool has_default_value_;
181  T default_value_;
182 };
183 
184 typedef IntegerMap<uid_t> UidMap;
185 typedef IntegerMap<gid_t> GidMap;
186 
187 #endif // CVMFS_UID_MAP_H_
string Trim(const string &raw, bool trim_newline)
Definition: string.cc:466
static void Read(void *buf, size_t nbyte)
Definition: helper_util.cc:97
assert((mem||(size==0))&&"Out Of Memory")
bool IsValid(const std::string &input) const
Definition: sanitizer.cc:112
bool GetLineFile(FILE *f, std::string *line)
Definition: string.cc:422
vector< string > SplitString(const string &str, char delim)
Definition: string.cc:306
uint64_t String2Uint64(const string &value)
Definition: string.cc:240
CVMFS_EXPORT void LogCvmfs(const LogSource source, const int mask, const char *format,...)
Definition: logging.cc:545