Skip to content

Commit

Permalink
Move WriteFile and WriteFileDescriptor from file_util to base namespace.
Browse files Browse the repository at this point in the history
R=viettrungluu@chromium.org
TBR=viettrungluu

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@255418 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
brettw@chromium.org committed Mar 6, 2014
1 parent 06b1d42 commit e5c2a22
Show file tree
Hide file tree
Showing 239 changed files with 582 additions and 611 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ class DesktopBackgroundControllerTest : public test::AshTestBase {
return false;
}

size_t bytes_written = file_util::WriteFile(
size_t bytes_written = base::WriteFile(
path, reinterpret_cast<const char*>(&output[0]), output.size());
if (bytes_written != output.size()) {
LOG(ERROR) << "Wrote " << bytes_written << " byte(s) instead of "
Expand Down
16 changes: 9 additions & 7 deletions base/file_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,20 +328,22 @@ BASE_EXPORT bool TruncateFile(FILE* file);
// the number of read bytes, or -1 on error.
BASE_EXPORT int ReadFile(const FilePath& filename, char* data, int size);

} // namespace base

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

namespace file_util {

// Writes the given buffer into the file, overwriting any data that was
// previously there. Returns the number of bytes written, or -1 on error.
BASE_EXPORT int WriteFile(const base::FilePath& filename, const char* data,
BASE_EXPORT int WriteFile(const FilePath& filename, const char* data,
int size);

#if defined(OS_POSIX)
// Append the data to |fd|. Does not close |fd| when done.
BASE_EXPORT int WriteFileDescriptor(const int fd, const char* data, int size);
#endif

} // namespace base

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

namespace file_util {

// Append the given buffer into the file. Returns the number of bytes written,
// or -1 on error.
BASE_EXPORT int AppendToFile(const base::FilePath& filename,
Expand Down
56 changes: 28 additions & 28 deletions base/file_util_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,33 @@ int ReadFile(const FilePath& filename, char* data, int size) {
return bytes_read;
}

int WriteFile(const FilePath& filename, const char* data, int size) {
ThreadRestrictions::AssertIOAllowed();
int fd = HANDLE_EINTR(creat(filename.value().c_str(), 0666));
if (fd < 0)
return -1;

int bytes_written = WriteFileDescriptor(fd, data, size);
if (int ret = IGNORE_EINTR(close(fd)) < 0)
return ret;
return bytes_written;
}

int WriteFileDescriptor(const int fd, const char* data, int size) {
// Allow for partial writes.
ssize_t bytes_written_total = 0;
for (ssize_t bytes_written_partial = 0; bytes_written_total < size;
bytes_written_total += bytes_written_partial) {
bytes_written_partial =
HANDLE_EINTR(write(fd, data + bytes_written_total,
size - bytes_written_total));
if (bytes_written_partial < 0)
return -1;
}

return bytes_written_total;
}

} // namespace base

// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -750,40 +777,13 @@ FILE* OpenFile(const std::string& filename, const char* mode) {
return OpenFile(FilePath(filename), mode);
}

int WriteFile(const FilePath& filename, const char* data, int size) {
base::ThreadRestrictions::AssertIOAllowed();
int fd = HANDLE_EINTR(creat(filename.value().c_str(), 0666));
if (fd < 0)
return -1;

int bytes_written = WriteFileDescriptor(fd, data, size);
if (int ret = IGNORE_EINTR(close(fd)) < 0)
return ret;
return bytes_written;
}

int WriteFileDescriptor(const int fd, const char* data, int size) {
// Allow for partial writes.
ssize_t bytes_written_total = 0;
for (ssize_t bytes_written_partial = 0; bytes_written_total < size;
bytes_written_total += bytes_written_partial) {
bytes_written_partial =
HANDLE_EINTR(write(fd, data + bytes_written_total,
size - bytes_written_total));
if (bytes_written_partial < 0)
return -1;
}

return bytes_written_total;
}

int AppendToFile(const FilePath& filename, const char* data, int size) {
base::ThreadRestrictions::AssertIOAllowed();
int fd = HANDLE_EINTR(open(filename.value().c_str(), O_WRONLY | O_APPEND));
if (fd < 0)
return -1;

int bytes_written = WriteFileDescriptor(fd, data, size);
int bytes_written = base::WriteFileDescriptor(fd, data, size);
if (int ret = IGNORE_EINTR(close(fd)) < 0)
return ret;
return bytes_written;
Expand Down
19 changes: 9 additions & 10 deletions base/file_util_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ TEST_F(FileUtilTest, ChangeFilePermissionsAndRead) {

// Write file.
EXPECT_EQ(static_cast<int>(kData.length()),
file_util::WriteFile(file_name, kData.data(), kData.length()));
WriteFile(file_name, kData.data(), kData.length()));
EXPECT_TRUE(PathExists(file_name));

// Make sure the file is readable.
Expand Down Expand Up @@ -742,7 +742,7 @@ TEST_F(FileUtilTest, ChangeFilePermissionsAndWrite) {

// Write file.
EXPECT_EQ(static_cast<int>(kData.length()),
file_util::WriteFile(file_name, kData.data(), kData.length()));
WriteFile(file_name, kData.data(), kData.length()));
EXPECT_TRUE(PathExists(file_name));

// Make sure the file is writable.
Expand All @@ -756,8 +756,7 @@ TEST_F(FileUtilTest, ChangeFilePermissionsAndWrite) {
EXPECT_TRUE(GetPosixFilePermissions(file_name, &mode));
EXPECT_FALSE(mode & FILE_PERMISSION_WRITE_BY_USER);
// Make sure the file can't be write.
EXPECT_EQ(-1,
file_util::WriteFile(file_name, kData.data(), kData.length()));
EXPECT_EQ(-1, WriteFile(file_name, kData.data(), kData.length()));
EXPECT_FALSE(PathIsWritable(file_name));

// Give read permission.
Expand All @@ -767,7 +766,7 @@ TEST_F(FileUtilTest, ChangeFilePermissionsAndWrite) {
EXPECT_TRUE(mode & FILE_PERMISSION_WRITE_BY_USER);
// Make sure the file can be write.
EXPECT_EQ(static_cast<int>(kData.length()),
file_util::WriteFile(file_name, kData.data(), kData.length()));
WriteFile(file_name, kData.data(), kData.length()));
EXPECT_TRUE(PathIsWritable(file_name));

// Delete the file.
Expand All @@ -787,7 +786,7 @@ TEST_F(FileUtilTest, ChangeDirectoryPermissionsAndEnumerate) {
EXPECT_FALSE(PathExists(file_name));
const std::string kData("hello");
EXPECT_EQ(static_cast<int>(kData.length()),
file_util::WriteFile(file_name, kData.data(), kData.length()));
WriteFile(file_name, kData.data(), kData.length()));
EXPECT_TRUE(PathExists(file_name));

// Make sure the directory has the all permissions.
Expand Down Expand Up @@ -1950,7 +1949,7 @@ TEST_F(FileUtilTest, AppendToFile) {
std::string data("hello");
EXPECT_EQ(-1, file_util::AppendToFile(foobar, data.c_str(), data.length()));
EXPECT_EQ(static_cast<int>(data.length()),
file_util::WriteFile(foobar, data.c_str(), data.length()));
WriteFile(foobar, data.c_str(), data.length()));
EXPECT_EQ(static_cast<int>(data.length()),
file_util::AppendToFile(foobar, data.c_str(), data.length()));

Expand All @@ -1965,7 +1964,7 @@ TEST_F(FileUtilTest, ReadFileToString) {
FilePath file_path =
temp_dir_.path().Append(FILE_PATH_LITERAL("ReadFileToStringTest"));

ASSERT_EQ(4, file_util::WriteFile(file_path, kTestData, 4));
ASSERT_EQ(4, WriteFile(file_path, kTestData, 4));

EXPECT_TRUE(ReadFileToString(file_path, &data));
EXPECT_EQ(kTestData, data);
Expand Down Expand Up @@ -2017,7 +2016,7 @@ TEST_F(FileUtilTest, TouchFile) {

FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt")));
std::string data("hello");
ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
ASSERT_TRUE(WriteFile(foobar, data.c_str(), data.length()));

Time access_time;
// This timestamp is divisible by one day (in local timezone),
Expand Down Expand Up @@ -2051,7 +2050,7 @@ TEST_F(FileUtilTest, IsDirectoryEmpty) {

FilePath foo(empty_dir.Append(FILE_PATH_LITERAL("foo.txt")));
std::string bar("baz");
ASSERT_TRUE(file_util::WriteFile(foo, bar.c_str(), bar.length()));
ASSERT_TRUE(WriteFile(foo, bar.c_str(), bar.length()));

EXPECT_FALSE(IsDirectoryEmpty(empty_dir));
}
Expand Down
40 changes: 20 additions & 20 deletions base/file_util_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -599,23 +599,8 @@ int ReadFile(const FilePath& filename, char* data, int size) {
return -1;
}

} // namespace base

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

namespace file_util {

using base::DirectoryExists;
using base::FilePath;
using base::kFileShareAll;

FILE* OpenFile(const std::string& filename, const char* mode) {
base::ThreadRestrictions::AssertIOAllowed();
return _fsopen(filename.c_str(), mode, _SH_DENYNO);
}

int WriteFile(const FilePath& filename, const char* data, int size) {
base::ThreadRestrictions::AssertIOAllowed();
ThreadRestrictions::AssertIOAllowed();
base::win::ScopedHandle file(CreateFile(filename.value().c_str(),
GENERIC_WRITE,
0,
Expand All @@ -625,7 +610,7 @@ int WriteFile(const FilePath& filename, const char* data, int size) {
NULL));
if (!file) {
DLOG_GETLASTERROR(WARNING) << "CreateFile failed for path "
<< filename.value();
<< UTF16ToUTF8(filename.value());
return -1;
}

Expand All @@ -636,16 +621,31 @@ int WriteFile(const FilePath& filename, const char* data, int size) {

if (!result) {
// WriteFile failed.
DLOG_GETLASTERROR(WARNING) << "writing file " << filename.value()
<< " failed";
DLOG_GETLASTERROR(WARNING) << "writing file "
<< UTF16ToUTF8(filename.value()) << " failed";
} else {
// Didn't write all the bytes.
DLOG(WARNING) << "wrote" << written << " bytes to "
<< filename.value() << " expected " << size;
<< UTF16ToUTF8(filename.value()) << " expected " << size;
}
return -1;
}

} // namespace base

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

namespace file_util {

using base::DirectoryExists;
using base::FilePath;
using base::kFileShareAll;

FILE* OpenFile(const std::string& filename, const char* mode) {
base::ThreadRestrictions::AssertIOAllowed();
return _fsopen(filename.c_str(), mode, _SH_DENYNO);
}

int AppendToFile(const FilePath& filename, const char* data, int size) {
base::ThreadRestrictions::AssertIOAllowed();
base::win::ScopedHandle file(CreateFile(filename.value().c_str(),
Expand Down
3 changes: 1 addition & 2 deletions base/files/file_path_watcher_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,7 @@ class FilePathWatcherTest : public testing::Test {

// Write |content| to |file|. Returns true on success.
bool WriteFile(const FilePath& file, const std::string& content) {
int write_size = file_util::WriteFile(file, content.c_str(),
content.length());
int write_size = ::base::WriteFile(file, content.c_str(), content.length());
return write_size == static_cast<int>(content.length());
}

Expand Down
10 changes: 5 additions & 5 deletions base/files/file_util_proxy_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ TEST_F(FileUtilProxyTest, CreateOrOpen_Create) {

TEST_F(FileUtilProxyTest, CreateOrOpen_Open) {
// Creates a file.
file_util::WriteFile(test_path(), NULL, 0);
WriteFile(test_path(), NULL, 0);
ASSERT_TRUE(PathExists(test_path()));

// Opens the created file.
Expand Down Expand Up @@ -223,7 +223,7 @@ TEST_F(FileUtilProxyTest, CreateTemporary) {

TEST_F(FileUtilProxyTest, GetFileInfo_File) {
// Setup.
ASSERT_EQ(4, file_util::WriteFile(test_path(), "test", 4));
ASSERT_EQ(4, WriteFile(test_path(), "test", 4));
File::Info expected_info;
GetFileInfo(test_path(), &expected_info);

Expand Down Expand Up @@ -272,7 +272,7 @@ TEST_F(FileUtilProxyTest, Read) {
const char expected_data[] = "bleh";
int expected_bytes = arraysize(expected_data);
ASSERT_EQ(expected_bytes,
file_util::WriteFile(test_path(), expected_data, expected_bytes));
WriteFile(test_path(), expected_data, expected_bytes));

// Run.
FileUtilProxy::Read(
Expand Down Expand Up @@ -354,7 +354,7 @@ TEST_F(FileUtilProxyTest, Touch) {
TEST_F(FileUtilProxyTest, Truncate_Shrink) {
// Setup.
const char kTestData[] = "0123456789";
ASSERT_EQ(10, file_util::WriteFile(test_path(), kTestData, 10));
ASSERT_EQ(10, WriteFile(test_path(), kTestData, 10));
File::Info info;
GetFileInfo(test_path(), &info);
ASSERT_EQ(10, info.size);
Expand All @@ -381,7 +381,7 @@ TEST_F(FileUtilProxyTest, Truncate_Shrink) {
TEST_F(FileUtilProxyTest, Truncate_Expand) {
// Setup.
const char kTestData[] = "9876543210";
ASSERT_EQ(10, file_util::WriteFile(test_path(), kTestData, 10));
ASSERT_EQ(10, WriteFile(test_path(), kTestData, 10));
File::Info info;
GetFileInfo(test_path(), &info);
ASSERT_EQ(10, info.size);
Expand Down
5 changes: 2 additions & 3 deletions base/json/json_file_value_serializer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ bool JSONFileValueSerializer::SerializeInternal(const base::Value& root,
return false;

int data_size = static_cast<int>(json_string.size());
if (file_util::WriteFile(json_file_path_,
json_string.data(),
data_size) != data_size)
if (base::WriteFile(json_file_path_, json_string.data(), data_size) !=
data_size)
return false;

return true;
Expand Down
7 changes: 3 additions & 4 deletions base/json/json_value_serializer_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ TEST(JSONValueSerializerTest, ReadProperJSONFromFile) {
// Write it down in the file.
FilePath temp_file(tempdir.path().AppendASCII("test.json"));
ASSERT_EQ(static_cast<int>(strlen(kProperJSON)),
file_util::WriteFile(temp_file, kProperJSON, strlen(kProperJSON)));
WriteFile(temp_file, kProperJSON, strlen(kProperJSON)));

// Try to deserialize it through the serializer.
JSONFileValueSerializer file_deserializer(temp_file);
Expand All @@ -142,9 +142,8 @@ TEST(JSONValueSerializerTest, ReadJSONWithCommasFromFile) {
// Write it down in the file.
FilePath temp_file(tempdir.path().AppendASCII("test.json"));
ASSERT_EQ(static_cast<int>(strlen(kProperJSONWithCommas)),
file_util::WriteFile(temp_file,
kProperJSONWithCommas,
strlen(kProperJSONWithCommas)));
WriteFile(temp_file, kProperJSONWithCommas,
strlen(kProperJSONWithCommas)));

// Try to deserialize it through the serializer.
JSONFileValueSerializer file_deserializer(temp_file);
Expand Down
2 changes: 1 addition & 1 deletion base/mac/mac_util_unittest.mm
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
const char dummy_data[] = "All your base are belong to us!";
// Dump something real into the file.
ASSERT_EQ(static_cast<int>(arraysize(dummy_data)),
file_util::WriteFile(dummy_file_path, dummy_data, arraysize(dummy_data)));
WriteFile(dummy_file_path, dummy_data, arraysize(dummy_data)));
NSString* fileURLString =
[NSString stringWithUTF8String:dummy_file_path.value().c_str()];
NSURL* fileURL = [NSURL URLWithString:fileURLString];
Expand Down
4 changes: 2 additions & 2 deletions base/path_service_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,12 @@ TEST_F(PathServiceTest, OverrideMultiple) {
base::FilePath fake_cache_dir1(temp_dir.path().AppendASCII("1"));
EXPECT_TRUE(PathService::Override(my_special_key, fake_cache_dir1));
EXPECT_TRUE(base::PathExists(fake_cache_dir1));
ASSERT_EQ(1, file_util::WriteFile(fake_cache_dir1.AppendASCII("t1"), ".", 1));
ASSERT_EQ(1, base::WriteFile(fake_cache_dir1.AppendASCII("t1"), ".", 1));

base::FilePath fake_cache_dir2(temp_dir.path().AppendASCII("2"));
EXPECT_TRUE(PathService::Override(my_special_key + 1, fake_cache_dir2));
EXPECT_TRUE(base::PathExists(fake_cache_dir2));
ASSERT_EQ(1, file_util::WriteFile(fake_cache_dir2.AppendASCII("t2"), ".", 1));
ASSERT_EQ(1, base::WriteFile(fake_cache_dir2.AppendASCII("t2"), ".", 1));

base::FilePath result;
EXPECT_TRUE(PathService::Get(my_special_key, &result));
Expand Down
8 changes: 2 additions & 6 deletions base/process/memory_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,7 @@ bool AdjustOOMScore(ProcessId process, int score) {
DVLOG(1) << "Adjusting oom_score_adj of " << process << " to "
<< score_str;
int score_len = static_cast<int>(score_str.length());
return (score_len == file_util::WriteFile(oom_file,
score_str.c_str(),
score_len));
return (score_len == WriteFile(oom_file, score_str.c_str(), score_len));
}

// If the oom_score_adj file doesn't exist, then we write the old
Expand All @@ -178,9 +176,7 @@ bool AdjustOOMScore(ProcessId process, int score) {
std::string score_str = IntToString(converted_score);
DVLOG(1) << "Adjusting oom_adj of " << process << " to " << score_str;
int score_len = static_cast<int>(score_str.length());
return (score_len == file_util::WriteFile(oom_file,
score_str.c_str(),
score_len));
return (score_len == WriteFile(oom_file, score_str.c_str(), score_len));
}

return false;
Expand Down
Loading

0 comments on commit e5c2a22

Please sign in to comment.