-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10047 from dbuenzli/unix-realpath
Add Unix.realpath.
- Loading branch information
Showing
14 changed files
with
197 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/**************************************************************************/ | ||
/* */ | ||
/* OCaml */ | ||
/* */ | ||
/* The OCaml programmers */ | ||
/* */ | ||
/* Copyright 2020 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. */ | ||
/* */ | ||
/**************************************************************************/ | ||
|
||
#include <caml/mlvalues.h> | ||
#include <caml/memory.h> | ||
#include <caml/alloc.h> | ||
#include <caml/fail.h> | ||
#include "unixsupport.h" | ||
|
||
#ifdef HAS_REALPATH | ||
|
||
CAMLprim value unix_realpath (value p) | ||
{ | ||
CAMLparam1 (p); | ||
char *r; | ||
value rp; | ||
|
||
caml_unix_check_path (p, "realpath"); | ||
r = realpath (String_val (p), NULL); | ||
if (r == NULL) { uerror ("realpath", p); } | ||
rp = caml_copy_string (r); | ||
free (r); | ||
CAMLreturn (rp); | ||
} | ||
|
||
#else | ||
|
||
CAMLprim value unix_realpath (value p) | ||
{ caml_invalid_argument ("realpath not implemented"); } | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/**************************************************************************/ | ||
/* */ | ||
/* OCaml */ | ||
/* */ | ||
/* The OCaml programmers */ | ||
/* */ | ||
/* Copyright 2020 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 | ||
|
||
/* | ||
* Windows Vista functions enabled | ||
*/ | ||
#undef _WIN32_WINNT | ||
#define _WIN32_WINNT 0x0600 | ||
|
||
#include <caml/mlvalues.h> | ||
#include <caml/memory.h> | ||
#include <caml/alloc.h> | ||
#include <caml/fail.h> | ||
#include <caml/osdeps.h> | ||
#include "unixsupport.h" | ||
|
||
#include <windows.h> | ||
#include <stdio.h> | ||
|
||
CAMLprim value unix_realpath (value p) | ||
{ | ||
CAMLparam1 (p); | ||
HANDLE h; | ||
wchar_t *wp; | ||
wchar_t *wr; | ||
DWORD wr_len; | ||
value rp; | ||
|
||
caml_unix_check_path (p, "realpath"); | ||
wp = caml_stat_strdup_to_utf16 (String_val (p)); | ||
h = CreateFile (wp, 0, | ||
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, | ||
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); | ||
caml_stat_free (wp); | ||
|
||
if (h == INVALID_HANDLE_VALUE) | ||
{ | ||
win32_maperr (GetLastError ()); | ||
uerror ("realpath", p); | ||
} | ||
|
||
wr_len = GetFinalPathNameByHandle (h, NULL, 0, VOLUME_NAME_DOS); | ||
if (wr_len == 0) | ||
{ | ||
win32_maperr (GetLastError ()); | ||
CloseHandle (h); | ||
uerror ("realpath", p); | ||
} | ||
|
||
wr = caml_stat_alloc ((wr_len + 1) * sizeof (wchar_t)); | ||
wr_len = GetFinalPathNameByHandle (h, wr, wr_len, VOLUME_NAME_DOS); | ||
|
||
if (wr_len == 0) | ||
{ | ||
win32_maperr (GetLastError ()); | ||
CloseHandle (h); | ||
caml_stat_free (wr); | ||
uerror ("realpath", p); | ||
} | ||
|
||
rp = caml_copy_string_of_utf16 (wr); | ||
CloseHandle (h); | ||
caml_stat_free (wr); | ||
CAMLreturn (rp); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
(* TEST | ||
* hasunix | ||
include unix | ||
** bytecode | ||
** native | ||
*) | ||
|
||
let main () = | ||
(* On Windows this tests that we strip \\?\ *) | ||
let cwd = String.lowercase_ascii (Sys.getcwd ()) in | ||
assert (cwd = String.lowercase_ascii (Unix.realpath cwd)); | ||
Unix.mkdir "test_dir" 0o755; | ||
close_out (open_out "test_dir/test_file"); | ||
let p0 = Unix.realpath "test_dir/.//test_file" in | ||
let p1 = Unix.realpath "test_dir/../test_dir/test_file" in | ||
assert (p0 = p1 && | ||
not (Filename.is_relative p0) && | ||
not (Filename.is_relative p1)); | ||
print_endline "Unix.realpath works with files"; | ||
let p2 = Unix.realpath "./test_dir/../test_dir/.." in | ||
let p3 = Unix.realpath "." in | ||
assert (p2 = p3 && | ||
not (Filename.is_relative p2) && | ||
not (Filename.is_relative p3)); | ||
print_endline "Unix.realpath works with directories"; | ||
() | ||
|
||
let () = Unix.handle_unix_error main () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Unix.realpath works with files | ||
Unix.realpath works with directories |