Permalink
Browse files

Part two of file_util porting. Almost all of the functionality has be…

…en ported, including the unit tests now. Some of this API isn't great, and should be cleaned up, but I'd like to hold off and do that in a followup changelist. More general code cleanup is likely needed here as well.

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@944 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information...
1 parent 8c6200f commit 85cd27aa833f81c2d4d9c83cc23736c67b199a69 erikkay@google.com committed Aug 15, 2008
@@ -102,6 +102,7 @@
A5A026550E4A214600498DA9 /* file_util.cc in Sources */ = {isa = PBXBuildFile; fileRef = A5A026540E4A214600498DA9 /* file_util.cc */; };
A5A0268E0E4A2BDC00498DA9 /* file_util_posix.cc in Sources */ = {isa = PBXBuildFile; fileRef = A5A0268D0E4A2BDC00498DA9 /* file_util_posix.cc */; };
A5A0270B0E4A630D00498DA9 /* file_util_mac.mm in Sources */ = {isa = PBXBuildFile; fileRef = A5A0270A0E4A630D00498DA9 /* file_util_mac.mm */; };
+ A5CE1D2B0E55F4D800AD0606 /* file_util_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = A5A0282D0E4CFA8500498DA9 /* file_util_unittest.cc */; };
ABF4B98F0DC2BA6900A6E319 /* base_paths_mac.mm in Sources */ = {isa = PBXBuildFile; fileRef = ABF4B98E0DC2BA6900A6E319 /* base_paths_mac.mm */; };
ABF4B99E0DC2BB6000A6E319 /* clipboard_mac.mm in Sources */ = {isa = PBXBuildFile; fileRef = ABF4B99D0DC2BB6000A6E319 /* clipboard_mac.mm */; };
ABF4B9AF0DC2BC6200A6E319 /* json_reader.cc in Sources */ = {isa = PBXBuildFile; fileRef = 8254031B0D92D1F40006B936 /* json_reader.cc */; };
@@ -1111,6 +1112,7 @@
files = (
7B78D38E0E54FE0100609465 /* at_exit_unittest.cc in Sources */,
7B78D38F0E54FE0100609465 /* command_line_unittest.cc in Sources */,
+ A5CE1D2B0E55F4D800AD0606 /* file_util_unittest.cc in Sources */,
7B78D3910E54FE0100609465 /* file_version_info_unittest.cc in Sources */,
7B78D3920E54FE0100609465 /* json_reader_unittest.cc in Sources */,
7B78D3930E54FE0100609465 /* json_writer_unittest.cc in Sources */,
View
@@ -51,13 +51,6 @@ bool PathProvider(int key, std::wstring* result) {
if (!file_util::GetTempDir(&cur))
return false;
break;
- case base::DIR_SOURCE_ROOT:
- // By default, unit tests execute two levels deep from the source root.
- // For example: chrome/{Debug|Release}/ui_tests.exe
- PathProvider(base::DIR_EXE, &cur);
- file_util::UpOneDirectory(&cur);
- file_util::UpOneDirectory(&cur);
- break;
default:
return false;
}
View
@@ -51,9 +51,6 @@ enum {
DIR_EXE, // directory containing FILE_EXE
DIR_MODULE, // directory containing FILE_MODULE
DIR_TEMP, // temporary directory
- DIR_SOURCE_ROOT, // Returns the root of the source tree. This key is useful
- // for tests that need to locate various resources. It
- // should not be used outside of test code.
PATH_END
};
View
@@ -42,10 +42,12 @@ enum {
FILE_EXE, // path and filename of the current executable
FILE_MODULE, // path and filename of the module containing the code for the
// PathService (which could differ from FILE_EXE if the
- // PathService were compiled into a DLL, for example)
- DIR_APP_DATA, // Application Data directory under the user profile.
- DIR_LOCAL_APP_DATA, // "Local Settings\Application Data" directory under the
- // user profile.
+ // PathService were compiled into a library, for example)
+ DIR_APP_DATA, // ~/Library/Application Support/Google/Chrome
+ DIR_LOCAL_APP_DATA, // same as above (can we remove?)
+ DIR_SOURCE_ROOT, // Returns the root of the source tree. This key is useful
+ // for tests that need to locate various resources. It
+ // should not be used outside of test code.
PATH_MAC_END
};
View
@@ -31,7 +31,9 @@
#import <Cocoa/Cocoa.h>
+#include "base/file_util.h"
#include "base/logging.h"
+#include "base/path_service.h"
#include "base/string_util.h"
namespace base {
@@ -61,6 +63,14 @@ bool PathProviderMac(int key, std::wstring* result) {
[path cStringUsingEncoding:NSUTF32StringEncoding]);
break;
}
+ case base::DIR_SOURCE_ROOT:
+ // On the mac, unit tests execute three levels deep from the source root.
+ // For example: chrome/build/{Debug|Release}/ui_tests
+ PathService::Get(base::DIR_EXE, &cur);
+ file_util::UpOneDirectory(&cur);
+ file_util::UpOneDirectory(&cur);
+ file_util::UpOneDirectory(&cur);
+ break;
default:
return false;
}
View
@@ -124,6 +124,13 @@ bool PathProviderWin(int key, std::wstring* result) {
return false;
cur = system_buffer;
break;
+ case base::DIR_SOURCE_ROOT:
+ // On Windows, unit tests execute two levels deep from the source root.
+ // For example: chrome/{Debug|Release}/ui_tests.exe
+ PathProvider(base::DIR_EXE, &cur);
+ file_util::UpOneDirectory(&cur);
+ file_util::UpOneDirectory(&cur);
+ break;
default:
return false;
}
View
@@ -56,6 +56,9 @@ enum {
DIR_LOCAL_APP_DATA_LOW, // Local AppData directory for low integrity level.
DIR_LOCAL_APP_DATA, // "Local Settings\Application Data" directory under the
// user profile.
+ DIR_SOURCE_ROOT, // Returns the root of the source tree. This key is useful
+ // for tests that need to locate various resources. It
+ // should not be used outside of test code.
PATH_WIN_END
};
View
@@ -30,7 +30,6 @@
#include "base/file_util.h"
#include <fstream>
-#include <string>
#include "base/logging.h"
#include "base/string_util.h"
@@ -40,12 +39,43 @@ namespace file_util {
const wchar_t kExtensionSeparator = L'.';
+void PathComponents(const std::wstring& path,
+ std::vector<std::wstring>* components) {
+ DCHECK(components != NULL);
+ if (components == NULL)
+ return;
+ std::wstring::size_type start = 0;
+ std::wstring::size_type end = path.find(kPathSeparator, start);
+
+ // Special case the "/" or "\" directory. On Windows with a drive letter,
+ // this code path won't hit, but the right thing should still happen.
+ // "E:\foo" will turn into "E:","foo".
+ if (end == start) {
+ components->push_back(std::wstring(path, 0, 1));
+ start = end + 1;
+ end = path.find(kPathSeparator, start);
+ }
+ while (end != std::wstring::npos) {
+ std::wstring component = std::wstring(path, start, end - start);
+ components->push_back(component);
+ start = end + 1;
+ end = path.find(kPathSeparator, start);
+ }
+ std::wstring component = std::wstring(path, start);
+ components->push_back(component);
+}
+
bool EndsWithSeparator(std::wstring* path) {
- return (!path->empty() && (*path)[path->length() - 1] == kPathSeparator);
+ return EndsWithSeparator(*path);
+}
+
+bool EndsWithSeparator(const std::wstring& path) {
+ bool is_sep = ((path)[path.length() - 1] == kPathSeparator);
+ return is_sep;
}
void TrimTrailingSeparator(std::wstring* dir) {
- while (EndsWithSeparator(dir))
+ while (dir->length() > 1 && EndsWithSeparator(dir))
dir->resize(dir->length() - 1);
}
@@ -77,9 +107,8 @@ void TrimFilename(std::wstring* path) {
}
}
-// TODO(mpcomplete): Make this platform-independent, etc.
std::wstring GetFilenameFromPath(const std::wstring& path) {
- std::wstring::size_type pos = path.find_last_of(L"\\/");
+ std::wstring::size_type pos = path.find_last_of(kPathSeparator);
return std::wstring(path, pos == std::wstring::npos ? 0 : pos+1);
}
@@ -180,6 +209,7 @@ void ReplaceIllegalCharacters(std::wstring* file_name, int replace_char) {
if (illegal_characters.contains(wstr[i])) {
(*file_name)[i] = replace_char;
}
+ ++i;
}
#else
#error wchar_t* should be either UTF-16 or UTF-32
View
@@ -37,10 +37,13 @@
#if defined(OS_WIN)
#include <windows.h>
+#elif defined(OS_POSIX)
+#include <fts.h>
#endif
#include <stack>
#include <string>
+#include <vector>
#include "base/basictypes.h"
@@ -55,9 +58,15 @@ extern const wchar_t kPathSeparator;
//-----------------------------------------------------------------------------
// Functions that operate purely on a path string w/o touching the filesystem:
+// Returns a vector of all of the components of the provided path.
+void PathComponents(const std::wstring& path,
+ std::vector<std::wstring>* components);
+
// Returns true if the given path ends with a path separator character.
+// TODO(erikkay): remove this pointer version
bool EndsWithSeparator(std::wstring* path);
-
+bool EndsWithSeparator(const std::wstring& path);
+
// Modifies a string by trimming all trailing separators from the end.
void TrimTrailingSeparator(std::wstring* dir);
@@ -188,6 +197,7 @@ bool ContentsEqual(const std::wstring& filename1,
// Useful for unit tests.
bool ReadFileToString(const std::wstring& path, std::string* contents);
+#if defined(OS_WINDOWS)
// Resolve Windows shortcut (.LNK file)
// Argument path specifies a valid LNK file. On success, return true and put
// the URL into path. If path is a invalid .LNK file, return false.
@@ -218,13 +228,16 @@ bool UpdateShortcutLink(const wchar_t *source, const wchar_t *destination,
const wchar_t *working_dir, const wchar_t *arguments,
const wchar_t *description, const wchar_t *icon,
int icon_index);
+#endif
+
// Get the temporary directory provided by the system.
bool GetTempDir(std::wstring* path);
-// Creates a temporary file name, but does it not create the file. It accesses
-// the disk to do this, however. The full path is placed in 'temp_file', and the
-// function returns true if was successful in creating the file name.
+// Creates a temporary file. The full path is placed in 'temp_file', and the
+// function returns true if was successful in creating the file. The file will
+// be empty and all handles closed after this function returns.
+// TODO(erikkay): rename this function and track down all of the callers.
bool CreateTemporaryFileName(std::wstring* temp_file);
// Create a new directory under TempPath. If prefix is provided, the new
@@ -275,9 +288,18 @@ class FileEnumerator {
// files in one directory will be returned before any files in a
// subdirectory.
//
- // The last parameter is an optional pattern for which files to match. This
- // works like a Windows file pattern. For example, "*.txt" or "Foo???.doc".
+ // |file_type| 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 std::wstring& root_path,
bool recursive,
FileEnumerator::FILE_TYPE file_type);
@@ -307,7 +329,9 @@ class FileEnumerator {
#if defined(OS_WIN)
WIN32_FIND_DATA find_data_;
HANDLE find_handle_;
-#endif // defined(OS_WIN)
+#elif defined(OS_POSIX)
+ FTS* fts_;
+#endif
DISALLOW_EVIL_CONSTRUCTORS(FileEnumerator);
};
Oops, something went wrong.

0 comments on commit 85cd27a

Please sign in to comment.