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

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: Added new ReadFileToString API with a max_size argument 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 bool ReadFileToString(const FilePath& path,
130 std::string* contents,
bartfab (slow) 2014/02/11 12:51:12 Nit: Align the arguments.
kaliamoorthi 2014/02/11 16:29:16 Done.
131 size_t maxsize) {
130 if (path.ReferencesParent()) 132 if (path.ReferencesParent())
131 return false; 133 return false;
132 FILE* file = OpenFile(path, "rb"); 134 FILE* file = OpenFile(path, "rb");
133 if (!file) { 135 if (!file) {
134 return false; 136 return false;
135 } 137 }
136 138
137 char buf[1 << 16]; 139 char buf[1 << 16];
138 size_t len; 140 size_t len;
141 size_t size = 0;
142
143 // Many files supplied in path have incorrect size (proc files, streams etc)
bartfab (slow) 2014/02/11 12:51:12 Nit: What do you mean by incorrect size? IIUC, the
kaliamoorthi 2014/02/11 16:29:16 Some files especially procfs have incorrect size (
144 // hence the file is read sequentially as opposed to one-shot read.
139 while ((len = fread(buf, 1, sizeof(buf), file)) > 0) { 145 while ((len = fread(buf, 1, sizeof(buf), file)) > 0) {
146 if ((maxsize - size) < len) {
bartfab (slow) 2014/02/11 12:51:12 Nit: A matter of taste but I think |len > maxsize
kaliamoorthi 2014/02/11 16:29:16 Since it is not a coding guideline, I think the cu
147 if (contents)
148 contents->clear();
149 CloseFile(file);
150 return false;
151 }
152 size += len;
140 if (contents) 153 if (contents)
141 contents->append(buf, len); 154 contents->append(buf, len);
142 } 155 }
143 CloseFile(file); 156 CloseFile(file);
144 157
145 return true; 158 return true;
146 } 159 }
147 160
161 bool ReadFileToString(const FilePath& path, std::string* contents) {
162 return ReadFileToString(path, contents, std::numeric_limits<size_t>::max());
bartfab (slow) 2014/02/11 12:51:12 Nit: #include <limits>
kaliamoorthi 2014/02/11 16:29:16 Done.
163 }
164
148 bool IsDirectoryEmpty(const FilePath& dir_path) { 165 bool IsDirectoryEmpty(const FilePath& dir_path) {
149 FileEnumerator files(dir_path, false, 166 FileEnumerator files(dir_path, false,
150 FileEnumerator::FILES | FileEnumerator::DIRECTORIES); 167 FileEnumerator::FILES | FileEnumerator::DIRECTORIES);
151 if (files.Next().empty()) 168 if (files.Next().empty())
152 return true; 169 return true;
153 return false; 170 return false;
154 } 171 }
155 172
156 FILE* CreateAndOpenTemporaryFile(FilePath* path) { 173 FILE* CreateAndOpenTemporaryFile(FilePath* path) {
157 FilePath directory; 174 FilePath directory;
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 if (!PathExists(new_path) && 257 if (!PathExists(new_path) &&
241 (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) { 258 (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) {
242 return count; 259 return count;
243 } 260 }
244 } 261 }
245 262
246 return -1; 263 return -1;
247 } 264 }
248 265
249 } // namespace file_util 266 } // namespace file_util
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698