Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Side by Side Diff: base/test/test_file_util_win.cc

Issue 593113004: Remove implicit HANDLE conversions from base. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/sync_socket_win.cc ('k') | base/win/event_trace_consumer_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/test/test_file_util.h" 5 #include "base/test/test_file_util.h"
6 6
7 #include <windows.h> 7 #include <windows.h>
8 #include <aclapi.h> 8 #include <aclapi.h>
9 #include <shlwapi.h> 9 #include <shlwapi.h>
10 10
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 PlatformThread::Sleep(kTimeout); 129 PlatformThread::Sleep(kTimeout);
130 } 130 }
131 return false; 131 return false;
132 } 132 }
133 133
134 bool EvictFileFromSystemCache(const FilePath& file) { 134 bool EvictFileFromSystemCache(const FilePath& file) {
135 // Request exclusive access to the file and overwrite it with no buffering. 135 // Request exclusive access to the file and overwrite it with no buffering.
136 base::win::ScopedHandle file_handle( 136 base::win::ScopedHandle file_handle(
137 CreateFile(file.value().c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, 137 CreateFile(file.value().c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL,
138 OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, NULL)); 138 OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, NULL));
139 if (!file_handle) 139 if (!file_handle.IsValid())
140 return false; 140 return false;
141 141
142 // Get some attributes to restore later. 142 // Get some attributes to restore later.
143 BY_HANDLE_FILE_INFORMATION bhi = {0}; 143 BY_HANDLE_FILE_INFORMATION bhi = {0};
144 CHECK(::GetFileInformationByHandle(file_handle, &bhi)); 144 CHECK(::GetFileInformationByHandle(file_handle.Get(), &bhi));
145 145
146 // Execute in chunks. It could be optimized. We want to do few of these since 146 // Execute in chunks. It could be optimized. We want to do few of these since
147 // these operations will be slow without the cache. 147 // these operations will be slow without the cache.
148 148
149 // Allocate a buffer for the reads and the writes. 149 // Allocate a buffer for the reads and the writes.
150 char* buffer = reinterpret_cast<char*>(VirtualAlloc(NULL, 150 char* buffer = reinterpret_cast<char*>(VirtualAlloc(NULL,
151 kOneMB, 151 kOneMB,
152 MEM_COMMIT | MEM_RESERVE, 152 MEM_COMMIT | MEM_RESERVE,
153 PAGE_READWRITE)); 153 PAGE_READWRITE));
154 154
155 // If the file size isn't a multiple of kOneMB, we'll need special 155 // If the file size isn't a multiple of kOneMB, we'll need special
156 // processing. 156 // processing.
157 bool file_is_aligned = true; 157 bool file_is_aligned = true;
158 int total_bytes = 0; 158 int total_bytes = 0;
159 DWORD bytes_read, bytes_written; 159 DWORD bytes_read, bytes_written;
160 for (;;) { 160 for (;;) {
161 bytes_read = 0; 161 bytes_read = 0;
162 ::ReadFile(file_handle, buffer, kOneMB, &bytes_read, NULL); 162 ::ReadFile(file_handle.Get(), buffer, kOneMB, &bytes_read, NULL);
163 if (bytes_read == 0) 163 if (bytes_read == 0)
164 break; 164 break;
165 165
166 if (bytes_read < kOneMB) { 166 if (bytes_read < kOneMB) {
167 // Zero out the remaining part of the buffer. 167 // Zero out the remaining part of the buffer.
168 // WriteFile will fail if we provide a buffer size that isn't a 168 // WriteFile will fail if we provide a buffer size that isn't a
169 // sector multiple, so we'll have to write the entire buffer with 169 // sector multiple, so we'll have to write the entire buffer with
170 // padded zeros and then use SetEndOfFile to truncate the file. 170 // padded zeros and then use SetEndOfFile to truncate the file.
171 ZeroMemory(buffer + bytes_read, kOneMB - bytes_read); 171 ZeroMemory(buffer + bytes_read, kOneMB - bytes_read);
172 file_is_aligned = false; 172 file_is_aligned = false;
173 } 173 }
174 174
175 // Move back to the position we just read from. 175 // Move back to the position we just read from.
176 // Note that SetFilePointer will also fail if total_bytes isn't sector 176 // Note that SetFilePointer will also fail if total_bytes isn't sector
177 // aligned, but that shouldn't happen here. 177 // aligned, but that shouldn't happen here.
178 DCHECK((total_bytes % kOneMB) == 0); 178 DCHECK((total_bytes % kOneMB) == 0);
179 SetFilePointer(file_handle, total_bytes, NULL, FILE_BEGIN); 179 SetFilePointer(file_handle.Get(), total_bytes, NULL, FILE_BEGIN);
180 if (!::WriteFile(file_handle, buffer, kOneMB, &bytes_written, NULL) || 180 if (!::WriteFile(file_handle.Get(), buffer, kOneMB, &bytes_written, NULL) ||
181 bytes_written != kOneMB) { 181 bytes_written != kOneMB) {
182 BOOL freed = VirtualFree(buffer, 0, MEM_RELEASE); 182 BOOL freed = VirtualFree(buffer, 0, MEM_RELEASE);
183 DCHECK(freed); 183 DCHECK(freed);
184 NOTREACHED(); 184 NOTREACHED();
185 return false; 185 return false;
186 } 186 }
187 187
188 total_bytes += bytes_read; 188 total_bytes += bytes_read;
189 189
190 // If this is false, then we just processed the last portion of the file. 190 // If this is false, then we just processed the last portion of the file.
191 if (!file_is_aligned) 191 if (!file_is_aligned)
192 break; 192 break;
193 } 193 }
194 194
195 BOOL freed = VirtualFree(buffer, 0, MEM_RELEASE); 195 BOOL freed = VirtualFree(buffer, 0, MEM_RELEASE);
196 DCHECK(freed); 196 DCHECK(freed);
197 197
198 if (!file_is_aligned) { 198 if (!file_is_aligned) {
199 // The size of the file isn't a multiple of 1 MB, so we'll have 199 // The size of the file isn't a multiple of 1 MB, so we'll have
200 // to open the file again, this time without the FILE_FLAG_NO_BUFFERING 200 // to open the file again, this time without the FILE_FLAG_NO_BUFFERING
201 // flag and use SetEndOfFile to mark EOF. 201 // flag and use SetEndOfFile to mark EOF.
202 file_handle.Set(NULL); 202 file_handle.Set(NULL);
203 file_handle.Set(CreateFile(file.value().c_str(), GENERIC_WRITE, 0, NULL, 203 file_handle.Set(CreateFile(file.value().c_str(), GENERIC_WRITE, 0, NULL,
204 OPEN_EXISTING, 0, NULL)); 204 OPEN_EXISTING, 0, NULL));
205 CHECK_NE(SetFilePointer(file_handle, total_bytes, NULL, FILE_BEGIN), 205 CHECK_NE(SetFilePointer(file_handle.Get(), total_bytes, NULL, FILE_BEGIN),
206 INVALID_SET_FILE_POINTER); 206 INVALID_SET_FILE_POINTER);
207 CHECK(::SetEndOfFile(file_handle)); 207 CHECK(::SetEndOfFile(file_handle.Get()));
208 } 208 }
209 209
210 // Restore the file attributes. 210 // Restore the file attributes.
211 CHECK(::SetFileTime(file_handle, &bhi.ftCreationTime, &bhi.ftLastAccessTime, 211 CHECK(::SetFileTime(file_handle.Get(), &bhi.ftCreationTime,
212 &bhi.ftLastWriteTime)); 212 &bhi.ftLastAccessTime, &bhi.ftLastWriteTime));
213 213
214 return true; 214 return true;
215 } 215 }
216 216
217 // Checks if the volume supports Alternate Data Streams. This is required for 217 // Checks if the volume supports Alternate Data Streams. This is required for
218 // the Zone Identifier implementation. 218 // the Zone Identifier implementation.
219 bool VolumeSupportsADS(const FilePath& path) { 219 bool VolumeSupportsADS(const FilePath& path) {
220 wchar_t drive[MAX_PATH] = {0}; 220 wchar_t drive[MAX_PATH] = {0};
221 wcscpy_s(drive, MAX_PATH, path.value().c_str()); 221 wcscpy_s(drive, MAX_PATH, path.value().c_str());
222 222
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 DCHECK(info_ != NULL); 273 DCHECK(info_ != NULL);
274 DCHECK_NE(0u, length_); 274 DCHECK_NE(0u, length_);
275 } 275 }
276 276
277 FilePermissionRestorer::~FilePermissionRestorer() { 277 FilePermissionRestorer::~FilePermissionRestorer() {
278 if (!RestorePermissionInfo(path_, info_, length_)) 278 if (!RestorePermissionInfo(path_, info_, length_))
279 NOTREACHED(); 279 NOTREACHED();
280 } 280 }
281 281
282 } // namespace base 282 } // namespace base
OLDNEW
« no previous file with comments | « base/sync_socket_win.cc ('k') | base/win/event_trace_consumer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698