| 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> |
| 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 Loading... |
| 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) { |
| 133 if (contents) |
| 134 contents->clear(); |
| 130 if (path.ReferencesParent()) | 135 if (path.ReferencesParent()) |
| 131 return false; | 136 return false; |
| 132 FILE* file = OpenFile(path, "rb"); | 137 FILE* file = OpenFile(path, "rb"); |
| 133 if (!file) { | 138 if (!file) { |
| 134 return false; | 139 return false; |
| 135 } | 140 } |
| 136 | 141 |
| 137 char buf[1 << 16]; | 142 char buf[1 << 16]; |
| 138 size_t len; | 143 size_t len; |
| 144 size_t size = 0; |
| 145 bool read_status = true; |
| 146 |
| 147 // 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. |
| 139 while ((len = fread(buf, 1, sizeof(buf), file)) > 0) { | 149 while ((len = fread(buf, 1, sizeof(buf), file)) > 0) { |
| 140 if (contents) | 150 if (contents) |
| 141 contents->append(buf, len); | 151 contents->append(buf, std::min(len, max_size - size)); |
| 152 |
| 153 if ((max_size - size) < len) { |
| 154 read_status = false; |
| 155 break; |
| 156 } |
| 157 |
| 158 size += len; |
| 142 } | 159 } |
| 143 CloseFile(file); | 160 CloseFile(file); |
| 144 | 161 |
| 145 return true; | 162 return read_status; |
| 163 } |
| 164 |
| 165 bool ReadFileToString(const FilePath& path, std::string* contents) { |
| 166 return ReadFileToString(path, contents, std::numeric_limits<size_t>::max()); |
| 146 } | 167 } |
| 147 | 168 |
| 148 bool IsDirectoryEmpty(const FilePath& dir_path) { | 169 bool IsDirectoryEmpty(const FilePath& dir_path) { |
| 149 FileEnumerator files(dir_path, false, | 170 FileEnumerator files(dir_path, false, |
| 150 FileEnumerator::FILES | FileEnumerator::DIRECTORIES); | 171 FileEnumerator::FILES | FileEnumerator::DIRECTORIES); |
| 151 if (files.Next().empty()) | 172 if (files.Next().empty()) |
| 152 return true; | 173 return true; |
| 153 return false; | 174 return false; |
| 154 } | 175 } |
| 155 | 176 |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 if (!PathExists(new_path) && | 261 if (!PathExists(new_path) && |
| 241 (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) { | 262 (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) { |
| 242 return count; | 263 return count; |
| 243 } | 264 } |
| 244 } | 265 } |
| 245 | 266 |
| 246 return -1; | 267 return -1; |
| 247 } | 268 } |
| 248 | 269 |
| 249 } // namespace file_util | 270 } // namespace file_util |
| OLD | NEW |