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/files/file_enumerator.h" | |
18 #include "base/logging.h" | 19 #include "base/logging.h" |
19 #include "base/metrics/histogram.h" | 20 #include "base/metrics/histogram.h" |
20 #include "base/process/process_handle.h" | 21 #include "base/process/process_handle.h" |
21 #include "base/rand_util.h" | 22 #include "base/rand_util.h" |
22 #include "base/strings/string_number_conversions.h" | 23 #include "base/strings/string_number_conversions.h" |
23 #include "base/strings/string_util.h" | 24 #include "base/strings/string_util.h" |
24 #include "base/strings/utf_string_conversions.h" | 25 #include "base/strings/utf_string_conversions.h" |
25 #include "base/threading/thread_restrictions.h" | 26 #include "base/threading/thread_restrictions.h" |
26 #include "base/time/time.h" | 27 #include "base/time/time.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 |
38 // ShellCopy can copy directories, unlike CopyFileUnsafe. Beside that, they are | |
39 // mostly similar but both date backs to ~2008. | |
40 // This function is used exclusively for CopyDirectory(). | |
37 bool ShellCopy(const FilePath& from_path, | 41 bool ShellCopy(const FilePath& from_path, |
38 const FilePath& to_path, | 42 const FilePath& to_path, |
39 bool recursive) { | 43 bool recursive) { |
40 // WinXP SHFileOperation doesn't like trailing separators. | 44 // WinXP SHFileOperation doesn't like trailing separators. |
41 FilePath stripped_from = from_path.StripTrailingSeparators(); | 45 FilePath stripped_from = from_path.StripTrailingSeparators(); |
42 FilePath stripped_to = to_path.StripTrailingSeparators(); | 46 FilePath stripped_to = to_path.StripTrailingSeparators(); |
43 | 47 |
44 ThreadRestrictions::AssertIOAllowed(); | 48 ThreadRestrictions::AssertIOAllowed(); |
45 | 49 |
46 // NOTE: I suspect we could support longer paths, but that would involve | 50 // NOTE: I suspect we could support longer paths, but that would involve |
(...skipping 11 matching lines...) Expand all Loading... | |
58 #pragma warning(suppress:4996) // don't complain about wcscpy deprecation | 62 #pragma warning(suppress:4996) // don't complain about wcscpy deprecation |
59 wcscpy(double_terminated_path_from, stripped_from.value().c_str()); | 63 wcscpy(double_terminated_path_from, stripped_from.value().c_str()); |
60 #pragma warning(suppress:4996) // don't complain about wcscpy deprecation | 64 #pragma warning(suppress:4996) // don't complain about wcscpy deprecation |
61 wcscpy(double_terminated_path_to, stripped_to.value().c_str()); | 65 wcscpy(double_terminated_path_to, stripped_to.value().c_str()); |
62 | 66 |
63 SHFILEOPSTRUCT file_operation = {0}; | 67 SHFILEOPSTRUCT file_operation = {0}; |
64 file_operation.wFunc = FO_COPY; | 68 file_operation.wFunc = FO_COPY; |
65 file_operation.pFrom = double_terminated_path_from; | 69 file_operation.pFrom = double_terminated_path_from; |
66 file_operation.pTo = double_terminated_path_to; | 70 file_operation.pTo = double_terminated_path_to; |
67 file_operation.fFlags = FOF_NOERRORUI | FOF_SILENT | FOF_NOCONFIRMATION | | 71 file_operation.fFlags = FOF_NOERRORUI | FOF_SILENT | FOF_NOCONFIRMATION | |
68 FOF_NOCONFIRMMKDIR; | 72 FOF_NOCONFIRMMKDIR | FOF_NOCOPYSECURITYATTRIBS; |
69 if (!recursive) | 73 if (!recursive) |
70 file_operation.fFlags |= FOF_NORECURSION | FOF_FILESONLY; | 74 file_operation.fFlags |= FOF_NORECURSION | FOF_FILESONLY; |
71 | 75 |
72 return (SHFileOperation(&file_operation) == 0); | 76 if (SHFileOperation(&file_operation) != 0) { |
77 return false; | |
78 } | |
79 // Sadly at this point, all the read-only attributes have to be removed | |
80 // manually, even if FOF_NOCOPYSECURITYATTRIBS is set. | |
81 FileEnumerator f(to_path, true, FileEnumerator::FILES); | |
82 while (true) { | |
83 FilePath p = f.Next(); | |
84 if (p.empty()) { | |
85 break; | |
86 } | |
87 DWORD attrs = GetFileAttributes(p.value().c_str()); | |
88 if (attrs == INVALID_FILE_ATTRIBUTES) { | |
89 continue; | |
90 } | |
91 if (attrs & FILE_ATTRIBUTE_READONLY) { | |
92 SetFileAttributes(p.value().c_str(), attrs & ~FILE_ATTRIBUTE_READONLY); | |
93 } | |
94 } | |
95 return true; | |
Nico
2014/01/21 04:12:18
Hm, that's kinda unfortunate that this requires en
M-A Ruel
2014/01/21 15:41:36
Indeed. It's a bit obscene in fact. Another option
| |
73 } | 96 } |
74 | 97 |
75 } // namespace | 98 } // namespace |
76 | 99 |
77 FilePath MakeAbsoluteFilePath(const FilePath& input) { | 100 FilePath MakeAbsoluteFilePath(const FilePath& input) { |
78 ThreadRestrictions::AssertIOAllowed(); | 101 ThreadRestrictions::AssertIOAllowed(); |
79 wchar_t file_path[MAX_PATH]; | 102 wchar_t file_path[MAX_PATH]; |
80 if (!_wfullpath(file_path, input.value().c_str(), MAX_PATH)) | 103 if (!_wfullpath(file_path, input.value().c_str(), MAX_PATH)) |
81 return FilePath(); | 104 return FilePath(); |
82 return FilePath(file_path); | 105 return FilePath(file_path); |
(...skipping 675 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
758 // Like Move, this function is not transactional, so we just | 781 // Like Move, this function is not transactional, so we just |
759 // leave the copied bits behind if deleting from_path fails. | 782 // leave the copied bits behind if deleting from_path fails. |
760 // If to_path exists previously then we have already overwritten | 783 // If to_path exists previously then we have already overwritten |
761 // it by now, we don't get better off by deleting the new bits. | 784 // it by now, we don't get better off by deleting the new bits. |
762 } | 785 } |
763 return false; | 786 return false; |
764 } | 787 } |
765 | 788 |
766 } // namespace internal | 789 } // namespace internal |
767 } // namespace base | 790 } // namespace base |
OLD | NEW |