-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathunistd.h
More file actions
128 lines (102 loc) · 2.31 KB
/
Copy pathunistd.h
File metadata and controls
128 lines (102 loc) · 2.31 KB
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <io.h>
#include <process.h>
#include <sys/_types.h>
int msleep(unsigned long microseconds);
#define usleep(usec) msleep(usec/1000)
#define sleep(sec) ((unsigned long)msleep(sec*1000))
#define ftruncate chsize
typedef intptr_t ssize_t;
typedef int mode_t;
#define S_IRUSR _S_IREAD
#define S_IWUSR _S_IWRITE
#define F_OK 0
#define O_NONBLOCK 0
__forceinline
int pread(int d, void* buf, int nbytes, __int64 offset)
{
int result = 0;
if (_lseeki64(d, offset, SEEK_SET) != offset)
{
return -1;
}
while (nbytes > 0)
{
int rc = _read(d, buf, nbytes);
if (rc < 0)
{
return rc;
}
if (rc == 0)
{
break;
}
nbytes -= rc;
result += rc;
buf = (char*)buf + rc;
}
return result;
}
__forceinline
int pwrite(int d, const void* buf, int nbytes, __int64 offset)
{
int rc;
if (_lseeki64(d, offset, SEEK_SET) != offset)
{
return -1;
}
rc = _write(d, buf, nbytes);
if ((rc >= 0) || (errno != 28))
{
return rc;
}
fprintf(stderr, "Extending file to %I64u bytes.\n", offset + nbytes);
#if defined(_M_IX86)
{
HANDLE h = (HANDLE)_get_osfhandle(d);
LARGE_INTEGER eof;
eof.QuadPart = offset + nbytes;
if (SetFilePointer(h, eof.LowPart, &eof.HighPart, FILE_BEGIN) ==
INVALID_SET_FILE_POINTER &&
GetLastError() != NO_ERROR)
{
errno = 28;
return -1;
}
if (!SetEndOfFile(h))
{
errno = 28;
return -1;
}
}
#elif defined(_M_X64)
{
HANDLE h = (HANDLE)_get_osfhandle(d);
LARGE_INTEGER eof;
eof.QuadPart = offset + nbytes;
if (!SetFilePointerEx(h, eof, NULL, FILE_BEGIN))
{
errno = 28;
return -1;
}
if (!SetEndOfFile(h))
{
errno = 28;
return -1;
}
}
#else
errno = _chsize_s(d, offset + nbytes);
if (errno != 0)
{
return -1;
}
#endif
if (_lseeki64(d, offset, SEEK_SET) != offset)
{
return -1;
}
return _write(d, buf, nbytes);
}