GCC Code Coverage Report
Directory: cvmfs/ Exec Total Coverage
File: cvmfs/path_filters/dirtab.h Lines: 11 16 68.8 %
Date: 2019-02-03 02:48:13 Branches: 7 9 77.8 %

Line Branch Exec Source
1
/**
2
 * This file is part of the CernVM File System.
3
 */
4
5
#ifndef CVMFS_PATH_FILTERS_DIRTAB_H_
6
#define CVMFS_PATH_FILTERS_DIRTAB_H_
7
8
#include <string>
9
#include <vector>
10
11
#include "pathspec/pathspec.h"
12
13
namespace catalog {
14
15
// TODO(jblomer): Dirtab and RelaxedPathFilter can use static inhertiance.  It
16
// is clear at compile time which one should be used.  They are not on a
17
// critical path though.
18
19
/**
20
 * A Dirtab is handling the parsing and processing of the .cvmfsdirtab file.
21
 * The .cvmfsdirtab contains a list of Pathspecs that define where CernVM-FS
22
 * should automatically create nested catalogs. Furthermore it can contain neg-
23
 * ative rules to omit the automatic creation of nested catalogs in certain
24
 * directories.
25
 *
26
 * Example (adding a space in front of * - silence compiler warning):
27
 *   # this is a .cvmfsdirtab comment
28
 *   /software/releases/ *
29
 *   /conditions_data/runs/ *
30
 *
31
 *   # ignore repository directories
32
 *   ! *.svn
33
 *   ! *.git
34
 *
35
 * This .cvmfsdirtab file would generate nested catalogs in all directories
36
 * directly inside /software/releases/ and /conditions_data/runs/ like:
37
 *   /software/releases/2.1.1-2/.cvmfscatalog
38
 *   /software/releases/2.3.4-1/.cvmfscatalog
39
 *   /software/releases/3.0.0-5/.cvmfscatalog
40
 *   ...
41
 *   /conditions_data/runs/27.11.2014/.cvmfscatalog
42
 *   /conditions_data/runs/11.09.2013/.cvmfscatalog
43
 *   ...
44
 *
45
 * Note: This class does not take care of the actual creation of nested catalogs
46
 *       but wraps the parsing and matching of the .cvmfsdirtab file and given
47
 *       path strings.
48
 *       See: swissknife_sync.{h,cc} or t_dirtab.cc for the usage of this class.
49
 *
50
 */
51
class Dirtab {
52
 public:
53
  static const char kCommentMarker  = '#';
54
  static const char kNegationMarker = '!';
55
56
  /**
57
   * A Rule represents a single line from a .cvmfsdirtab file. It wraps the
58
   * parsed Pathspec for the path pattern in this line and stores if this Path-
59
   * spec should be seen as a negation rule.
60
   */
61
720
  struct Rule {
62
164
    Rule(const Pathspec &pathspec, const bool is_negation) :
63
164
      pathspec(pathspec), is_negation(is_negation) {}
64
    Pathspec  pathspec;
65
    bool      is_negation;
66
  };
67
68
  typedef std::vector<Rule> Rules;
69
70
  /**
71
   * Creates an empty Dirtab (mainly for testing purposes)
72
   */
73
  Dirtab();
74
75
66
  virtual ~Dirtab() {}
76
77
  /**
78
   * Returns an already filled Dirtab
79
   *
80
   * @param dirtab_path path of the dirtab file that will be used to fill
81
   *                    the Dirtab object
82
   * @return the already filled Dirtab
83
   */
84
  static Dirtab* Create(const std::string &dirtab_path) {
85
    Dirtab *dt = new Dirtab();
86
    dt->Open(dirtab_path);
87
    return dt;
88
  }
89
90
  /**
91
   * Parses the content of a .cvmfsdirtab file. This is called by the filepath-
92
   * constructor or can be used on an empty Dirtab for testing purposes and in
93
   * inherited classes.
94
   *
95
   * @param dirtab  a string containing the full content of a .cvmfsdirtab file
96
   * @return        true on successful parsing
97
   */
98
  virtual bool Parse(const std::string &dirtab);
99
  virtual bool Parse(FILE *dirtab_file);
100
101
  /**
102
   * Matches a given path string against this Dirtab. The path is considered a
103
   * match if it matches against (at least) one positive rule and is not matched
104
   * by any negative rule.
105
   *
106
   * @param path  the path string to be matched against this Dirtab
107
   * @return      true if path string is matching this Dirtab
108
   */
109
  virtual bool IsMatching(const std::string &path) const;
110
111
  /**
112
   * Matches a given path string against all negative rules in this Dirtab. This
113
   * bypasses the check for positive rules, thus a path string can be opposed by
114
   * this Dirtab while it would also not match any positive rule.
115
   *
116
   * @param path  the path string to be checked for opposition of this Dirtab
117
   * @return      true if (at least) one negative rule matches
118
   */
119
  virtual bool IsOpposing(const std::string &path) const;
120
121
  const Rules& positive_rules() const { return positive_rules_; }
122
  const Rules& negative_rules() const { return negative_rules_; }
123
124
57
  size_t RuleCount() const { return NegativeRuleCount() + PositiveRuleCount(); }
125
114
  size_t NegativeRuleCount() const { return negative_rules_.size(); }
126
114
  size_t PositiveRuleCount() const { return positive_rules_.size(); }
127
59
  bool   IsValid() const { return valid_; }
128
129
 protected:
130
  /**
131
   * Fill a Dirtab from a given .cvmfsdirtab file path.
132
   */
133
  bool Open(const std::string &dirtab_path);
134
  bool ParseLine(const std::string &line);
135
  virtual bool ParsePathspec(const std::string &pathspec_str, bool negation);
136
  void AddRule(const Rule &rule);
137
138
 private:
139
364
  void SkipWhitespace(
140
    const std::string::const_iterator &end,
141
    std::string::const_iterator *itr) const
142
  {
143

364
    for (; *itr != end && **itr == ' '; ++(*itr)) { }
144
364
  }
145
  bool CheckRuleValidity() const;
146
147
 private:
148
  bool  valid_;
149
  Rules positive_rules_;
150
  Rules negative_rules_;
151
};
152
153
}  // namespace catalog
154
155
#endif  // CVMFS_PATH_FILTERS_DIRTAB_H_
156