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

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

Issue 2678813003: Disable fd/handle sharing in base::ReadFileToString. (Closed)
Patch Set: Created 3 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
« base/files/file_util.h ('K') | « base/files/file_util.h ('k') | 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 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 } 124 }
125 #endif // !defined(OS_NACL_NONSFI) 125 #endif // !defined(OS_NACL_NONSFI)
126 126
127 bool ReadFileToStringWithMaxSize(const FilePath& path, 127 bool ReadFileToStringWithMaxSize(const FilePath& path,
128 std::string* contents, 128 std::string* contents,
129 size_t max_size) { 129 size_t max_size) {
130 if (contents) 130 if (contents)
131 contents->clear(); 131 contents->clear();
132 if (path.ReferencesParent()) 132 if (path.ReferencesParent())
133 return false; 133 return false;
134 FILE* file = OpenFile(path, "rb"); 134 FILE* file = OpenFile(path, "rb" FONE);
135 if (!file) { 135 if (!file) {
136 return false; 136 return false;
137 } 137 }
138 138
139 const size_t kBufferSize = 1 << 16; 139 const size_t kBufferSize = 1 << 16;
140 std::unique_ptr<char[]> buf(new char[kBufferSize]); 140 std::unique_ptr<char[]> buf(new char[kBufferSize]);
141 size_t len; 141 size_t len;
142 size_t size = 0; 142 size_t size = 0;
143 bool read_status = true; 143 bool read_status = true;
144 144
145 // Many files supplied in |path| have incorrect size (proc files etc). 145 // Many files supplied in |path| have incorrect size (proc files etc).
146 // Hence, the file is read sequentially as opposed to a one-shot read. 146 // Hence, the file is read sequentially as opposed to a one-shot read.
147 while ((len = fread(buf.get(), 1, kBufferSize, file)) > 0) { 147 while ((len = fread(buf.get(), 1, kBufferSize, file)) > 0) {
148 if (contents) 148 if (contents)
149 contents->append(buf.get(), std::min(len, max_size - size)); 149 contents->append(buf.get(), std::min(len, max_size - size));
150 150
151 if ((max_size - size) < len) { 151 if ((max_size - size) < len) {
152 read_status = false; 152 read_status = false;
153 break; 153 break;
154 } 154 }
155 155
156 size += len; 156 size += len;
157 } 157 }
158 read_status = read_status && !ferror(file); 158 read_status = read_status && !ferror(file);
159 CloseFile(file); 159 CloseFile(file);
gab 2017/02/06 21:13:19 Since we CloseFile here, is the issue that |file|
grt (UTC plus 2) 2017/02/06 21:51:21 Yes, racy and actively happening in tests due to t
160 160
161 return read_status; 161 return read_status;
162 } 162 }
163 163
164 bool ReadFileToString(const FilePath& path, std::string* contents) { 164 bool ReadFileToString(const FilePath& path, std::string* contents) {
165 return ReadFileToStringWithMaxSize(path, contents, 165 return ReadFileToStringWithMaxSize(path, contents,
166 std::numeric_limits<size_t>::max()); 166 std::numeric_limits<size_t>::max());
167 } 167 }
168 168
169 #if !defined(OS_NACL_NONSFI) 169 #if !defined(OS_NACL_NONSFI)
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) { 254 (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) {
255 return count; 255 return count;
256 } 256 }
257 } 257 }
258 258
259 return -1; 259 return -1;
260 } 260 }
261 #endif // !defined(OS_NACL_NONSFI) 261 #endif // !defined(OS_NACL_NONSFI)
262 262
263 } // namespace base 263 } // namespace base
OLDNEW
« base/files/file_util.h ('K') | « base/files/file_util.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698