Skip to content

Commit

Permalink
Move FileEnumerator to its own file, do some refactoring.
Browse files Browse the repository at this point in the history
It creates a class FileInfo to contain the details rather than using a platform-specific typedef. This allows the accessors GetName, GetSize, etc. to be moved directly to this class (previously they were static helpers on the FileEnumerator class) which makes a bunch of code much cleaner. It also gives reasonable getting and initialization which the previous version lacked.

BUG=175002

Reland of 198820 and 298824
Original review = https://codereview.chromium.org/13165005
R=rvargas@chromium.org

Review URL: https://codereview.chromium.org/16392011

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@205019 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
brettw@chromium.org committed Jun 8, 2013
1 parent aff39ac commit 25a4c1c
Show file tree
Hide file tree
Showing 110 changed files with 976 additions and 917 deletions.
5 changes: 5 additions & 0 deletions base/base.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@
'files/dir_reader_fallback.h',
'files/dir_reader_linux.h',
'files/dir_reader_posix.h',
'files/file_enumerator.cc',
'files/file_enumerator.h',
'files/file_enumerator_posix.cc',
'files/file_enumerator_win.cc',
'files/file_path.cc',
'files/file_path.h',
'files/file_path_constants.cc',
Expand Down Expand Up @@ -663,6 +667,7 @@
'file_util.cc',
'file_util_posix.cc',
'file_util_proxy.cc',
'files/file_enumerator_posix.cc',
'files/file_path_watcher_kqueue.cc',
'memory/shared_memory_posix.cc',
'native_library_posix.cc',
Expand Down
29 changes: 5 additions & 24 deletions base/file_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@

#include <fstream>

#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/string_util.h"
#include "base/stringprintf.h"
#include "base/strings/string_piece.h"
#include "base/strings/utf_string_conversions.h"

using base::FileEnumerator;
using base::FilePath;

namespace {
Expand Down Expand Up @@ -170,7 +172,7 @@ bool ReadFileToString(const FilePath& path, std::string* contents) {
bool IsDirectoryEmpty(const FilePath& dir_path) {
FileEnumerator files(dir_path, false,
FileEnumerator::FILES | FileEnumerator::DIRECTORIES);
if (files.Next().value().empty())
if (files.Next().empty())
return true;
return false;
}
Expand Down Expand Up @@ -267,30 +269,9 @@ int GetUniquePathNumber(
int64 ComputeDirectorySize(const FilePath& root_path) {
int64 running_size = 0;
FileEnumerator file_iter(root_path, true, FileEnumerator::FILES);
for (FilePath current = file_iter.Next(); !current.empty();
current = file_iter.Next()) {
FileEnumerator::FindInfo info;
file_iter.GetFindInfo(&info);
#if defined(OS_WIN)
LARGE_INTEGER li = { info.nFileSizeLow, info.nFileSizeHigh };
running_size += li.QuadPart;
#else
running_size += info.stat.st_size;
#endif
}
while (!file_iter.Next().empty())
running_size += file_iter.GetInfo().GetSize();
return running_size;
}

///////////////////////////////////////////////
// FileEnumerator
//
// Note: the main logic is in file_util_<platform>.cc

bool FileEnumerator::ShouldSkip(const FilePath& path) {
FilePath::StringType basename = path.BaseName().value();
return basename == FILE_PATH_LITERAL(".") ||
(basename == FILE_PATH_LITERAL("..") &&
!(INCLUDE_DOT_DOT & file_type_));
}

} // namespace
114 changes: 3 additions & 111 deletions base/file_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include <stdio.h>

#include <set>
#include <stack>
#include <string>
#include <vector>

Expand Down Expand Up @@ -123,9 +122,9 @@ BASE_EXPORT bool CopyFileUnsafe(const base::FilePath& from_path,

// Copies the given path, and optionally all subdirectories and their contents
// as well.
// If there are files existing under to_path, always overwrite.
// Returns true if successful, false otherwise.
// Don't use wildcards on the names, it may stop working without notice.
//
// If there are files existing under to_path, always overwrite. Returns true
// if successful, false otherwise. Wildcards on the names are not supported.
//
// If you only need to copy a file use CopyFile, it's faster.
BASE_EXPORT bool CopyDirectory(const base::FilePath& from_path,
Expand Down Expand Up @@ -428,113 +427,6 @@ class ScopedFDClose {
typedef scoped_ptr_malloc<int, ScopedFDClose> ScopedFD;
#endif // OS_POSIX

// A class for enumerating the files in a provided path. The order of the
// results is not guaranteed.
//
// DO NOT USE FROM THE MAIN THREAD of your application unless it is a test
// program where latency does not matter. This class is blocking.
class BASE_EXPORT FileEnumerator {
public:
#if defined(OS_WIN)
typedef WIN32_FIND_DATA FindInfo;
#elif defined(OS_POSIX)
typedef struct {
struct stat stat;
std::string filename;
} FindInfo;
#endif

enum FileType {
FILES = 1 << 0,
DIRECTORIES = 1 << 1,
INCLUDE_DOT_DOT = 1 << 2,
#if defined(OS_POSIX)
SHOW_SYM_LINKS = 1 << 4,
#endif
};

// |root_path| is the starting directory to search for. It may or may not end
// in a slash.
//
// If |recursive| is true, this will enumerate all matches in any
// subdirectories matched as well. It does a breadth-first search, so all
// files in one directory will be returned before any files in a
// subdirectory.
//
// |file_type|, a bit mask of FileType, specifies whether the enumerator
// should match files, directories, or both.
//
// |pattern| is an optional pattern for which files to match. This
// works like shell globbing. For example, "*.txt" or "Foo???.doc".
// However, be careful in specifying patterns that aren't cross platform
// since the underlying code uses OS-specific matching routines. In general,
// Windows matching is less featureful than others, so test there first.
// If unspecified, this will match all files.
// NOTE: the pattern only matches the contents of root_path, not files in
// recursive subdirectories.
// TODO(erikkay): Fix the pattern matching to work at all levels.
FileEnumerator(const base::FilePath& root_path,
bool recursive,
int file_type);
FileEnumerator(const base::FilePath& root_path,
bool recursive,
int file_type,
const base::FilePath::StringType& pattern);
~FileEnumerator();

// Returns an empty string if there are no more results.
base::FilePath Next();

// Write the file info into |info|.
void GetFindInfo(FindInfo* info);

// Looks inside a FindInfo and determines if it's a directory.
static bool IsDirectory(const FindInfo& info);

static base::FilePath GetFilename(const FindInfo& find_info);
static int64 GetFilesize(const FindInfo& find_info);
static base::Time GetLastModifiedTime(const FindInfo& find_info);

private:
// Returns true if the given path should be skipped in enumeration.
bool ShouldSkip(const base::FilePath& path);


#if defined(OS_WIN)
// True when find_data_ is valid.
bool has_find_data_;
WIN32_FIND_DATA find_data_;
HANDLE find_handle_;
#elif defined(OS_POSIX)
struct DirectoryEntryInfo {
base::FilePath filename;
struct stat stat;
};

// Read the filenames in source into the vector of DirectoryEntryInfo's
static bool ReadDirectory(std::vector<DirectoryEntryInfo>* entries,
const base::FilePath& source, bool show_links);

// The files in the current directory
std::vector<DirectoryEntryInfo> directory_entries_;

// The next entry to use from the directory_entries_ vector
size_t current_directory_entry_;
#endif

base::FilePath root_path_;
bool recursive_;
int file_type_;
base::FilePath::StringType pattern_; // Empty when we want to find
// everything.

// A stack that keeps track of which subdirectories we still need to
// enumerate in the breadth-first search.
std::stack<base::FilePath> pending_paths_;

DISALLOW_COPY_AND_ASSIGN(FileEnumerator);
};

#if defined(OS_LINUX)
// Broad categories of file systems as returned by statfs() on Linux.
enum FileSystemType {
Expand Down
Loading

0 comments on commit 25a4c1c

Please sign in to comment.