OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 #include <windows.h> | 7 #include <windows.h> |
8 #include <shellapi.h> | 8 #include <shellapi.h> |
9 #include <shlobj.h> | 9 #include <shlobj.h> |
10 #include <time.h> | 10 #include <time.h> |
(...skipping 353 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 return false; | 364 return false; |
365 } | 365 } |
366 | 366 |
367 HRESULT result = i_persist_file->Save(destination, TRUE); | 367 HRESULT result = i_persist_file->Save(destination, TRUE); |
368 i_persist_file->Release(); | 368 i_persist_file->Release(); |
369 i_shell_link->Release(); | 369 i_shell_link->Release(); |
370 return SUCCEEDED(result); | 370 return SUCCEEDED(result); |
371 } | 371 } |
372 | 372 |
373 bool IsDirectoryEmpty(const std::wstring& dir_path) { | 373 bool IsDirectoryEmpty(const std::wstring& dir_path) { |
374 FileEnumerator files(dir_path, false, FileEnumerator::FILES_AND_DIRECTORIES); | 374 FileEnumerator files(FilePath(dir_path), |
375 if (files.Next().empty()) | 375 false, FileEnumerator::FILES_AND_DIRECTORIES); |
| 376 if (files.Next().value().empty()) |
376 return true; | 377 return true; |
377 return false; | 378 return false; |
378 } | 379 } |
379 | 380 |
380 bool GetTempDir(FilePath* path) { | 381 bool GetTempDir(FilePath* path) { |
381 wchar_t temp_path[MAX_PATH + 1]; | 382 wchar_t temp_path[MAX_PATH + 1]; |
382 DWORD path_len = ::GetTempPath(MAX_PATH, temp_path); | 383 DWORD path_len = ::GetTempPath(MAX_PATH, temp_path); |
383 if (path_len >= MAX_PATH || path_len <= 0) | 384 if (path_len >= MAX_PATH || path_len <= 0) |
384 return false; | 385 return false; |
385 // TODO(evanm): the old behavior of this function was to always strip the | 386 // TODO(evanm): the old behavior of this function was to always strip the |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
589 | 590 |
590 // Sets the current working directory for the process. | 591 // Sets the current working directory for the process. |
591 bool SetCurrentDirectory(const FilePath& directory) { | 592 bool SetCurrentDirectory(const FilePath& directory) { |
592 BOOL ret = ::SetCurrentDirectory(directory.value().c_str()); | 593 BOOL ret = ::SetCurrentDirectory(directory.value().c_str()); |
593 return ret != 0; | 594 return ret != 0; |
594 } | 595 } |
595 | 596 |
596 /////////////////////////////////////////////// | 597 /////////////////////////////////////////////// |
597 | 598 |
598 | 599 |
599 FileEnumerator::FileEnumerator(const std::wstring& root_path, | 600 FileEnumerator::FileEnumerator(const FilePath& root_path, |
600 bool recursive, | 601 bool recursive, |
601 FileEnumerator::FILE_TYPE file_type) | 602 FileEnumerator::FILE_TYPE file_type) |
602 : recursive_(recursive), | 603 : recursive_(recursive), |
603 file_type_(file_type), | 604 file_type_(file_type), |
604 is_in_find_op_(false), | 605 is_in_find_op_(false), |
605 find_handle_(INVALID_HANDLE_VALUE) { | 606 find_handle_(INVALID_HANDLE_VALUE) { |
606 pending_paths_.push(root_path); | 607 pending_paths_.push(root_path); |
607 } | 608 } |
608 | 609 |
609 FileEnumerator::FileEnumerator(const std::wstring& root_path, | 610 FileEnumerator::FileEnumerator(const FilePath& root_path, |
610 bool recursive, | 611 bool recursive, |
611 FileEnumerator::FILE_TYPE file_type, | 612 FileEnumerator::FILE_TYPE file_type, |
612 const std::wstring& pattern) | 613 const FilePath::StringType& pattern) |
613 : recursive_(recursive), | 614 : recursive_(recursive), |
614 file_type_(file_type), | 615 file_type_(file_type), |
615 is_in_find_op_(false), | 616 is_in_find_op_(false), |
616 pattern_(pattern), | 617 pattern_(pattern), |
617 find_handle_(INVALID_HANDLE_VALUE) { | 618 find_handle_(INVALID_HANDLE_VALUE) { |
618 pending_paths_.push(root_path); | 619 pending_paths_.push(root_path); |
619 } | 620 } |
620 | 621 |
621 FileEnumerator::~FileEnumerator() { | 622 FileEnumerator::~FileEnumerator() { |
622 if (find_handle_ != INVALID_HANDLE_VALUE) | 623 if (find_handle_ != INVALID_HANDLE_VALUE) |
623 FindClose(find_handle_); | 624 FindClose(find_handle_); |
624 } | 625 } |
625 | 626 |
626 void FileEnumerator::GetFindInfo(FindInfo* info) { | 627 void FileEnumerator::GetFindInfo(FindInfo* info) { |
627 DCHECK(info); | 628 DCHECK(info); |
628 | 629 |
629 if (!is_in_find_op_) | 630 if (!is_in_find_op_) |
630 return; | 631 return; |
631 | 632 |
632 memcpy(info, &find_data_, sizeof(*info)); | 633 memcpy(info, &find_data_, sizeof(*info)); |
633 } | 634 } |
634 | 635 |
635 std::wstring FileEnumerator::Next() { | 636 FilePath FileEnumerator::Next() { |
636 if (!is_in_find_op_) { | 637 if (!is_in_find_op_) { |
637 if (pending_paths_.empty()) | 638 if (pending_paths_.empty()) |
638 return std::wstring(); | 639 return FilePath(); |
639 | 640 |
640 // The last find FindFirstFile operation is done, prepare a new one. | 641 // The last find FindFirstFile operation is done, prepare a new one. |
641 // root_path_ must have the trailing directory character. | |
642 root_path_ = pending_paths_.top(); | 642 root_path_ = pending_paths_.top(); |
643 file_util::AppendToPath(&root_path_, std::wstring()); | |
644 pending_paths_.pop(); | 643 pending_paths_.pop(); |
645 | 644 |
646 // Start a new find operation. | 645 // Start a new find operation. |
647 std::wstring src(root_path_); | 646 FilePath src = root_path_; |
648 | 647 |
649 if (pattern_.empty()) | 648 if (pattern_.value().empty()) |
650 file_util::AppendToPath(&src, L"*"); // No pattern = match everything. | 649 src = src.Append(L"*"); // No pattern = match everything. |
651 else | 650 else |
652 file_util::AppendToPath(&src, pattern_); | 651 src = src.Append(pattern_); |
653 | 652 |
654 find_handle_ = FindFirstFile(src.c_str(), &find_data_); | 653 find_handle_ = FindFirstFile(src.value().c_str(), &find_data_); |
655 is_in_find_op_ = true; | 654 is_in_find_op_ = true; |
656 | 655 |
657 } else { | 656 } else { |
658 // Search for the next file/directory. | 657 // Search for the next file/directory. |
659 if (!FindNextFile(find_handle_, &find_data_)) { | 658 if (!FindNextFile(find_handle_, &find_data_)) { |
660 FindClose(find_handle_); | 659 FindClose(find_handle_); |
661 find_handle_ = INVALID_HANDLE_VALUE; | 660 find_handle_ = INVALID_HANDLE_VALUE; |
662 } | 661 } |
663 } | 662 } |
664 | 663 |
665 if (INVALID_HANDLE_VALUE == find_handle_) { | 664 if (INVALID_HANDLE_VALUE == find_handle_) { |
666 is_in_find_op_ = false; | 665 is_in_find_op_ = false; |
667 | 666 |
668 // This is reached when we have finished a directory and are advancing to | 667 // This is reached when we have finished a directory and are advancing to |
669 // the next one in the queue. We applied the pattern (if any) to the files | 668 // the next one in the queue. We applied the pattern (if any) to the files |
670 // in the root search directory, but for those directories which were | 669 // in the root search directory, but for those directories which were |
671 // matched, we want to enumerate all files inside them. This will happen | 670 // matched, we want to enumerate all files inside them. This will happen |
672 // when the handle is empty. | 671 // when the handle is empty. |
673 pattern_.clear(); | 672 pattern_ = FilePath(); |
674 | 673 |
675 return Next(); | 674 return Next(); |
676 } | 675 } |
677 | 676 |
678 std::wstring cur_file(find_data_.cFileName); | 677 FilePath cur_file(find_data_.cFileName); |
679 // Skip over . and .. | 678 // Skip over . and .. |
680 if (L"." == cur_file || L".." == cur_file) | 679 if (L"." == cur_file.value() || L".." == cur_file.value()) |
681 return Next(); | 680 return Next(); |
682 | 681 |
683 // Construct the absolute filename. | 682 // Construct the absolute filename. |
684 cur_file.insert(0, root_path_); | 683 cur_file = root_path_.Append(cur_file); |
685 | 684 |
686 if (find_data_.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { | 685 if (find_data_.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { |
687 if (recursive_) { | 686 if (recursive_) { |
688 // If |cur_file| is a directory, and we are doing recursive searching, add | 687 // If |cur_file| is a directory, and we are doing recursive searching, add |
689 // it to pending_paths_ so we scan it after we finish scanning this | 688 // it to pending_paths_ so we scan it after we finish scanning this |
690 // directory. | 689 // directory. |
691 pending_paths_.push(cur_file); | 690 pending_paths_.push(cur_file); |
692 } | 691 } |
693 return (file_type_ & FileEnumerator::DIRECTORIES) ? cur_file : Next(); | 692 return (file_type_ & FileEnumerator::DIRECTORIES) ? cur_file : Next(); |
694 } | 693 } |
(...skipping 11 matching lines...) Expand all Loading... |
706 void PathComponents(const std::wstring& path, | 705 void PathComponents(const std::wstring& path, |
707 std::vector<std::wstring>* components) { | 706 std::vector<std::wstring>* components) { |
708 PathComponents(FilePath(path), components); | 707 PathComponents(FilePath(path), components); |
709 } | 708 } |
710 void ReplaceExtension(std::wstring* file_name, const std::wstring& extension) { | 709 void ReplaceExtension(std::wstring* file_name, const std::wstring& extension) { |
711 FilePath path(*file_name); | 710 FilePath path(*file_name); |
712 ReplaceExtension(&path, extension); | 711 ReplaceExtension(&path, extension); |
713 file_name->assign(path.value()); | 712 file_name->assign(path.value()); |
714 } | 713 } |
715 } // namespace file_util | 714 } // namespace file_util |
OLD | NEW |