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 670 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
681 return true; | 681 return true; |
682 } | 682 } |
683 | 683 |
684 // Sets the current working directory for the process. | 684 // Sets the current working directory for the process. |
685 bool SetCurrentDirectory(const FilePath& directory) { | 685 bool SetCurrentDirectory(const FilePath& directory) { |
686 ThreadRestrictions::AssertIOAllowed(); | 686 ThreadRestrictions::AssertIOAllowed(); |
687 BOOL ret = ::SetCurrentDirectory(directory.value().c_str()); | 687 BOOL ret = ::SetCurrentDirectory(directory.value().c_str()); |
688 return ret != 0; | 688 return ret != 0; |
689 } | 689 } |
690 | 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 | |
706 int GetMaximumPathComponentLength(const FilePath& path) { | 691 int GetMaximumPathComponentLength(const FilePath& path) { |
707 base::ThreadRestrictions::AssertIOAllowed(); | 692 ThreadRestrictions::AssertIOAllowed(); |
708 | 693 |
709 wchar_t volume_path[MAX_PATH]; | 694 wchar_t volume_path[MAX_PATH]; |
710 if (!GetVolumePathNameW(path.NormalizePathSeparators().value().c_str(), | 695 if (!GetVolumePathNameW(path.NormalizePathSeparators().value().c_str(), |
711 volume_path, | 696 volume_path, |
712 arraysize(volume_path))) { | 697 arraysize(volume_path))) { |
713 return -1; | 698 return -1; |
714 } | 699 } |
715 | 700 |
716 DWORD max_length = 0; | 701 DWORD max_length = 0; |
717 if (!GetVolumeInformationW(volume_path, NULL, 0, NULL, &max_length, NULL, | 702 if (!GetVolumeInformationW(volume_path, NULL, 0, NULL, &max_length, NULL, |
718 NULL, 0)) { | 703 NULL, 0)) { |
719 return -1; | 704 return -1; |
720 } | 705 } |
721 | 706 |
722 // Length of |path| with path separator appended. | 707 // Length of |path| with path separator appended. |
723 size_t prefix = path.StripTrailingSeparators().value().size() + 1; | 708 size_t prefix = path.StripTrailingSeparators().value().size() + 1; |
724 // The whole path string must be shorter than MAX_PATH. That is, it must be | 709 // The whole path string must be shorter than MAX_PATH. That is, it must be |
725 // prefix + component_length < MAX_PATH (or equivalently, <= MAX_PATH - 1). | 710 // prefix + component_length < MAX_PATH (or equivalently, <= MAX_PATH - 1). |
726 int whole_path_limit = std::max(0, MAX_PATH - 1 - static_cast<int>(prefix)); | 711 int whole_path_limit = std::max(0, MAX_PATH - 1 - static_cast<int>(prefix)); |
727 return std::min(whole_path_limit, static_cast<int>(max_length)); | 712 return std::min(whole_path_limit, static_cast<int>(max_length)); |
728 } | 713 } |
729 | 714 |
730 } // namespace file_util | 715 // ----------------------------------------------------------------------------- |
731 | 716 |
732 namespace base { | |
733 namespace internal { | 717 namespace internal { |
734 | 718 |
735 bool MoveUnsafe(const FilePath& from_path, const FilePath& to_path) { | 719 bool MoveUnsafe(const FilePath& from_path, const FilePath& to_path) { |
736 ThreadRestrictions::AssertIOAllowed(); | 720 ThreadRestrictions::AssertIOAllowed(); |
737 | 721 |
738 // NOTE: I suspect we could support longer paths, but that would involve | 722 // NOTE: I suspect we could support longer paths, but that would involve |
739 // analyzing all our usage of files. | 723 // analyzing all our usage of files. |
740 if (from_path.value().length() >= MAX_PATH || | 724 if (from_path.value().length() >= MAX_PATH || |
741 to_path.value().length() >= MAX_PATH) { | 725 to_path.value().length() >= MAX_PATH) { |
742 return false; | 726 return false; |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
805 // Like Move, this function is not transactional, so we just | 789 // Like Move, this function is not transactional, so we just |
806 // leave the copied bits behind if deleting from_path fails. | 790 // leave the copied bits behind if deleting from_path fails. |
807 // If to_path exists previously then we have already overwritten | 791 // If to_path exists previously then we have already overwritten |
808 // it by now, we don't get better off by deleting the new bits. | 792 // it by now, we don't get better off by deleting the new bits. |
809 } | 793 } |
810 return false; | 794 return false; |
811 } | 795 } |
812 | 796 |
813 } // namespace internal | 797 } // namespace internal |
814 } // namespace base | 798 } // namespace base |
OLD | NEW |