Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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, |
| 130 std::string* contents, | |
| 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) | |
| 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) { | |
| 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, | |
| 162 std::string* contents, | |
| 163 size_t maxsize) { | |
| 164 return ReadFileToStringImpl(path, contents, maxsize); | |
| 165 } | |
| 166 | |
| 167 bool ReadFileToString(const FilePath& path, std::string* contents) { | |
| 168 return ReadFileToStringImpl(path, contents, ~(size_t)0); | |
|
Andrew T Wilson (Slow)
2014/02/11 10:27:12
Can you use SIZE_MAX here instead of ~0?
I'm OK w
kaliamoorthi
2014/02/11 16:29:16
Changed to std::numeric_limits<size_t>::max() as d
| |
| 169 } | |
| 170 | |
| 148 bool IsDirectoryEmpty(const FilePath& dir_path) { | 171 bool IsDirectoryEmpty(const FilePath& dir_path) { |
| 149 FileEnumerator files(dir_path, false, | 172 FileEnumerator files(dir_path, false, |
| 150 FileEnumerator::FILES | FileEnumerator::DIRECTORIES); | 173 FileEnumerator::FILES | FileEnumerator::DIRECTORIES); |
| 151 if (files.Next().empty()) | 174 if (files.Next().empty()) |
| 152 return true; | 175 return true; |
| 153 return false; | 176 return false; |
| 154 } | 177 } |
| 155 | 178 |
| 156 FILE* CreateAndOpenTemporaryFile(FilePath* path) { | 179 FILE* CreateAndOpenTemporaryFile(FilePath* path) { |
| 157 FilePath directory; | 180 FilePath directory; |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 240 if (!PathExists(new_path) && | 263 if (!PathExists(new_path) && |
| 241 (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) { | 264 (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) { |
| 242 return count; | 265 return count; |
| 243 } | 266 } |
| 244 } | 267 } |
| 245 | 268 |
| 246 return -1; | 269 return -1; |
| 247 } | 270 } |
| 248 | 271 |
| 249 } // namespace file_util | 272 } // namespace file_util |
| OLD | NEW |