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

Side by Side 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/file_util.h" 5 #include "base/file_util.h"
6 6
7 #if defined(OS_WIN) 7 #if defined(OS_WIN)
8 #include <io.h> 8 #include <io.h>
9 #endif 9 #endif
10 #include <stdio.h> 10 #include <stdio.h>
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 else if (end2 + 1 < line2.length()) 119 else if (end2 + 1 < line2.length())
120 line2.erase(end2 + 1); 120 line2.erase(end2 + 1);
121 121
122 if (line1 != line2) 122 if (line1 != line2)
123 return false; 123 return false;
124 } while (!file1.eof() || !file2.eof()); 124 } while (!file1.eof() || !file2.eof());
125 125
126 return true; 126 return true;
127 } 127 }
128 128
129 bool ReadFileToString(const FilePath& path, std::string* contents) { 129 static bool ReadFileToStringImpl(const FilePath& path, std::string* contents,
130 if (path.ReferencesParent()) 130 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.
131 return false; 131 if(path.ReferencesParent())
132 return false;
132 FILE* file = OpenFile(path, "rb"); 133 FILE* file = OpenFile(path, "rb");
133 if (!file) { 134 if(!file)
134 return false; 135 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.
136
137 if(!contents) {
Andrew T Wilson (Slow) 2014/02/10 14:29:46 space before open paren
kaliamoorthi 2014/02/11 10:10:45 Done.
138 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
139 return true;
135 } 140 }
136 141
137 char buf[1 << 16]; 142 char buf[1 << 16];
138 size_t len; 143 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.
144
145 // Many files supplied in path have incorrect size (proc files, streams etc)
146 // 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.
139 while ((len = fread(buf, 1, sizeof(buf), file)) > 0) { 147 while ((len = fread(buf, 1, sizeof(buf), file)) > 0) {
140 if (contents) 148 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.
141 contents->append(buf, len); 149 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.
150 contents->clear();
151 CloseFile(file);
152 return false;
153 }
154 contents->append(buf, len);
142 } 155 }
156
143 CloseFile(file); 157 CloseFile(file);
158 return true;
159 }
144 160
145 return true; 161 bool ReadFileToString(const FilePath& path, std::string* contents,
162 size_t maxsize) {
163 return ReadFileToStringImpl(path, contents, true, maxsize);
164 }
165
166 bool ReadFileToString(const FilePath& path, std::string* contents) {
167 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.
146 } 168 }
147 169
148 bool IsDirectoryEmpty(const FilePath& dir_path) { 170 bool IsDirectoryEmpty(const FilePath& dir_path) {
149 FileEnumerator files(dir_path, false, 171 FileEnumerator files(dir_path, false,
150 FileEnumerator::FILES | FileEnumerator::DIRECTORIES); 172 FileEnumerator::FILES | FileEnumerator::DIRECTORIES);
151 if (files.Next().empty()) 173 if (files.Next().empty())
152 return true; 174 return true;
153 return false; 175 return false;
154 } 176 }
155 177
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 if (!PathExists(new_path) && 262 if (!PathExists(new_path) &&
241 (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) { 263 (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) {
242 return count; 264 return count;
243 } 265 }
244 } 266 }
245 267
246 return -1; 268 return -1;
247 } 269 }
248 270
249 } // namespace file_util 271 } // namespace file_util
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698