| 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> |
| 11 #include <time.h> | 11 #include <time.h> |
| 12 | 12 |
| 13 #include <algorithm> | 13 #include <algorithm> |
| 14 #include <limits> | 14 #include <limits> |
| 15 #include <string> | 15 #include <string> |
| 16 | 16 |
| 17 #include "base/files/file_path.h" | 17 #include "base/files/file_path.h" |
| 18 #include "base/logging.h" | 18 #include "base/logging.h" |
| 19 #include "base/metrics/histogram.h" | 19 #include "base/metrics/histogram.h" |
| 20 #include "base/process/process_handle.h" | 20 #include "base/process/process_handle.h" |
| 21 #include "base/rand_util.h" | 21 #include "base/rand_util.h" |
| 22 #include "base/strings/string_number_conversions.h" | 22 #include "base/strings/string_number_conversions.h" |
| 23 #include "base/strings/string_util.h" | 23 #include "base/strings/string_util.h" |
| 24 #include "base/strings/utf_string_conversions.h" | 24 #include "base/strings/utf_string_conversions.h" |
| 25 #include "base/threading/thread_restrictions.h" | 25 #include "base/threading/thread_restrictions.h" |
| 26 #include "base/time/time.h" | 26 #include "base/time/time.h" |
| 27 #include "base/win/scoped_comptr.h" |
| 27 #include "base/win/scoped_handle.h" | 28 #include "base/win/scoped_handle.h" |
| 28 #include "base/win/windows_version.h" | 29 #include "base/win/windows_version.h" |
| 29 | 30 |
| 30 namespace base { | 31 namespace base { |
| 31 | 32 |
| 32 namespace { | 33 namespace { |
| 33 | 34 |
| 34 const DWORD kFileShareAll = | 35 const DWORD kFileShareAll = |
| 35 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; | 36 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; |
| 36 | 37 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 65 file_operation.pFrom = double_terminated_path_from; | 66 file_operation.pFrom = double_terminated_path_from; |
| 66 file_operation.pTo = double_terminated_path_to; | 67 file_operation.pTo = double_terminated_path_to; |
| 67 file_operation.fFlags = FOF_NOERRORUI | FOF_SILENT | FOF_NOCONFIRMATION | | 68 file_operation.fFlags = FOF_NOERRORUI | FOF_SILENT | FOF_NOCONFIRMATION | |
| 68 FOF_NOCONFIRMMKDIR; | 69 FOF_NOCONFIRMMKDIR; |
| 69 if (!recursive) | 70 if (!recursive) |
| 70 file_operation.fFlags |= FOF_NORECURSION | FOF_FILESONLY; | 71 file_operation.fFlags |= FOF_NORECURSION | FOF_FILESONLY; |
| 71 | 72 |
| 72 return (SHFileOperation(&file_operation) == 0); | 73 return (SHFileOperation(&file_operation) == 0); |
| 73 } | 74 } |
| 74 | 75 |
| 76 // Sets the Zone Identifier on the file to "Internet" (3). Returns true if the |
| 77 // function succeeds, false otherwise. A failure is expected on system where |
| 78 // the Zone Identifier is not supported, like a machine with a FAT32 filesystem. |
| 79 // This function does not invoke Windows Attachment Execution Services. |
| 80 // |
| 81 // |full_path| is the path to the downloaded file. |
| 82 bool SetInternetZoneIdentifierDirectly(const FilePath& full_path) { |
| 83 const DWORD kShare = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; |
| 84 std::wstring path = full_path.value() + L":Zone.Identifier"; |
| 85 HANDLE file = CreateFile(path.c_str(), GENERIC_WRITE, kShare, NULL, |
| 86 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
| 87 if (INVALID_HANDLE_VALUE == file) |
| 88 return false; |
| 89 |
| 90 static const char kIdentifier[] = "[ZoneTransfer]\r\nZoneId=3\r\n"; |
| 91 // Don't include trailing null in data written. |
| 92 static const DWORD kIdentifierSize = arraysize(kIdentifier) - 1; |
| 93 DWORD written = 0; |
| 94 BOOL result = WriteFile(file, kIdentifier, kIdentifierSize, &written, NULL); |
| 95 BOOL flush_result = FlushFileBuffers(file); |
| 96 CloseHandle(file); |
| 97 |
| 98 if (!result || !flush_result || written != kIdentifierSize) { |
| 99 NOTREACHED(); |
| 100 return false; |
| 101 } |
| 102 |
| 103 return true; |
| 104 } |
| 105 |
| 75 } // namespace | 106 } // namespace |
| 76 | 107 |
| 77 FilePath MakeAbsoluteFilePath(const FilePath& input) { | 108 FilePath MakeAbsoluteFilePath(const FilePath& input) { |
| 78 ThreadRestrictions::AssertIOAllowed(); | 109 ThreadRestrictions::AssertIOAllowed(); |
| 79 wchar_t file_path[MAX_PATH]; | 110 wchar_t file_path[MAX_PATH]; |
| 80 if (!_wfullpath(file_path, input.value().c_str(), MAX_PATH)) | 111 if (!_wfullpath(file_path, input.value().c_str(), MAX_PATH)) |
| 81 return FilePath(); | 112 return FilePath(); |
| 82 return FilePath(file_path); | 113 return FilePath(file_path); |
| 83 } | 114 } |
| 84 | 115 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 ThreadRestrictions::AssertIOAllowed(); | 171 ThreadRestrictions::AssertIOAllowed(); |
| 141 | 172 |
| 142 if (path.value().length() >= MAX_PATH) | 173 if (path.value().length() >= MAX_PATH) |
| 143 return false; | 174 return false; |
| 144 | 175 |
| 145 return MoveFileEx(path.value().c_str(), NULL, | 176 return MoveFileEx(path.value().c_str(), NULL, |
| 146 MOVEFILE_DELAY_UNTIL_REBOOT | | 177 MOVEFILE_DELAY_UNTIL_REBOOT | |
| 147 MOVEFILE_REPLACE_EXISTING) != FALSE; | 178 MOVEFILE_REPLACE_EXISTING) != FALSE; |
| 148 } | 179 } |
| 149 | 180 |
| 181 HRESULT AVScanFile(const FilePath& full_path, |
| 182 const std::string& source_url, |
| 183 const GUID& client_guid) { |
| 184 win::ScopedComPtr<IAttachmentExecute> attachment_services; |
| 185 HRESULT hr = attachment_services.CreateInstance(CLSID_AttachmentServices); |
| 186 |
| 187 if (FAILED(hr)) { |
| 188 // The thread must have COM initialized. |
| 189 DCHECK_NE(CO_E_NOTINITIALIZED, hr); |
| 190 |
| 191 // We don't have Attachment Execution Services, it must be a pre-XP.SP2 |
| 192 // Windows installation, or the thread does not have COM initialized. Try to |
| 193 // set the zone information directly. Failure is not considered an error. |
| 194 SetInternetZoneIdentifierDirectly(full_path); |
| 195 return hr; |
| 196 } |
| 197 |
| 198 if (client_guid != GUID_NULL) { |
| 199 hr = attachment_services->SetClientGuid(client_guid); |
| 200 if (FAILED(hr)) |
| 201 return hr; |
| 202 } |
| 203 |
| 204 hr = attachment_services->SetLocalPath(full_path.value().c_str()); |
| 205 if (FAILED(hr)) |
| 206 return hr; |
| 207 |
| 208 if (!source_url.empty()) { |
| 209 hr = attachment_services->SetSource(UTF8ToWide(source_url).c_str()); |
| 210 if (FAILED(hr)) |
| 211 return hr; |
| 212 } |
| 213 |
| 214 // A failure in the Save() call below could result in the downloaded file |
| 215 // being deleted. |
| 216 return attachment_services->Save(); |
| 217 } |
| 218 |
| 150 bool ReplaceFile(const FilePath& from_path, | 219 bool ReplaceFile(const FilePath& from_path, |
| 151 const FilePath& to_path, | 220 const FilePath& to_path, |
| 152 PlatformFileError* error) { | 221 PlatformFileError* error) { |
| 153 ThreadRestrictions::AssertIOAllowed(); | 222 ThreadRestrictions::AssertIOAllowed(); |
| 154 // Try a simple move first. It will only succeed when |to_path| doesn't | 223 // Try a simple move first. It will only succeed when |to_path| doesn't |
| 155 // already exist. | 224 // already exist. |
| 156 if (::MoveFile(from_path.value().c_str(), to_path.value().c_str())) | 225 if (::MoveFile(from_path.value().c_str(), to_path.value().c_str())) |
| 157 return true; | 226 return true; |
| 158 // Try the full-blown replace if the move fails, as ReplaceFile will only | 227 // Try the full-blown replace if the move fails, as ReplaceFile will only |
| 159 // succeed when |to_path| does exist. When writing to a network share, we may | 228 // succeed when |to_path| does exist. When writing to a network share, we may |
| (...skipping 590 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 750 // Like Move, this function is not transactional, so we just | 819 // Like Move, this function is not transactional, so we just |
| 751 // leave the copied bits behind if deleting from_path fails. | 820 // leave the copied bits behind if deleting from_path fails. |
| 752 // If to_path exists previously then we have already overwritten | 821 // If to_path exists previously then we have already overwritten |
| 753 // it by now, we don't get better off by deleting the new bits. | 822 // it by now, we don't get better off by deleting the new bits. |
| 754 } | 823 } |
| 755 return false; | 824 return false; |
| 756 } | 825 } |
| 757 | 826 |
| 758 } // namespace internal | 827 } // namespace internal |
| 759 } // namespace base | 828 } // namespace base |
| OLD | NEW |