ObjFW  Check-in [103d934719]

Overview
Comment:OFURLHandler_file: Fix leaked find handle on Win32
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 103d9347193d447984518517beeea18e2db4ece8fbf8e8dd5286814c9c2c7dbe
User & Date: js on 2019-04-02 21:55:28
Other Links: manifest | tags
Context
2019-04-06
20:35
Improve path handling on Windows/DOS check-in: ec17b9225a user: js tags: trunk
2019-04-02
21:55
OFURLHandler_file: Fix leaked find handle on Win32 check-in: 103d934719 user: js tags: trunk
2019-03-25
00:21
tests: Use dot syntax check-in: b045cbb9c7 user: js tags: trunk
Changes

Modified src/OFURLHandler_file.m from [9e11b1d009] to [f54ca9d551].

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
setSymbolicLinkDestinationAttribute(of_mutable_file_attributes_t attributes,
    of_stat_t *s, OFURL *URL)
{
#ifdef OF_FILE_MANAGER_SUPPORTS_SYMLINKS
	OFString *path = URL.fileSystemRepresentation;

# ifndef OF_WINDOWS
	if (S_ISLNK(s->st_mode)) {
		of_string_encoding_t encoding = [OFLocale encoding];
		char destinationC[PATH_MAX];
		ssize_t length;
		OFString *destination;
		of_file_attribute_key_t key;




		length = readlink([path cStringWithEncoding: encoding],
		    destinationC, PATH_MAX);

		if (length < 0)
			@throw [OFRetrieveItemAttributesFailedException
			    exceptionWithURL: URL
				       errNo: errno];

		destination = [OFString stringWithCString: destinationC
						 encoding: encoding
						   length: length];

		key = of_file_attribute_key_symbolic_link_destination;
		[attributes setObject: destination
			       forKey: key];
	}
# else

	WIN32_FIND_DATAW data;



	if (func_CreateSymbolicLinkW != NULL &&


	    FindFirstFileW(path.UTF16String, &data) &&




	    (data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&


	    data.dwReserved0 == IO_REPARSE_TAG_SYMLINK) {
		HANDLE handle;
		OFString *destination;



		if ((handle = CreateFileW(path.UTF16String, 0,
		    (FILE_SHARE_READ | FILE_SHARE_WRITE), NULL, OPEN_EXISTING,
		    FILE_FLAG_OPEN_REPARSE_POINT, NULL)) ==
		    INVALID_HANDLE_VALUE)
			@throw [OFRetrieveItemAttributesFailedException
			    exceptionWithURL: URL
				       errNo: 0];

		@try {
			union {
				char bytes[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
				REPARSE_DATA_BUFFER data;
			} buffer;
			DWORD size;
			wchar_t *tmp;
			of_file_attribute_key_t key;

			if (!DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT,
			    NULL, 0, buffer.bytes,
			    MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &size, NULL))

				@throw [OFRetrieveItemAttributesFailedException
				    exceptionWithURL: URL
					       errNo: 0];

			if (buffer.data.ReparseTag != IO_REPARSE_TAG_SYMLINK)
				@throw [OFRetrieveItemAttributesFailedException
				    exceptionWithURL: URL
					       errNo: 0];

#  define slrb buffer.data.SymbolicLinkReparseBuffer
			tmp = slrb.PathBuffer +
			    (slrb.SubstituteNameOffset / sizeof(wchar_t));

			destination = [OFString
			    stringWithUTF16String: tmp
					   length: slrb.SubstituteNameLength /
						   sizeof(wchar_t)];

			[attributes setObject: of_file_type_symbolic_link
				       forKey: of_file_attribute_key_type];
			key = of_file_attribute_key_symbolic_link_destination;
			[attributes setObject: destination
				       forKey: key];
#  undef slrb
		} @finally {
			CloseHandle(handle);
		}
	}
# endif
#endif
}

@implementation OFURLHandler_file
+ (void)initialize







<
|
|
|
|
|

>
>
>
|
|

|
|
|
|

|
|
|

|
|
|
<

>
|
>
>

|
>
>
|
>
>
>
>
|
>
>
|
|
|
>
|
>
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|

|
<
|
>
|
|
|

|
|
|
|


|
|

|
|
|
|

|
|
|
|
|

|
|
<







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
setSymbolicLinkDestinationAttribute(of_mutable_file_attributes_t attributes,
    of_stat_t *s, OFURL *URL)
{
#ifdef OF_FILE_MANAGER_SUPPORTS_SYMLINKS
	OFString *path = URL.fileSystemRepresentation;

# ifndef OF_WINDOWS

	of_string_encoding_t encoding = [OFLocale encoding];
	char destinationC[PATH_MAX];
	ssize_t length;
	OFString *destination;
	of_file_attribute_key_t key;

	if (!S_ISLNK(s->st_mode))
		return;

	length = readlink([path cStringWithEncoding: encoding], destinationC,
	    PATH_MAX);

	if (length < 0)
		@throw [OFRetrieveItemAttributesFailedException
		    exceptionWithURL: URL
			       errNo: errno];

	destination = [OFString stringWithCString: destinationC
					 encoding: encoding
					   length: length];

	key = of_file_attribute_key_symbolic_link_destination;
	[attributes setObject: destination
		       forKey: key];

# else
	HANDLE findHandle;
	WIN32_FIND_DATAW findData;
	HANDLE fileHandle;
	OFString *destination;

	if (func_CreateSymbolicLinkW == NULL)
		return;

	findHandle = FindFirstFileW(path.UTF16String, &findData);
	if (findHandle == INVALID_HANDLE_VALUE)
		return;

	@try {
		if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
			return;

		if (findData.dwReserved0 != IO_REPARSE_TAG_SYMLINK)
			return;
	} @finally {
		FindClose(findHandle);
	}

	fileHandle = CreateFileW(path.UTF16String, 0, (FILE_SHARE_READ |
	    FILE_SHARE_WRITE), NULL, OPEN_EXISTING,
	    FILE_FLAG_OPEN_REPARSE_POINT, NULL);
	if (fileHandle == INVALID_HANDLE_VALUE)
		@throw [OFRetrieveItemAttributesFailedException
		    exceptionWithURL: URL
			       errNo: 0];

	@try {
		union {
			char bytes[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
			REPARSE_DATA_BUFFER data;
		} buffer;
		DWORD size;
		wchar_t *tmp;
		of_file_attribute_key_t key;

		if (!DeviceIoControl(fileHandle, FSCTL_GET_REPARSE_POINT, NULL,

		    0, buffer.bytes, MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &size,
		    NULL))
			@throw [OFRetrieveItemAttributesFailedException
			    exceptionWithURL: URL
				       errNo: 0];

		if (buffer.data.ReparseTag != IO_REPARSE_TAG_SYMLINK)
			@throw [OFRetrieveItemAttributesFailedException
			    exceptionWithURL: URL
				       errNo: 0];

#  define slrb buffer.data.SymbolicLinkReparseBuffer
		tmp = slrb.PathBuffer +
		    (slrb.SubstituteNameOffset / sizeof(wchar_t));

		destination = [OFString
		    stringWithUTF16String: tmp
				   length: slrb.SubstituteNameLength /
					   sizeof(wchar_t)];

		[attributes setObject: of_file_type_symbolic_link
			       forKey: of_file_attribute_key_type];
		key = of_file_attribute_key_symbolic_link_destination;
		[attributes setObject: destination
			       forKey: key];
#  undef slrb
	} @finally {
		CloseHandle(fileHandle);

	}
# endif
#endif
}

@implementation OFURLHandler_file
+ (void)initialize