| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // This file contains utility functions for dealing with the local | 5 // This file contains utility functions for dealing with the local |
| 6 // filesystem. | 6 // filesystem. |
| 7 | 7 |
| 8 #ifndef BASE_FILE_UTIL_H_ | 8 #ifndef BASE_FILE_UTIL_H_ |
| 9 #define BASE_FILE_UTIL_H_ | 9 #define BASE_FILE_UTIL_H_ |
| 10 | 10 |
| 11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 12 | 12 |
| 13 #if defined(OS_WIN) | 13 #if defined(OS_WIN) |
| 14 #include <windows.h> | 14 #include <windows.h> |
| 15 #elif defined(OS_POSIX) | 15 #elif defined(OS_POSIX) |
| 16 #include <sys/stat.h> | 16 #include <sys/stat.h> |
| 17 #include <unistd.h> | 17 #include <unistd.h> |
| 18 #endif | 18 #endif |
| 19 | 19 |
| 20 #include <stdio.h> | 20 #include <stdio.h> |
| 21 | 21 |
| 22 #include <set> | 22 #include <set> |
| 23 #include <stack> | |
| 24 #include <string> | 23 #include <string> |
| 25 #include <vector> | 24 #include <vector> |
| 26 | 25 |
| 27 #include "base/base_export.h" | 26 #include "base/base_export.h" |
| 28 #include "base/basictypes.h" | 27 #include "base/basictypes.h" |
| 29 #include "base/files/file_path.h" | 28 #include "base/files/file_path.h" |
| 30 #include "base/memory/scoped_ptr.h" | 29 #include "base/memory/scoped_ptr.h" |
| 31 #include "base/platform_file.h" | 30 #include "base/platform_file.h" |
| 32 #include "base/string16.h" | 31 #include "base/string16.h" |
| 33 | 32 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 BASE_EXPORT bool CopyFile(const base::FilePath& from_path, | 109 BASE_EXPORT bool CopyFile(const base::FilePath& from_path, |
| 111 const base::FilePath& to_path); | 110 const base::FilePath& to_path); |
| 112 | 111 |
| 113 // Same as CopyFile but allows paths with traversal components. | 112 // Same as CopyFile but allows paths with traversal components. |
| 114 // Use only with extreme care. | 113 // Use only with extreme care. |
| 115 BASE_EXPORT bool CopyFileUnsafe(const base::FilePath& from_path, | 114 BASE_EXPORT bool CopyFileUnsafe(const base::FilePath& from_path, |
| 116 const base::FilePath& to_path); | 115 const base::FilePath& to_path); |
| 117 | 116 |
| 118 // Copies the given path, and optionally all subdirectories and their contents | 117 // Copies the given path, and optionally all subdirectories and their contents |
| 119 // as well. | 118 // as well. |
| 120 // If there are files existing under to_path, always overwrite. | 119 // |
| 121 // Returns true if successful, false otherwise. | 120 // If there are files existing under to_path, always overwrite. Returns true |
| 122 // Don't use wildcards on the names, it may stop working without notice. | 121 // if successful, false otherwise. Wildcards on the names are not supported. |
| 123 // | 122 // |
| 124 // If you only need to copy a file use CopyFile, it's faster. | 123 // If you only need to copy a file use CopyFile, it's faster. |
| 125 BASE_EXPORT bool CopyDirectory(const base::FilePath& from_path, | 124 BASE_EXPORT bool CopyDirectory(const base::FilePath& from_path, |
| 126 const base::FilePath& to_path, | 125 const base::FilePath& to_path, |
| 127 bool recursive); | 126 bool recursive); |
| 128 | 127 |
| 129 // Returns true if the given path exists on the local filesystem, | 128 // Returns true if the given path exists on the local filesystem, |
| 130 // false otherwise. | 129 // false otherwise. |
| 131 BASE_EXPORT bool PathExists(const base::FilePath& path); | 130 BASE_EXPORT bool PathExists(const base::FilePath& path); |
| 132 | 131 |
| (...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 415 if (x && *x >= 0) { | 414 if (x && *x >= 0) { |
| 416 if (HANDLE_EINTR(close(*x)) < 0) | 415 if (HANDLE_EINTR(close(*x)) < 0) |
| 417 DPLOG(ERROR) << "close"; | 416 DPLOG(ERROR) << "close"; |
| 418 } | 417 } |
| 419 } | 418 } |
| 420 }; | 419 }; |
| 421 | 420 |
| 422 typedef scoped_ptr_malloc<int, ScopedFDClose> ScopedFD; | 421 typedef scoped_ptr_malloc<int, ScopedFDClose> ScopedFD; |
| 423 #endif // OS_POSIX | 422 #endif // OS_POSIX |
| 424 | 423 |
| 425 // A class for enumerating the files in a provided path. The order of the | |
| 426 // results is not guaranteed. | |
| 427 // | |
| 428 // DO NOT USE FROM THE MAIN THREAD of your application unless it is a test | |
| 429 // program where latency does not matter. This class is blocking. | |
| 430 class BASE_EXPORT FileEnumerator { | |
| 431 public: | |
| 432 #if defined(OS_WIN) | |
| 433 typedef WIN32_FIND_DATA FindInfo; | |
| 434 #elif defined(OS_POSIX) | |
| 435 typedef struct { | |
| 436 struct stat stat; | |
| 437 std::string filename; | |
| 438 } FindInfo; | |
| 439 #endif | |
| 440 | |
| 441 enum FileType { | |
| 442 FILES = 1 << 0, | |
| 443 DIRECTORIES = 1 << 1, | |
| 444 INCLUDE_DOT_DOT = 1 << 2, | |
| 445 #if defined(OS_POSIX) | |
| 446 SHOW_SYM_LINKS = 1 << 4, | |
| 447 #endif | |
| 448 }; | |
| 449 | |
| 450 // |root_path| is the starting directory to search for. It may or may not end | |
| 451 // in a slash. | |
| 452 // | |
| 453 // If |recursive| is true, this will enumerate all matches in any | |
| 454 // subdirectories matched as well. It does a breadth-first search, so all | |
| 455 // files in one directory will be returned before any files in a | |
| 456 // subdirectory. | |
| 457 // | |
| 458 // |file_type|, a bit mask of FileType, specifies whether the enumerator | |
| 459 // should match files, directories, or both. | |
| 460 // | |
| 461 // |pattern| is an optional pattern for which files to match. This | |
| 462 // works like shell globbing. For example, "*.txt" or "Foo???.doc". | |
| 463 // However, be careful in specifying patterns that aren't cross platform | |
| 464 // since the underlying code uses OS-specific matching routines. In general, | |
| 465 // Windows matching is less featureful than others, so test there first. | |
| 466 // If unspecified, this will match all files. | |
| 467 // NOTE: the pattern only matches the contents of root_path, not files in | |
| 468 // recursive subdirectories. | |
| 469 // TODO(erikkay): Fix the pattern matching to work at all levels. | |
| 470 FileEnumerator(const base::FilePath& root_path, | |
| 471 bool recursive, | |
| 472 int file_type); | |
| 473 FileEnumerator(const base::FilePath& root_path, | |
| 474 bool recursive, | |
| 475 int file_type, | |
| 476 const base::FilePath::StringType& pattern); | |
| 477 ~FileEnumerator(); | |
| 478 | |
| 479 // Returns an empty string if there are no more results. | |
| 480 base::FilePath Next(); | |
| 481 | |
| 482 // Write the file info into |info|. | |
| 483 void GetFindInfo(FindInfo* info); | |
| 484 | |
| 485 // Looks inside a FindInfo and determines if it's a directory. | |
| 486 static bool IsDirectory(const FindInfo& info); | |
| 487 | |
| 488 static base::FilePath GetFilename(const FindInfo& find_info); | |
| 489 static int64 GetFilesize(const FindInfo& find_info); | |
| 490 static base::Time GetLastModifiedTime(const FindInfo& find_info); | |
| 491 | |
| 492 private: | |
| 493 // Returns true if the given path should be skipped in enumeration. | |
| 494 bool ShouldSkip(const base::FilePath& path); | |
| 495 | |
| 496 | |
| 497 #if defined(OS_WIN) | |
| 498 // True when find_data_ is valid. | |
| 499 bool has_find_data_; | |
| 500 WIN32_FIND_DATA find_data_; | |
| 501 HANDLE find_handle_; | |
| 502 #elif defined(OS_POSIX) | |
| 503 struct DirectoryEntryInfo { | |
| 504 base::FilePath filename; | |
| 505 struct stat stat; | |
| 506 }; | |
| 507 | |
| 508 // Read the filenames in source into the vector of DirectoryEntryInfo's | |
| 509 static bool ReadDirectory(std::vector<DirectoryEntryInfo>* entries, | |
| 510 const base::FilePath& source, bool show_links); | |
| 511 | |
| 512 // The files in the current directory | |
| 513 std::vector<DirectoryEntryInfo> directory_entries_; | |
| 514 | |
| 515 // The next entry to use from the directory_entries_ vector | |
| 516 size_t current_directory_entry_; | |
| 517 #endif | |
| 518 | |
| 519 base::FilePath root_path_; | |
| 520 bool recursive_; | |
| 521 int file_type_; | |
| 522 base::FilePath::StringType pattern_; // Empty when we want to find | |
| 523 // everything. | |
| 524 | |
| 525 // A stack that keeps track of which subdirectories we still need to | |
| 526 // enumerate in the breadth-first search. | |
| 527 std::stack<base::FilePath> pending_paths_; | |
| 528 | |
| 529 DISALLOW_COPY_AND_ASSIGN(FileEnumerator); | |
| 530 }; | |
| 531 | |
| 532 #if defined(OS_LINUX) | 424 #if defined(OS_LINUX) |
| 533 // Broad categories of file systems as returned by statfs() on Linux. | 425 // Broad categories of file systems as returned by statfs() on Linux. |
| 534 enum FileSystemType { | 426 enum FileSystemType { |
| 535 FILE_SYSTEM_UNKNOWN, // statfs failed. | 427 FILE_SYSTEM_UNKNOWN, // statfs failed. |
| 536 FILE_SYSTEM_0, // statfs.f_type == 0 means unknown, may indicate AFS. | 428 FILE_SYSTEM_0, // statfs.f_type == 0 means unknown, may indicate AFS. |
| 537 FILE_SYSTEM_ORDINARY, // on-disk filesystem like ext2 | 429 FILE_SYSTEM_ORDINARY, // on-disk filesystem like ext2 |
| 538 FILE_SYSTEM_NFS, | 430 FILE_SYSTEM_NFS, |
| 539 FILE_SYSTEM_SMB, | 431 FILE_SYSTEM_SMB, |
| 540 FILE_SYSTEM_CODA, | 432 FILE_SYSTEM_CODA, |
| 541 FILE_SYSTEM_MEMORY, // in-memory file system | 433 FILE_SYSTEM_MEMORY, // in-memory file system |
| 542 FILE_SYSTEM_CGROUP, // cgroup control. | 434 FILE_SYSTEM_CGROUP, // cgroup control. |
| 543 FILE_SYSTEM_OTHER, // any other value. | 435 FILE_SYSTEM_OTHER, // any other value. |
| 544 FILE_SYSTEM_TYPE_COUNT | 436 FILE_SYSTEM_TYPE_COUNT |
| 545 }; | 437 }; |
| 546 | 438 |
| 547 // Attempts determine the FileSystemType for |path|. | 439 // Attempts determine the FileSystemType for |path|. |
| 548 // Returns false if |path| doesn't exist. | 440 // Returns false if |path| doesn't exist. |
| 549 BASE_EXPORT bool GetFileSystemType(const base::FilePath& path, | 441 BASE_EXPORT bool GetFileSystemType(const base::FilePath& path, |
| 550 FileSystemType* type); | 442 FileSystemType* type); |
| 551 #endif | 443 #endif |
| 552 | 444 |
| 553 } // namespace file_util | 445 } // namespace file_util |
| 554 | 446 |
| 555 #endif // BASE_FILE_UTIL_H_ | 447 #endif // BASE_FILE_UTIL_H_ |
| OLD | NEW |