summaryrefslogtreecommitdiff
path: root/otherlibs/unix/link_win32.c
blob: b9366b1dac8e5971a576846f12d554f396bc0711 (plain)
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
/**************************************************************************/
/*                                                                        */
/*                                 OCaml                                  */
/*                                                                        */
/*                  File contributed by Lionel Fourquaux                  */
/*                                                                        */
/*   Copyright 2001 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 <caml/mlvalues.h>
#include <caml/fail.h>
#include <caml/memory.h>
#include <caml/osdeps.h>
#include "unixsupport.h"
#include <errno.h>
#include <windows.h>

typedef
BOOL (WINAPI *tCreateHardLink)(
  LPCWSTR lpFileName,
  LPCWSTR lpExistingFileName,
  LPSECURITY_ATTRIBUTES lpSecurityAttributes
);

CAMLprim value caml_unix_link(value follow, value path1, value path2)
{
  HMODULE hModKernel32;
  tCreateHardLink pCreateHardLink;
  BOOL result;
  wchar_t * wpath1, * wpath2;
  if (Is_some(follow) && !Bool_val(Some_val(follow))) {
    errno = ENOSYS;
    caml_uerror("link", path2);
  }
  hModKernel32 = GetModuleHandle(L"KERNEL32.DLL");
  pCreateHardLink =
    (tCreateHardLink) GetProcAddress(hModKernel32, "CreateHardLinkW");
  if (pCreateHardLink == NULL)
    caml_invalid_argument("Unix.link not implemented");
  caml_unix_check_path(path1, "link");
  caml_unix_check_path(path2, "link");

  wpath1 = caml_stat_strdup_to_utf16(String_val(path1));
  wpath2 = caml_stat_strdup_to_utf16(String_val(path2));

  result = pCreateHardLink(wpath2, wpath1, NULL);

  caml_stat_free(wpath1);
  caml_stat_free(wpath2);

  if (! result) {
    caml_win32_maperr(GetLastError());
    caml_uerror("link", path2);
  }
  return Val_unit;
}