Debian Codegraph @

Release/src/streams/fileio_win32.cpp

raw
1 /***
2 * Copyright (C) Microsoft. All rights reserved.
3 * Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
4 *
5 * =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
6 *
7 * Asynchronous I/O: stream buffer implementation details
8 *
9 * We're going to some lengths to avoid exporting C++ class member functions and implementation details across
10 * module boundaries, and the factoring requires that we keep the implementation details away from the main header
11 * files. The supporting functions, which are in this file, use C-like signatures to avoid as many issues as
12 * possible.
13 *
14 * For the latest on this and related APIs, please see: https://github.com/Microsoft/cpprestsdk
15 *
16 * =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
17 ****/
18 #include "stdafx.h"
19
20 #include "cpprest/details/fileio.h"
21
22 using namespace web;
23 using namespace utility;
24 using namespace concurrency;
25 using namespace utility::conversions;
26
27 namespace Concurrency
28 {
29 namespace streams
30 {
31 namespace details
32 {
33 /***
34 * ==++==
35 *
36 * Implementation details of the file stream buffer
37 *
38 * =-=-=-
39 ****/
40
41 /// <summary>
42 /// The public parts of the file information record contain only what is implementation-
43 /// independent. The actual allocated record is larger and has details that the implementation
44 /// require in order to function.
45 /// </summary>
46 struct _file_info_impl : _file_info
47 {
48 _file_info_impl(HANDLE handle, _In_ void* io_ctxt, std::ios_base::openmode mode, size_t buffer_size)
49 : _file_info(mode, buffer_size), m_io_context(io_ctxt), m_handle(handle)
50 {
51 }
52
53 /// <summary>
54 /// The Win32 file handle of the file
55 /// </summary>
56 HANDLE m_handle;
57
58 /// <summary>
59 /// A Win32 I/O context, used by the thread pool to scheduler work.
60 /// </summary>
61 void* m_io_context;
62 };
63
64 } // namespace details
65 } // namespace streams
66 } // namespace Concurrency
67
68 using namespace streams::details;
69
70 /// <summary>
71 /// Our extended OVERLAPPED record.
72 /// </summary>
73 /// <remarks>
74 /// The standard OVERLAPPED structure doesn't have any fields for application-specific
75 /// data, so we must extend it.
76 /// </remarks>
77 struct EXTENDED_OVERLAPPED : OVERLAPPED
78 {
79 EXTENDED_OVERLAPPED(LPOVERLAPPED_COMPLETION_ROUTINE func, streams::details::_filestream_callback* cb)
80 : callback(cb), func(func)
81 {
82 ZeroMemory(this, sizeof(OVERLAPPED));
83 }
84
85 streams::details::_filestream_callback* callback;
86 LPOVERLAPPED_COMPLETION_ROUTINE func;
87 };
88
89 #if _WIN32_WINNT < _WIN32_WINNT_VISTA
90 void CALLBACK IoCompletionCallback(DWORD dwErrorCode, DWORD dwNumberOfBytesTransfered, LPOVERLAPPED pOverlapped)
91 {
92 EXTENDED_OVERLAPPED* pExtOverlapped = static_cast<EXTENDED_OVERLAPPED*>(pOverlapped);
93
94 ////If dwErrorCode is 0xc0000011, it means STATUS_END_OF_FILE.
95 ////Map this error code to system error code:ERROR_HANDLE_EOF
96 if (dwErrorCode == 0xc0000011) dwErrorCode = ERROR_HANDLE_EOF;
97 pExtOverlapped->func(dwErrorCode, dwNumberOfBytesTransfered, pOverlapped);
98 delete pOverlapped;
99 }
100 #else
101 void CALLBACK IoCompletionCallback(PTP_CALLBACK_INSTANCE instance,
102 PVOID ctxt,
103 PVOID pOverlapped,
104 ULONG result,
105 ULONG_PTR numberOfBytesTransferred,
106 PTP_IO io)
107 {
108 (void)io;
109 (void)ctxt;
110 (void)instance;
111
112 EXTENDED_OVERLAPPED* pExtOverlapped = static_cast<EXTENDED_OVERLAPPED*>(pOverlapped);
113 pExtOverlapped->func(result, static_cast<DWORD>(numberOfBytesTransferred), static_cast<LPOVERLAPPED>(pOverlapped));
114 delete pExtOverlapped;
115 }
116 #endif
117
118 /// <summary>
119 /// Translate from C++ STL file open modes to Win32 flags.
120 /// </summary>
121 /// <param name="mode">The C++ file open mode</param>
122 /// <param name="prot">The C++ file open protection</param>
123 /// <param name="dwDesiredAccess">A pointer to a DWORD that will hold the desired access flags</param>
124 /// <param name="dwCreationDisposition">A pointer to a DWORD that will hold the creation disposition</param>
125 /// <param name="dwShareMode">A pointer to a DWORD that will hold the share mode</param>
126 void _get_create_flags(
127 std::ios_base::openmode mode, int prot, DWORD& dwDesiredAccess, DWORD& dwCreationDisposition, DWORD& dwShareMode)
128 {
129 dwDesiredAccess = 0x0;
130 if (mode & std::ios_base::in) dwDesiredAccess |= GENERIC_READ;
131 if (mode & std::ios_base::out) dwDesiredAccess |= GENERIC_WRITE;
132
133 if (mode & std::ios_base::in)
134 {
135 if (mode & std::ios_base::out)
136 dwCreationDisposition = OPEN_ALWAYS;
137 else
138 dwCreationDisposition = OPEN_EXISTING;
139 }
140 else if (mode & std::ios_base::trunc)
141 {
142 dwCreationDisposition = CREATE_ALWAYS;
143 }
144 else
145 {
146 dwCreationDisposition = OPEN_ALWAYS;
147 }
148
149 // C++ specifies what permissions to deny, Windows which permissions to give,
150 dwShareMode = 0x3;
151 switch (prot)
152 {
153 case _SH_DENYRW: dwShareMode = 0x0; break;
154 case _SH_DENYWR: dwShareMode = 0x1; break;
155 case _SH_DENYRD: dwShareMode = 0x2; break;
156 }
157 }
158
159 /// <summary>
160 /// Perform post-CreateFile processing.
161 /// </summary>
162 /// <param name="fh">The Win32 file handle</param>
163 /// <param name="callback">The callback interface pointer</param>
164 /// <param name="mode">The C++ file open mode</param>
165 void _finish_create(HANDLE fh, _In_ _filestream_callback* callback, std::ios_base::openmode mode, int prot)
166 {
167 if (fh == INVALID_HANDLE_VALUE)
168 {
169 callback->on_error(std::make_exception_ptr(utility::details::create_system_error(GetLastError())));
170 return;
171 }
172
173 void* io_ctxt = nullptr;
174 #if _WIN32_WINNT < _WIN32_WINNT_VISTA
175 if (!BindIoCompletionCallback(fh, IoCompletionCallback, 0))
176 {
177 callback->on_error(std::make_exception_ptr(utility::details::create_system_error(GetLastError())));
178 return;
179 }
180 #else
181 io_ctxt = CreateThreadpoolIo(fh, IoCompletionCallback, nullptr, nullptr);
182 if (io_ctxt == nullptr)
183 {
184 callback->on_error(std::make_exception_ptr(utility::details::create_system_error(GetLastError())));
185 return;
186 }
187
188 if (!SetFileCompletionNotificationModes(fh, FILE_SKIP_COMPLETION_PORT_ON_SUCCESS))
189 {
190 CloseThreadpoolIo(static_cast<PTP_IO>(io_ctxt));
191 callback->on_error(std::make_exception_ptr(utility::details::create_system_error(GetLastError())));
192 return;
193 }
194 #endif
195
196 // Buffer reads internally if and only if we're just reading (not also writing) and
197 // if the file is opened exclusively. If either is false, we're better off just
198 // letting the OS do its buffering, even if it means that prompt reads won't
199 // happen.
200 bool buffer = (mode == std::ios_base::in) && (prot == _SH_DENYRW);
201
202 auto info = new _file_info_impl(fh, io_ctxt, mode, buffer ? 512 : 0);
203
204 if (mode & std::ios_base::app || mode & std::ios_base::ate)
205 {
206 info->m_wrpos = static_cast<size_t>(-1); // Start at the end of the file.
207 }
208
209 callback->on_opened(info);
210 }
211
212 /// <summary>
213 /// Open a file and create a streambuf instance to represent it.
214 /// </summary>
215 /// <param name="callback">A pointer to the callback interface to invoke when the file has been opened.</param>
216 /// <param name="filename">The name of the file to open</param>
217 /// <param name="mode">A creation mode for the stream buffer</param>
218 /// <param name="prot">A file protection mode to use for the file stream</param>
219 /// <returns><c>true</c> if the opening operation could be initiated, <c>false</c> otherwise.</returns>
220 /// <remarks>
221 /// True does not signal that the file will eventually be successfully opened, just that the process was started.
222 /// </remarks>
223 bool __cdecl _open_fsb_str(_In_ _filestream_callback* callback,
224 const utility::char_t* filename,
225 std::ios_base::openmode mode,
226 int prot)
227 {
228 _ASSERTE(callback != nullptr);
229 _ASSERTE(filename != nullptr);
230
231 std::wstring name(filename);
232
233 pplx::create_task([=]() {
234 DWORD dwDesiredAccess, dwCreationDisposition, dwShareMode;
235 _get_create_flags(mode, prot, dwDesiredAccess, dwCreationDisposition, dwShareMode);
236
237 HANDLE fh = ::CreateFileW(
238 name.c_str(), dwDesiredAccess, dwShareMode, nullptr, dwCreationDisposition, FILE_FLAG_OVERLAPPED, 0);
239
240 _finish_create(fh, callback, mode, prot);
241 });
242
243 return true;
244 }
245
246 /// <summary>
247 /// Close a file stream buffer.
248 /// </summary>
249 /// <param name="info">The file info record of the file</param>
250 /// <param name="callback">A pointer to the callback interface to invoke when the file has been opened.</param>
251 /// <returns><c>true</c> if the closing operation could be initiated, <c>false</c> otherwise.</returns>
252 /// <remarks>
253 /// True does not signal that the file will eventually be successfully closed, just that the process was started.
254 /// </remarks>
255 bool __cdecl _close_fsb_nolock(_In_ _file_info** info, _In_ streams::details::_filestream_callback* callback)
256 {
257 _ASSERTE(callback != nullptr);
258 _ASSERTE(info != nullptr);
259 _ASSERTE(*info != nullptr);
260
261 _file_info_impl* fInfo = static_cast<_file_info_impl*>(*info);
262
263 if (fInfo->m_handle == INVALID_HANDLE_VALUE) return false;
264
265 // Since closing a file may involve waiting for outstanding writes which can take some time
266 // if the file is on a network share, the close action is done in a separate task, as
267 // CloseHandle doesn't have I/O completion events.
268 pplx::create_task([=]() {
269 bool result = false;
270
271 {
272 pplx::extensibility::scoped_recursive_lock_t lck(fInfo->m_lock);
273
274 if (fInfo->m_handle != INVALID_HANDLE_VALUE)
275 {
276 #if _WIN32_WINNT >= _WIN32_WINNT_VISTA
277 CloseThreadpoolIo(static_cast<PTP_IO>(fInfo->m_io_context));
278 #endif // _WIN32_WINNT >= _WIN32_WINNT_VISTA
279
280 result = CloseHandle(fInfo->m_handle) != FALSE;
281 }
282
283 delete fInfo->m_buffer;
284 }
285
286 delete fInfo;
287
288 if (result)
289 callback->on_closed();
290 else
291 callback->on_error(std::make_exception_ptr(utility::details::create_system_error(GetLastError())));
292 });
293
294 *info = nullptr;
295
296 return true;
297 }
298
299 bool __cdecl _close_fsb(_In_ _file_info** info, _In_ streams::details::_filestream_callback* callback)
300 {
301 _ASSERTE(callback != nullptr);
302 _ASSERTE(info != nullptr);
303 _ASSERTE(*info != nullptr);
304
305 return _close_fsb_nolock(info, callback);
306 }
307
308 /// <summary>
309 /// The completion routine used when a write request finishes.
310 /// </summary>
311 /// <remarks>
312 /// The signature is the standard IO completion signature, documented on MSDN
313 /// </remarks>
314 template<typename InfoType>
315 VOID CALLBACK _WriteFileCompletionRoutine(DWORD dwErrorCode, DWORD dwNumberOfBytesTransfered, LPOVERLAPPED lpOverlapped)
316 {
317 EXTENDED_OVERLAPPED* pOverlapped = static_cast<EXTENDED_OVERLAPPED*>(lpOverlapped);
318
319 if (dwErrorCode != ERROR_SUCCESS && dwErrorCode != ERROR_HANDLE_EOF)
320 {
321 pOverlapped->callback->on_error(std::make_exception_ptr(utility::details::create_system_error(dwErrorCode)));
322 }
323 else
324 {
325 pOverlapped->callback->on_completed(static_cast<size_t>(dwNumberOfBytesTransfered));
326 }
327 }
328
329 /// <summary>
330 /// The completion routine used when a read request finishes.
331 /// </summary>
332 /// <remarks>
333 /// The signature is the standard IO completion signature, documented on MSDN
334 /// </remarks>
335 template<typename InfoType>
336 VOID CALLBACK _ReadFileCompletionRoutine(DWORD dwErrorCode, DWORD dwNumberOfBytesTransfered, LPOVERLAPPED lpOverlapped)
337 {
338 EXTENDED_OVERLAPPED* pOverlapped = static_cast<EXTENDED_OVERLAPPED*>(lpOverlapped);
339
340 if (dwErrorCode != ERROR_SUCCESS && dwErrorCode != ERROR_HANDLE_EOF)
341 {
342 pOverlapped->callback->on_error(std::make_exception_ptr(utility::details::create_system_error(dwErrorCode)));
343 }
344 else
345 {
346 pOverlapped->callback->on_completed(static_cast<size_t>(dwNumberOfBytesTransfered));
347 }
348 }
349
350 /// <summary>
351 /// Initiate an asynchronous (overlapped) write to the file stream.
352 /// </summary>
353 /// <param name="info">The file info record of the file</param>
354 /// <param name="callback">A pointer to the callback interface to invoke when the write request is completed.</param>
355 /// <param name="ptr">A pointer to the data to write</param>
356 /// <param name="count">The size (in bytes) of the data</param>
357 /// <returns>0 if the write request is still outstanding, -1 if the request failed, otherwise the size of the data
358 /// written</returns>
359 size_t _write_file_async(_In_ streams::details::_file_info_impl* fInfo,
360 _In_ streams::details::_filestream_callback* callback,
361 const void* ptr,
362 size_t count,
363 size_t position)
364 {
365 auto pOverlapped = std::unique_ptr<EXTENDED_OVERLAPPED>(
366 new EXTENDED_OVERLAPPED(_WriteFileCompletionRoutine<streams::details::_file_info_impl>, callback));
367
368 if (position == static_cast<size_t>(-1))
369 {
370 pOverlapped->Offset = 0xFFFFFFFF;
371 pOverlapped->OffsetHigh = 0xFFFFFFFF;
372 }
373 else
374 {
375 pOverlapped->Offset = static_cast<DWORD>(position);
376 #ifdef _WIN64
377 pOverlapped->OffsetHigh = static_cast<DWORD>(position >> 32);
378 #else
379 pOverlapped->OffsetHigh = 0;
380 #endif
381 }
382
383 #if _WIN32_WINNT >= _WIN32_WINNT_VISTA
384 StartThreadpoolIo(static_cast<PTP_IO>(fInfo->m_io_context));
385
386 BOOL wrResult = WriteFile(fInfo->m_handle, ptr, static_cast<DWORD>(count), nullptr, pOverlapped.get());
387 DWORD error = GetLastError();
388
389 // WriteFile will return false when a) the operation failed, or b) when the request is still
390 // pending. The error code will tell us which is which.
391 if (wrResult == FALSE && error == ERROR_IO_PENDING)
392 {
393 // Overlapped is deleted in the threadpool callback.
394 pOverlapped.release();
395 return 0;
396 }
397
398 CancelThreadpoolIo(static_cast<PTP_IO>(fInfo->m_io_context));
399
400 size_t result = static_cast<size_t>(-1);
401
402 if (wrResult)
403 {
404 // If WriteFile returned true, it must be because the operation completed immediately.
405 // However, we didn't pass in an address for the number of bytes written, so
406 // we have to retrieve it using 'GetOverlappedResult,' which may, in turn, fail.
407 DWORD written = 0;
408 result = GetOverlappedResult(fInfo->m_handle, pOverlapped.get(), &written, FALSE) ? static_cast<size_t>(written)
409 : static_cast<size_t>(-1);
410 }
411
412 if (result == static_cast<size_t>(-1))
413 callback->on_error(std::make_exception_ptr(utility::details::create_system_error(error)));
414
415 return result;
416 #else
417 BOOL wrResult = WriteFile(fInfo->m_handle, ptr, (DWORD)count, nullptr, pOverlapped.get());
418 DWORD error = GetLastError();
419
420 // 1. If WriteFile returned true, it must be because the operation completed immediately.
421 // The xp threadpool immediately creates a workerthread to run "_WriteFileCompletionRoutine".
422 // If this function return value > 0, the condition "if (written == sizeof(_CharType))" in the filestreams.h
423 // "_getcImpl()" function will be satisfied. The main thread will delete the input "callback", while the threadpool
424 // workerthread is accessing this "callback"; there will be a race condition and AV error. We directly return 0 and
425 // leave all the completion callbacks working on the workerthread. We do not need to call GetOverlappedResult, the
426 // workerthread will call the "on_error()" if the WriteFaile failed. "req" is deleted in
427 // "_WriteFileCompletionRoutine, "pOverlapped" is deleted in io_scheduler::FileIOCompletionRoutine.
428 if (wrResult == TRUE)
429 {
430 pOverlapped.release();
431 return 0;
432 }
433
434 // 2. If WriteFile returned false and GetLastError is ERROR_IO_PENDING, return 0,
435 // The xp threadpool will create a workerthread to run "_WriteFileCompletionRoutine" after the operation
436 // completed.
437 if (wrResult == FALSE && error == ERROR_IO_PENDING)
438 {
439 // Overlapped is deleted in the threadpool callback.
440 pOverlapped.release();
441 return 0;
442 }
443
444 // 3. If ReadFile returned false and GetLastError is not ERROR_IO_PENDING, we must call "callback->on_error()".
445 // The threadpools will not start the workerthread.
446 callback->on_error(std::make_exception_ptr(utility::details::create_system_error(error)));
447
448 return static_cast<size_t>(-1);
449 #endif // _WIN32_WINNT >= _WIN32_WINNT_VISTA
450 }
451
452 /// <summary>
453 /// Initiate an asynchronous (overlapped) read from the file stream.
454 /// </summary>
455 /// <param name="info">The file info record of the file</param>
456 /// <param name="callback">A pointer to the callback interface to invoke when the write request is completed.</param>
457 /// <param name="ptr">A pointer to a buffer where the data should be placed</param>
458 /// <param name="count">The size (in bytes) of the buffer</param>
459 /// <param name="offset">The offset in the file to read from</param>
460 /// <returns>0 if the read request is still outstanding, -1 if the request failed, otherwise the size of the data read
461 /// into the buffer</returns>
462 size_t _read_file_async(_In_ streams::details::_file_info_impl* fInfo,
463 _In_ streams::details::_filestream_callback* callback,
464 _Out_writes_(count) void* ptr,
465 _In_ size_t count,
466 size_t offset)
467 {
468 auto pOverlapped = std::unique_ptr<EXTENDED_OVERLAPPED>(
469 new EXTENDED_OVERLAPPED(_ReadFileCompletionRoutine<streams::details::_file_info_impl>, callback));
470 pOverlapped->Offset = static_cast<DWORD>(offset);
471 #ifdef _WIN64
472 pOverlapped->OffsetHigh = static_cast<DWORD>(offset >> 32);
473 #else
474 pOverlapped->OffsetHigh = 0;
475 #endif
476
477 #if _WIN32_WINNT >= _WIN32_WINNT_VISTA
478 StartThreadpoolIo((PTP_IO)fInfo->m_io_context);
479
480 BOOL wrResult = ReadFile(fInfo->m_handle, ptr, static_cast<DWORD>(count), nullptr, pOverlapped.get());
481 DWORD error = GetLastError();
482
483 // ReadFile will return false when a) the operation failed, or b) when the request is still
484 // pending. The error code will tell us which is which.
485 if (wrResult == FALSE && error == ERROR_IO_PENDING)
486 {
487 // Overlapped is deleted in the threadpool callback.
488 pOverlapped.release();
489 return 0;
490 }
491
492 // We find ourselves here because there was a synchronous completion, either with an error or
493 // success. Either way, we don't need the thread pool I/O request here, or the request and
494 // overlapped structures.
495 CancelThreadpoolIo(static_cast<PTP_IO>(fInfo->m_io_context));
496
497 size_t result = static_cast<size_t>(-1);
498
499 if (wrResult)
500 {
501 // If ReadFile returned true, it must be because the operation completed immediately.
502 // However, we didn't pass in an address for the number of bytes written, so
503 // we have to retrieve it using 'GetOverlappedResult,' which may, in turn, fail.
504 DWORD read = 0;
505 result = GetOverlappedResult(fInfo->m_handle, pOverlapped.get(), &read, FALSE) ? static_cast<size_t>(read)
506 : static_cast<size_t>(-1);
507 }
508
509 if (wrResult == FALSE && error == ERROR_HANDLE_EOF)
510 {
511 callback->on_completed(0);
512 return 0;
513 }
514
515 if (result == static_cast<size_t>(-1))
516 callback->on_error(std::make_exception_ptr(utility::details::create_system_error(error)));
517
518 return result;
519 #else
520 BOOL wrResult = ReadFile(fInfo->m_handle, ptr, static_cast<DWORD>(count), nullptr, pOverlapped.get());
521 DWORD error = GetLastError();
522
523 // 1. If ReadFile returned true, it must be because the operation completed immediately.
524 // The xp threadpool immediately creates a workerthread to run "_WriteFileCompletionRoutine".
525 // If this function return value > 0, the condition "if ( ch == sizeof(_CharType) )" in the filestreams.h
526 // "_getcImpl()" function will be satisfied. The main thread will delete the input "callback", while the threadpool
527 // workerthread is accessing this "callback"; there will be a race condition and AV error. We can directly return 0
528 // and leave all the completion callbacks working on the workerthread. We do not need to call GetOverlappedResult,
529 // the workerthread will call the "on_error()" if the ReadFile failed. "req" is deleted in
530 // "_ReadFileCompletionRoutine, "pOverlapped" is deleted in io_scheduler::FileIOCompletionRoutine.
531 if (wrResult == TRUE)
532 {
533 pOverlapped.release();
534 return 0;
535 }
536
537 // 2. If ReadFile returned false and GetLastError is ERROR_IO_PENDING, return 0.
538 // The xp threadpool will create a workerthread to run "_WriteFileCompletionRoutine" after the operation
539 // completed.
540 if (wrResult == FALSE && error == ERROR_IO_PENDING)
541 {
542 // Overlapped is deleted in the threadpool callback.
543 pOverlapped.release();
544 return 0;
545 }
546
547 // 3. If ReadFile returned false and GetLastError is ERROR_HANDLE_EOF, we must call "callback->on_completed(0)".
548 // The threadpool will not start the workerthread.
549 if (wrResult == FALSE && error == ERROR_HANDLE_EOF)
550 {
551 callback->on_completed(0);
552 return 0;
553 }
554
555 // 4. If ReadFile returned false and GetLastError is not a valid error code, we must call "callback->on_error()".
556 // The threadpool will not start the workerthread.
557 callback->on_error(std::make_exception_ptr(utility::details::create_system_error(error)));
558
559 return static_cast<size_t>(-1);
560 #endif // _WIN32_WINNT >= _WIN32_WINNT_VISTA
561 }
562
563 template<typename Func>
564 class _filestream_callback_fill_buffer : public _filestream_callback
565 {
566 public:
567 _filestream_callback_fill_buffer(_In_ _file_info* info, const Func& func) : m_func(func), m_info(info) {}
568
569 virtual void on_completed(size_t result)
570 {
571 m_func(result);
572 delete this;
573 }
574
575 private:
576 _file_info* m_info;
577 Func m_func;
578 };
579
580 template<typename Func>
581 _filestream_callback_fill_buffer<Func>* create_callback(_In_ _file_info* info, const Func& func)
582 {
583 return new _filestream_callback_fill_buffer<Func>(info, func);
584 }
585
586 size_t _fill_buffer_fsb(_In_ _file_info_impl* fInfo,
587 _In_ _filestream_callback* callback,
588 size_t count,
589 size_t char_size)
590 {
591 msl::safeint3::SafeInt<size_t> safeCount = count;
592
593 if (fInfo->m_buffer == nullptr || safeCount > fInfo->m_bufsize)
594 {
595 if (fInfo->m_buffer != nullptr) delete fInfo->m_buffer;
596
597 fInfo->m_bufsize = safeCount.Max(fInfo->m_buffer_size);
598 fInfo->m_buffer = new char[fInfo->m_bufsize * char_size];
599 fInfo->m_bufoff = fInfo->m_rdpos;
600
601 auto cb = create_callback(fInfo, [=](size_t result) {
602 pplx::extensibility::scoped_recursive_lock_t lck(fInfo->m_lock);
603 fInfo->m_buffill = result / char_size;
604 callback->on_completed(result);
605 });
606
607 auto read = _read_file_async(fInfo,
608 cb,
609 reinterpret_cast<uint8_t*>(fInfo->m_buffer),
610 fInfo->m_bufsize * char_size,
611 fInfo->m_rdpos * char_size);
612
613 switch (read)
614 {
615 case 0:
616 // pending
617 return read;
618
619 case (-1):
620 // error
621 delete cb;
622 return read;
623
624 default:
625 // operation is complete. The pattern of returning synchronously
626 // has the expectation that we duplicate the callback code here...
627 // Do the expedient thing for now.
628 cb->on_completed(read);
629
630 // return pending
631 return 0;
632 };
633 }
634
635 // First, we need to understand how far into the buffer we have already read
636 // and how much remains.
637
638 size_t bufpos = fInfo->m_rdpos - fInfo->m_bufoff;
639 size_t bufrem = fInfo->m_buffill - bufpos;
640
641 // We have four different scenarios:
642 // 1. The read position is before the start of the buffer, in which case we will just reuse the buffer.
643 // 2. The read position is in the middle of the buffer, and we need to read some more.
644 // 3. The read position is beyond the end of the buffer. Do as in #1.
645 // 4. We have everything we need.
646
647 if ((fInfo->m_rdpos < fInfo->m_bufoff) || (fInfo->m_rdpos >= (fInfo->m_bufoff + fInfo->m_buffill)))
648 {
649 // Reuse the existing buffer.
650
651 fInfo->m_bufoff = fInfo->m_rdpos;
652
653 auto cb = create_callback(fInfo, [=](size_t result) {
654 pplx::extensibility::scoped_recursive_lock_t lck(fInfo->m_lock);
655 fInfo->m_buffill = result / char_size;
656 callback->on_completed(bufrem * char_size + result);
657 });
658
659 auto read = _read_file_async(fInfo,
660 cb,
661 reinterpret_cast<uint8_t*>(fInfo->m_buffer),
662 fInfo->m_bufsize * char_size,
663 fInfo->m_rdpos * char_size);
664
665 switch (read)
666 {
667 case 0:
668 // pending
669 return read;
670
671 case (-1):
672 // error
673 delete cb;
674 return read;
675
676 default:
677 // operation is complete. The pattern of returning synchronously
678 // has the expectation that we duplicate the callback code here...
679 // Do the expedient thing for now.
680 cb->on_completed(read);
681
682 // return pending
683 return 0;
684 };
685 }
686 else if (bufrem < count)
687 {
688 fInfo->m_bufsize = safeCount.Max(fInfo->m_buffer_size);
689
690 // Then, we allocate a new buffer.
691
692 char* newbuf = new char[fInfo->m_bufsize * char_size];
693
694 // Then, we copy the unread part to the new buffer and delete the old buffer
695
696 if (bufrem > 0) memcpy(newbuf, fInfo->m_buffer + bufpos * char_size, bufrem * char_size);
697
698 delete fInfo->m_buffer;
699 fInfo->m_buffer = newbuf;
700
701 // Then, we read the remainder of the count into the new buffer
702 fInfo->m_bufoff = fInfo->m_rdpos;
703
704 auto cb = create_callback(fInfo, [=](size_t result) {
705 pplx::extensibility::scoped_recursive_lock_t lck(fInfo->m_lock);
706 fInfo->m_buffill = result / char_size;
707 callback->on_completed(bufrem * char_size + result);
708 });
709
710 auto read = _read_file_async(fInfo,
711 cb,
712 reinterpret_cast<uint8_t*>(fInfo->m_buffer) + bufrem * char_size,
713 (fInfo->m_bufsize - bufrem) * char_size,
714 (fInfo->m_rdpos + bufrem) * char_size);
715
716 switch (read)
717 {
718 case 0:
719 // pending
720 return read;
721
722 case (-1):
723 // error
724 delete cb;
725 return read;
726
727 default:
728 // operation is complete. The pattern of returning synchronously
729 // has the expectation that we duplicate the callback code here...
730 // Do the expedient thing for now.
731 cb->on_completed(read);
732
733 // return pending
734 return 0;
735 };
736 }
737 else
738 {
739 // If we are here, it means that we didn't need to read, we already have enough data in the buffer
740 return count * char_size;
741 }
742 }
743
744 /// <summary>
745 /// Read data from a file stream into a buffer
746 /// </summary>
747 /// <param name="info">The file info record of the file</param>
748 /// <param name="callback">A pointer to the callback interface to invoke when the write request is completed.</param>
749 /// <param name="ptr">A pointer to a buffer where the data should be placed</param>
750 /// <param name="count">The size (in characters) of the buffer</param>
751 /// <returns>0 if the read request is still outstanding, -1 if the request failed, otherwise the size of the data read
752 /// into the buffer</returns>
753 size_t __cdecl _getn_fsb(_In_ streams::details::_file_info* info,
754 _In_ streams::details::_filestream_callback* callback,
755 _Out_writes_(count) void* ptr,
756 _In_ size_t count,
757 size_t char_size)
758 {
759 _ASSERTE(callback != nullptr);
760 _ASSERTE(info != nullptr);
761
762 _file_info_impl* fInfo = static_cast<_file_info_impl*>(info);
763
764 pplx::extensibility::scoped_recursive_lock_t lck(info->m_lock);
765
766 if (fInfo->m_handle == INVALID_HANDLE_VALUE)
767 {
768 callback->on_error(std::make_exception_ptr(utility::details::create_system_error(ERROR_INVALID_HANDLE)));
769 return (size_t)-1;
770 }
771
772 if (fInfo->m_buffer_size > 0)
773 {
774 auto cb = create_callback(fInfo, [=](size_t read) {
775 auto sz = count * char_size;
776 auto copy = (read < sz) ? read : sz;
777 auto bufoff = fInfo->m_rdpos - fInfo->m_bufoff;
778 memcpy(ptr, fInfo->m_buffer + bufoff * char_size, copy);
779 fInfo->m_atend = copy < sz;
780 callback->on_completed(copy);
781 });
782
783 size_t read = _fill_buffer_fsb(fInfo, cb, count, char_size);
784
785 if (read > 0)
786 {
787 auto sz = count * char_size;
788 auto copy = (read < sz) ? read : sz;
789 auto bufoff = fInfo->m_rdpos - fInfo->m_bufoff;
790 memcpy(ptr, fInfo->m_buffer + bufoff * char_size, copy);
791 fInfo->m_atend = copy < sz;
792 return copy;
793 }
794
795 return read;
796 }
797 else
798 {
799 return _read_file_async(fInfo, callback, ptr, count * char_size, fInfo->m_rdpos * char_size);
800 }
801 }
802
803 /// <summary>
804 /// Write data from a buffer into the file stream.
805 /// </summary>
806 /// <param name="info">The file info record of the file</param>
807 /// <param name="callback">A pointer to the callback interface to invoke when the write request is completed.</param>
808 /// <param name="ptr">A pointer to a buffer where the data should be placed</param>
809 /// <param name="count">The size (in characters) of the buffer</param>
810 /// <returns>0 if the read request is still outstanding, -1 if the request failed, otherwise the size of the data read
811 /// into the buffer</returns>
812 size_t __cdecl _putn_fsb(_In_ streams::details::_file_info* info,
813 _In_ streams::details::_filestream_callback* callback,
814 const void* ptr,
815 size_t count,
816 size_t char_size)
817 {
818 _ASSERTE(info != nullptr);
819
820 _file_info_impl* fInfo = static_cast<_file_info_impl*>(info);
821
822 pplx::extensibility::scoped_recursive_lock_t lck(fInfo->m_lock);
823
824 if (fInfo->m_handle == INVALID_HANDLE_VALUE)
825 {
826 callback->on_error(std::make_exception_ptr(utility::details::create_system_error(ERROR_INVALID_HANDLE)));
827 return static_cast<size_t>(-1);
828 }
829
830 // To preserve the async write order, we have to move the write head before read.
831 auto lastPos = fInfo->m_wrpos;
832 if (fInfo->m_wrpos != static_cast<size_t>(-1))
833 {
834 fInfo->m_wrpos += count;
835 lastPos *= char_size;
836 }
837 return _write_file_async(fInfo, callback, ptr, count * char_size, lastPos);
838 }
839
840 /// <summary>
841 /// Flush all buffered data to the underlying file.
842 /// </summary>
843 /// <param name="info">The file info record of the file</param>
844 /// <param name="callback">A pointer to the callback interface to invoke when the write request is completed.</param>
845 /// <returns><c>true</c> if the request was initiated</returns>
846 bool __cdecl _sync_fsb(_In_ streams::details::_file_info*, _In_ streams::details::_filestream_callback* callback)
847 {
848 _ASSERTE(callback != nullptr);
849
850 // Writes are not cached
851 callback->on_completed(0);
852
853 return true;
854 }
855
856 /// <summary>
857 /// Adjust the internal buffers and pointers when the application seeks to a new read location in the stream.
858 /// </summary>
859 /// <param name="info">The file info record of the file</param>
860 /// <param name="pos">The new position (offset from the start) in the file stream</param>
861 /// <returns>New file position or -1 if error</returns>
862 size_t __cdecl _seekrdpos_fsb(_In_ streams::details::_file_info* info, size_t pos, size_t)
863 {
864 _ASSERTE(info != nullptr);
865
866 _file_info_impl* fInfo = static_cast<_file_info_impl*>(info);
867
868 pplx::extensibility::scoped_recursive_lock_t lck(info->m_lock);
869
870 if (fInfo->m_handle == INVALID_HANDLE_VALUE) return static_cast<size_t>(-1);
871
872 if (pos < fInfo->m_bufoff || pos > (fInfo->m_bufoff + fInfo->m_buffill))
873 {
874 delete fInfo->m_buffer;
875 fInfo->m_buffer = nullptr;
876 fInfo->m_bufoff = fInfo->m_buffill = fInfo->m_bufsize = 0;
877 }
878
879 fInfo->m_rdpos = pos;
880 return fInfo->m_rdpos;
881 }
882
883 /// <summary>
884 /// Adjust the internal buffers and pointers when the application seeks to a new read location in the stream.
885 /// </summary>
886 /// <param name="info">The file info record of the file</param>
887 /// <param name="offset">The new position (offset from the end of the stream) in the file stream</param>
888 /// <param name="char_size">The size of the character type used for this stream</param>
889 /// <returns>New file position or -1 if error</returns>
890 size_t __cdecl _seekrdtoend_fsb(_In_ streams::details::_file_info* info, int64_t offset, size_t char_size)
891 {
892 _ASSERTE(info != nullptr);
893
894 _file_info_impl* fInfo = static_cast<_file_info_impl*>(info);
895
896 pplx::extensibility::scoped_recursive_lock_t lck(info->m_lock);
897
898 if (fInfo->m_handle == INVALID_HANDLE_VALUE) return static_cast<size_t>(-1);
899
900 if (fInfo->m_buffer != nullptr)
901 {
902 // Clear the internal buffer.
903 delete fInfo->m_buffer;
904 fInfo->m_buffer = nullptr;
905 fInfo->m_bufoff = fInfo->m_buffill = fInfo->m_bufsize = 0;
906 }
907
908 #ifdef _WIN64
909 LARGE_INTEGER filesize;
910 filesize.QuadPart = 0;
911
912 BOOL result = GetFileSizeEx(fInfo->m_handle, &filesize);
913 if (FALSE == result)
914 {
915 return static_cast<size_t>(-1);
916 }
917 else
918 {
919 fInfo->m_rdpos = static_cast<size_t>(filesize.QuadPart) / char_size;
920 }
921 #else
922 auto newpos = SetFilePointer(fInfo->m_handle, (LONG)(offset * char_size), nullptr, FILE_END);
923
924 if (newpos == INVALID_SET_FILE_POINTER) return static_cast<size_t>(-1);
925
926 fInfo->m_rdpos = static_cast<size_t>(newpos) / char_size;
927 #endif
928
929 return fInfo->m_rdpos;
930 }
931
932 utility::size64_t __cdecl _get_size(_In_ concurrency::streams::details::_file_info* info, size_t char_size)
933 {
934 _ASSERTE(info != nullptr);
935
936 _file_info_impl* fInfo = static_cast<_file_info_impl*>(info);
937
938 pplx::extensibility::scoped_recursive_lock_t lck(info->m_lock);
939
940 if (fInfo->m_handle == INVALID_HANDLE_VALUE) return (utility::size64_t)-1;
941
942 LARGE_INTEGER size;
943
944 if (GetFileSizeEx(fInfo->m_handle, &size))
945 return utility::size64_t(size.QuadPart / char_size);
946 else
947 return 0;
948 }
949
950 /// <summary>
951 /// Adjust the internal buffers and pointers when the application seeks to a new write location in the stream.
952 /// </summary>
953 /// <param name="info">The file info record of the file</param>
954 /// <param name="pos">The new position (offset from the start) in the file stream</param>
955 /// <returns>New file position or -1 if error</returns>
956 size_t __cdecl _seekwrpos_fsb(_In_ streams::details::_file_info* info, size_t pos, size_t)
957 {
958 _ASSERTE(info != nullptr);
959
960 _file_info_impl* fInfo = static_cast<_file_info_impl*>(info);
961
962 pplx::extensibility::scoped_recursive_lock_t lck(info->m_lock);
963
964 if (fInfo->m_handle == INVALID_HANDLE_VALUE) return static_cast<size_t>(-1);
965
966 fInfo->m_wrpos = pos;
967 return fInfo->m_wrpos;
968 }