OLD | NEW |
1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 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/test_file_util.h" | 5 #include "base/test_file_util.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
| 11 #include "base/file_path.h" |
11 #include "base/file_util.h" | 12 #include "base/file_util.h" |
12 #include "base/scoped_handle.h" | 13 #include "base/scoped_handle.h" |
13 | 14 |
14 namespace file_util { | 15 namespace file_util { |
15 | 16 |
16 // We could use GetSystemInfo to get the page size, but this serves | 17 // We could use GetSystemInfo to get the page size, but this serves |
17 // our purpose fine since 4K is the page size on x86 as well as x64. | 18 // our purpose fine since 4K is the page size on x86 as well as x64. |
18 static const ptrdiff_t kPageSize = 4096; | 19 static const ptrdiff_t kPageSize = 4096; |
19 | 20 |
20 bool EvictFileFromSystemCache(const wchar_t* file) { | 21 bool EvictFileFromSystemCache(const FilePath& file) { |
21 // Request exclusive access to the file and overwrite it with no buffering. | 22 // Request exclusive access to the file and overwrite it with no buffering. |
22 ScopedHandle file_handle( | 23 ScopedHandle file_handle( |
23 CreateFile(file, GENERIC_READ | GENERIC_WRITE, 0, NULL, | 24 CreateFile(file.value().c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, |
24 OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, NULL)); | 25 OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, NULL)); |
25 if (!file_handle) | 26 if (!file_handle) |
26 return false; | 27 return false; |
27 | 28 |
28 // Get some attributes to restore later. | 29 // Get some attributes to restore later. |
29 BY_HANDLE_FILE_INFORMATION bhi = {0}; | 30 BY_HANDLE_FILE_INFORMATION bhi = {0}; |
30 CHECK(::GetFileInformationByHandle(file_handle, &bhi)); | 31 CHECK(::GetFileInformationByHandle(file_handle, &bhi)); |
31 | 32 |
32 // Execute in chunks. It could be optimized. We want to do few of these since | 33 // Execute in chunks. It could be optimized. We want to do few of these since |
33 // these operations will be slow without the cache. | 34 // these operations will be slow without the cache. |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 // If this is false, then we just processed the last portion of the file. | 79 // If this is false, then we just processed the last portion of the file. |
79 if (!file_is_page_aligned) | 80 if (!file_is_page_aligned) |
80 break; | 81 break; |
81 } | 82 } |
82 | 83 |
83 if (!file_is_page_aligned) { | 84 if (!file_is_page_aligned) { |
84 // The size of the file isn't a multiple of the page size, so we'll have | 85 // The size of the file isn't a multiple of the page size, so we'll have |
85 // to open the file again, this time without the FILE_FLAG_NO_BUFFERING | 86 // to open the file again, this time without the FILE_FLAG_NO_BUFFERING |
86 // flag and use SetEndOfFile to mark EOF. | 87 // flag and use SetEndOfFile to mark EOF. |
87 file_handle.Set(NULL); | 88 file_handle.Set(NULL); |
88 file_handle.Set(CreateFile(file, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, | 89 file_handle.Set(CreateFile(file.value().c_str(), GENERIC_WRITE, 0, NULL, |
89 0, NULL)); | 90 OPEN_EXISTING, 0, NULL)); |
90 CHECK(SetFilePointer(file_handle, total_bytes, NULL, FILE_BEGIN) != | 91 CHECK(SetFilePointer(file_handle, total_bytes, NULL, FILE_BEGIN) != |
91 INVALID_SET_FILE_POINTER); | 92 INVALID_SET_FILE_POINTER); |
92 CHECK(::SetEndOfFile(file_handle)); | 93 CHECK(::SetEndOfFile(file_handle)); |
93 } | 94 } |
94 | 95 |
95 // Restore the file attributes. | 96 // Restore the file attributes. |
96 CHECK(::SetFileTime(file_handle, &bhi.ftCreationTime, &bhi.ftLastAccessTime, | 97 CHECK(::SetFileTime(file_handle, &bhi.ftCreationTime, &bhi.ftLastAccessTime, |
97 &bhi.ftLastWriteTime)); | 98 &bhi.ftLastWriteTime)); |
98 | 99 |
99 return true; | 100 return true; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 // Copy the file. | 141 // Copy the file. |
141 if (!::CopyFile(cur_source_path.c_str(), cur_dest_path.c_str(), false)) { | 142 if (!::CopyFile(cur_source_path.c_str(), cur_dest_path.c_str(), false)) { |
142 FindClose(fh); | 143 FindClose(fh); |
143 return false; | 144 return false; |
144 } | 145 } |
145 | 146 |
146 // We don't check for errors from this function, often, we are copying | 147 // We don't check for errors from this function, often, we are copying |
147 // files that are in the repository, and they will have read-only set. | 148 // files that are in the repository, and they will have read-only set. |
148 // This will prevent us from evicting from the cache, but these don't | 149 // This will prevent us from evicting from the cache, but these don't |
149 // matter anyway. | 150 // matter anyway. |
150 EvictFileFromSystemCache(cur_dest_path.c_str()); | 151 EvictFileFromSystemCache(FilePath::FromWStringHack(cur_dest_path)); |
151 } | 152 } |
152 } while (FindNextFile(fh, &fd)); | 153 } while (FindNextFile(fh, &fd)); |
153 | 154 |
154 FindClose(fh); | 155 FindClose(fh); |
155 return true; | 156 return true; |
156 } | 157 } |
157 | 158 |
158 } // namespace file_util | 159 } // namespace file_util |
159 | 160 |
OLD | NEW |