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> | 23 #include <stack> |
24 #include <string> | 24 #include <string> |
25 #include <vector> | 25 #include <vector> |
26 | 26 |
27 #include "base/base_export.h" | 27 #include "base/base_export.h" |
28 #include "base/basictypes.h" | 28 #include "base/basictypes.h" |
29 #include "base/files/file_path.h" | 29 #include "base/files/file_path.h" |
| 30 #include "base/files/file_enumerator.h" |
30 #include "base/memory/scoped_ptr.h" | 31 #include "base/memory/scoped_ptr.h" |
31 #include "base/platform_file.h" | 32 #include "base/platform_file.h" |
32 #include "base/string16.h" | 33 #include "base/string16.h" |
33 | 34 |
34 #if defined(OS_POSIX) | 35 #if defined(OS_POSIX) |
35 #include "base/file_descriptor_posix.h" | 36 #include "base/file_descriptor_posix.h" |
36 #include "base/logging.h" | 37 #include "base/logging.h" |
37 #include "base/posix/eintr_wrapper.h" | 38 #include "base/posix/eintr_wrapper.h" |
38 #endif | 39 #endif |
39 | 40 |
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
446 if (x && *x >= 0) { | 447 if (x && *x >= 0) { |
447 if (HANDLE_EINTR(close(*x)) < 0) | 448 if (HANDLE_EINTR(close(*x)) < 0) |
448 DPLOG(ERROR) << "close"; | 449 DPLOG(ERROR) << "close"; |
449 } | 450 } |
450 } | 451 } |
451 }; | 452 }; |
452 | 453 |
453 typedef scoped_ptr_malloc<int, ScopedFDClose> ScopedFD; | 454 typedef scoped_ptr_malloc<int, ScopedFDClose> ScopedFD; |
454 #endif // OS_POSIX | 455 #endif // OS_POSIX |
455 | 456 |
456 // A class for enumerating the files in a provided path. The order of the | |
457 // results is not guaranteed. | |
458 // | |
459 // DO NOT USE FROM THE MAIN THREAD of your application unless it is a test | |
460 // program where latency does not matter. This class is blocking. | |
461 class BASE_EXPORT FileEnumerator { | |
462 public: | |
463 #if defined(OS_WIN) | |
464 typedef WIN32_FIND_DATA FindInfo; | |
465 #elif defined(OS_POSIX) | |
466 typedef struct { | |
467 struct stat stat; | |
468 std::string filename; | |
469 } FindInfo; | |
470 #endif | |
471 | |
472 enum FileType { | |
473 FILES = 1 << 0, | |
474 DIRECTORIES = 1 << 1, | |
475 INCLUDE_DOT_DOT = 1 << 2, | |
476 #if defined(OS_POSIX) | |
477 SHOW_SYM_LINKS = 1 << 4, | |
478 #endif | |
479 }; | |
480 | |
481 // |root_path| is the starting directory to search for. It may or may not end | |
482 // in a slash. | |
483 // | |
484 // If |recursive| is true, this will enumerate all matches in any | |
485 // subdirectories matched as well. It does a breadth-first search, so all | |
486 // files in one directory will be returned before any files in a | |
487 // subdirectory. | |
488 // | |
489 // |file_type|, a bit mask of FileType, specifies whether the enumerator | |
490 // should match files, directories, or both. | |
491 // | |
492 // |pattern| is an optional pattern for which files to match. This | |
493 // works like shell globbing. For example, "*.txt" or "Foo???.doc". | |
494 // However, be careful in specifying patterns that aren't cross platform | |
495 // since the underlying code uses OS-specific matching routines. In general, | |
496 // Windows matching is less featureful than others, so test there first. | |
497 // If unspecified, this will match all files. | |
498 // NOTE: the pattern only matches the contents of root_path, not files in | |
499 // recursive subdirectories. | |
500 // TODO(erikkay): Fix the pattern matching to work at all levels. | |
501 FileEnumerator(const base::FilePath& root_path, | |
502 bool recursive, | |
503 int file_type); | |
504 FileEnumerator(const base::FilePath& root_path, | |
505 bool recursive, | |
506 int file_type, | |
507 const base::FilePath::StringType& pattern); | |
508 ~FileEnumerator(); | |
509 | |
510 // Returns an empty string if there are no more results. | |
511 base::FilePath Next(); | |
512 | |
513 // Write the file info into |info|. | |
514 void GetFindInfo(FindInfo* info); | |
515 | |
516 // Looks inside a FindInfo and determines if it's a directory. | |
517 static bool IsDirectory(const FindInfo& info); | |
518 | |
519 static base::FilePath GetFilename(const FindInfo& find_info); | |
520 static int64 GetFilesize(const FindInfo& find_info); | |
521 static base::Time GetLastModifiedTime(const FindInfo& find_info); | |
522 | |
523 private: | |
524 // Returns true if the given path should be skipped in enumeration. | |
525 bool ShouldSkip(const base::FilePath& path); | |
526 | |
527 | |
528 #if defined(OS_WIN) | |
529 // True when find_data_ is valid. | |
530 bool has_find_data_; | |
531 WIN32_FIND_DATA find_data_; | |
532 HANDLE find_handle_; | |
533 #elif defined(OS_POSIX) | |
534 struct DirectoryEntryInfo { | |
535 base::FilePath filename; | |
536 struct stat stat; | |
537 }; | |
538 | |
539 // Read the filenames in source into the vector of DirectoryEntryInfo's | |
540 static bool ReadDirectory(std::vector<DirectoryEntryInfo>* entries, | |
541 const base::FilePath& source, bool show_links); | |
542 | |
543 // The files in the current directory | |
544 std::vector<DirectoryEntryInfo> directory_entries_; | |
545 | |
546 // The next entry to use from the directory_entries_ vector | |
547 size_t current_directory_entry_; | |
548 #endif | |
549 | |
550 base::FilePath root_path_; | |
551 bool recursive_; | |
552 int file_type_; | |
553 base::FilePath::StringType pattern_; // Empty when we want to find | |
554 // everything. | |
555 | |
556 // A stack that keeps track of which subdirectories we still need to | |
557 // enumerate in the breadth-first search. | |
558 std::stack<base::FilePath> pending_paths_; | |
559 | |
560 DISALLOW_COPY_AND_ASSIGN(FileEnumerator); | |
561 }; | |
562 | |
563 // Returns whether the file has been modified since a particular date. | 457 // Returns whether the file has been modified since a particular date. |
564 BASE_EXPORT bool HasFileBeenModifiedSince( | 458 BASE_EXPORT bool HasFileBeenModifiedSince( |
565 const FileEnumerator::FindInfo& find_info, | 459 const FileEnumerator::FindInfo& find_info, |
566 const base::Time& cutoff_time); | 460 const base::Time& cutoff_time); |
567 | 461 |
568 #if defined(OS_LINUX) | 462 #if defined(OS_LINUX) |
569 // Broad categories of file systems as returned by statfs() on Linux. | 463 // Broad categories of file systems as returned by statfs() on Linux. |
570 enum FileSystemType { | 464 enum FileSystemType { |
571 FILE_SYSTEM_UNKNOWN, // statfs failed. | 465 FILE_SYSTEM_UNKNOWN, // statfs failed. |
572 FILE_SYSTEM_0, // statfs.f_type == 0 means unknown, may indicate AFS. | 466 FILE_SYSTEM_0, // statfs.f_type == 0 means unknown, may indicate AFS. |
573 FILE_SYSTEM_ORDINARY, // on-disk filesystem like ext2 | 467 FILE_SYSTEM_ORDINARY, // on-disk filesystem like ext2 |
574 FILE_SYSTEM_NFS, | 468 FILE_SYSTEM_NFS, |
575 FILE_SYSTEM_SMB, | 469 FILE_SYSTEM_SMB, |
576 FILE_SYSTEM_CODA, | 470 FILE_SYSTEM_CODA, |
577 FILE_SYSTEM_MEMORY, // in-memory file system | 471 FILE_SYSTEM_MEMORY, // in-memory file system |
578 FILE_SYSTEM_CGROUP, // cgroup control. | 472 FILE_SYSTEM_CGROUP, // cgroup control. |
579 FILE_SYSTEM_OTHER, // any other value. | 473 FILE_SYSTEM_OTHER, // any other value. |
580 FILE_SYSTEM_TYPE_COUNT | 474 FILE_SYSTEM_TYPE_COUNT |
581 }; | 475 }; |
582 | 476 |
583 // Attempts determine the FileSystemType for |path|. | 477 // Attempts determine the FileSystemType for |path|. |
584 // Returns false if |path| doesn't exist. | 478 // Returns false if |path| doesn't exist. |
585 BASE_EXPORT bool GetFileSystemType(const base::FilePath& path, | 479 BASE_EXPORT bool GetFileSystemType(const base::FilePath& path, |
586 FileSystemType* type); | 480 FileSystemType* type); |
587 #endif | 481 #endif |
588 | 482 |
589 } // namespace file_util | 483 } // namespace file_util |
590 | 484 |
591 #endif // BASE_FILE_UTIL_H_ | 485 #endif // BASE_FILE_UTIL_H_ |
OLD | NEW |