Skip to content

Commit

Permalink
Move ReadFileToString to the base namespace.
Browse files Browse the repository at this point in the history
BUG=

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@220612 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
brettw@chromium.org committed Aug 30, 2013
1 parent 88a8115 commit 82f84b9
Show file tree
Hide file tree
Showing 253 changed files with 387 additions and 413 deletions.
2 changes: 1 addition & 1 deletion ash/desktop_background/desktop_background_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class DesktopBackgroundController::WallpaperLoader
static scoped_ptr<SkBitmap> LoadSkBitmapFromJPEGFile(
const base::FilePath& path) {
std::string data;
if (!file_util::ReadFileToString(path, &data)) {
if (!base::ReadFileToString(path, &data)) {
LOG(ERROR) << "Unable to read data from " << path.value();
return scoped_ptr<SkBitmap>();
}
Expand Down
2 changes: 1 addition & 1 deletion base/debug/proc_maps_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace debug {

bool ReadProcMaps(std::string* proc_maps) {
FilePath proc_maps_path("/proc/self/maps");
return file_util::ReadFileToString(proc_maps_path, proc_maps);
return ReadFileToString(proc_maps_path, proc_maps);
}

bool ParseProcMaps(const std::string& input,
Expand Down
26 changes: 13 additions & 13 deletions base/file_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,10 @@ bool TextContentsEqual(const FilePath& filename1, const FilePath& filename2) {
return true;
}

} // namespace base

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

namespace file_util {

using base::FileEnumerator;
using base::FilePath;
using base::kExtensionSeparator;
using base::kMaxUniqueFiles;

bool ReadFileToString(const FilePath& path, std::string* contents) {
if (path.ReferencesParent())
return false;
FILE* file = OpenFile(path, "rb");
FILE* file = file_util::OpenFile(path, "rb");
if (!file) {
return false;
}
Expand All @@ -155,11 +144,22 @@ bool ReadFileToString(const FilePath& path, std::string* contents) {
if (contents)
contents->append(buf, len);
}
CloseFile(file);
file_util::CloseFile(file);

return true;
}

} // namespace base

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

namespace file_util {

using base::FileEnumerator;
using base::FilePath;
using base::kExtensionSeparator;
using base::kMaxUniqueFiles;

bool IsDirectoryEmpty(const FilePath& dir_path) {
FileEnumerator files(dir_path, false,
FileEnumerator::FILES | FileEnumerator::DIRECTORIES);
Expand Down
15 changes: 7 additions & 8 deletions base/file_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,18 @@ BASE_EXPORT bool ContentsEqual(const FilePath& filename1,
BASE_EXPORT bool TextContentsEqual(const FilePath& filename1,
const FilePath& filename2);

} // namespace base

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

namespace file_util {

// Read the file at |path| into |contents|, returning true on success.
// This function fails if the |path| contains path traversal components ('..').
// |contents| may be NULL, in which case this function is useful for its
// side effect of priming the disk cache.
// Useful for unit tests.
BASE_EXPORT bool ReadFileToString(const base::FilePath& path,
std::string* contents);
BASE_EXPORT bool ReadFileToString(const FilePath& path, std::string* contents);

} // namespace base

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

namespace file_util {

#if defined(OS_POSIX)
// Read exactly |bytes| bytes from file descriptor |fd|, storing the result
Expand Down
2 changes: 1 addition & 1 deletion base/files/file_util_proxy_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ TEST_F(FileUtilProxyTest, CreateTemporary) {

// Make sure the written data can be read from the returned path.
std::string data;
EXPECT_TRUE(file_util::ReadFileToString(path_, &data));
EXPECT_TRUE(ReadFileToString(path_, &data));
EXPECT_EQ("test", data);

// Make sure we can & do delete the created file to prevent leaks on the bots.
Expand Down
2 changes: 1 addition & 1 deletion base/files/important_file_writer_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace {

std::string GetFileContent(const FilePath& path) {
std::string content;
if (!file_util::ReadFileToString(path, &content)) {
if (!ReadFileToString(path, &content)) {
NOTREACHED();
}
return content;
Expand Down
2 changes: 1 addition & 1 deletion base/json/json_file_value_serializer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ bool JSONFileValueSerializer::SerializeInternal(const base::Value& root,

int JSONFileValueSerializer::ReadFileToString(std::string* json_string) {
DCHECK(json_string);
if (!file_util::ReadFileToString(json_file_path_, json_string)) {
if (!base::ReadFileToString(json_file_path_, json_string)) {
#if defined(OS_WIN)
int error = ::GetLastError();
if (error == ERROR_SHARING_VIOLATION || error == ERROR_LOCK_VIOLATION) {
Expand Down
4 changes: 2 additions & 2 deletions base/json/json_file_value_serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ class BASE_EXPORT JSONFileValueSerializer : public base::ValueSerializer {
base::FilePath json_file_path_;
bool allow_trailing_comma_;

// A wrapper for file_util::ReadFileToString which returns a non-zero
// JsonFileError if there were file errors.
// A wrapper for ReadFileToString which returns a non-zero JsonFileError if
// there were file errors.
int ReadFileToString(std::string* json_string);

DISALLOW_IMPLICIT_CONSTRUCTORS(JSONFileValueSerializer);
Expand Down
2 changes: 1 addition & 1 deletion base/json/json_reader_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ TEST(JSONReaderTest, ReadFromFile) {
ASSERT_TRUE(base::PathExists(path));

std::string input;
ASSERT_TRUE(file_util::ReadFileToString(
ASSERT_TRUE(ReadFileToString(
path.Append(FILE_PATH_LITERAL("bom_feff.json")), &input));

JSONReader reader;
Expand Down
2 changes: 1 addition & 1 deletion base/process/internal_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ bool ReadProcFile(const FilePath& file, std::string* buffer) {
// Synchronously reading files in /proc is safe.
ThreadRestrictions::ScopedAllowIO allow_io;

if (!file_util::ReadFileToString(file, buffer)) {
if (!ReadFileToString(file, buffer)) {
DLOG(WARNING) << "Failed to read " << file.MaybeAsASCII();
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion base/process/process_iterator_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ bool GetProcCmdline(pid_t pid, std::vector<std::string>* proc_cmd_line_args) {

FilePath cmd_line_file = internal::GetProcPidDir(pid).Append("cmdline");
std::string cmd_line;
if (!file_util::ReadFileToString(cmd_line_file, &cmd_line))
if (!ReadFileToString(cmd_line_file, &cmd_line))
return false;
std::string delimiters;
delimiters.push_back('\0');
Expand Down
6 changes: 3 additions & 3 deletions base/process/process_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ bool Process::IsProcessBackgrounded() const {
#if defined(OS_CHROMEOS)
if (cgroups.Get().enabled) {
std::string proc;
if (file_util::ReadFileToString(
base::FilePath(StringPrintf(kProcPath, process_)),
&proc)) {
if (base::ReadFileToString(
base::FilePath(StringPrintf(kProcPath, process_)),
&proc)) {
std::vector<std::string> proc_parts;
base::SplitString(proc, ':', &proc_parts);
DCHECK(proc_parts.size() == 3);
Expand Down
18 changes: 9 additions & 9 deletions base/process/process_metrics_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ enum ParsingState {
// Read a file with a single number string and return the number as a uint64.
static uint64 ReadFileToUint64(const base::FilePath file) {
std::string file_as_string;
if (!file_util::ReadFileToString(file, &file_as_string))
if (!ReadFileToString(file, &file_as_string))
return 0;
TrimWhitespaceASCII(file_as_string, TRIM_ALL, &file_as_string);
uint64 file_as_uint64 = 0;
Expand All @@ -52,7 +52,7 @@ size_t ReadProcStatusAndGetFieldAsSizeT(pid_t pid, const std::string& field) {
{
// Synchronously reading files in /proc is safe.
ThreadRestrictions::ScopedAllowIO allow_io;
if (!file_util::ReadFileToString(stat_file, &status))
if (!ReadFileToString(stat_file, &status))
return 0;
}

Expand Down Expand Up @@ -117,7 +117,7 @@ int GetProcessCPU(pid_t pid) {
std::string stat;
FilePath stat_path =
task_path.Append(ent->d_name).Append(internal::kStatFile);
if (file_util::ReadFileToString(stat_path, &stat)) {
if (ReadFileToString(stat_path, &stat)) {
int cpu = ParseProcStatCPU(stat);
if (cpu > 0)
total_cpu += cpu;
Expand Down Expand Up @@ -223,7 +223,7 @@ bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const {

std::string proc_io_contents;
FilePath io_file = internal::GetProcPidDir(process_).Append("io");
if (!file_util::ReadFileToString(io_file, &proc_io_contents))
if (!ReadFileToString(io_file, &proc_io_contents))
return false;

(*io_counters).OtherOperationCount = 0;
Expand Down Expand Up @@ -295,7 +295,7 @@ bool ProcessMetrics::GetWorkingSetKBytesTotmaps(WorkingSetKBytes *ws_usage)
{
FilePath totmaps_file = internal::GetProcPidDir(process_).Append("totmaps");
ThreadRestrictions::ScopedAllowIO allow_io;
bool ret = file_util::ReadFileToString(totmaps_file, &totmaps_data);
bool ret = ReadFileToString(totmaps_file, &totmaps_data);
if (!ret || totmaps_data.length() == 0)
return false;
}
Expand Down Expand Up @@ -347,7 +347,7 @@ bool ProcessMetrics::GetWorkingSetKBytesStatm(WorkingSetKBytes* ws_usage)
FilePath statm_file = internal::GetProcPidDir(process_).Append("statm");
// Synchronously reading files in /proc is safe.
ThreadRestrictions::ScopedAllowIO allow_io;
bool ret = file_util::ReadFileToString(statm_file, &statm);
bool ret = ReadFileToString(statm_file, &statm);
if (!ret || statm.length() == 0)
return false;
}
Expand Down Expand Up @@ -444,7 +444,7 @@ bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo) {
// Used memory is: total - free - buffers - caches
FilePath meminfo_file("/proc/meminfo");
std::string meminfo_data;
if (!file_util::ReadFileToString(meminfo_file, &meminfo_data)) {
if (!ReadFileToString(meminfo_file, &meminfo_data)) {
DLOG(WARNING) << "Failed to open " << meminfo_file.value();
return false;
}
Expand Down Expand Up @@ -499,7 +499,7 @@ bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo) {
std::string geminfo_data;
meminfo->gem_objects = -1;
meminfo->gem_size = -1;
if (file_util::ReadFileToString(geminfo_file, &geminfo_data)) {
if (ReadFileToString(geminfo_file, &geminfo_data)) {
int gem_objects = -1;
long long gem_size = -1;
int num_res = sscanf(geminfo_data.c_str(),
Expand All @@ -515,7 +515,7 @@ bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo) {
// Incorporate Mali graphics memory if present.
FilePath mali_memory_file("/sys/devices/platform/mali.0/memory");
std::string mali_memory_data;
if (file_util::ReadFileToString(mali_memory_file, &mali_memory_data)) {
if (ReadFileToString(mali_memory_file, &mali_memory_data)) {
long long mali_size = -1;
int num_res = sscanf(mali_memory_data.c_str(), "%lld bytes", &mali_size);
if (num_res == 1)
Expand Down
2 changes: 1 addition & 1 deletion base/sys_info_chromeos.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void SysInfo::OperatingSystemVersionNumbers(int32* major_version,

FilePath path(kLinuxStandardBaseReleaseFile);
std::string contents;
if (file_util::ReadFileToString(path, &contents)) {
if (ReadFileToString(path, &contents)) {
g_chrome_os_version_numbers.Get().parsed = true;
ParseLsbRelease(contents,
&(g_chrome_os_version_numbers.Get().major_version),
Expand Down
4 changes: 2 additions & 2 deletions base/sys_info_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ size_t SysInfo::MaxSharedMemorySize() {
static bool limit_valid = false;
if (!limit_valid) {
std::string contents;
file_util::ReadFileToString(FilePath("/proc/sys/kernel/shmmax"), &contents);
ReadFileToString(FilePath("/proc/sys/kernel/shmmax"), &contents);
DCHECK(!contents.empty());
if (!contents.empty() && contents[contents.length() - 1] == '\n') {
contents.erase(contents.length() - 1);
Expand All @@ -67,7 +67,7 @@ std::string SysInfo::CPUModelName() {
const char kCpuModelPrefix[] = "model name";
#endif
std::string contents;
file_util::ReadFileToString(FilePath("/proc/cpuinfo"), &contents);
ReadFileToString(FilePath("/proc/cpuinfo"), &contents);
DCHECK(!contents.empty());
if (!contents.empty()) {
std::istringstream iss(contents);
Expand Down
2 changes: 1 addition & 1 deletion base/test/gtest_xml_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ bool ProcessGTestOutput(const base::FilePath& output_file,
DCHECK(results);

std::string xml_contents;
if (!file_util::ReadFileToString(output_file, &xml_contents))
if (!ReadFileToString(output_file, &xml_contents))
return false;

// Silence XML errors - otherwise they go to stderr.
Expand Down
2 changes: 1 addition & 1 deletion base/test/test_file_util_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ bool VolumeSupportsADS(const base::FilePath& path) {
bool HasInternetZoneIdentifier(const base::FilePath& full_path) {
base::FilePath zone_path(full_path.value() + L":Zone.Identifier");
std::string zone_path_contents;
if (!file_util::ReadFileToString(zone_path, &zone_path_contents))
if (!base::ReadFileToString(zone_path, &zone_path_contents))
return false;

std::vector<std::string> lines;
Expand Down
2 changes: 1 addition & 1 deletion cc/test/pixel_test_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ bool WritePNGFile(const SkBitmap& bitmap, const base::FilePath& file_path,
bool ReadPNGFile(const base::FilePath& file_path, SkBitmap* bitmap) {
DCHECK(bitmap);
std::string png_data;
return file_util::ReadFileToString(file_path, &png_data) &&
return base::ReadFileToString(file_path, &png_data) &&
gfx::PNGCodec::Decode(reinterpret_cast<unsigned char*>(&png_data[0]),
png_data.length(),
bitmap);
Expand Down
2 changes: 1 addition & 1 deletion cc/trees/layer_tree_host_perftest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class LayerTreeHostPerfTestJsonReader : public LayerTreeHostPerfTest {
base::FilePath test_data_dir;
ASSERT_TRUE(PathService::Get(cc::DIR_TEST_DATA, &test_data_dir));
base::FilePath json_file = test_data_dir.AppendASCII(name + ".json");
ASSERT_TRUE(file_util::ReadFileToString(json_file, &json_));
ASSERT_TRUE(base::ReadFileToString(json_file, &json_));
}

virtual void BuildTree() OVERRIDE {
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/autofill/autofill_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class AutofillTest : public InProcessBrowserTest {
base::FilePath data_file =
ui_test_utils::GetTestFilePath(base::FilePath().AppendASCII("autofill"),
base::FilePath().AppendASCII(filename));
CHECK(file_util::ReadFileToString(data_file, &data));
CHECK(base::ReadFileToString(data_file, &data));
std::vector<std::string> lines;
base::SplitString(data, '\n', &lines);
int parsed_profiles = 0;
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/browser_shutdown.cc
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ void ReadLastShutdownFile(ShutdownType type,
base::FilePath shutdown_ms_file = GetShutdownMsPath();
std::string shutdown_ms_str;
int64 shutdown_ms = 0;
if (file_util::ReadFileToString(shutdown_ms_file, &shutdown_ms_str))
if (base::ReadFileToString(shutdown_ms_file, &shutdown_ms_str))
base::StringToInt64(shutdown_ms_str, &shutdown_ms);
base::DeleteFile(shutdown_ms_file, false);

Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/chromeos/app_mode/kiosk_app_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class KioskAppData::IconLoader : public ImageDecoder::Delegate {
DCHECK(task_runner_->RunsTasksOnCurrentThread());

std::string data;
if (!file_util::ReadFileToString(base::FilePath(icon_path_), &data)) {
if (!base::ReadFileToString(base::FilePath(icon_path_), &data)) {
ReportResultOnBlockingPool(FAILED_TO_LOAD);
return;
}
Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/chromeos/boot_times_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ BootTimesLoader::Stats BootTimesLoader::GetCurrentStats() {
const base::FilePath kDiskStat(FPL("/sys/block/sda/stat"));
Stats stats;
base::ThreadRestrictions::ScopedAllowIO allow_io;
file_util::ReadFileToString(kProcUptime, &stats.uptime);
file_util::ReadFileToString(kDiskStat, &stats.disk);
base::ReadFileToString(kProcUptime, &stats.uptime);
base::ReadFileToString(kDiskStat, &stats.disk);
return stats;
}

Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/chromeos/customization_document.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ CustomizationDocument::~CustomizationDocument() {}
bool CustomizationDocument::LoadManifestFromFile(
const base::FilePath& manifest_path) {
std::string manifest;
if (!file_util::ReadFileToString(manifest_path, &manifest))
if (!base::ReadFileToString(manifest_path, &manifest))
return false;
return LoadManifestFromString(manifest);
}
Expand Down Expand Up @@ -280,7 +280,7 @@ void ServicesCustomizationDocument::ReadFileInBackground(
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));

std::string manifest;
if (file_util::ReadFileToString(file, &manifest)) {
if (base::ReadFileToString(file, &manifest)) {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(
base::IgnoreResult(
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/chromeos/drive/fake_file_system_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ TEST_F(FakeFileSystemTest, GetFileContentByPath) {
// Make sure the cached file's content.
std::string cache_file_content;
ASSERT_TRUE(
file_util::ReadFileToString(cache_file_path, &cache_file_content));
base::ReadFileToString(cache_file_path, &cache_file_content));
EXPECT_EQ(content, cache_file_content);
}

Expand Down
Loading

0 comments on commit 82f84b9

Please sign in to comment.