Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(41)

Side by Side Diff: base/file_path.cc

Issue 5754002: Moving away from shell api to support long path names on windows for filesystem. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/file_path.h" 5 #include "base/file_path.h"
6 6
7 #if defined(OS_WIN) 7 #if defined(OS_WIN)
8 #include <windows.h> 8 #include <windows.h>
9 #elif defined(OS_MACOSX) 9 #elif defined(OS_MACOSX)
10 #include <CoreFoundation/CoreFoundation.h> 10 #include <CoreFoundation/CoreFoundation.h>
(...skipping 16 matching lines...) Expand all
27 27
28 #if defined(FILE_PATH_USES_WIN_SEPARATORS) 28 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
29 const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("\\/"); 29 const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("\\/");
30 #else // FILE_PATH_USES_WIN_SEPARATORS 30 #else // FILE_PATH_USES_WIN_SEPARATORS
31 const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("/"); 31 const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("/");
32 #endif // FILE_PATH_USES_WIN_SEPARATORS 32 #endif // FILE_PATH_USES_WIN_SEPARATORS
33 33
34 const FilePath::CharType FilePath::kCurrentDirectory[] = FILE_PATH_LITERAL("."); 34 const FilePath::CharType FilePath::kCurrentDirectory[] = FILE_PATH_LITERAL(".");
35 const FilePath::CharType FilePath::kParentDirectory[] = FILE_PATH_LITERAL(".."); 35 const FilePath::CharType FilePath::kParentDirectory[] = FILE_PATH_LITERAL("..");
36 36
37 #if defined(OS_WIN)
38 const FilePath::CharType FilePath::kExtendedPathPrefix[] =
39 FILE_PATH_LITERAL("\\\\?\\");
40
41 const FilePath::CharType FilePath::kUNCExtendedPathPrefix[] =
42 FILE_PATH_LITERAL("\\\\?\\UNC\\");
43
44 const FilePath::CharType FilePath::kSharePrefix[] =
45 FILE_PATH_LITERAL("\\\\");
46 #endif
47
37 const FilePath::CharType FilePath::kExtensionSeparator = FILE_PATH_LITERAL('.'); 48 const FilePath::CharType FilePath::kExtensionSeparator = FILE_PATH_LITERAL('.');
38 49
39 typedef FilePath::StringType StringType; 50 typedef FilePath::StringType StringType;
40 51
41 namespace { 52 namespace {
42 53
43 const char* kCommonDoubleExtensions[] = { "gz", "z", "bz2" }; 54 const char* kCommonDoubleExtensions[] = { "gz", "z", "bz2" };
44 55
45 // If this FilePath contains a drive letter specification, returns the 56 // If this FilePath contains a drive letter specification, returns the
46 // position of the last character of the drive letter specification, 57 // position of the last character of the drive letter specification,
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 (last_separator == StringType::npos || 159 (last_separator == StringType::npos ||
149 penultimate_dot > last_separator) && 160 penultimate_dot > last_separator) &&
150 last_dot - penultimate_dot <= 5U && 161 last_dot - penultimate_dot <= 5U &&
151 last_dot - penultimate_dot > 1U) { 162 last_dot - penultimate_dot > 1U) {
152 return penultimate_dot; 163 return penultimate_dot;
153 } 164 }
154 165
155 return last_dot; 166 return last_dot;
156 } 167 }
157 168
169 #if defined(OS_WIN)
170 // TODO(kkanetkar): Taken from gears. When crbug.com/67384 is addressed
171 // this logic will be used in a different way such that the callers
172 // would not be aware of the extended path or UNC formats.
173 StringType ToLongFileNameHack(const FilePath::StringType& path) {
174 StringType long_path;
175 // This cast is for countering the compiler warning.
176 DWORD length_to_try = static_cast<DWORD>(path.length() * 2);
Erik does not do reviews 2010/12/23 17:57:27 I don't understand this *2 logic. Why not just pa
177 long_path.resize(length_to_try);
178 DWORD actual_length = GetLongPathName(path.c_str(),
179 &long_path.at(0),
180 length_to_try);
181 if (actual_length == 0) {
182 return path;
Erik does not do reviews 2010/12/23 17:57:27 I'm a bit nervous that this won't work in all case
183 } else if (actual_length <= length_to_try) {
184 long_path.resize(actual_length);
185 return long_path;
186 }
187
188 length_to_try = actual_length;
189 long_path.resize(length_to_try);
190 actual_length = GetLongPathName(path.c_str(),
191 &long_path.at(0),
192 length_to_try + 1);
193 if (actual_length > 0 && actual_length <= length_to_try) {
194 long_path.resize(actual_length);
195 return long_path;
196 } else {
197 return path;
198 }
199 }
200 #endif
201
158 } // namespace 202 } // namespace
159 203
160 FilePath::FilePath() { 204 FilePath::FilePath() {
161 } 205 }
162 206
163 FilePath::FilePath(const FilePath& that) : path_(that.path_) { 207 FilePath::FilePath(const FilePath& that) : path_(that.path_) {
164 } 208 }
165 209
166 FilePath::FilePath(const StringType& path) : path_(path) { 210 FilePath::FilePath(const StringType& path) : path_(path) {
167 } 211 }
(...skipping 1015 matching lines...) Expand 10 before | Expand all | Expand 10 after
1183 1227
1184 #if defined(FILE_PATH_USES_WIN_SEPARATORS) 1228 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
1185 FilePath FilePath::NormalizeWindowsPathSeparators() const { 1229 FilePath FilePath::NormalizeWindowsPathSeparators() const {
1186 StringType copy = path_; 1230 StringType copy = path_;
1187 for (size_t i = 1; i < arraysize(kSeparators); ++i) { 1231 for (size_t i = 1; i < arraysize(kSeparators); ++i) {
1188 std::replace(copy.begin(), copy.end(), kSeparators[i], kSeparators[0]); 1232 std::replace(copy.begin(), copy.end(), kSeparators[i], kSeparators[0]);
1189 } 1233 }
1190 return FilePath(copy); 1234 return FilePath(copy);
1191 } 1235 }
1192 #endif 1236 #endif
1237
1238 #if defined(OS_WIN)
1239 FilePath FilePath::GetLongPathHack() const {
1240 if (StartsWith(path_, kExtendedPathPrefix, false) ||
Erik does not do reviews 2010/12/23 17:57:27 why not call StartsWithExtendedPathOrUNCPrefix?
1241 StartsWith(path_, kUNCExtendedPathPrefix, false)) {
1242 return FilePath(path_);
1243 }
1244
1245 // Note that the 8.3 short file name can not be simply prefixed with the long
1246 // path prefix. It has to be converted to long file name first.
1247 if (StartsWith(path_, kSharePrefix, false)) {
1248 return FilePath(kUNCExtendedPathPrefix + ToLongFileNameHack(
1249 path_.substr(arraysize(FilePath::kSharePrefix) - 1)));
1250 } else {
1251 return FilePath(kExtendedPathPrefix + ToLongFileNameHack(path_));
1252 }
1253 }
1254 #endif
1255
1256 #if defined(OS_WIN)
1257 bool FilePath::StartsWithExtendedPathOrUNCPrefix() const {
1258 return StartsWith(path_, kExtendedPathPrefix, false) ||
1259 StartsWith(path_, kUNCExtendedPathPrefix, false);
1260 }
1261 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698