| 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/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 Loading... |
| 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 Loading... |
| 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 |
| OLD | NEW |