aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/parser/pg_wrapper/postgresql/src/port/win32fseek.c
blob: 985313c825feef9e38652f9e08622396f489c47e (plain) (blame)
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
/*-------------------------------------------------------------------------
 *
 * win32fseek.c
 *	  Replacements for fseeko() and ftello().
 *
 * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
 *
 * IDENTIFICATION
 *	  src/port/win32fseek.c
 *
 *-------------------------------------------------------------------------
 */

#ifdef FRONTEND
#include "postgres_fe.h"
#else
#include "postgres.h"
#endif

#if defined(WIN32) && defined(_MSC_VER)

/*
 * _pgfseeko64
 *
 * Calling fseek() on a handle to a non-seeking device such as a pipe or
 * a communications device is not supported, and fseek() may not return
 * an error.  This wrapper relies on the file type to check which cases
 * are supported.
 */
int
_pgfseeko64(FILE *stream, pgoff_t offset, int origin)
{
	DWORD		fileType;
	HANDLE		hFile = (HANDLE) _get_osfhandle(_fileno(stream));

	fileType = pgwin32_get_file_type(hFile);
	if (errno != 0)
		return -1;

	if (fileType == FILE_TYPE_DISK)
		return _fseeki64(stream, offset, origin);
	else if (fileType == FILE_TYPE_CHAR || fileType == FILE_TYPE_PIPE)
		errno = ESPIPE;
	else
		errno = EINVAL;

	return -1;
}

/*
 * _pgftello64
 *
 * Same as _pgfseeko64().
 */
pgoff_t
_pgftello64(FILE *stream)
{
	DWORD		fileType;
	HANDLE		hFile = (HANDLE) _get_osfhandle(_fileno(stream));

	fileType = pgwin32_get_file_type(hFile);
	if (errno != 0)
		return -1;

	if (fileType == FILE_TYPE_DISK)
		return _ftelli64(stream);
	else if (fileType == FILE_TYPE_CHAR || fileType == FILE_TYPE_PIPE)
		errno = ESPIPE;
	else
		errno = EINVAL;

	return -1;
}

#endif							/* defined(WIN32) && defined(_MSC_VER) */