Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(124)

Unified Diff: base/file_util.cc

Issue 157593005: Added new ReadFileToString API with a max_size argument (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/file_util.cc
diff --git a/base/file_util.cc b/base/file_util.cc
index e44f713cdb31828010ad9be92739a3d021dc4685..59f313d9e98f69e97f6d8191da9b52e515f7bc53 100644
--- a/base/file_util.cc
+++ b/base/file_util.cc
@@ -126,25 +126,47 @@ bool TextContentsEqual(const FilePath& filename1, const FilePath& filename2) {
return true;
}
-bool ReadFileToString(const FilePath& path, std::string* contents) {
- if (path.ReferencesParent())
- return false;
+static bool ReadFileToStringImpl(const FilePath& path, std::string* contents,
+ bool checkSz, size_t maxsize) {
Andrew T Wilson (Slow) 2014/02/10 14:29:46 nit: checkSz does not adhere to the style guidelin
bartfab (slow) 2014/02/10 14:55:37 Further nit, also per style guide: Either put all
kaliamoorthi 2014/02/11 10:10:45 Done.
kaliamoorthi 2014/02/11 10:10:45 Done.
+ if(path.ReferencesParent())
+ return false;
FILE* file = OpenFile(path, "rb");
- if (!file) {
- return false;
+ if(!file)
+ return false;
Andrew T Wilson (Slow) 2014/02/10 14:29:46 nit: space between if and ( and also make sure ind
kaliamoorthi 2014/02/11 10:10:45 Done.
+
+ if(!contents) {
Andrew T Wilson (Slow) 2014/02/10 14:29:46 space before open paren
kaliamoorthi 2014/02/11 10:10:45 Done.
+ CloseFile(file);
Andrew T Wilson (Slow) 2014/02/10 14:29:46 This breaks the old behavior of priming the disk c
kaliamoorthi 2014/02/11 10:10:45 I changed the code to retain the old behavior of p
+ return true;
}
char buf[1 << 16];
- size_t len;
+ size_t len, sz = 0;
bartfab (slow) 2014/02/10 14:55:37 Nit 1: Per style guide, avid abbreviations wheneve
kaliamoorthi 2014/02/11 10:10:45 Done.
+
+ // Many files supplied in path have incorrect size (proc files, streams etc)
+ // hence the file is read sequentially as opposed to one-shot read
Andrew T Wilson (Slow) 2014/02/10 14:29:46 nit: period at the end of this sentence.
kaliamoorthi 2014/02/11 10:10:45 Done.
while ((len = fread(buf, 1, sizeof(buf), file)) > 0) {
- if (contents)
- contents->append(buf, len);
+ sz += len;
bartfab (slow) 2014/02/10 14:55:37 Nit: What happens when |sz + len| overflows size_t
kaliamoorthi 2014/02/11 10:10:45 Done.
+ if(checkSz && maxsize < sz) {
Andrew T Wilson (Slow) 2014/02/10 14:29:46 space before open paren. Suggestion - instead of
kaliamoorthi 2014/02/11 10:10:45 Done.
+ contents->clear();
+ CloseFile(file);
+ return false;
+ }
+ contents->append(buf, len);
}
- CloseFile(file);
+ CloseFile(file);
return true;
}
+bool ReadFileToString(const FilePath& path, std::string* contents,
+ size_t maxsize) {
+ return ReadFileToStringImpl(path, contents, true, maxsize);
+}
+
+bool ReadFileToString(const FilePath& path, std::string* contents) {
+ return ReadFileToStringImpl(path, contents, false, 0);
Andrew T Wilson (Slow) 2014/02/10 14:29:46 Suggest passing SIZE_MAX for an unlimited read, ra
kaliamoorthi 2014/02/11 10:10:45 Done.
+}
+
bool IsDirectoryEmpty(const FilePath& dir_path) {
FileEnumerator files(dir_path, false,
FileEnumerator::FILES | FileEnumerator::DIRECTORIES);

Powered by Google App Engine
This is Rietveld 408576698