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

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
« no previous file with comments | « base/file_util.h ('k') | base/file_util_unittest.cc » ('j') | 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/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>
11 11
12 #include <fstream> 12 #include <fstream>
13 #include <limits>
13 14
14 #include "base/files/file_enumerator.h" 15 #include "base/files/file_enumerator.h"
15 #include "base/files/file_path.h" 16 #include "base/files/file_path.h"
16 #include "base/logging.h" 17 #include "base/logging.h"
17 #include "base/strings/string_piece.h" 18 #include "base/strings/string_piece.h"
18 #include "base/strings/string_util.h" 19 #include "base/strings/string_util.h"
19 #include "base/strings/stringprintf.h" 20 #include "base/strings/stringprintf.h"
20 #include "base/strings/utf_string_conversions.h" 21 #include "base/strings/utf_string_conversions.h"
21 22
22 namespace base { 23 namespace base {
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 else if (end2 + 1 < line2.length()) 120 else if (end2 + 1 < line2.length())
120 line2.erase(end2 + 1); 121 line2.erase(end2 + 1);
121 122
122 if (line1 != line2) 123 if (line1 != line2)
123 return false; 124 return false;
124 } while (!file1.eof() || !file2.eof()); 125 } while (!file1.eof() || !file2.eof());
125 126
126 return true; 127 return true;
127 } 128 }
128 129
129 bool ReadFileToString(const FilePath& path, std::string* contents) { 130 bool ReadFileToString(const FilePath& path,
131 std::string* contents,
132 size_t max_size) {
130 if (path.ReferencesParent()) 133 if (path.ReferencesParent())
131 return false; 134 return false;
132 FILE* file = OpenFile(path, "rb"); 135 FILE* file = OpenFile(path, "rb");
133 if (!file) { 136 if (!file) {
134 return false; 137 return false;
135 } 138 }
136 139
137 char buf[1 << 16]; 140 char buf[1 << 16];
138 size_t len; 141 size_t len;
142 size_t size = 0;
143
144 // Many files supplied in path have incorrect size (proc files, streams etc)
bartfab (slow) 2014/02/12 16:27:11 Nit 1: s/path/|path|/ Nit 2: s/etc/etc./ Nit 3: En
kaliamoorthi 2014/02/17 11:09:41 Done.
145 // hence the file is read sequentially as opposed to one-shot read.
bartfab (slow) 2014/02/12 16:27:11 Nit 1: Add comma after "hence." Nit 2: s/one-shot/
kaliamoorthi 2014/02/17 11:09:41 Done.
139 while ((len = fread(buf, 1, sizeof(buf), file)) > 0) { 146 while ((len = fread(buf, 1, sizeof(buf), file)) > 0) {
147 if ((max_size - size) < len) {
148 if (contents)
149 contents->clear();
150 CloseFile(file);
151 return false;
152 }
153 size += len;
140 if (contents) 154 if (contents)
141 contents->append(buf, len); 155 contents->append(buf, len);
142 } 156 }
143 CloseFile(file); 157 CloseFile(file);
144 158
145 return true; 159 return true;
146 } 160 }
147 161
162 bool ReadFileToString(const FilePath& path, std::string* contents) {
163 return ReadFileToString(path, contents, std::numeric_limits<size_t>::max());
164 }
165
148 bool IsDirectoryEmpty(const FilePath& dir_path) { 166 bool IsDirectoryEmpty(const FilePath& dir_path) {
149 FileEnumerator files(dir_path, false, 167 FileEnumerator files(dir_path, false,
150 FileEnumerator::FILES | FileEnumerator::DIRECTORIES); 168 FileEnumerator::FILES | FileEnumerator::DIRECTORIES);
151 if (files.Next().empty()) 169 if (files.Next().empty())
152 return true; 170 return true;
153 return false; 171 return false;
154 } 172 }
155 173
156 FILE* CreateAndOpenTemporaryFile(FilePath* path) { 174 FILE* CreateAndOpenTemporaryFile(FilePath* path) {
157 FilePath directory; 175 FilePath directory;
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 if (!PathExists(new_path) && 258 if (!PathExists(new_path) &&
241 (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) { 259 (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) {
242 return count; 260 return count;
243 } 261 }
244 } 262 }
245 263
246 return -1; 264 return -1;
247 } 265 }
248 266
249 } // namespace file_util 267 } // namespace file_util
OLDNEW
« no previous file with comments | « base/file_util.h ('k') | base/file_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698