[go: nahoru, domu]

Added new ReadFileToString API with a max_size argument

The CL creates a new API that takes a max_size argument. When the file size exceed the max_size, the API primes the cache and returns false.

BUG=339417

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@251771 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/base/file_util.cc b/base/file_util.cc
index e44f713c..87b9afb99 100644
--- a/base/file_util.cc
+++ b/base/file_util.cc
@@ -10,6 +10,7 @@
 #include <stdio.h>
 
 #include <fstream>
+#include <limits>
 
 #include "base/files/file_enumerator.h"
 #include "base/files/file_path.h"
@@ -126,7 +127,11 @@
   return true;
 }
 
-bool ReadFileToString(const FilePath& path, std::string* contents) {
+bool ReadFileToString(const FilePath& path,
+                      std::string* contents,
+                      size_t max_size) {
+  if (contents)
+    contents->clear();
   if (path.ReferencesParent())
     return false;
   FILE* file = OpenFile(path, "rb");
@@ -136,13 +141,29 @@
 
   char buf[1 << 16];
   size_t len;
+  size_t size = 0;
+  bool read_status = true;
+
+  // Many files supplied in |path| have incorrect size (proc files etc).
+  // Hence, the file is read sequentially as opposed to a one-shot read.
   while ((len = fread(buf, 1, sizeof(buf), file)) > 0) {
     if (contents)
-      contents->append(buf, len);
+      contents->append(buf, std::min(len, max_size - size));
+
+    if ((max_size - size) < len) {
+      read_status = false;
+      break;
+    }
+
+    size += len;
   }
   CloseFile(file);
 
-  return true;
+  return read_status;
+}
+
+bool ReadFileToString(const FilePath& path, std::string* contents) {
+  return ReadFileToString(path, contents, std::numeric_limits<size_t>::max());
 }
 
 bool IsDirectoryEmpty(const FilePath& dir_path) {