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

Side by Side Diff: base/files/file_util.cc

Issue 640373006: Don't use 64KB of stack on a single call. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git/+/master
Patch Set: Created 6 years, 2 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/files/file_util.h" 5 #include "base/files/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 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 size_t max_size) { 132 size_t max_size) {
133 if (contents) 133 if (contents)
134 contents->clear(); 134 contents->clear();
135 if (path.ReferencesParent()) 135 if (path.ReferencesParent())
136 return false; 136 return false;
137 FILE* file = OpenFile(path, "rb"); 137 FILE* file = OpenFile(path, "rb");
138 if (!file) { 138 if (!file) {
139 return false; 139 return false;
140 } 140 }
141 141
142 char buf[1 << 16]; 142 const size_t kBufferSize = 1 << 16;
143 scoped_ptr<char[]> buf(new char[kBufferSize]);
143 size_t len; 144 size_t len;
144 size_t size = 0; 145 size_t size = 0;
145 bool read_status = true; 146 bool read_status = true;
146 147
147 // Many files supplied in |path| have incorrect size (proc files etc). 148 // Many files supplied in |path| have incorrect size (proc files etc).
148 // Hence, the file is read sequentially as opposed to a one-shot read. 149 // Hence, the file is read sequentially as opposed to a one-shot read.
149 while ((len = fread(buf, 1, sizeof(buf), file)) > 0) { 150 while ((len = fread(buf.get(), 1, kBufferSize, file)) > 0) {
150 if (contents) 151 if (contents)
151 contents->append(buf, std::min(len, max_size - size)); 152 contents->append(buf.get(), std::min(len, max_size - size));
152 153
153 if ((max_size - size) < len) { 154 if ((max_size - size) < len) {
154 read_status = false; 155 read_status = false;
155 break; 156 break;
156 } 157 }
157 158
158 size += len; 159 size += len;
159 } 160 }
160 read_status = read_status && !ferror(file); 161 read_status = read_status && !ferror(file);
161 CloseFile(file); 162 CloseFile(file);
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
251 if (!PathExists(new_path) && 252 if (!PathExists(new_path) &&
252 (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) { 253 (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) {
253 return count; 254 return count;
254 } 255 }
255 } 256 }
256 257
257 return -1; 258 return -1;
258 } 259 }
259 260
260 } // namespace base 261 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698