OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_util.h" | 5 #include "base/file_util.h" |
6 | 6 |
7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
8 #include <io.h> | 8 #include <io.h> |
9 #endif | 9 #endif |
10 #include <stdio.h> | 10 #include <stdio.h> |
11 | 11 |
12 #include <fstream> | 12 #include <fstream> |
13 | 13 |
14 #include "base/file_path.h" | 14 #include "base/file_path.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/stringprintf.h" |
16 #include "base/string_piece.h" | 17 #include "base/string_piece.h" |
17 #include "base/string_util.h" | 18 #include "base/string_util.h" |
18 #include "base/utf_string_conversions.h" | 19 #include "base/utf_string_conversions.h" |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
22 const FilePath::CharType kExtensionSeparator = FILE_PATH_LITERAL('.'); | 23 const FilePath::CharType kExtensionSeparator = FILE_PATH_LITERAL('.'); |
23 | 24 |
| 25 // The maximum number of 'uniquified' files we will try to create. |
| 26 // This is used when the filename we're trying to download is already in use, |
| 27 // so we create a new unique filename by appending " (nnn)" before the |
| 28 // extension, where 1 <= nnn <= kMaxUniqueFiles. |
| 29 // Also used by code that cleans up said files. |
| 30 static const int kMaxUniqueFiles = 100; |
| 31 |
24 } // namespace | 32 } // namespace |
25 | 33 |
26 namespace file_util { | 34 namespace file_util { |
27 | 35 |
28 bool EndsWithSeparator(const FilePath& path) { | 36 bool EndsWithSeparator(const FilePath& path) { |
29 FilePath::StringType value = path.value(); | 37 FilePath::StringType value = path.value(); |
30 if (value.empty()) | 38 if (value.empty()) |
31 return false; | 39 return false; |
32 | 40 |
33 return FilePath::IsSeparator(value[value.size() - 1]); | 41 return FilePath::IsSeparator(value[value.size() - 1]); |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 if (_chsize(fd, current_offset) != 0) | 238 if (_chsize(fd, current_offset) != 0) |
231 return false; | 239 return false; |
232 #else | 240 #else |
233 int fd = fileno(file); | 241 int fd = fileno(file); |
234 if (ftruncate(fd, current_offset) != 0) | 242 if (ftruncate(fd, current_offset) != 0) |
235 return false; | 243 return false; |
236 #endif | 244 #endif |
237 return true; | 245 return true; |
238 } | 246 } |
239 | 247 |
| 248 int GetUniquePathNumber( |
| 249 const FilePath& path, |
| 250 const FilePath::StringType& suffix) { |
| 251 bool have_suffix = !suffix.empty(); |
| 252 if (!PathExists(path) && |
| 253 (!have_suffix || !PathExists(FilePath(path.value() + suffix)))) { |
| 254 return 0; |
| 255 } |
| 256 |
| 257 FilePath new_path; |
| 258 for (int count = 1; count <= kMaxUniqueFiles; ++count) { |
| 259 new_path = path.InsertBeforeExtensionASCII(StringPrintf(" (%d)", count)); |
| 260 if (!PathExists(new_path) && |
| 261 (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) { |
| 262 return count; |
| 263 } |
| 264 } |
| 265 |
| 266 return -1; |
| 267 } |
| 268 |
240 bool ContainsPath(const FilePath &parent, const FilePath& child) { | 269 bool ContainsPath(const FilePath &parent, const FilePath& child) { |
241 FilePath abs_parent = FilePath(parent); | 270 FilePath abs_parent = FilePath(parent); |
242 FilePath abs_child = FilePath(child); | 271 FilePath abs_child = FilePath(child); |
243 | 272 |
244 if (!file_util::AbsolutePath(&abs_parent) || | 273 if (!file_util::AbsolutePath(&abs_parent) || |
245 !file_util::AbsolutePath(&abs_child)) | 274 !file_util::AbsolutePath(&abs_child)) |
246 return false; | 275 return false; |
247 | 276 |
248 #if defined(OS_WIN) | 277 #if defined(OS_WIN) |
249 // file_util::AbsolutePath() does not flatten case on Windows, so we must do | 278 // file_util::AbsolutePath() does not flatten case on Windows, so we must do |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 // FileEnumerator | 422 // FileEnumerator |
394 // | 423 // |
395 // Note: the main logic is in file_util_<platform>.cc | 424 // Note: the main logic is in file_util_<platform>.cc |
396 | 425 |
397 bool FileEnumerator::ShouldSkip(const FilePath& path) { | 426 bool FileEnumerator::ShouldSkip(const FilePath& path) { |
398 FilePath::StringType basename = path.BaseName().value(); | 427 FilePath::StringType basename = path.BaseName().value(); |
399 return IsDot(path) || (IsDotDot(path) && !(INCLUDE_DOT_DOT & file_type_)); | 428 return IsDot(path) || (IsDotDot(path) && !(INCLUDE_DOT_DOT & file_type_)); |
400 } | 429 } |
401 | 430 |
402 } // namespace | 431 } // namespace |
OLD | NEW |