File: restrictions.cc

package info (click to toggle)
monotone 0.48-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 20,096 kB
  • ctags: 8,077
  • sloc: cpp: 81,000; sh: 6,402; perl: 1,241; lisp: 1,045; makefile: 655; python: 566; sql: 112; ansic: 52
file content (500 lines) | stat: -rw-r--r-- 15,211 bytes parent folder | download
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// Copyright (C) 2005 Derek Scherger <derek@echologic.com>
//
// This program is made available under the GNU GPL version 2.0 or
// greater. See the accompanying file COPYING for details.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE.

#include "base.hh"
#include "safe_map.hh"
#include "vector.hh"
#include "restrictions.hh"
#include "file_io.hh"
#include "roster.hh"
#include "database.hh" // for parent_roster

using std::make_pair;
using std::map;
using std::set;
using std::vector;

// TODO: add check for relevant rosters to be used by log
//
// i.e.  as log goes back through older and older rosters it may hit one
// that pre-dates any of the nodes in the restriction. the nodes that the
// restriction includes or excludes may not have been born in a sufficiently
// old roster. at this point log should stop because no earlier roster will
// include these nodes.

typedef map<node_id, restricted_path::status>::const_iterator
        node_status_iterator;

typedef map<file_path, restricted_path::status>::const_iterator
        path_status_iterator;

typedef set<file_path>::const_iterator path_iterator;

static void
add_parents(map<node_id, restricted_path::status> & node_map,
            roster_t const & roster)
{
  set<node_id> parents;

  // accumulate the set of parents of all included nodes
  for (node_status_iterator i = node_map.begin(); i != node_map.end(); ++i)
    {
      if (i->second == restricted_path::included && roster.has_node(i->first))
        {
          node_id parent = roster.get_node(i->first)->parent;
          while (!null_node(parent) && parents.find(parent) == parents.end())
            {
              parents.insert(parent);
              parent = roster.get_node(parent)->parent;
            }
        }
    }

  // add implicit includes for all required parents
  for (set<node_id>::const_iterator i = parents.begin();
       i != parents.end(); ++i)
    {
      node_status_iterator n = node_map.find(*i);
      if (n == node_map.end())
        {
          file_path fp;
          roster.get_name(*i, fp);
          W(F("including missing parent '%s'") % fp);
          node_map[*i] = restricted_path::required;
        }
      else if (n->second == restricted_path::included)
        {
          node_map[*i] = restricted_path::included_required;
        }
      else if (n->second == restricted_path::excluded)
        {
          file_path fp;
          roster.get_name(*i, fp);
          W(F("including required parent '%s'") % fp);
          node_map[*i] = restricted_path::excluded_required;
        }
    }
}

static void
map_nodes(map<node_id, restricted_path::status> & node_map,
          roster_t const & roster,
          set<file_path> const & paths,
          set<file_path> & known_paths,
          restricted_path::status const status)
{
  for (path_iterator i = paths.begin(); i != paths.end(); ++i)
    {
      if (roster.has_node(*i))
        {
          known_paths.insert(*i);
          node_id nid = roster.get_node(*i)->self;

          node_status_iterator n = node_map.find(nid);
          if (n != node_map.end())
            E(n->second == status, origin::user,
              F("conflicting include/exclude on path '%s'") % *i);
          else
            node_map[nid] = status;

        }
    }
}

static void
map_nodes(map<node_id, restricted_path::status> & node_map,
          roster_t const & roster,
          set<file_path> const & included_paths,
          set<file_path> const & excluded_paths,
          set<file_path> & known_paths)
{
  map_nodes(node_map, roster, included_paths, known_paths,
            restricted_path::included);
  map_nodes(node_map, roster, excluded_paths, known_paths,
            restricted_path::excluded);
}

static void
add_parents(map<file_path, restricted_path::status> & path_map)
{
  set<file_path> parents;

  // accumulate the set of parents of all included paths
  for (path_status_iterator i = path_map.begin(); i != path_map.end(); ++i)
    {
      if (i->second == restricted_path::included)
        {
          file_path parent = i->first.dirname();
          while (!parent.empty() && parents.find(parent) == parents.end())
            {
              parents.insert(parent);
              parent = parent.dirname();
            }
          parents.insert(parent);
        }
    }

  // add implicit includes for all required parents
  for (set<file_path>::const_iterator i = parents.begin();
       i != parents.end(); ++i)
    {
      path_status_iterator p = path_map.find(*i);
      if (p == path_map.end())
        {
          W(F("including missing parent '%s'") % *i);
          path_map[*i] = restricted_path::required;
        }
      else if (p->second == restricted_path::included)
        {
          path_map[*i] = restricted_path::included_required;
        }
      else if (p->second == restricted_path::excluded)
        {
          W(F("including required parent '%s'") % *i);
          path_map[*i] = restricted_path::excluded_required;
        }
    }

}

static void
map_paths(map<file_path, restricted_path::status> & path_map,
          set<file_path> const & paths,
          restricted_path::status const status)
{
  for (path_iterator i = paths.begin(); i != paths.end(); ++i)
    {
      path_status_iterator p = path_map.find(*i);
      if (p != path_map.end())
        E(p->second == status, origin::user,
          F("conflicting include/exclude on path '%s'") % *i);
      else
        path_map[*i] = status;
    }
}

namespace
{
  // ignored paths are allowed into the restriction but are not considered
  // invalid if they are found in none of the restriction's rosters
  struct unknown_unignored_node : public path_predicate<file_path>
  {
    explicit unknown_unignored_node(set<file_path> const & known_paths,
                                    path_predicate<file_path> const & ignore)
      : known_paths(known_paths), ignore_file(ignore)
    {}
    virtual bool operator()(file_path const & p) const
    {
      return (known_paths.find(p) == known_paths.end() && !ignore_file(p));
    }

  private:
    set<file_path> const & known_paths;
    path_predicate<file_path> const & ignore_file;
  };

  struct unknown_unignored_path : public path_predicate<file_path>
  {
    explicit unknown_unignored_path(path_predicate<file_path> const & ignore)
      : ignore_file(ignore)
    {}
    virtual bool operator()(file_path const & p) const
    {
      return !path_exists(p) && !ignore_file(p);
    }
  private:
    path_predicate<file_path> const & ignore_file;
  };
}

static void
validate_paths(set<file_path> const & included_paths,
               set<file_path> const & excluded_paths,
               path_predicate<file_path> const & is_unknown)
{
  int bad = 0;

  for (path_iterator i = included_paths.begin(); i != included_paths.end(); ++i)
    if (is_unknown(*i))
      {
        bad++;
        W(F("restriction includes unknown path '%s'") % *i);
      }

  for (path_iterator i = excluded_paths.begin(); i != excluded_paths.end(); ++i)
    if (is_unknown(*i))
      {
        bad++;
        W(F("restriction excludes unknown path '%s'") % *i);
      }

  E(bad == 0, origin::user,
    FP("%d unknown path", "%d unknown paths", bad) % bad);
}

restriction::restriction(vector<file_path> const & includes,
                         vector<file_path> const & excludes,
                         long depth)
  : included_paths(includes.begin(), includes.end()),
    excluded_paths(excludes.begin(), excludes.end()),
    depth(depth)
{}

node_restriction::node_restriction(vector<file_path> const & includes,
                                   vector<file_path> const & excludes,
                                   long depth,
                                   roster_t const & roster,
                                   path_predicate<file_path> const & ignore)
  : restriction(includes, excludes, depth)
{
  map_nodes(node_map, roster, included_paths, excluded_paths, known_paths);
  add_parents(node_map, roster);
  validate_paths(included_paths, excluded_paths,
                 unknown_unignored_node(known_paths, ignore));
}

node_restriction::node_restriction(vector<file_path> const & includes,
                                   vector<file_path> const & excludes,
                                   long depth,
                                   roster_t const & roster1,
                                   roster_t const & roster2,
                                   path_predicate<file_path> const & ignore,
                                   include_rules const & rules)
  : restriction(includes, excludes, depth)
{
  map_nodes(node_map, roster1, included_paths, excluded_paths, known_paths);
  map_nodes(node_map, roster2, included_paths, excluded_paths, known_paths);

  if (rules == implicit_includes)
    {
      add_parents(node_map, roster1);
      add_parents(node_map, roster2);
    }

  validate_paths(included_paths, excluded_paths,
                 unknown_unignored_node(known_paths, ignore));
}

node_restriction::node_restriction(vector<file_path> const & includes,
                                   vector<file_path> const & excludes,
                                   long depth,
                                   parent_map const & rosters1,
                                   roster_t const & roster2,
                                   path_predicate<file_path> const & ignore)
  : restriction(includes, excludes, depth)
{
  for (parent_map::const_iterator i = rosters1.begin();
       i != rosters1.end(); i++)
    map_nodes(node_map, parent_roster(i),
              included_paths, excluded_paths, known_paths);
  map_nodes(node_map, roster2, included_paths, excluded_paths, known_paths);

  for (parent_map::const_iterator i = rosters1.begin();
       i != rosters1.end(); i++)
    add_parents(node_map, parent_roster(i));
  add_parents(node_map, roster2);

  validate_paths(included_paths, excluded_paths,
                 unknown_unignored_node(known_paths, ignore));
}

path_restriction::path_restriction(vector<file_path> const & includes,
                                   vector<file_path> const & excludes,
                                   long depth,
                                   path_predicate<file_path> const & ignore)
  : restriction(includes, excludes, depth)
{
  map_paths(path_map, included_paths, restricted_path::included);
  map_paths(path_map, excluded_paths, restricted_path::excluded);
  add_parents(path_map);
  validate_paths(included_paths, excluded_paths,
                 unknown_unignored_path(ignore));
}

path_restriction::path_restriction(vector<file_path> const & includes,
                                   vector<file_path> const & excludes,
                                   long depth,
                                   path_restriction::skip_check_t)
  : restriction(includes, excludes, depth)
{
  map_paths(path_map, included_paths, restricted_path::included);
  map_paths(path_map, excluded_paths, restricted_path::excluded);
  add_parents(path_map);
}

bool
node_restriction::includes(roster_t const & roster, node_id nid) const
{
  MM(roster);
  I(roster.has_node(nid));

  file_path fp;
  roster.get_name(nid, fp);

  if (empty())
    {
      if (depth != -1)
        {
          int path_depth = fp.depth();
          if (path_depth <= depth)
            {
              L(FL("depth includes nid %d path '%s'") % nid % fp);
              return true;
            }
          else
            {
              L(FL("depth excludes nid %d path '%s'") % nid % fp);
              return false;
            }
        }
      else
        {
          // don't log this, we end up using rather a bit of cpu time just
          // in the logging code, for totally unrestricted operations.
          return true;
        }
    }

  node_id current = nid;
  int path_depth = 0;

  while (!null_node(current) && (depth == -1 || path_depth <= depth))
    {
      node_status_iterator r = node_map.find(current);

      if (r != node_map.end())
        {
          switch (r->second)
            {
            case restricted_path::included:
            case restricted_path::included_required:
              L(FL("explicit include of nid %d path '%s'") % current % fp);
              return true;

            case restricted_path::excluded:
              L(FL("explicit exclude of nid %d path '%s'") % current % fp);
              return false;

            case restricted_path::required:
            case restricted_path::excluded_required:
              if (path_depth == 0)
                {
                  L(FL("implicit include of nid %d path '%s'") % current % fp);
                  return true;
                }
              else
                {
                  return false;
                }
            }
        }

      const_node_t node = roster.get_node(current);
      current = node->parent;
      path_depth++;
    }

  if (included_paths.empty())
    {
      L(FL("default include of nid %d path '%s'")
        % nid % fp);
      return true;
    }
  else
    {
      L(FL("(debug) default exclude of nid %d path '%s'")
        % nid % fp);
      return false;
    }
}

bool
path_restriction::includes(file_path const & pth) const
{
  if (empty())
    {
      if (depth != -1)
        {
          int path_depth = pth.depth();
          if (path_depth <= depth)
            {
              L(FL("depth includes path '%s'") % pth);
              return true;
            }
          else
            {
              L(FL("depth excludes path '%s'") % pth);
              return false;
            }
        }
      else
        {
          L(FL("empty include of path '%s'") % pth);
          return true;
        }
    }

  int path_depth = 0;
  file_path fp = pth;
  while (depth == -1 || path_depth <= depth)
    {
      path_status_iterator r = path_map.find(fp);

      if (r != path_map.end())
        {
          switch (r->second)
            {
            case restricted_path::included:
            case restricted_path::included_required:
              L(FL("explicit include of path '%s'") % pth);
              return true;

            case restricted_path::excluded:
              L(FL("explicit exclude of path '%s'") % pth);
              return false;

            case restricted_path::required:
            case restricted_path::excluded_required:
              if (path_depth == 0)
                {
                  L(FL("implicit include of path '%s'") % pth);
                  return true;
                }
              else
                {
                  return false;
                }
            }
        }

      if (fp.empty())
        break;
      fp = fp.dirname();
      path_depth++;
    }

  if (included_paths.empty())
    {
      L(FL("default include of path '%s'") % pth);
      return true;
    }
  else
    {
      L(FL("default exclude of path '%s'") % pth);
      return false;
    }
}

// Local Variables:
// mode: C++
// fill-column: 76
// c-file-style: "gnu"
// indent-tabs-mode: nil
// End:
// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s: