OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <propvarutil.h> | 8 #include <propvarutil.h> |
9 #include <psapi.h> | 9 #include <psapi.h> |
10 #include <shellapi.h> | 10 #include <shellapi.h> |
(...skipping 26 matching lines...) Expand all Loading... |
37 | 37 |
38 // Helper for NormalizeFilePath(), defined below. | 38 // Helper for NormalizeFilePath(), defined below. |
39 bool DevicePathToDriveLetterPath(const FilePath& device_path, | 39 bool DevicePathToDriveLetterPath(const FilePath& device_path, |
40 FilePath* drive_letter_path) { | 40 FilePath* drive_letter_path) { |
41 base::ThreadRestrictions::AssertIOAllowed(); | 41 base::ThreadRestrictions::AssertIOAllowed(); |
42 | 42 |
43 // Get the mapping of drive letters to device paths. | 43 // Get the mapping of drive letters to device paths. |
44 const int kDriveMappingSize = 1024; | 44 const int kDriveMappingSize = 1024; |
45 wchar_t drive_mapping[kDriveMappingSize] = {'\0'}; | 45 wchar_t drive_mapping[kDriveMappingSize] = {'\0'}; |
46 if (!::GetLogicalDriveStrings(kDriveMappingSize - 1, drive_mapping)) { | 46 if (!::GetLogicalDriveStrings(kDriveMappingSize - 1, drive_mapping)) { |
47 LOG(ERROR) << "Failed to get drive mapping."; | 47 DLOG(ERROR) << "Failed to get drive mapping."; |
48 return false; | 48 return false; |
49 } | 49 } |
50 | 50 |
51 // The drive mapping is a sequence of null terminated strings. | 51 // The drive mapping is a sequence of null terminated strings. |
52 // The last string is empty. | 52 // The last string is empty. |
53 wchar_t* drive_map_ptr = drive_mapping; | 53 wchar_t* drive_map_ptr = drive_mapping; |
54 wchar_t device_name[MAX_PATH]; | 54 wchar_t device_name[MAX_PATH]; |
55 wchar_t drive[] = L" :"; | 55 wchar_t drive[] = L" :"; |
56 | 56 |
57 // For each string in the drive mapping, get the junction that links | 57 // For each string in the drive mapping, get the junction that links |
(...skipping 538 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
596 return OpenFile(*path, "wb+"); | 596 return OpenFile(*path, "wb+"); |
597 } | 597 } |
598 | 598 |
599 bool CreateTemporaryFileInDir(const FilePath& dir, | 599 bool CreateTemporaryFileInDir(const FilePath& dir, |
600 FilePath* temp_file) { | 600 FilePath* temp_file) { |
601 base::ThreadRestrictions::AssertIOAllowed(); | 601 base::ThreadRestrictions::AssertIOAllowed(); |
602 | 602 |
603 wchar_t temp_name[MAX_PATH + 1]; | 603 wchar_t temp_name[MAX_PATH + 1]; |
604 | 604 |
605 if (!GetTempFileName(dir.value().c_str(), L"", 0, temp_name)) { | 605 if (!GetTempFileName(dir.value().c_str(), L"", 0, temp_name)) { |
606 PLOG(WARNING) << "Failed to get temporary file name in " << dir.value(); | 606 DPLOG(WARNING) << "Failed to get temporary file name in " << dir.value(); |
607 return false; | 607 return false; |
608 } | 608 } |
609 | 609 |
610 DWORD path_len = GetLongPathName(temp_name, temp_name, MAX_PATH); | 610 DWORD path_len = GetLongPathName(temp_name, temp_name, MAX_PATH); |
611 if (path_len > MAX_PATH + 1 || path_len == 0) { | 611 if (path_len > MAX_PATH + 1 || path_len == 0) { |
612 PLOG(WARNING) << "Failed to get long path name for " << temp_name; | 612 DPLOG(WARNING) << "Failed to get long path name for " << temp_name; |
613 return false; | 613 return false; |
614 } | 614 } |
615 | 615 |
616 std::wstring temp_file_str; | 616 std::wstring temp_file_str; |
617 temp_file_str.assign(temp_name, path_len); | 617 temp_file_str.assign(temp_name, path_len); |
618 *temp_file = FilePath(temp_file_str); | 618 *temp_file = FilePath(temp_file_str); |
619 return true; | 619 return true; |
620 } | 620 } |
621 | 621 |
622 bool CreateTemporaryDirInDir(const FilePath& base_dir, | 622 bool CreateTemporaryDirInDir(const FilePath& base_dir, |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
660 | 660 |
661 // If the path exists, we've succeeded if it's a directory, failed otherwise. | 661 // If the path exists, we've succeeded if it's a directory, failed otherwise. |
662 const wchar_t* full_path_str = full_path.value().c_str(); | 662 const wchar_t* full_path_str = full_path.value().c_str(); |
663 DWORD fileattr = ::GetFileAttributes(full_path_str); | 663 DWORD fileattr = ::GetFileAttributes(full_path_str); |
664 if (fileattr != INVALID_FILE_ATTRIBUTES) { | 664 if (fileattr != INVALID_FILE_ATTRIBUTES) { |
665 if ((fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0) { | 665 if ((fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0) { |
666 DVLOG(1) << "CreateDirectory(" << full_path_str << "), " | 666 DVLOG(1) << "CreateDirectory(" << full_path_str << "), " |
667 << "directory already exists."; | 667 << "directory already exists."; |
668 return true; | 668 return true; |
669 } | 669 } |
670 LOG(WARNING) << "CreateDirectory(" << full_path_str << "), " | 670 DLOG(WARNING) << "CreateDirectory(" << full_path_str << "), " |
671 << "conflicts with existing file."; | 671 << "conflicts with existing file."; |
672 return false; | 672 return false; |
673 } | 673 } |
674 | 674 |
675 // Invariant: Path does not exist as file or directory. | 675 // Invariant: Path does not exist as file or directory. |
676 | 676 |
677 // Attempt to create the parent recursively. This will immediately return | 677 // Attempt to create the parent recursively. This will immediately return |
678 // true if it already exists, otherwise will create all required parent | 678 // true if it already exists, otherwise will create all required parent |
679 // directories starting with the highest-level missing parent. | 679 // directories starting with the highest-level missing parent. |
680 FilePath parent_path(full_path.DirName()); | 680 FilePath parent_path(full_path.DirName()); |
681 if (parent_path.value() == full_path.value()) { | 681 if (parent_path.value() == full_path.value()) { |
682 return false; | 682 return false; |
683 } | 683 } |
684 if (!CreateDirectory(parent_path)) { | 684 if (!CreateDirectory(parent_path)) { |
685 DLOG(WARNING) << "Failed to create one of the parent directories."; | 685 DLOG(WARNING) << "Failed to create one of the parent directories."; |
686 return false; | 686 return false; |
687 } | 687 } |
688 | 688 |
689 if (!::CreateDirectory(full_path_str, NULL)) { | 689 if (!::CreateDirectory(full_path_str, NULL)) { |
690 DWORD error_code = ::GetLastError(); | 690 DWORD error_code = ::GetLastError(); |
691 if (error_code == ERROR_ALREADY_EXISTS && DirectoryExists(full_path)) { | 691 if (error_code == ERROR_ALREADY_EXISTS && DirectoryExists(full_path)) { |
692 // This error code ERROR_ALREADY_EXISTS doesn't indicate whether we | 692 // This error code ERROR_ALREADY_EXISTS doesn't indicate whether we |
693 // were racing with someone creating the same directory, or a file | 693 // were racing with someone creating the same directory, or a file |
694 // with the same path. If DirectoryExists() returns true, we lost the | 694 // with the same path. If DirectoryExists() returns true, we lost the |
695 // race to create the same directory. | 695 // race to create the same directory. |
696 return true; | 696 return true; |
697 } else { | 697 } else { |
698 LOG(WARNING) << "Failed to create directory " << full_path_str | 698 DLOG(WARNING) << "Failed to create directory " << full_path_str |
699 << ", last error is " << error_code << "."; | 699 << ", last error is " << error_code << "."; |
700 return false; | 700 return false; |
701 } | 701 } |
702 } else { | 702 } else { |
703 return true; | 703 return true; |
704 } | 704 } |
705 } | 705 } |
706 | 706 |
707 // TODO(rkc): Work out if we want to handle NTFS junctions here or not, handle | 707 // TODO(rkc): Work out if we want to handle NTFS junctions here or not, handle |
708 // them if we do decide to. | 708 // them if we do decide to. |
709 bool IsLink(const FilePath& file_path) { | 709 bool IsLink(const FilePath& file_path) { |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
766 int WriteFile(const FilePath& filename, const char* data, int size) { | 766 int WriteFile(const FilePath& filename, const char* data, int size) { |
767 base::ThreadRestrictions::AssertIOAllowed(); | 767 base::ThreadRestrictions::AssertIOAllowed(); |
768 base::win::ScopedHandle file(CreateFile(filename.value().c_str(), | 768 base::win::ScopedHandle file(CreateFile(filename.value().c_str(), |
769 GENERIC_WRITE, | 769 GENERIC_WRITE, |
770 0, | 770 0, |
771 NULL, | 771 NULL, |
772 CREATE_ALWAYS, | 772 CREATE_ALWAYS, |
773 0, | 773 0, |
774 NULL)); | 774 NULL)); |
775 if (!file) { | 775 if (!file) { |
776 LOG(WARNING) << "CreateFile failed for path " << filename.value() | 776 DLOG(WARNING) << "CreateFile failed for path " << filename.value() |
777 << " error code=" << GetLastError(); | 777 << " error code=" << GetLastError(); |
778 return -1; | 778 return -1; |
779 } | 779 } |
780 | 780 |
781 DWORD written; | 781 DWORD written; |
782 BOOL result = ::WriteFile(file, data, size, &written, NULL); | 782 BOOL result = ::WriteFile(file, data, size, &written, NULL); |
783 if (result && static_cast<int>(written) == size) | 783 if (result && static_cast<int>(written) == size) |
784 return written; | 784 return written; |
785 | 785 |
786 if (!result) { | 786 if (!result) { |
787 // WriteFile failed. | 787 // WriteFile failed. |
788 LOG(WARNING) << "writing file " << filename.value() | 788 DLOG(WARNING) << "writing file " << filename.value() |
789 << " failed, error code=" << GetLastError(); | 789 << " failed, error code=" << GetLastError(); |
790 } else { | 790 } else { |
791 // Didn't write all the bytes. | 791 // Didn't write all the bytes. |
792 LOG(WARNING) << "wrote" << written << " bytes to " << | 792 DLOG(WARNING) << "wrote" << written << " bytes to " |
793 filename.value() << " expected " << size; | 793 << filename.value() << " expected " << size; |
794 } | 794 } |
795 return -1; | 795 return -1; |
796 } | 796 } |
797 | 797 |
798 // Gets the current working directory for the process. | 798 // Gets the current working directory for the process. |
799 bool GetCurrentDirectory(FilePath* dir) { | 799 bool GetCurrentDirectory(FilePath* dir) { |
800 base::ThreadRestrictions::AssertIOAllowed(); | 800 base::ThreadRestrictions::AssertIOAllowed(); |
801 | 801 |
802 wchar_t system_buffer[MAX_PATH]; | 802 wchar_t system_buffer[MAX_PATH]; |
803 system_buffer[0] = 0; | 803 system_buffer[0] = 0; |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
960 } | 960 } |
961 | 961 |
962 bool MemoryMappedFile::InitializeAsImageSection(const FilePath& file_name) { | 962 bool MemoryMappedFile::InitializeAsImageSection(const FilePath& file_name) { |
963 if (IsValid()) | 963 if (IsValid()) |
964 return false; | 964 return false; |
965 file_ = base::CreatePlatformFile( | 965 file_ = base::CreatePlatformFile( |
966 file_name, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, | 966 file_name, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, |
967 NULL, NULL); | 967 NULL, NULL); |
968 | 968 |
969 if (file_ == base::kInvalidPlatformFileValue) { | 969 if (file_ == base::kInvalidPlatformFileValue) { |
970 LOG(ERROR) << "Couldn't open " << file_name.value(); | 970 DLOG(ERROR) << "Couldn't open " << file_name.value(); |
971 return false; | 971 return false; |
972 } | 972 } |
973 | 973 |
974 if (!MapFileToMemoryInternalEx(SEC_IMAGE)) { | 974 if (!MapFileToMemoryInternalEx(SEC_IMAGE)) { |
975 CloseHandles(); | 975 CloseHandles(); |
976 return false; | 976 return false; |
977 } | 977 } |
978 | 978 |
979 return true; | 979 return true; |
980 } | 980 } |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1160 uint8 unused = *(touch + offset); | 1160 uint8 unused = *(touch + offset); |
1161 offset += step_size; | 1161 offset += step_size; |
1162 } | 1162 } |
1163 FreeLibrary(dll_module); | 1163 FreeLibrary(dll_module); |
1164 } | 1164 } |
1165 | 1165 |
1166 return true; | 1166 return true; |
1167 } | 1167 } |
1168 | 1168 |
1169 } // namespace file_util | 1169 } // namespace file_util |
OLD | NEW |