summaryrefslogtreecommitdiff
path: root/chromium/base/files/safe_base_name.cc
blob: cc0cc8de4a5e7496abc26c5c8d9e746081306f70 (plain)
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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/files/safe_base_name.h"

namespace base {

// static
absl::optional<SafeBaseName> SafeBaseName::Create(const FilePath& path) {
  auto basename = path.BaseName();

  if (!basename.IsAbsolute() && !basename.ReferencesParent() &&
      !basename.EndsWithSeparator()) {
    return absl::make_optional(SafeBaseName(basename));
  }

  return absl::nullopt;
}

// static
absl::optional<SafeBaseName> SafeBaseName::Create(
    FilePath::StringPieceType path) {
  return Create(FilePath(path));
}

SafeBaseName::SafeBaseName(const FilePath& path) : path_(path) {}

bool SafeBaseName::operator==(const SafeBaseName& that) const {
  return path_ == that.path_;
}

}  // namespace base