-
Notifications
You must be signed in to change notification settings - Fork 19
/
fallocate.c
144 lines (109 loc) · 2.83 KB
/
fallocate.c
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
/*
* posix_fallocate binding
*
* Copyright 2008-2009 Talend, Inc.
*
* License LGPL-2.1 with OCaml linking static exception
*
* For more information go to: www.talend.com
*
* author: Sylvain Le Gall
*
*/
#define EXTUNIX_WANT_FALLOCATE
#include "config.h"
#if defined(EXTUNIX_HAVE_FALLOCATE)
#if defined(_WIN32)
static void caml_fallocate_error (void)
{
win32_maperr(GetLastError());
uerror("fallocate", Val_unit);
}
static __int64 caml_fallocate_lseek (HANDLE hFile, __int64 i64Pos, DWORD dwMoveMethod)
{
LARGE_INTEGER liRes;
liRes.QuadPart = i64Pos;
liRes.LowPart = SetFilePointer(hFile, liRes.LowPart, &liRes.HighPart, dwMoveMethod);
if (liRes.LowPart == INVALID_SET_FILE_POINTER &&
GetLastError() != NO_ERROR)
{
caml_fallocate_error();
};
return liRes.QuadPart;
}
static void caml_fallocate_do (HANDLE hFile, __int64 i64Off, __int64 i64Len)
{
__int64 i64Cur = 0;
LARGE_INTEGER liFileSize;
/* Check that off + len > file size */
if (!GetFileSizeEx(hFile, &liFileSize))
{
caml_fallocate_error();
};
if (i64Off + i64Len <= liFileSize.QuadPart)
{
return;
};
/* Get the current position in the file */
i64Cur = caml_fallocate_lseek(hFile, 0, FILE_CURRENT);
/* Go to the expected end of file */
caml_fallocate_lseek(hFile, i64Off, FILE_BEGIN);
caml_fallocate_lseek(hFile, i64Len, FILE_CURRENT);
/* Extend file */
if (!SetEndOfFile(hFile))
{
caml_fallocate_error();
};
/* Restore initial file pointer position */
caml_fallocate_lseek(hFile, i64Cur, FILE_BEGIN);
}
CAMLprim value caml_extunix_fallocate64(value vfd, value voff, value vlen)
{
CAMLparam3(vfd, voff, vlen);
caml_fallocate_do(Handle_val(vfd), Int64_val(voff), Int64_val(vlen));
CAMLreturn(Val_unit);
}
CAMLprim value caml_extunix_fallocate(value vfd, value voff, value vlen)
{
CAMLparam3(vfd, voff, vlen);
caml_fallocate_do(Handle_val(vfd), Long_val(voff), Long_val(vlen));
CAMLreturn(Val_unit);
}
#else
static void caml_fallocate_error (int errcode)
{
if (errcode != 0)
{
unix_error(errcode, "fallocate", Nothing);
};
}
CAMLprim value caml_extunix_fallocate64(value vfd, value voff, value vlen)
{
int errcode = 0;
int fd = -1;
off64_t off = 0;
off64_t len = 0;
CAMLparam3(vfd, voff, vlen);
fd = Int_val(vfd);
off = Int64_val(voff);
len = Int64_val(vlen);
errcode = posix_fallocate64(fd, off, len);
caml_fallocate_error(errcode);
CAMLreturn(Val_unit);
}
CAMLprim value caml_extunix_fallocate(value vfd, value voff, value vlen)
{
int errcode = 0;
int fd = -1;
off_t off = 0;
off_t len = 0;
CAMLparam3(vfd, voff, vlen);
fd = Int_val(vfd);
off = Long_val(voff);
len = Long_val(vlen);
errcode = posix_fallocate(fd, off, len);
caml_fallocate_error(errcode);
CAMLreturn(Val_unit);
}
#endif /* _WIN32 */
#endif /* EXTUNIX_HAVE_FALLOCATE */