Skip to content

Commit

Permalink
Move GetFileSize, NormalizeFilePath to base namespace
Browse files Browse the repository at this point in the history
BUG=
R=viettrungluu@chromium.org

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@238722 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
brettw@chromium.org committed Dec 4, 2013
1 parent 1c6d404 commit 5628570
Show file tree
Hide file tree
Showing 77 changed files with 323 additions and 330 deletions.
16 changes: 8 additions & 8 deletions base/file_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,14 @@ bool CreateDirectory(const FilePath& full_path) {
return CreateDirectoryAndGetError(full_path, NULL);
}

bool GetFileSize(const FilePath& file_path, int64* file_size) {
PlatformFileInfo info;
if (!file_util::GetFileInfo(file_path, &info))
return false;
*file_size = info.size;
return true;
}

} // namespace base

// -----------------------------------------------------------------------------
Expand All @@ -175,14 +183,6 @@ using base::FileEnumerator;
using base::FilePath;
using base::kMaxUniqueFiles;

bool GetFileSize(const FilePath& file_path, int64* file_size) {
base::PlatformFileInfo info;
if (!GetFileInfo(file_path, &info))
return false;
*file_size = info.size;
return true;
}

bool TouchFile(const FilePath& path,
const base::Time& last_accessed,
const base::Time& last_modified) {
Expand Down
17 changes: 8 additions & 9 deletions base/file_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,23 +261,22 @@ BASE_EXPORT bool CreateDirectoryAndGetError(const FilePath& full_path,
// Backward-compatible convenience method for the above.
BASE_EXPORT bool CreateDirectory(const FilePath& full_path);

} // namespace base

// -----------------------------------------------------------------------------

namespace file_util {

// Returns the file size. Returns true on success.
BASE_EXPORT bool GetFileSize(const base::FilePath& file_path, int64* file_size);
BASE_EXPORT bool GetFileSize(const FilePath& file_path, int64* file_size);

// Sets |real_path| to |path| with symbolic links and junctions expanded.
// On windows, make sure the path starts with a lettered drive.
// |path| must reference a file. Function will fail if |path| points to
// a directory or to a nonexistent path. On windows, this function will
// fail if |path| is a junction or symlink that points to an empty file,
// or if |real_path| would be longer than MAX_PATH characters.
BASE_EXPORT bool NormalizeFilePath(const base::FilePath& path,
base::FilePath* real_path);
BASE_EXPORT bool NormalizeFilePath(const FilePath& path, FilePath* real_path);

} // namespace base

// -----------------------------------------------------------------------------

namespace file_util {

#if defined(OS_WIN)

Expand Down
33 changes: 16 additions & 17 deletions base/file_util_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,22 @@ bool CreateDirectoryAndGetError(const FilePath& full_path,
return true;
}

bool NormalizeFilePath(const FilePath& path, FilePath* normalized_path) {
FilePath real_path_result;
if (!RealPath(path, &real_path_result))
return false;

// To be consistant with windows, fail if |real_path_result| is a
// directory.
stat_wrapper_t file_info;
if (CallStat(real_path_result.value().c_str(), &file_info) != 0 ||
S_ISDIR(file_info.st_mode))
return false;

*normalized_path = real_path_result;
return true;
}

} // namespace base

// -----------------------------------------------------------------------------
Expand All @@ -637,7 +653,6 @@ using base::DirectoryExists;
using base::FileEnumerator;
using base::FilePath;
using base::MakeAbsoluteFilePath;
using base::RealPath;
using base::VerifySpecificPathControlledByUser;

base::FilePath MakeUniqueDirectory(const base::FilePath& path) {
Expand Down Expand Up @@ -804,22 +819,6 @@ bool SetCurrentDirectory(const FilePath& path) {
return !ret;
}

bool NormalizeFilePath(const FilePath& path, FilePath* normalized_path) {
FilePath real_path_result;
if (!RealPath(path, &real_path_result))
return false;

// To be consistant with windows, fail if |real_path_result| is a
// directory.
stat_wrapper_t file_info;
if (CallStat(real_path_result.value().c_str(), &file_info) != 0 ||
S_ISDIR(file_info.st_mode))
return false;

*normalized_path = real_path_result;
return true;
}

bool VerifyPathControlledByUser(const FilePath& base,
const FilePath& path,
uid_t owner_uid,
Expand Down
37 changes: 17 additions & 20 deletions base/file_util_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ TEST_F(FileUtilTest, FileAndDirectorySize) {
FilePath file_01 = temp_dir_.path().Append(FPL("The file 01.txt"));
CreateTextFile(file_01, L"12345678901234567890");
int64 size_f1 = 0;
ASSERT_TRUE(file_util::GetFileSize(file_01, &size_f1));
ASSERT_TRUE(GetFileSize(file_01, &size_f1));
EXPECT_EQ(20ll, size_f1);

FilePath subdir_path = temp_dir_.path().Append(FPL("Level2"));
Expand All @@ -259,7 +259,7 @@ TEST_F(FileUtilTest, FileAndDirectorySize) {
FilePath file_02 = subdir_path.Append(FPL("The file 02.txt"));
CreateTextFile(file_02, L"123456789012345678901234567890");
int64 size_f2 = 0;
ASSERT_TRUE(file_util::GetFileSize(file_02, &size_f2));
ASSERT_TRUE(GetFileSize(file_02, &size_f2));
EXPECT_EQ(30ll, size_f2);

FilePath subsubdir_path = subdir_path.Append(FPL("Level3"));
Expand All @@ -282,19 +282,16 @@ TEST_F(FileUtilTest, NormalizeFilePathBasic) {

FilePath normalized_file_a_path, normalized_file_b_path;
ASSERT_FALSE(PathExists(file_a_path));
ASSERT_FALSE(file_util::NormalizeFilePath(file_a_path,
&normalized_file_a_path))
ASSERT_FALSE(NormalizeFilePath(file_a_path, &normalized_file_a_path))
<< "NormalizeFilePath() should fail on nonexistent paths.";

CreateTextFile(file_a_path, bogus_content);
ASSERT_TRUE(PathExists(file_a_path));
ASSERT_TRUE(file_util::NormalizeFilePath(file_a_path,
&normalized_file_a_path));
ASSERT_TRUE(NormalizeFilePath(file_a_path, &normalized_file_a_path));

CreateTextFile(file_b_path, bogus_content);
ASSERT_TRUE(PathExists(file_b_path));
ASSERT_TRUE(file_util::NormalizeFilePath(file_b_path,
&normalized_file_b_path));
ASSERT_TRUE(NormalizeFilePath(file_b_path, &normalized_file_b_path));

// Beacuse this test created |dir_path|, we know it is not a link
// or junction. So, the real path of the directory holding file a
Expand Down Expand Up @@ -376,18 +373,18 @@ TEST_F(FileUtilTest, NormalizeFilePathReparsePoints) {
ASSERT_TRUE(reparse_to_sub_long.IsValid());

// Normalize a junction free path: base_a\sub_a\file.txt .
ASSERT_TRUE(file_util::NormalizeFilePath(file_txt, &normalized_path));
ASSERT_TRUE(NormalizeFilePath(file_txt, &normalized_path));
ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());

// Check that the path base_b\to_sub_a\file.txt can be normalized to exclude
// the junction to_sub_a.
ASSERT_TRUE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
ASSERT_TRUE(NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
&normalized_path));
ASSERT_STREQ(file_txt.value().c_str(), normalized_path.value().c_str());

// Check that the path base_b\to_base_b\to_base_b\to_sub_a\file.txt can be
// normalized to exclude junctions to_base_b and to_sub_a .
ASSERT_TRUE(file_util::NormalizeFilePath(base_b.Append(FPL("to_base_b"))
ASSERT_TRUE(NormalizeFilePath(base_b.Append(FPL("to_base_b"))
.Append(FPL("to_base_b"))
.Append(FPL("to_sub_a"))
.Append(FPL("file.txt")),
Expand All @@ -405,18 +402,18 @@ TEST_F(FileUtilTest, NormalizeFilePathReparsePoints) {
long_path = long_path.Append(FPL("to_sub_a"))
.Append(FPL("file.txt"));

ASSERT_FALSE(file_util::NormalizeFilePath(long_path, &normalized_path));
ASSERT_FALSE(NormalizeFilePath(long_path, &normalized_path));

// Normalizing the junction to deep.txt should fail, because the expanded
// path to deep.txt is longer than MAX_PATH.
ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_long.Append(deep_txt),
ASSERT_FALSE(NormalizeFilePath(to_sub_long.Append(deep_txt),
&normalized_path));

// Delete the reparse points, and see that NormalizeFilePath() fails
// to traverse them.
}

ASSERT_FALSE(file_util::NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
ASSERT_FALSE(NormalizeFilePath(to_sub_a.Append(FPL("file.txt")),
&normalized_path));
}

Expand Down Expand Up @@ -610,7 +607,7 @@ TEST_F(FileUtilTest, NormalizeFilePathSymlinks) {

// Check that NormalizeFilePath sees the link.
FilePath normalized_path;
ASSERT_TRUE(file_util::NormalizeFilePath(link_from, &normalized_path));
ASSERT_TRUE(NormalizeFilePath(link_from, &normalized_path));
EXPECT_NE(link_from, link_to);
EXPECT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
EXPECT_EQ(link_to.BaseName().value(), normalized_path.BaseName().value());
Expand All @@ -622,7 +619,7 @@ TEST_F(FileUtilTest, NormalizeFilePathSymlinks) {
ASSERT_TRUE(CreateSymbolicLink(link_to, link_from))
<< "Failed to create directory symlink.";

EXPECT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path))
EXPECT_FALSE(NormalizeFilePath(link_from, &normalized_path))
<< "Links to directories should return false.";

// Test that a loop in the links causes NormalizeFilePath() to return false.
Expand All @@ -634,7 +631,7 @@ TEST_F(FileUtilTest, NormalizeFilePathSymlinks) {
<< "Failed to create loop symlink b.";

// Infinite loop!
EXPECT_FALSE(file_util::NormalizeFilePath(link_from, &normalized_path));
EXPECT_FALSE(NormalizeFilePath(link_from, &normalized_path));
}
#endif // defined(OS_POSIX)

Expand Down Expand Up @@ -2318,7 +2315,7 @@ TEST_F(FileUtilTest, ValidContentUriTest) {
ASSERT_TRUE(PathExists(data_dir));
FilePath image_file = data_dir.Append(FILE_PATH_LITERAL("red.png"));
int64 image_size;
file_util::GetFileSize(image_file, &image_size);
GetFileSize(image_file, &image_size);
EXPECT_LT(0, image_size);

// Insert the image into MediaStore. MediaStore will do some conversions, and
Expand All @@ -2329,7 +2326,7 @@ TEST_F(FileUtilTest, ValidContentUriTest) {
// The file size may not equal to the input image as MediaStore may convert
// the image.
int64 content_uri_size;
file_util::GetFileSize(path, &content_uri_size);
GetFileSize(path, &content_uri_size);
EXPECT_EQ(image_size, content_uri_size);

// We should be able to read the file.
Expand All @@ -2346,7 +2343,7 @@ TEST_F(FileUtilTest, NonExistentContentUriTest) {
EXPECT_FALSE(PathExists(path));
// Size should be smaller than 0.
int64 size;
EXPECT_FALSE(file_util::GetFileSize(path, &size));
EXPECT_FALSE(GetFileSize(path, &size));

// We should not be able to read the file.
int fd = OpenContentUriForRead(path);
Expand Down
24 changes: 12 additions & 12 deletions base/file_util_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,18 @@ bool CreateDirectoryAndGetError(const FilePath& full_path,
}
}

bool NormalizeFilePath(const FilePath& path, FilePath* real_path) {
ThreadRestrictions::AssertIOAllowed();
FilePath mapped_file;
if (!file_util::NormalizeToNativeFilePath(path, &mapped_file))
return false;
// NormalizeToNativeFilePath() will return a path that starts with
// "\Device\Harddisk...". Helper DevicePathToDriveLetterPath()
// will find a drive letter which maps to the path's device, so
// that we return a path starting with a drive letter.
return file_util::DevicePathToDriveLetterPath(mapped_file, real_path);
}

} // namespace base

// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -547,18 +559,6 @@ bool SetCurrentDirectory(const FilePath& directory) {
return ret != 0;
}

bool NormalizeFilePath(const FilePath& path, FilePath* real_path) {
base::ThreadRestrictions::AssertIOAllowed();
FilePath mapped_file;
if (!NormalizeToNativeFilePath(path, &mapped_file))
return false;
// NormalizeToNativeFilePath() will return a path that starts with
// "\Device\Harddisk...". Helper DevicePathToDriveLetterPath()
// will find a drive letter which maps to the path's device, so
// that we return a path starting with a drive letter.
return DevicePathToDriveLetterPath(mapped_file, real_path);
}

bool DevicePathToDriveLetterPath(const FilePath& nt_device_path,
FilePath* out_drive_letter_path) {
base::ThreadRestrictions::AssertIOAllowed();
Expand Down
6 changes: 3 additions & 3 deletions base/files/file_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ TEST(File, ReadWrite) {

// Make sure the file was extended.
int64 file_size = 0;
EXPECT_TRUE(file_util::GetFileSize(file_path, &file_size));
EXPECT_TRUE(GetFileSize(file_path, &file_size));
EXPECT_EQ(kOffsetBeyondEndOfFile + kPartialWriteLength, file_size);

// Make sure the file was zero-padded.
Expand Down Expand Up @@ -240,7 +240,7 @@ TEST(File, Truncate) {
const int kExtendedFileLength = 10;
int64 file_size = 0;
EXPECT_TRUE(file.Truncate(kExtendedFileLength));
EXPECT_TRUE(file_util::GetFileSize(file_path, &file_size));
EXPECT_TRUE(GetFileSize(file_path, &file_size));
EXPECT_EQ(kExtendedFileLength, file_size);

// Make sure the file was zero-padded.
Expand All @@ -255,7 +255,7 @@ TEST(File, Truncate) {
// Truncate the file.
const int kTruncatedFileLength = 2;
EXPECT_TRUE(file.Truncate(kTruncatedFileLength));
EXPECT_TRUE(file_util::GetFileSize(file_path, &file_size));
EXPECT_TRUE(GetFileSize(file_path, &file_size));
EXPECT_EQ(kTruncatedFileLength, file_size);

// Make sure the file was truncated.
Expand Down
Loading

0 comments on commit 5628570

Please sign in to comment.