File: subtree_set.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (111 lines) | stat: -rw-r--r-- 3,643 bytes parent folder | download | duplicates (4)
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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/sync_file_system/subtree_set.h"

#include "base/check_op.h"
#include "base/containers/contains.h"
#include "base/notreached.h"
#include "storage/common/file_system/file_system_util.h"

namespace sync_file_system {

SubtreeSet::Node::Node()
    : contained_as_subtree_root(false),
      number_of_subtrees_below(0) {
}

SubtreeSet::Node::Node(bool contained_as_subtree_root,
                       size_t number_of_subtrees_below)
    : contained_as_subtree_root(contained_as_subtree_root),
      number_of_subtrees_below(number_of_subtrees_below) {
}

SubtreeSet::SubtreeSet() = default;
SubtreeSet::SubtreeSet(const SubtreeSet& other) = default;
SubtreeSet::~SubtreeSet() = default;

bool SubtreeSet::IsDisjointWith(const base::FilePath& subtree_root) const {
  base::FilePath::StringType normalized_subtree_root =
      storage::VirtualPath::GetNormalizedFilePath(subtree_root);

  // Check if |subtree_root| contains any of subtrees in the container.
  if (base::Contains(inclusive_ancestors_of_subtree_roots_,
                     normalized_subtree_root))
    return false;

  base::FilePath path(normalized_subtree_root);
  while (!storage::VirtualPath::IsRootPath(path)) {
    path = storage::VirtualPath::DirName(path);

    auto found = inclusive_ancestors_of_subtree_roots_.find(path.value());
    if (found != inclusive_ancestors_of_subtree_roots_.end())
      return !found->second.contained_as_subtree_root;
  }

  return true;
}

bool SubtreeSet::insert(const base::FilePath& subtree_root) {
  base::FilePath::StringType normalized_subtree_root =
      storage::VirtualPath::GetNormalizedFilePath(subtree_root);

  if (!IsDisjointWith(subtree_root))
    return false;
  inclusive_ancestors_of_subtree_roots_[normalized_subtree_root]
      = Node(true, 1);

  base::FilePath path(normalized_subtree_root);
  while (!storage::VirtualPath::IsRootPath(path)) {
    path = storage::VirtualPath::DirName(path);
    DCHECK(!inclusive_ancestors_of_subtree_roots_[path.value()]
                .contained_as_subtree_root);
    ++(inclusive_ancestors_of_subtree_roots_[path.value()]
           .number_of_subtrees_below);
  }

  return true;
}

bool SubtreeSet::erase(const base::FilePath& subtree_root) {
  base::FilePath::StringType normalized_subtree_root =
      storage::VirtualPath::GetNormalizedFilePath(subtree_root);

  {
    auto found =
        inclusive_ancestors_of_subtree_roots_.find(normalized_subtree_root);
    if (found == inclusive_ancestors_of_subtree_roots_.end() ||
        !found->second.contained_as_subtree_root)
      return false;

    DCHECK_EQ(1u, found->second.number_of_subtrees_below);
    inclusive_ancestors_of_subtree_roots_.erase(found);
  }

  base::FilePath path(normalized_subtree_root);
  while (!storage::VirtualPath::IsRootPath(path)) {
    path = storage::VirtualPath::DirName(path);

    auto found = inclusive_ancestors_of_subtree_roots_.find(path.value());
    if (found == inclusive_ancestors_of_subtree_roots_.end()) {
      NOTREACHED();
    }

    DCHECK(!found->second.contained_as_subtree_root);
    if (!--(found->second.number_of_subtrees_below))
      inclusive_ancestors_of_subtree_roots_.erase(found);
  }

  return true;
}

size_t SubtreeSet::size() const {
  auto found =
      inclusive_ancestors_of_subtree_roots_.find(storage::VirtualPath::kRoot);
  if (found == inclusive_ancestors_of_subtree_roots_.end())
    return 0;
  return found->second.number_of_subtrees_below;
}

}  // namespace sync_file_system