| 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 #include <windows.h> | 7 #include <windows.h> |
| 8 #include <psapi.h> | 8 #include <psapi.h> |
| 9 #include <shellapi.h> | 9 #include <shellapi.h> |
| 10 #include <shlobj.h> | 10 #include <shlobj.h> |
| (...skipping 613 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 624 DLOG_GETLASTERROR(WARNING) << "writing file " | 624 DLOG_GETLASTERROR(WARNING) << "writing file " |
| 625 << UTF16ToUTF8(filename.value()) << " failed"; | 625 << UTF16ToUTF8(filename.value()) << " failed"; |
| 626 } else { | 626 } else { |
| 627 // Didn't write all the bytes. | 627 // Didn't write all the bytes. |
| 628 DLOG(WARNING) << "wrote" << written << " bytes to " | 628 DLOG(WARNING) << "wrote" << written << " bytes to " |
| 629 << UTF16ToUTF8(filename.value()) << " expected " << size; | 629 << UTF16ToUTF8(filename.value()) << " expected " << size; |
| 630 } | 630 } |
| 631 return -1; | 631 return -1; |
| 632 } | 632 } |
| 633 | 633 |
| 634 } // namespace base | |
| 635 | |
| 636 // ----------------------------------------------------------------------------- | |
| 637 | |
| 638 namespace file_util { | |
| 639 | |
| 640 using base::DirectoryExists; | |
| 641 using base::FilePath; | |
| 642 using base::kFileShareAll; | |
| 643 | |
| 644 FILE* OpenFile(const std::string& filename, const char* mode) { | |
| 645 base::ThreadRestrictions::AssertIOAllowed(); | |
| 646 return _fsopen(filename.c_str(), mode, _SH_DENYNO); | |
| 647 } | |
| 648 | |
| 649 int AppendToFile(const FilePath& filename, const char* data, int size) { | 634 int AppendToFile(const FilePath& filename, const char* data, int size) { |
| 650 base::ThreadRestrictions::AssertIOAllowed(); | 635 ThreadRestrictions::AssertIOAllowed(); |
| 651 base::win::ScopedHandle file(CreateFile(filename.value().c_str(), | 636 base::win::ScopedHandle file(CreateFile(filename.value().c_str(), |
| 652 FILE_APPEND_DATA, | 637 FILE_APPEND_DATA, |
| 653 0, | 638 0, |
| 654 NULL, | 639 NULL, |
| 655 OPEN_EXISTING, | 640 OPEN_EXISTING, |
| 656 0, | 641 0, |
| 657 NULL)); | 642 NULL)); |
| 658 if (!file) { | 643 if (!file) { |
| 659 DLOG_GETLASTERROR(WARNING) << "CreateFile failed for path " | 644 DLOG_GETLASTERROR(WARNING) << "CreateFile failed for path " |
| 660 << filename.value(); | 645 << filename.value(); |
| 661 return -1; | 646 return -1; |
| 662 } | 647 } |
| 663 | 648 |
| 664 DWORD written; | 649 DWORD written; |
| 665 BOOL result = ::WriteFile(file, data, size, &written, NULL); | 650 BOOL result = ::WriteFile(file, data, size, &written, NULL); |
| 666 if (result && static_cast<int>(written) == size) | 651 if (result && static_cast<int>(written) == size) |
| 667 return written; | 652 return written; |
| 668 | 653 |
| 669 if (!result) { | 654 if (!result) { |
| 670 // WriteFile failed. | 655 // WriteFile failed. |
| 671 DLOG_GETLASTERROR(WARNING) << "writing file " << filename.value() | 656 DLOG_GETLASTERROR(WARNING) << "writing file " |
| 657 << UTF16ToUTF8(filename.value()) |
| 672 << " failed"; | 658 << " failed"; |
| 673 } else { | 659 } else { |
| 674 // Didn't write all the bytes. | 660 // Didn't write all the bytes. |
| 675 DLOG(WARNING) << "wrote" << written << " bytes to " | 661 DLOG(WARNING) << "wrote" << written << " bytes to " |
| 676 << filename.value() << " expected " << size; | 662 << UTF16ToUTF8(filename.value()) << " expected " << size; |
| 677 } | 663 } |
| 678 return -1; | 664 return -1; |
| 679 } | 665 } |
| 680 | 666 |
| 681 // Gets the current working directory for the process. | 667 // Gets the current working directory for the process. |
| 682 bool GetCurrentDirectory(FilePath* dir) { | 668 bool GetCurrentDirectory(FilePath* dir) { |
| 683 base::ThreadRestrictions::AssertIOAllowed(); | 669 ThreadRestrictions::AssertIOAllowed(); |
| 684 | 670 |
| 685 wchar_t system_buffer[MAX_PATH]; | 671 wchar_t system_buffer[MAX_PATH]; |
| 686 system_buffer[0] = 0; | 672 system_buffer[0] = 0; |
| 687 DWORD len = ::GetCurrentDirectory(MAX_PATH, system_buffer); | 673 DWORD len = ::GetCurrentDirectory(MAX_PATH, system_buffer); |
| 688 if (len == 0 || len > MAX_PATH) | 674 if (len == 0 || len > MAX_PATH) |
| 689 return false; | 675 return false; |
| 690 // TODO(evanm): the old behavior of this function was to always strip the | 676 // TODO(evanm): the old behavior of this function was to always strip the |
| 691 // trailing slash. We duplicate this here, but it shouldn't be necessary | 677 // trailing slash. We duplicate this here, but it shouldn't be necessary |
| 692 // when everyone is using the appropriate FilePath APIs. | 678 // when everyone is using the appropriate FilePath APIs. |
| 693 std::wstring dir_str(system_buffer); | 679 std::wstring dir_str(system_buffer); |
| 694 *dir = FilePath(dir_str).StripTrailingSeparators(); | 680 *dir = FilePath(dir_str).StripTrailingSeparators(); |
| 695 return true; | 681 return true; |
| 696 } | 682 } |
| 697 | 683 |
| 698 // Sets the current working directory for the process. | 684 // Sets the current working directory for the process. |
| 699 bool SetCurrentDirectory(const FilePath& directory) { | 685 bool SetCurrentDirectory(const FilePath& directory) { |
| 700 base::ThreadRestrictions::AssertIOAllowed(); | 686 ThreadRestrictions::AssertIOAllowed(); |
| 701 BOOL ret = ::SetCurrentDirectory(directory.value().c_str()); | 687 BOOL ret = ::SetCurrentDirectory(directory.value().c_str()); |
| 702 return ret != 0; | 688 return ret != 0; |
| 703 } | 689 } |
| 704 | 690 |
| 691 } // namespace base |
| 692 |
| 693 // ----------------------------------------------------------------------------- |
| 694 |
| 695 namespace file_util { |
| 696 |
| 697 using base::DirectoryExists; |
| 698 using base::FilePath; |
| 699 using base::kFileShareAll; |
| 700 |
| 701 FILE* OpenFile(const std::string& filename, const char* mode) { |
| 702 base::ThreadRestrictions::AssertIOAllowed(); |
| 703 return _fsopen(filename.c_str(), mode, _SH_DENYNO); |
| 704 } |
| 705 |
| 705 int GetMaximumPathComponentLength(const FilePath& path) { | 706 int GetMaximumPathComponentLength(const FilePath& path) { |
| 706 base::ThreadRestrictions::AssertIOAllowed(); | 707 base::ThreadRestrictions::AssertIOAllowed(); |
| 707 | 708 |
| 708 wchar_t volume_path[MAX_PATH]; | 709 wchar_t volume_path[MAX_PATH]; |
| 709 if (!GetVolumePathNameW(path.NormalizePathSeparators().value().c_str(), | 710 if (!GetVolumePathNameW(path.NormalizePathSeparators().value().c_str(), |
| 710 volume_path, | 711 volume_path, |
| 711 arraysize(volume_path))) { | 712 arraysize(volume_path))) { |
| 712 return -1; | 713 return -1; |
| 713 } | 714 } |
| 714 | 715 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 804 // Like Move, this function is not transactional, so we just | 805 // Like Move, this function is not transactional, so we just |
| 805 // leave the copied bits behind if deleting from_path fails. | 806 // leave the copied bits behind if deleting from_path fails. |
| 806 // If to_path exists previously then we have already overwritten | 807 // If to_path exists previously then we have already overwritten |
| 807 // it by now, we don't get better off by deleting the new bits. | 808 // it by now, we don't get better off by deleting the new bits. |
| 808 } | 809 } |
| 809 return false; | 810 return false; |
| 810 } | 811 } |
| 811 | 812 |
| 812 } // namespace internal | 813 } // namespace internal |
| 813 } // namespace base | 814 } // namespace base |
| OLD | NEW |