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
|
/**************************************************************************/
/* */
/* OCaml */
/* */
/* Florent Monnier */
/* Nicolas Ojeda Bar, LexiFi */
/* */
/* Copyright 2019 Institut National de Recherche en Informatique et */
/* en Automatique. */
/* */
/* All rights reserved. This file is distributed under the terms of */
/* the GNU Lesser General Public License version 2.1, with the */
/* special exception on linking described in the file LICENSE. */
/* */
/**************************************************************************/
#define CAML_INTERNALS
#include <sys/types.h>
#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/fail.h>
#include <caml/signals.h>
#include <caml/io.h>
#include <caml/osdeps.h>
#include "caml/unixsupport.h"
#include <windows.h>
static int truncate_handle(HANDLE fh, __int64 len)
{
LARGE_INTEGER fp;
fp.QuadPart = len;
if (SetFilePointerEx(fh, fp, NULL, FILE_BEGIN) == 0 ||
SetEndOfFile(fh) == 0) {
caml_win32_maperr(GetLastError());
return -1;
}
return 0;
}
static int ftruncate(HANDLE fh, __int64 len)
{
HANDLE dupfh, currproc;
int ret;
currproc = GetCurrentProcess();
/* Duplicate the handle, so we are free to modify its file position. */
if (DuplicateHandle(currproc, fh, currproc, &dupfh, 0, FALSE,
DUPLICATE_SAME_ACCESS) == 0) {
caml_win32_maperr(GetLastError());
return -1;
}
ret = truncate_handle(dupfh, len);
CloseHandle(dupfh);
return ret;
}
static int truncate(WCHAR * path, __int64 len)
{
HANDLE fh;
int ret;
fh = CreateFile(path, GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (fh == INVALID_HANDLE_VALUE) {
caml_win32_maperr(GetLastError());
return -1;
}
ret = truncate_handle(fh, len);
CloseHandle(fh);
return ret;
}
CAMLprim value caml_unix_truncate(value path, value vlen)
{
CAMLparam2(path, vlen);
WCHAR * p;
int ret;
file_offset len = Long_val(vlen);
caml_unix_check_path(path, "truncate");
p = caml_stat_strdup_to_utf16(String_val(path));
caml_enter_blocking_section();
ret = truncate(p, len);
caml_leave_blocking_section();
caml_stat_free(p);
if (ret == -1)
caml_uerror("truncate", path);
CAMLreturn(Val_unit);
}
CAMLprim value caml_unix_truncate_64(value path, value vlen)
{
CAMLparam2(path, vlen);
WCHAR * p;
int ret;
__int64 len = Int64_val(vlen);
caml_unix_check_path(path, "truncate");
p = caml_stat_strdup_to_utf16(String_val(path));
caml_enter_blocking_section();
ret = truncate(p, len);
caml_leave_blocking_section();
caml_stat_free(p);
if (ret == -1)
caml_uerror("truncate", path);
CAMLreturn(Val_unit);
}
CAMLprim value caml_unix_ftruncate(value fd, value len)
{
int ret;
HANDLE h = Handle_val(fd);
caml_enter_blocking_section();
ret = ftruncate(h, Long_val(len));
caml_leave_blocking_section();
if (ret == -1)
caml_uerror("ftruncate", Nothing);
return Val_unit;
}
CAMLprim value caml_unix_ftruncate_64(value fd, value vlen)
{
int ret;
HANDLE h = Handle_val(fd);
__int64 len = Int64_val(vlen);
caml_enter_blocking_section();
ret = ftruncate(h, len);
caml_leave_blocking_section();
if (ret == -1)
caml_uerror("ftruncate", Nothing);
return Val_unit;
}
|