1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "partition_alloc/partition_alloc_base/files/file_path.h"
6
7 #include <algorithm>
8 #include <cstring>
9
10 #include "partition_alloc/partition_alloc_base/check.h"
11
12 #if BUILDFLAG(IS_WIN)
13 #include <windows.h>
14 #elif BUILDFLAG(IS_APPLE)
15 #include <CoreFoundation/CoreFoundation.h>
16 #endif
17
18 namespace partition_alloc::internal::base {
19
20 using StringType = FilePath::StringType;
21 const FilePath::CharType kStringTerminator = PA_FILE_PATH_LITERAL('\0');
22
23 // If this FilePath contains a drive letter specification, returns the
24 // position of the last character of the drive letter specification,
25 // otherwise returns npos. This can only be true on Windows, when a pathname
26 // begins with a letter followed by a colon. On other platforms, this always
27 // returns npos.
FindDriveLetter(const StringType & path)28 StringType::size_type FindDriveLetter(const StringType& path) {
29 #if defined(PA_FILE_PATH_USES_DRIVE_LETTERS)
30 // This is dependent on an ASCII-based character set, but that's a
31 // reasonable assumption. iswalpha can be too inclusive here.
32 if (path.length() >= 2 && path[1] == L':' &&
33 ((path[0] >= L'A' && path[0] <= L'Z') ||
34 (path[0] >= L'a' && path[0] <= L'z'))) {
35 return 1;
36 }
37 #endif // PA_FILE_PATH_USES_DRIVE_LETTERS
38 return StringType::npos;
39 }
40
IsPathAbsolute(const StringType & path)41 bool IsPathAbsolute(const StringType& path) {
42 #if defined(PA_FILE_PATH_USES_DRIVE_LETTERS)
43 StringType::size_type letter = FindDriveLetter(path);
44 if (letter != StringType::npos) {
45 // Look for a separator right after the drive specification.
46 return path.length() > letter + 1 &&
47 FilePath::IsSeparator(path[letter + 1]);
48 }
49 // Look for a pair of leading separators.
50 return path.length() > 1 && FilePath::IsSeparator(path[0]) &&
51 FilePath::IsSeparator(path[1]);
52 #else // PA_FILE_PATH_USES_DRIVE_LETTERS
53 // Look for a separator in the first position.
54 return path.length() > 0 && FilePath::IsSeparator(path[0]);
55 #endif // PA_FILE_PATH_USES_DRIVE_LETTERS
56 }
57
58 FilePath::FilePath() = default;
59
60 FilePath::FilePath(const FilePath& that) = default;
61 FilePath::FilePath(FilePath&& that) noexcept = default;
62
FilePath(const StringType & path)63 FilePath::FilePath(const StringType& path) : path_(path) {
64 StringType::size_type nul_pos = path_.find(kStringTerminator);
65 if (nul_pos != StringType::npos) {
66 path_.erase(nul_pos, StringType::npos);
67 }
68 }
69
70 FilePath::~FilePath() = default;
71
72 FilePath& FilePath::operator=(const FilePath& that) = default;
73
74 FilePath& FilePath::operator=(FilePath&& that) noexcept = default;
75
76 // static
IsSeparator(CharType character)77 bool FilePath::IsSeparator(CharType character) {
78 for (size_t i = 0; i < kSeparatorsLength - 1; ++i) {
79 if (character == kSeparators[i]) {
80 return true;
81 }
82 }
83
84 return false;
85 }
86
Append(const StringType & component) const87 FilePath FilePath::Append(const StringType& component) const {
88 StringType appended = component;
89 StringType without_nuls;
90
91 StringType::size_type nul_pos = component.find(kStringTerminator);
92 if (nul_pos != StringType::npos) {
93 without_nuls = component.substr(0, nul_pos);
94 appended = without_nuls;
95 }
96
97 PA_BASE_DCHECK(!IsPathAbsolute(appended));
98
99 if (path_.compare(kCurrentDirectory) == 0 && !appended.empty()) {
100 // Append normally doesn't do any normalization, but as a special case,
101 // when appending to kCurrentDirectory, just return a new path for the
102 // component argument. Appending component to kCurrentDirectory would
103 // serve no purpose other than needlessly lengthening the path, and
104 // it's likely in practice to wind up with FilePath objects containing
105 // only kCurrentDirectory when calling DirName on a single relative path
106 // component.
107 return FilePath(appended);
108 }
109
110 FilePath new_path(path_);
111 new_path.StripTrailingSeparatorsInternal();
112
113 // Don't append a separator if the path is empty (indicating the current
114 // directory) or if the path component is empty (indicating nothing to
115 // append).
116 if (!appended.empty() && !new_path.path_.empty()) {
117 // Don't append a separator if the path still ends with a trailing
118 // separator after stripping (indicating the root directory).
119 if (!IsSeparator(new_path.path_.back())) {
120 // Don't append a separator if the path is just a drive letter.
121 if (FindDriveLetter(new_path.path_) + 1 != new_path.path_.length()) {
122 new_path.path_.append(1, kSeparators[0]);
123 }
124 }
125 }
126
127 new_path.path_.append(appended);
128 return new_path;
129 }
130
Append(const FilePath & component) const131 FilePath FilePath::Append(const FilePath& component) const {
132 return Append(component.value());
133 }
134
StripTrailingSeparatorsInternal()135 void FilePath::StripTrailingSeparatorsInternal() {
136 // If there is no drive letter, start will be 1, which will prevent stripping
137 // the leading separator if there is only one separator. If there is a drive
138 // letter, start will be set appropriately to prevent stripping the first
139 // separator following the drive letter, if a separator immediately follows
140 // the drive letter.
141 StringType::size_type start = FindDriveLetter(path_) + 2;
142
143 StringType::size_type last_stripped = StringType::npos;
144 for (StringType::size_type pos = path_.length();
145 pos > start && IsSeparator(path_[pos - 1]); --pos) {
146 // If the string only has two separators and they're at the beginning,
147 // don't strip them, unless the string began with more than two separators.
148 if (pos != start + 1 || last_stripped == start + 2 ||
149 !IsSeparator(path_[start - 1])) {
150 path_.resize(pos - 1);
151 last_stripped = pos;
152 }
153 }
154 }
155
156 } // namespace partition_alloc::internal::base
157