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

Side by Side Diff: base/file_util.h

Issue 16392011: Move FileEnumerator to its own file, do some refactoring. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix incorrect includes Created 7 years, 6 months 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
« no previous file with comments | « base/base.gypi ('k') | base/file_util.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 BASE_EXPORT bool CopyFile(const base::FilePath& from_path, 115 BASE_EXPORT bool CopyFile(const base::FilePath& from_path,
117 const base::FilePath& to_path); 116 const base::FilePath& to_path);
118 117
119 // Same as CopyFile but allows paths with traversal components. 118 // Same as CopyFile but allows paths with traversal components.
120 // Use only with extreme care. 119 // Use only with extreme care.
121 BASE_EXPORT bool CopyFileUnsafe(const base::FilePath& from_path, 120 BASE_EXPORT bool CopyFileUnsafe(const base::FilePath& from_path,
122 const base::FilePath& to_path); 121 const base::FilePath& to_path);
123 122
124 // Copies the given path, and optionally all subdirectories and their contents 123 // Copies the given path, and optionally all subdirectories and their contents
125 // as well. 124 // as well.
126 // If there are files existing under to_path, always overwrite. 125 //
127 // Returns true if successful, false otherwise. 126 // If there are files existing under to_path, always overwrite. Returns true
128 // Don't use wildcards on the names, it may stop working without notice. 127 // if successful, false otherwise. Wildcards on the names are not supported.
129 // 128 //
130 // If you only need to copy a file use CopyFile, it's faster. 129 // If you only need to copy a file use CopyFile, it's faster.
131 BASE_EXPORT bool CopyDirectory(const base::FilePath& from_path, 130 BASE_EXPORT bool CopyDirectory(const base::FilePath& from_path,
132 const base::FilePath& to_path, 131 const base::FilePath& to_path,
133 bool recursive); 132 bool recursive);
134 133
135 // Returns true if the given path exists on the local filesystem, 134 // Returns true if the given path exists on the local filesystem,
136 // false otherwise. 135 // false otherwise.
137 BASE_EXPORT bool PathExists(const base::FilePath& path); 136 BASE_EXPORT bool PathExists(const base::FilePath& path);
138 137
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after
421 if (x && *x >= 0) { 420 if (x && *x >= 0) {
422 if (HANDLE_EINTR(close(*x)) < 0) 421 if (HANDLE_EINTR(close(*x)) < 0)
423 DPLOG(ERROR) << "close"; 422 DPLOG(ERROR) << "close";
424 } 423 }
425 } 424 }
426 }; 425 };
427 426
428 typedef scoped_ptr_malloc<int, ScopedFDClose> ScopedFD; 427 typedef scoped_ptr_malloc<int, ScopedFDClose> ScopedFD;
429 #endif // OS_POSIX 428 #endif // OS_POSIX
430 429
431 // A class for enumerating the files in a provided path. The order of the
432 // results is not guaranteed.
433 //
434 // DO NOT USE FROM THE MAIN THREAD of your application unless it is a test
435 // program where latency does not matter. This class is blocking.
436 class BASE_EXPORT FileEnumerator {
437 public:
438 #if defined(OS_WIN)
439 typedef WIN32_FIND_DATA FindInfo;
440 #elif defined(OS_POSIX)
441 typedef struct {
442 struct stat stat;
443 std::string filename;
444 } FindInfo;
445 #endif
446
447 enum FileType {
448 FILES = 1 << 0,
449 DIRECTORIES = 1 << 1,
450 INCLUDE_DOT_DOT = 1 << 2,
451 #if defined(OS_POSIX)
452 SHOW_SYM_LINKS = 1 << 4,
453 #endif
454 };
455
456 // |root_path| is the starting directory to search for. It may or may not end
457 // in a slash.
458 //
459 // If |recursive| is true, this will enumerate all matches in any
460 // subdirectories matched as well. It does a breadth-first search, so all
461 // files in one directory will be returned before any files in a
462 // subdirectory.
463 //
464 // |file_type|, a bit mask of FileType, specifies whether the enumerator
465 // should match files, directories, or both.
466 //
467 // |pattern| is an optional pattern for which files to match. This
468 // works like shell globbing. For example, "*.txt" or "Foo???.doc".
469 // However, be careful in specifying patterns that aren't cross platform
470 // since the underlying code uses OS-specific matching routines. In general,
471 // Windows matching is less featureful than others, so test there first.
472 // If unspecified, this will match all files.
473 // NOTE: the pattern only matches the contents of root_path, not files in
474 // recursive subdirectories.
475 // TODO(erikkay): Fix the pattern matching to work at all levels.
476 FileEnumerator(const base::FilePath& root_path,
477 bool recursive,
478 int file_type);
479 FileEnumerator(const base::FilePath& root_path,
480 bool recursive,
481 int file_type,
482 const base::FilePath::StringType& pattern);
483 ~FileEnumerator();
484
485 // Returns an empty string if there are no more results.
486 base::FilePath Next();
487
488 // Write the file info into |info|.
489 void GetFindInfo(FindInfo* info);
490
491 // Looks inside a FindInfo and determines if it's a directory.
492 static bool IsDirectory(const FindInfo& info);
493
494 static base::FilePath GetFilename(const FindInfo& find_info);
495 static int64 GetFilesize(const FindInfo& find_info);
496 static base::Time GetLastModifiedTime(const FindInfo& find_info);
497
498 private:
499 // Returns true if the given path should be skipped in enumeration.
500 bool ShouldSkip(const base::FilePath& path);
501
502
503 #if defined(OS_WIN)
504 // True when find_data_ is valid.
505 bool has_find_data_;
506 WIN32_FIND_DATA find_data_;
507 HANDLE find_handle_;
508 #elif defined(OS_POSIX)
509 struct DirectoryEntryInfo {
510 base::FilePath filename;
511 struct stat stat;
512 };
513
514 // Read the filenames in source into the vector of DirectoryEntryInfo's
515 static bool ReadDirectory(std::vector<DirectoryEntryInfo>* entries,
516 const base::FilePath& source, bool show_links);
517
518 // The files in the current directory
519 std::vector<DirectoryEntryInfo> directory_entries_;
520
521 // The next entry to use from the directory_entries_ vector
522 size_t current_directory_entry_;
523 #endif
524
525 base::FilePath root_path_;
526 bool recursive_;
527 int file_type_;
528 base::FilePath::StringType pattern_; // Empty when we want to find
529 // everything.
530
531 // A stack that keeps track of which subdirectories we still need to
532 // enumerate in the breadth-first search.
533 std::stack<base::FilePath> pending_paths_;
534
535 DISALLOW_COPY_AND_ASSIGN(FileEnumerator);
536 };
537
538 #if defined(OS_LINUX) 430 #if defined(OS_LINUX)
539 // Broad categories of file systems as returned by statfs() on Linux. 431 // Broad categories of file systems as returned by statfs() on Linux.
540 enum FileSystemType { 432 enum FileSystemType {
541 FILE_SYSTEM_UNKNOWN, // statfs failed. 433 FILE_SYSTEM_UNKNOWN, // statfs failed.
542 FILE_SYSTEM_0, // statfs.f_type == 0 means unknown, may indicate AFS. 434 FILE_SYSTEM_0, // statfs.f_type == 0 means unknown, may indicate AFS.
543 FILE_SYSTEM_ORDINARY, // on-disk filesystem like ext2 435 FILE_SYSTEM_ORDINARY, // on-disk filesystem like ext2
544 FILE_SYSTEM_NFS, 436 FILE_SYSTEM_NFS,
545 FILE_SYSTEM_SMB, 437 FILE_SYSTEM_SMB,
546 FILE_SYSTEM_CODA, 438 FILE_SYSTEM_CODA,
547 FILE_SYSTEM_MEMORY, // in-memory file system 439 FILE_SYSTEM_MEMORY, // in-memory file system
548 FILE_SYSTEM_CGROUP, // cgroup control. 440 FILE_SYSTEM_CGROUP, // cgroup control.
549 FILE_SYSTEM_OTHER, // any other value. 441 FILE_SYSTEM_OTHER, // any other value.
550 FILE_SYSTEM_TYPE_COUNT 442 FILE_SYSTEM_TYPE_COUNT
551 }; 443 };
552 444
553 // Attempts determine the FileSystemType for |path|. 445 // Attempts determine the FileSystemType for |path|.
554 // Returns false if |path| doesn't exist. 446 // Returns false if |path| doesn't exist.
555 BASE_EXPORT bool GetFileSystemType(const base::FilePath& path, 447 BASE_EXPORT bool GetFileSystemType(const base::FilePath& path,
556 FileSystemType* type); 448 FileSystemType* type);
557 #endif 449 #endif
558 450
559 } // namespace file_util 451 } // namespace file_util
560 452
561 #endif // BASE_FILE_UTIL_H_ 453 #endif // BASE_FILE_UTIL_H_
OLDNEW
« no previous file with comments | « base/base.gypi ('k') | base/file_util.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698