1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
|
/* gwin32file-sync-stream.c - a simple IStream implementation
*
* Copyright 2020 Руслан Ижбулатов
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
/* A COM object that implements an IStream backed by a file HANDLE.
* Works just like `SHCreateStreamOnFileEx()`, but does not
* support locking, and doesn't force us to link to libshlwapi.
* Only supports synchronous access.
*/
#include "config.h"
#define COBJMACROS
#define INITGUID
#include <windows.h>
#include "gwin32file-sync-stream.h"
static HRESULT STDMETHODCALLTYPE _file_sync_stream_query_interface (IStream *self_ptr,
REFIID ref_interface_guid,
LPVOID *output_object_ptr);
static ULONG STDMETHODCALLTYPE _file_sync_stream_release (IStream *self_ptr);
static ULONG STDMETHODCALLTYPE _file_sync_stream_add_ref (IStream *self_ptr);
static HRESULT STDMETHODCALLTYPE _file_sync_stream_read (IStream *self_ptr,
void *output_data,
ULONG bytes_to_read,
ULONG *output_bytes_read);
static HRESULT STDMETHODCALLTYPE _file_sync_stream_write (IStream *self_ptr,
const void *data,
ULONG bytes_to_write,
ULONG *output_bytes_written);
static HRESULT STDMETHODCALLTYPE _file_sync_stream_clone (IStream *self_ptr,
IStream **output_clone_ptr);
static HRESULT STDMETHODCALLTYPE _file_sync_stream_commit (IStream *self_ptr,
DWORD commit_flags);
static HRESULT STDMETHODCALLTYPE _file_sync_stream_copy_to (IStream *self_ptr,
IStream *output_stream,
ULARGE_INTEGER bytes_to_copy,
ULARGE_INTEGER *output_bytes_read,
ULARGE_INTEGER *output_bytes_written);
static HRESULT STDMETHODCALLTYPE _file_sync_stream_lock_region (IStream *self_ptr,
ULARGE_INTEGER lock_offset,
ULARGE_INTEGER lock_bytes,
DWORD lock_type);
static HRESULT STDMETHODCALLTYPE _file_sync_stream_revert (IStream *self_ptr);
static HRESULT STDMETHODCALLTYPE _file_sync_stream_seek (IStream *self_ptr,
LARGE_INTEGER move_distance,
DWORD origin,
ULARGE_INTEGER *output_new_position);
static HRESULT STDMETHODCALLTYPE _file_sync_stream_set_size (IStream *self_ptr,
ULARGE_INTEGER new_size);
static HRESULT STDMETHODCALLTYPE _file_sync_stream_stat (IStream *self_ptr,
STATSTG *output_stat,
DWORD flags);
static HRESULT STDMETHODCALLTYPE _file_sync_stream_unlock_region (IStream *self_ptr,
ULARGE_INTEGER lock_offset,
ULARGE_INTEGER lock_bytes,
DWORD lock_type);
static void _file_sync_stream_free (GWin32FileSyncStream *self);
static HRESULT STDMETHODCALLTYPE
_file_sync_stream_query_interface (IStream *self_ptr,
REFIID ref_interface_guid,
LPVOID *output_object_ptr)
{
*output_object_ptr = NULL;
if (IsEqualGUID (ref_interface_guid, &IID_IUnknown))
{
IUnknown_AddRef ((IUnknown *) self_ptr);
*output_object_ptr = self_ptr;
return S_OK;
}
else if (IsEqualGUID (ref_interface_guid, &IID_IStream))
{
IStream_AddRef (self_ptr);
*output_object_ptr = self_ptr;
return S_OK;
}
return E_NOINTERFACE;
}
static ULONG STDMETHODCALLTYPE
_file_sync_stream_add_ref (IStream *self_ptr)
{
GWin32FileSyncStream *self = (GWin32FileSyncStream *) self_ptr;
return ++self->ref_count;
}
static ULONG STDMETHODCALLTYPE
_file_sync_stream_release (IStream *self_ptr)
{
GWin32FileSyncStream *self = (GWin32FileSyncStream *) self_ptr;
int ref_count = --self->ref_count;
if (ref_count == 0)
_file_sync_stream_free (self);
return ref_count;
}
static HRESULT STDMETHODCALLTYPE
_file_sync_stream_read (IStream *self_ptr,
void *output_data,
ULONG bytes_to_read,
ULONG *output_bytes_read)
{
GWin32FileSyncStream *self = (GWin32FileSyncStream *) self_ptr;
DWORD bytes_read;
if (!ReadFile (self->file_handle, output_data, bytes_to_read, &bytes_read, NULL))
{
DWORD error = GetLastError ();
return __HRESULT_FROM_WIN32 (error);
}
if (output_bytes_read)
*output_bytes_read = bytes_read;
return S_OK;
}
static HRESULT STDMETHODCALLTYPE
_file_sync_stream_write (IStream *self_ptr,
const void *data,
ULONG bytes_to_write,
ULONG *output_bytes_written)
{
GWin32FileSyncStream *self = (GWin32FileSyncStream *) self_ptr;
DWORD bytes_written;
if (!WriteFile (self->file_handle, data, bytes_to_write, &bytes_written, NULL))
{
DWORD error = GetLastError ();
return __HRESULT_FROM_WIN32 (error);
}
if (output_bytes_written)
*output_bytes_written = bytes_written;
return S_OK;
}
static HRESULT STDMETHODCALLTYPE
_file_sync_stream_seek (IStream *self_ptr,
LARGE_INTEGER move_distance,
DWORD origin,
ULARGE_INTEGER *output_new_position)
{
GWin32FileSyncStream *self = (GWin32FileSyncStream *) self_ptr;
LARGE_INTEGER new_position;
DWORD move_method;
switch (origin)
{
case STREAM_SEEK_SET:
move_method = FILE_BEGIN;
break;
case STREAM_SEEK_CUR:
move_method = FILE_CURRENT;
break;
case STREAM_SEEK_END:
move_method = FILE_END;
break;
default:
return E_INVALIDARG;
}
if (!SetFilePointerEx (self->file_handle, move_distance, &new_position, move_method))
{
DWORD error = GetLastError ();
return __HRESULT_FROM_WIN32 (error);
}
(*output_new_position).QuadPart = new_position.QuadPart;
return S_OK;
}
static HRESULT STDMETHODCALLTYPE
_file_sync_stream_set_size (IStream *self_ptr,
ULARGE_INTEGER new_size)
{
GWin32FileSyncStream *self = (GWin32FileSyncStream *) self_ptr;
FILE_END_OF_FILE_INFO info;
info.EndOfFile.QuadPart = new_size.QuadPart;
if (SetFileInformationByHandle (self->file_handle, FileEndOfFileInfo, &info, sizeof (info)))
{
DWORD error = GetLastError ();
return __HRESULT_FROM_WIN32 (error);
}
return S_OK;
}
static HRESULT STDMETHODCALLTYPE
_file_sync_stream_copy_to (IStream *self_ptr,
IStream *output_stream,
ULARGE_INTEGER bytes_to_copy,
ULARGE_INTEGER *output_bytes_read,
ULARGE_INTEGER *output_bytes_written)
{
ULARGE_INTEGER counter;
ULARGE_INTEGER written_counter;
ULARGE_INTEGER read_counter;
counter.QuadPart = bytes_to_copy.QuadPart;
written_counter.QuadPart = 0;
read_counter.QuadPart = 0;
while (counter.QuadPart > 0)
{
HRESULT hr;
ULONG bytes_read;
ULONG bytes_written;
ULONG bytes_index;
#define _INTERNAL_BUFSIZE 1024
BYTE buffer[_INTERNAL_BUFSIZE];
#undef _INTERNAL_BUFSIZE
ULONG buffer_size = sizeof (buffer);
ULONG to_read = buffer_size;
if (counter.QuadPart < buffer_size)
to_read = (ULONG) counter.QuadPart;
/* Because MS SDK has a function IStream_Read() with 3 arguments */
hr = self_ptr->lpVtbl->Read (self_ptr, buffer, to_read, &bytes_read);
if (!SUCCEEDED (hr))
return hr;
read_counter.QuadPart += bytes_read;
if (bytes_read == 0)
break;
bytes_index = 0;
while (bytes_index < bytes_read)
{
/* Because MS SDK has a function IStream_Write() with 3 arguments */
hr = output_stream->lpVtbl->Write (output_stream, &buffer[bytes_index], bytes_read - bytes_index, &bytes_written);
if (!SUCCEEDED (hr))
return hr;
if (bytes_written == 0)
return __HRESULT_FROM_WIN32 (ERROR_WRITE_FAULT);
bytes_index += bytes_written;
written_counter.QuadPart += bytes_written;
}
}
if (output_bytes_read)
output_bytes_read->QuadPart = read_counter.QuadPart;
if (output_bytes_written)
output_bytes_written->QuadPart = written_counter.QuadPart;
return S_OK;
}
static HRESULT STDMETHODCALLTYPE
_file_sync_stream_commit (IStream *self_ptr,
DWORD commit_flags)
{
GWin32FileSyncStream *self = (GWin32FileSyncStream *) self_ptr;
if (!FlushFileBuffers (self->file_handle))
{
DWORD error = GetLastError ();
return __HRESULT_FROM_WIN32 (error);
}
return S_OK;
}
static HRESULT STDMETHODCALLTYPE
_file_sync_stream_revert (IStream *self_ptr)
{
return E_NOTIMPL;
}
static HRESULT STDMETHODCALLTYPE
_file_sync_stream_lock_region (IStream *self_ptr,
ULARGE_INTEGER lock_offset,
ULARGE_INTEGER lock_bytes,
DWORD lock_type)
{
return STG_E_INVALIDFUNCTION;
}
static HRESULT STDMETHODCALLTYPE
_file_sync_stream_unlock_region (IStream *self_ptr,
ULARGE_INTEGER lock_offset,
ULARGE_INTEGER lock_bytes,
DWORD lock_type)
{
return STG_E_INVALIDFUNCTION;
}
static HRESULT STDMETHODCALLTYPE
_file_sync_stream_stat (IStream *self_ptr,
STATSTG *output_stat,
DWORD flags)
{
GWin32FileSyncStream *self = (GWin32FileSyncStream *) self_ptr;
BOOL get_name = FALSE;
FILE_BASIC_INFO bi;
FILE_STANDARD_INFO si;
if (output_stat == NULL)
return STG_E_INVALIDPOINTER;
switch (flags)
{
case STATFLAG_DEFAULT:
get_name = TRUE;
break;
case STATFLAG_NONAME:
get_name = FALSE;
break;
default:
return STG_E_INVALIDFLAG;
}
if (!GetFileInformationByHandleEx (self->file_handle, FileBasicInfo, &bi, sizeof (bi)) ||
!GetFileInformationByHandleEx (self->file_handle, FileStandardInfo, &si, sizeof (si)))
{
DWORD error = GetLastError ();
return __HRESULT_FROM_WIN32 (error);
}
output_stat->type = STGTY_STREAM;
output_stat->mtime.dwLowDateTime = bi.LastWriteTime.LowPart;
output_stat->mtime.dwHighDateTime = bi.LastWriteTime.HighPart;
output_stat->ctime.dwLowDateTime = bi.CreationTime.LowPart;
output_stat->ctime.dwHighDateTime = bi.CreationTime.HighPart;
output_stat->atime.dwLowDateTime = bi.LastAccessTime.LowPart;
output_stat->atime.dwHighDateTime = bi.LastAccessTime.HighPart;
output_stat->grfLocksSupported = 0;
memset (&output_stat->clsid, 0, sizeof (CLSID));
output_stat->grfStateBits = 0;
output_stat->reserved = 0;
output_stat->cbSize.QuadPart = si.EndOfFile.QuadPart;
output_stat->grfMode = self->stgm_mode;
if (get_name)
{
DWORD tries;
wchar_t *buffer;
/* Nothing in the documentation guarantees that the name
* won't change between two invocations (one - to get the
* buffer size, the other - to fill the buffer).
* Re-try up to 5 times in case the required buffer size
* doesn't match.
*/
for (tries = 5; tries > 0; tries--)
{
DWORD buffer_size;
DWORD buffer_size2;
DWORD error;
buffer_size = GetFinalPathNameByHandleW (self->file_handle, NULL, 0, 0);
if (buffer_size == 0)
{
DWORD my_error = GetLastError ();
return __HRESULT_FROM_WIN32 (my_error);
}
buffer = CoTaskMemAlloc (buffer_size);
buffer[buffer_size - 1] = 0;
buffer_size2 = GetFinalPathNameByHandleW (self->file_handle, buffer, buffer_size, 0);
if (buffer_size2 < buffer_size)
break;
error = GetLastError ();
CoTaskMemFree (buffer);
if (buffer_size2 == 0)
return __HRESULT_FROM_WIN32 (error);
}
if (tries == 0)
return __HRESULT_FROM_WIN32 (ERROR_BAD_LENGTH);
output_stat->pwcsName = buffer;
}
else
output_stat->pwcsName = NULL;
return S_OK;
}
static HRESULT STDMETHODCALLTYPE
_file_sync_stream_clone (IStream *self_ptr,
IStream **output_clone_ptr)
{
return E_NOTIMPL;
}
static IStreamVtbl _file_sync_stream_vtbl = {
_file_sync_stream_query_interface,
_file_sync_stream_add_ref,
_file_sync_stream_release,
_file_sync_stream_read,
_file_sync_stream_write,
_file_sync_stream_seek,
_file_sync_stream_set_size,
_file_sync_stream_copy_to,
_file_sync_stream_commit,
_file_sync_stream_revert,
_file_sync_stream_lock_region,
_file_sync_stream_unlock_region,
_file_sync_stream_stat,
_file_sync_stream_clone,
};
static void
_file_sync_stream_free (GWin32FileSyncStream *self)
{
if (self->owns_handle)
CloseHandle (self->file_handle);
g_free (self);
}
/**
* g_win32_file_sync_stream_new:
* @handle: a Win32 HANDLE for a file.
* @owns_handle: %TRUE if newly-created stream owns the handle
* (and closes it when destroyed)
* @stgm_mode: a combination of [STGM constants](https://docs.microsoft.com/en-us/windows/win32/stg/stgm-constants)
* that specify the mode with which the stream
* is opened.
* @output_hresult: (out) (optional): a HRESULT from the internal COM calls.
* Will be `S_OK` on success.
*
* Creates an IStream object backed by a HANDLE.
*
* @stgm_mode should match the mode of the @handle, otherwise the stream might
* attempt to perform operations that the @handle does not allow. The implementation
* itself ignores these flags completely, they are only used to report
* the mode of the stream to third parties.
*
* The stream only does synchronous access and will never return `E_PENDING` on I/O.
*
* The returned stream object should be treated just like any other
* COM object, and released via `IUnknown_Release()`.
* its elements have been unreffed with g_object_unref().
*
* Returns: (nullable) (transfer full): a new IStream object on success, %NULL on failure.
**/
IStream *
g_win32_file_sync_stream_new (HANDLE file_handle,
gboolean owns_handle,
DWORD stgm_mode,
HRESULT *output_hresult)
{
GWin32FileSyncStream *new_stream;
IStream *result;
HRESULT hr;
new_stream = g_new0 (GWin32FileSyncStream, 1);
new_stream->self.lpVtbl = &_file_sync_stream_vtbl;
hr = IUnknown_QueryInterface ((IUnknown *) new_stream, &IID_IStream, (void **) &result);
if (!SUCCEEDED (hr))
{
g_free (new_stream);
if (output_hresult)
*output_hresult = hr;
return NULL;
}
new_stream->stgm_mode = stgm_mode;
new_stream->file_handle = file_handle;
new_stream->owns_handle = owns_handle;
if (output_hresult)
*output_hresult = S_OK;
return result;
}
|