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 |
| 35 // This GUID is associated with any 'don't ask me again' settings that the |
| 36 // user can select for different file types. |
| 37 // {2676A9A2-D919-4fee-9187-152100393AB2} |
| 38 const GUID kClientID = { 0x2676a9a2, 0xd919, 0x4fee, |
| 39 { 0x91, 0x87, 0x15, 0x21, 0x0, 0x39, 0x3a, 0xb2 } }; |
| 40 |
34 const DWORD kFileShareAll = | 41 const DWORD kFileShareAll = |
35 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; | 42 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; |
36 | 43 |
37 bool ShellCopy(const FilePath& from_path, | 44 bool ShellCopy(const FilePath& from_path, |
38 const FilePath& to_path, | 45 const FilePath& to_path, |
39 bool recursive) { | 46 bool recursive) { |
40 // WinXP SHFileOperation doesn't like trailing separators. | 47 // WinXP SHFileOperation doesn't like trailing separators. |
41 FilePath stripped_from = from_path.StripTrailingSeparators(); | 48 FilePath stripped_from = from_path.StripTrailingSeparators(); |
42 FilePath stripped_to = to_path.StripTrailingSeparators(); | 49 FilePath stripped_to = to_path.StripTrailingSeparators(); |
43 | 50 |
(...skipping 21 matching lines...) Expand all Loading... |
65 file_operation.pFrom = double_terminated_path_from; | 72 file_operation.pFrom = double_terminated_path_from; |
66 file_operation.pTo = double_terminated_path_to; | 73 file_operation.pTo = double_terminated_path_to; |
67 file_operation.fFlags = FOF_NOERRORUI | FOF_SILENT | FOF_NOCONFIRMATION | | 74 file_operation.fFlags = FOF_NOERRORUI | FOF_SILENT | FOF_NOCONFIRMATION | |
68 FOF_NOCONFIRMMKDIR; | 75 FOF_NOCONFIRMMKDIR; |
69 if (!recursive) | 76 if (!recursive) |
70 file_operation.fFlags |= FOF_NORECURSION | FOF_FILESONLY; | 77 file_operation.fFlags |= FOF_NORECURSION | FOF_FILESONLY; |
71 | 78 |
72 return (SHFileOperation(&file_operation) == 0); | 79 return (SHFileOperation(&file_operation) == 0); |
73 } | 80 } |
74 | 81 |
| 82 // Sets the Zone Identifier on the file to "Internet" (3). Returns true if the |
| 83 // function succeeds, false otherwise. A failure is expected on system where |
| 84 // the Zone Identifier is not supported, like a machine with a FAT32 filesystem. |
| 85 // This function does not invoke Windows Attachment Execution Services. |
| 86 // |
| 87 // |full_path| is the path to the downloaded file. |
| 88 bool SetInternetZoneIdentifierDirectly(const base::FilePath& full_path) { |
| 89 const DWORD kShare = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; |
| 90 std::wstring path = full_path.value() + L":Zone.Identifier"; |
| 91 HANDLE file = CreateFile(path.c_str(), GENERIC_WRITE, kShare, NULL, |
| 92 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
| 93 if (INVALID_HANDLE_VALUE == file) |
| 94 return false; |
| 95 |
| 96 static const char kIdentifier[] = "[ZoneTransfer]\r\nZoneId=3\r\n"; |
| 97 // Don't include trailing null in data written. |
| 98 static const DWORD kIdentifierSize = arraysize(kIdentifier) - 1; |
| 99 DWORD written = 0; |
| 100 BOOL result = WriteFile(file, kIdentifier, kIdentifierSize, &written, NULL); |
| 101 BOOL flush_result = FlushFileBuffers(file); |
| 102 CloseHandle(file); |
| 103 |
| 104 if (!result || !flush_result || written != kIdentifierSize) { |
| 105 NOTREACHED(); |
| 106 return false; |
| 107 } |
| 108 |
| 109 return true; |
| 110 } |
| 111 |
75 } // namespace | 112 } // namespace |
76 | 113 |
77 FilePath MakeAbsoluteFilePath(const FilePath& input) { | 114 FilePath MakeAbsoluteFilePath(const FilePath& input) { |
78 ThreadRestrictions::AssertIOAllowed(); | 115 ThreadRestrictions::AssertIOAllowed(); |
79 wchar_t file_path[MAX_PATH]; | 116 wchar_t file_path[MAX_PATH]; |
80 if (!_wfullpath(file_path, input.value().c_str(), MAX_PATH)) | 117 if (!_wfullpath(file_path, input.value().c_str(), MAX_PATH)) |
81 return FilePath(); | 118 return FilePath(); |
82 return FilePath(file_path); | 119 return FilePath(file_path); |
83 } | 120 } |
84 | 121 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 ThreadRestrictions::AssertIOAllowed(); | 177 ThreadRestrictions::AssertIOAllowed(); |
141 | 178 |
142 if (path.value().length() >= MAX_PATH) | 179 if (path.value().length() >= MAX_PATH) |
143 return false; | 180 return false; |
144 | 181 |
145 return MoveFileEx(path.value().c_str(), NULL, | 182 return MoveFileEx(path.value().c_str(), NULL, |
146 MOVEFILE_DELAY_UNTIL_REBOOT | | 183 MOVEFILE_DELAY_UNTIL_REBOOT | |
147 MOVEFILE_REPLACE_EXISTING) != FALSE; | 184 MOVEFILE_REPLACE_EXISTING) != FALSE; |
148 } | 185 } |
149 | 186 |
| 187 HRESULT AVScanFile(const FilePath& full_path, const std::string& source_url) { |
| 188 win::ScopedComPtr<IAttachmentExecute> attachment_services; |
| 189 HRESULT hr = attachment_services.CreateInstance(CLSID_AttachmentServices); |
| 190 |
| 191 if (FAILED(hr)) { |
| 192 // The thread must have COM initialized. |
| 193 DCHECK_NE(CO_E_NOTINITIALIZED, hr); |
| 194 |
| 195 // We don't have Attachment Execution Services, it must be a pre-XP.SP2 |
| 196 // Windows installation, or the thread does not have COM initialized. Try to |
| 197 // set the zone information directly. Failure is not considered an error. |
| 198 SetInternetZoneIdentifierDirectly(full_path); |
| 199 return hr; |
| 200 } |
| 201 |
| 202 hr = attachment_services->SetClientGuid(kClientID); |
| 203 if (FAILED(hr)) |
| 204 return hr; |
| 205 |
| 206 hr = attachment_services->SetLocalPath(full_path.value().c_str()); |
| 207 if (FAILED(hr)) |
| 208 return hr; |
| 209 |
| 210 if (!source_url.empty()) { |
| 211 hr = attachment_services->SetSource(UTF8ToWide(source_url).c_str()); |
| 212 if (FAILED(hr)) |
| 213 return hr; |
| 214 } |
| 215 |
| 216 // A failure in the Save() call below could result in the downloaded file |
| 217 // being deleted. |
| 218 return attachment_services->Save(); |
| 219 } |
| 220 |
150 bool ReplaceFile(const FilePath& from_path, | 221 bool ReplaceFile(const FilePath& from_path, |
151 const FilePath& to_path, | 222 const FilePath& to_path, |
152 PlatformFileError* error) { | 223 PlatformFileError* error) { |
153 ThreadRestrictions::AssertIOAllowed(); | 224 ThreadRestrictions::AssertIOAllowed(); |
154 // Try a simple move first. It will only succeed when |to_path| doesn't | 225 // Try a simple move first. It will only succeed when |to_path| doesn't |
155 // already exist. | 226 // already exist. |
156 if (::MoveFile(from_path.value().c_str(), to_path.value().c_str())) | 227 if (::MoveFile(from_path.value().c_str(), to_path.value().c_str())) |
157 return true; | 228 return true; |
158 // Try the full-blown replace if the move fails, as ReplaceFile will only | 229 // 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 | 230 // 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 | 821 // Like Move, this function is not transactional, so we just |
751 // leave the copied bits behind if deleting from_path fails. | 822 // leave the copied bits behind if deleting from_path fails. |
752 // If to_path exists previously then we have already overwritten | 823 // 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. | 824 // it by now, we don't get better off by deleting the new bits. |
754 } | 825 } |
755 return false; | 826 return false; |
756 } | 827 } |
757 | 828 |
758 } // namespace internal | 829 } // namespace internal |
759 } // namespace base | 830 } // namespace base |
OLD | NEW |