Skip to content

Commit

Permalink
Begin the first small step towards using FilePath everywhere:
Browse files Browse the repository at this point in the history
- Add some transition APIs.
- Start migrating some code to transition APIs.

Review URL: http://codereview.chromium.org/8825


git-svn-id: svn://svn.chromium.org/chrome/trunk/src@4254 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
evanm@google.com committed Oct 30, 2008
1 parent f92ed21 commit 640517f
Show file tree
Hide file tree
Showing 18 changed files with 475 additions and 312 deletions.
9 changes: 5 additions & 4 deletions base/base_paths.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "base/base_paths.h"

#include "base/file_path.h"
#include "base/file_util.h"
#include "base/path_service.h"

Expand All @@ -12,15 +13,15 @@ namespace base {
bool PathProvider(int key, std::wstring* result) {
// NOTE: DIR_CURRENT is a special cased in PathService::Get

std::wstring cur;
FilePath cur;
switch (key) {
case base::DIR_EXE:
PathService::Get(base::FILE_EXE, &cur);
file_util::TrimFilename(&cur);
cur = cur.DirName();
break;
case base::DIR_MODULE:
PathService::Get(base::FILE_MODULE, &cur);
file_util::TrimFilename(&cur);
cur = cur.DirName();
break;
case base::DIR_TEMP:
if (!file_util::GetTempDir(&cur))
Expand All @@ -30,7 +31,7 @@ bool PathProvider(int key, std::wstring* result) {
return false;
}

result->swap(cur);
*result = cur.ToWStringHack();
return true;
}

Expand Down
12 changes: 7 additions & 5 deletions base/base_paths_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <unistd.h>

#include "base/file_path.h"
#include "base/file_util.h"
#include "base/logging.h"
#include "base/path_service.h"
Expand All @@ -15,7 +16,7 @@
namespace base {

bool PathProviderLinux(int key, std::wstring* result) {
std::wstring cur;
FilePath path;
switch (key) {
case base::FILE_EXE:
case base::FILE_MODULE: { // TODO(evanm): is this correct?
Expand All @@ -32,10 +33,11 @@ bool PathProviderLinux(int key, std::wstring* result) {
case base::DIR_SOURCE_ROOT:
// On linux, unit tests execute two levels deep from the source root.
// For example: chrome/{Debug|Hammer}/net_unittest
PathService::Get(base::DIR_EXE, &cur);
file_util::UpOneDirectory(&cur);
file_util::UpOneDirectory(&cur);
*result = cur;
if (!PathService::Get(base::DIR_EXE, &path))
return false;
path = path.Append(FilePath::kParentDirectory)
.Append(FilePath::kParentDirectory);
*result = path.ToWStringHack();
return true;
}
return false;
Expand Down
28 changes: 28 additions & 0 deletions base/file_path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

#include "base/file_path.h"

// These includes are just for the *Hack functions, and should be removed
// when those functions are removed.
#include "base/string_piece.h"
#include "base/sys_string_conversions.h"

#if defined(FILE_PATH_USES_WIN_SEPARATORS)
const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("\\/");
#else // FILE_PATH_USES_WIN_SEPARATORS
Expand Down Expand Up @@ -152,6 +157,29 @@ bool FilePath::IsAbsolute() const {
#endif // FILE_PATH_USES_DRIVE_LETTERS
}

#if defined(OS_POSIX)
// See file_path.h for a discussion of the encoding of paths on POSIX
// platforms. These *Hack() functions are not quite correct, but they're
// only temporary while we fix the remainder of the code.
// Remember to remove the #includes at the top when you remove these.

// static
FilePath FilePath::FromWStringHack(const std::wstring& wstring) {
return FilePath(base::SysWideToNativeMB(wstring));
}
std::wstring FilePath::ToWStringHack() const {
return base::SysNativeMBToWide(path_);
}
#elif defined(OS_WIN)
// static
FilePath FilePath::FromWStringHack(const std::wstring& wstring) {
return FilePath(wstring);
}
std::wstring FilePath::ToWStringHack() const {
return path_;
}
#endif

void FilePath::StripTrailingSeparators() {
// If there is no drive letter, start will be 1, which will prevent stripping
// the leading separator if there is only one separator. If there is a drive
Expand Down
14 changes: 14 additions & 0 deletions base/file_path.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,20 @@ class FilePath {
// platforms, an absolute path begins with a separator character.
bool IsAbsolute() const;

// Older Chromium code assumes that paths are always wstrings.
// This function converts a wstring to a FilePath, and is useful to smooth
// porting that old code to the FilePath API.
// It has "Hack" in its name so people feel bad about using it.
// TODO(port): remove these functions.
static FilePath FromWStringHack(const std::wstring& wstring);

// Older Chromium code assumes that paths are always wstrings.
// This function produces a wstring from a FilePath, and is useful to smooth
// porting that old code to the FilePath API.
// It has "Hack" in its name so people feel bad about using it.
// TODO(port): remove these functions.
std::wstring ToWStringHack() const;

private:
// If this FilePath contains a drive letter specification, returns the
// position of the last character of the drive letter specification,
Expand Down
68 changes: 59 additions & 9 deletions base/file_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <fstream>

#include "base/file_path.h"
#include "base/logging.h"
#include "base/string_util.h"
#include "unicode/uniset.h"
Expand Down Expand Up @@ -229,20 +230,14 @@ void ReplaceExtension(std::wstring* file_name, const std::wstring& extension) {
file_name->swap(result);
}

bool ContentsEqual(const std::wstring& filename1,
const std::wstring& filename2) {
bool ContentsEqual(const FilePath& filename1, const FilePath& filename2) {
// We open the file in binary format even if they are text files because
// we are just comparing that bytes are exactly same in both files and not
// doing anything smart with text formatting.
#if defined(OS_WIN)
std::ifstream file1(filename1.c_str(), std::ios::in | std::ios::binary);
std::ifstream file2(filename2.c_str(), std::ios::in | std::ios::binary);
#elif defined(OS_POSIX)
std::ifstream file1(WideToUTF8(filename1).c_str(),
std::ifstream file1(filename1.value().c_str(),
std::ios::in | std::ios::binary);
std::ifstream file2(WideToUTF8(filename2).c_str(),
std::ifstream file2(filename2.value().c_str(),
std::ios::in | std::ios::binary);
#endif

// Even if both files aren't openable (and thus, in some sense, "equal"),
// any unusable file yields a result of "false".
Expand Down Expand Up @@ -300,5 +295,60 @@ bool CloseFile(FILE* file) {
return fclose(file) == 0;
}

// Deprecated functions ----------------------------------------------------

bool AbsolutePath(std::wstring* path_str) {
FilePath path;
if (!AbsolutePath(&path))
return false;
*path_str = path.ToWStringHack();
return true;
}
bool Delete(const std::wstring& path, bool recursive) {
return Delete(FilePath::FromWStringHack(path), recursive);
}
bool Move(const std::wstring& from_path, const std::wstring& to_path) {
return Move(FilePath::FromWStringHack(from_path),
FilePath::FromWStringHack(to_path));
}
bool CopyFile(const std::wstring& from_path, const std::wstring& to_path) {
return CopyFile(FilePath::FromWStringHack(from_path),
FilePath::FromWStringHack(to_path));
}
bool CopyDirectory(const std::wstring& from_path, const std::wstring& to_path,
bool recursive) {
return CopyDirectory(FilePath::FromWStringHack(from_path),
FilePath::FromWStringHack(to_path),
recursive);
}
bool PathExists(const std::wstring& path) {
return PathExists(FilePath::FromWStringHack(path));
}
bool DirectoryExists(const std::wstring& path) {
return DirectoryExists(FilePath::FromWStringHack(path));
}
bool ContentsEqual(const std::wstring& filename1,
const std::wstring& filename2) {
return ContentsEqual(FilePath::FromWStringHack(filename1),
FilePath::FromWStringHack(filename2));
}
bool CreateDirectory(const std::wstring& full_path) {
return CreateDirectory(FilePath::FromWStringHack(full_path));
}
bool GetCurrentDirectory(std::wstring* path_str) {
FilePath path;
if (!GetCurrentDirectory(&path))
return false;
*path_str = path.ToWStringHack();
return true;
}
bool GetTempDir(std::wstring* path_str) {
FilePath path;
if (!GetTempDir(&path))
return false;
*path_str = path.ToWStringHack();
return true;
}

} // namespace

28 changes: 28 additions & 0 deletions base/file_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

#include "base/basictypes.h"

class FilePath;

namespace file_util {

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -83,6 +85,8 @@ void AppendToPath(std::wstring* path, const std::wstring& new_ending);

// Convert provided relative path into an absolute path. Returns false on
// error.
bool AbsolutePath(FilePath* path);
// Deprecated temporary compatibility function.
bool AbsolutePath(std::wstring* path);

// Inserts |suffix| after the file name portion of |path| but before the
Expand Down Expand Up @@ -127,13 +131,19 @@ int CountFilesCreatedAfter(const std::wstring& path,
//
// WARNING: USING THIS WITH recursive==true IS EQUIVALENT
// TO "rm -rf", SO USE WITH CAUTION.
bool Delete(const FilePath& path, bool recursive);
// Deprecated temporary compatibility function.
bool Delete(const std::wstring& path, bool recursive);

// Moves the given path, whether it's a file or a directory.
// Returns true if successful, false otherwise.
bool Move(const FilePath& from_path, const FilePath& to_path);
// Deprecated temporary compatibility function.
bool Move(const std::wstring& from_path, const std::wstring& to_path);

// Copies a single file. Use CopyDirectory to copy directories.
bool CopyFile(const FilePath& from_path, const FilePath& to_path);
// Deprecated temporary compatibility function.
bool CopyFile(const std::wstring& from_path, const std::wstring& to_path);

// Copies the given path, and optionally all subdirectories and their contents
Expand All @@ -143,17 +153,24 @@ bool CopyFile(const std::wstring& from_path, const std::wstring& to_path);
// Dont't use wildcards on the names, it may stop working without notice.
//
// If you only need to copy a file use CopyFile, it's faster.
bool CopyDirectory(const FilePath& from_path, const FilePath& to_path,
bool recursive);
// Deprecated temporary compatibility function.
bool CopyDirectory(const std::wstring& from_path, const std::wstring& to_path,
bool recursive);

// Returns true if the given path exists on the local filesystem,
// false otherwise.
bool PathExists(const FilePath& path);
// Deprecated temporary compatibility function.
bool PathExists(const std::wstring& path);

// Returns true if the given path is writable by the user, false otherwise.
bool PathIsWritable(const std::wstring& path);

// Returns true if the given path exists and is a directory, false otherwise.
bool DirectoryExists(const FilePath& path);
// Deprecated temporary compatibility function.
bool DirectoryExists(const std::wstring& path);

#if defined(OS_WIN)
Expand All @@ -170,6 +187,9 @@ bool GetFileCreationLocalTimeFromHandle(HANDLE file_handle,

// Returns true if the contents of the two files given are equal, false
// otherwise. If either file can't be read, returns false.
bool ContentsEqual(const FilePath& filename1,
const FilePath& filename2);
// Deprecated temporary compatibility function.
bool ContentsEqual(const std::wstring& filename1,
const std::wstring& filename2);

Expand Down Expand Up @@ -216,6 +236,8 @@ bool IsDirectoryEmpty(const std::wstring& dir_path);


// Get the temporary directory provided by the system.
bool GetTempDir(FilePath* path);
// Deprecated temporary compatibility function.
bool GetTempDir(std::wstring* path);

// Creates a temporary file. The full path is placed in 'temp_file', and the
Expand All @@ -237,6 +259,8 @@ bool CreateNewTempDirectory(const std::wstring& prefix,
// Creates a directory, as well as creating any parent directories, if they
// don't exist. Returns 'true' on successful creation, or if the directory
// already exists.
bool CreateDirectory(const FilePath& full_path);
// Deprecated temporary compatibility function.
bool CreateDirectory(const std::wstring& full_path);

// Returns the file size. Returns true on success.
Expand All @@ -257,6 +281,8 @@ struct FileInfo {
bool GetFileInfo(const std::wstring& file_path, FileInfo* info);

// Wrapper for fopen-like calls. Returns non-NULL FILE* on success.
FILE* OpenFile(const FilePath& filename, const char* mode);
// Deprecated temporary compatibility functions.
FILE* OpenFile(const std::string& filename, const char* mode);
FILE* OpenFile(const std::wstring& filename, const char* mode);

Expand All @@ -272,6 +298,8 @@ int ReadFile(const std::wstring& filename, char* data, int size);
int WriteFile(const std::wstring& filename, const char* data, int size);

// Gets the current working directory for the process.
bool GetCurrentDirectory(FilePath* path);
// Deprecated temporary compatibility function.
bool GetCurrentDirectory(std::wstring* path);

// Sets the current working directory for the process.
Expand Down
13 changes: 7 additions & 6 deletions base/file_util_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,29 @@
#include <string>
#include <vector>

#include "base/file_path.h"
#include "base/logging.h"
#include "base/string_util.h"

namespace file_util {

const wchar_t kPathSeparator = L'/';

bool GetTempDir(std::wstring* path) {
bool GetTempDir(FilePath* path) {
const char* tmp = getenv("TMPDIR");
if (tmp)
*path = UTF8ToWide(tmp);
*path = FilePath(tmp);
else
*path = L"/tmp";
*path = FilePath("/tmp");
return true;
}

bool CopyFile(const std::wstring& from_path, const std::wstring& to_path) {
int infile = open(WideToUTF8(from_path).c_str(), O_RDONLY);
bool CopyFile(const FilePath& from_path, const FilePath& to_path) {
int infile = open(from_path.value().c_str(), O_RDONLY);
if (infile < 0)
return false;

int outfile = creat(WideToUTF8(to_path).c_str(), 0666);
int outfile = creat(to_path.value().c_str(), 0666);
if (outfile < 0) {
close(infile);
return false;
Expand Down
Loading

0 comments on commit 640517f

Please sign in to comment.