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