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 | 13 |
| 14 #include "base/files/file_enumerator.h" |
14 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
15 #include "base/logging.h" | 16 #include "base/logging.h" |
16 #include "base/string_util.h" | 17 #include "base/string_util.h" |
17 #include "base/stringprintf.h" | 18 #include "base/stringprintf.h" |
18 #include "base/strings/string_piece.h" | 19 #include "base/strings/string_piece.h" |
19 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
20 | 21 |
| 22 using base::FileEnumerator; |
21 using base::FilePath; | 23 using base::FilePath; |
22 | 24 |
23 namespace { | 25 namespace { |
24 | 26 |
25 const FilePath::CharType kExtensionSeparator = FILE_PATH_LITERAL('.'); | 27 const FilePath::CharType kExtensionSeparator = FILE_PATH_LITERAL('.'); |
26 | 28 |
27 // The maximum number of 'uniquified' files we will try to create. | 29 // The maximum number of 'uniquified' files we will try to create. |
28 // This is used when the filename we're trying to download is already in use, | 30 // This is used when the filename we're trying to download is already in use, |
29 // so we create a new unique filename by appending " (nnn)" before the | 31 // so we create a new unique filename by appending " (nnn)" before the |
30 // extension, where 1 <= nnn <= kMaxUniqueFiles. | 32 // extension, where 1 <= nnn <= kMaxUniqueFiles. |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 contents->append(buf, len); | 165 contents->append(buf, len); |
164 } | 166 } |
165 CloseFile(file); | 167 CloseFile(file); |
166 | 168 |
167 return true; | 169 return true; |
168 } | 170 } |
169 | 171 |
170 bool IsDirectoryEmpty(const FilePath& dir_path) { | 172 bool IsDirectoryEmpty(const FilePath& dir_path) { |
171 FileEnumerator files(dir_path, false, | 173 FileEnumerator files(dir_path, false, |
172 FileEnumerator::FILES | FileEnumerator::DIRECTORIES); | 174 FileEnumerator::FILES | FileEnumerator::DIRECTORIES); |
173 if (files.Next().value().empty()) | 175 if (files.Next().empty()) |
174 return true; | 176 return true; |
175 return false; | 177 return false; |
176 } | 178 } |
177 | 179 |
178 FILE* CreateAndOpenTemporaryFile(FilePath* path) { | 180 FILE* CreateAndOpenTemporaryFile(FilePath* path) { |
179 FilePath directory; | 181 FilePath directory; |
180 if (!GetTempDir(&directory)) | 182 if (!GetTempDir(&directory)) |
181 return NULL; | 183 return NULL; |
182 | 184 |
183 return CreateAndOpenTemporaryFileInDir(directory, path); | 185 return CreateAndOpenTemporaryFileInDir(directory, path); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 return count; | 262 return count; |
261 } | 263 } |
262 } | 264 } |
263 | 265 |
264 return -1; | 266 return -1; |
265 } | 267 } |
266 | 268 |
267 int64 ComputeDirectorySize(const FilePath& root_path) { | 269 int64 ComputeDirectorySize(const FilePath& root_path) { |
268 int64 running_size = 0; | 270 int64 running_size = 0; |
269 FileEnumerator file_iter(root_path, true, FileEnumerator::FILES); | 271 FileEnumerator file_iter(root_path, true, FileEnumerator::FILES); |
270 for (FilePath current = file_iter.Next(); !current.empty(); | 272 while (!file_iter.Next().empty()) |
271 current = file_iter.Next()) { | 273 running_size += file_iter.GetInfo().GetSize(); |
272 FileEnumerator::FindInfo info; | |
273 file_iter.GetFindInfo(&info); | |
274 #if defined(OS_WIN) | |
275 LARGE_INTEGER li = { info.nFileSizeLow, info.nFileSizeHigh }; | |
276 running_size += li.QuadPart; | |
277 #else | |
278 running_size += info.stat.st_size; | |
279 #endif | |
280 } | |
281 return running_size; | 274 return running_size; |
282 } | 275 } |
283 | 276 |
284 /////////////////////////////////////////////// | |
285 // FileEnumerator | |
286 // | |
287 // Note: the main logic is in file_util_<platform>.cc | |
288 | |
289 bool FileEnumerator::ShouldSkip(const FilePath& path) { | |
290 FilePath::StringType basename = path.BaseName().value(); | |
291 return basename == FILE_PATH_LITERAL(".") || | |
292 (basename == FILE_PATH_LITERAL("..") && | |
293 !(INCLUDE_DOT_DOT & file_type_)); | |
294 } | |
295 | |
296 } // namespace | 277 } // namespace |
OLD | NEW |