Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- From 7d85b872fd714590a158c9ae47a8bb53d5571c54 Mon Sep 17 00:00:00 2001
- From: David Allsopp <david.allsopp@metastack.com>
- Date: Thu, 30 Jul 2015 16:05:33 +0200
- Subject: [PATCH] Populate st_ino for Unix.stat/lstat on Windows
- ---
- otherlibs/win32unix/stat.c | 71 +++++++++++++++++++++++++---------------------
- 1 file changed, 39 insertions(+), 32 deletions(-)
- diff --git a/otherlibs/win32unix/stat.c b/otherlibs/win32unix/stat.c
- index 4d910e1..50da744 100644
- --- a/otherlibs/win32unix/stat.c
- +++ b/otherlibs/win32unix/stat.c
- @@ -46,14 +46,14 @@ static int file_kind_table[] = {
- S_IFREG, S_IFDIR, S_IFCHR, S_IFBLK, S_IFLNK, S_IFIFO, S_IFSOCK
- };
- -static value stat_aux(int use_64, struct _stat64 *buf)
- +static value stat_aux(int use_64, __int64 st_ino, struct _stat64 *buf)
- {
- CAMLparam0 ();
- CAMLlocal1 (v);
- v = caml_alloc (12, 0);
- Store_field (v, 0, Val_int (buf->st_dev));
- - Store_field (v, 1, Val_int (buf->st_ino));
- + Store_field (v, 1, Val_int (st_ino ? st_ino & Max_long : buf->st_ino));
- Store_field (v, 2, cst_to_constr (buf->st_mode & S_IFMT, file_kind_table,
- sizeof(file_kind_table) / sizeof(int), 0));
- Store_field (v, 3, Val_int(buf->st_mode & 07777));
- @@ -149,32 +149,16 @@ int IsRootUNCName(char* path)
- return 0;
- }
- -static int do_stat(int do_lstat, int use_64, char* path, mlsize_t l, struct _stat64* res)
- +static int do_stat(int do_lstat, int use_64, char* path, mlsize_t l, __int64* st_ino, struct _stat64* res)
- {
- BY_HANDLE_FILE_INFORMATION info;
- - int drive, i;
- + int i;
- char* ptr;
- char c;
- HANDLE h;
- unsigned short mode;
- int is_symlink = 0;
- - /*
- - * Get the drive number
- - */
- - if (l > 1 && path[1] == ':') {
- - if (path[2]) {
- - drive = tolower(*path) - 'a';
- - }
- - else {
- - errno = ENOENT;
- - return 0;
- - }
- - }
- - else {
- - drive = _getdrive() - 1;
- - }
- -
- caml_enter_blocking_section();
- h = CreateFile(path, FILE_READ_ATTRIBUTES, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
- caml_leave_blocking_section();
- @@ -199,6 +183,23 @@ static int do_stat(int do_lstat, int use_64, char* path, mlsize_t l, struct _sta
- info.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
- res->st_size = 0;
- res->st_nlink = 1;
- +
- + /*
- + * Get the drive number
- + */
- + if (l > 1 && path[1] == ':') {
- + if (path[2]) {
- + res->st_dev = tolower(*path) - 'a';
- + }
- + else {
- + errno = ENOENT;
- + return 0;
- + }
- + }
- + else {
- + res->st_dev = _getdrive() - 1;
- + }
- +
- /*
- * January 1, 1980 UTC
- */
- @@ -283,9 +284,11 @@ static int do_stat(int do_lstat, int use_64, char* path, mlsize_t l, struct _sta
- }
- /*
- - * Note MS CRT (still) puts st_nlink = 1
- + * Note MS CRT (still) puts st_nlink = 1 and gives st_ino = 0
- */
- res->st_nlink = info.nNumberOfLinks;
- + res->st_dev = info.dwVolumeSerialNumber;
- + *st_ino = ((__int64)(info.nFileIndexHigh)) << 32 | ((__int64)info.nFileIndexLow);
- }
- mode = (do_lstat && is_symlink ? S_IFLNK | S_IEXEC | S_IWRITE : (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ? _S_IFDIR | _S_IEXEC : _S_IFREG)) | (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY ? _S_IREAD : _S_IREAD | _S_IWRITE);
- @@ -296,7 +299,7 @@ static int do_stat(int do_lstat, int use_64, char* path, mlsize_t l, struct _sta
- mode |= (mode & 0700) >> 6;
- res->st_mode = mode;
- res->st_uid = res->st_gid = res->st_ino = 0;
- - res->st_rdev = res->st_dev = drive;
- + res->st_rdev = res->st_dev;
- return 1;
- }
- @@ -304,37 +307,41 @@ static int do_stat(int do_lstat, int use_64, char* path, mlsize_t l, struct _sta
- CAMLprim value unix_stat(value path)
- {
- struct _stat64 buf;
- - if (!do_stat(0, 0, String_val(path), caml_string_length(path), &buf)) {
- + __int64 st_ino;
- + if (!do_stat(0, 0, String_val(path), caml_string_length(path), &st_ino, &buf)) {
- uerror("stat", path);
- }
- - return stat_aux(0, &buf);
- + return stat_aux(0, st_ino, &buf);
- }
- CAMLprim value unix_stat_64(value path)
- {
- struct _stat64 buf;
- - if (!do_stat(0, 1, String_val(path), caml_string_length(path), &buf)) {
- + __int64 st_ino;
- + if (!do_stat(0, 1, String_val(path), caml_string_length(path), &st_ino, &buf)) {
- uerror("stat", path);
- }
- - return stat_aux(1, &buf);
- + return stat_aux(1, st_ino, &buf);
- }
- CAMLprim value unix_lstat(value path)
- {
- struct _stat64 buf;
- - if (!do_stat(1, 0, String_val(path), caml_string_length(path), &buf)) {
- + __int64 st_ino;
- + if (!do_stat(1, 0, String_val(path), caml_string_length(path), &st_ino, &buf)) {
- uerror("lstat", path);
- }
- - return stat_aux(0, &buf);
- + return stat_aux(0, st_ino, &buf);
- }
- CAMLprim value unix_lstat_64(value path)
- {
- struct _stat64 buf;
- - if (!do_stat(1, 1, String_val(path), caml_string_length(path), &buf)) {
- + __int64 st_ino;
- + if (!do_stat(1, 1, String_val(path), caml_string_length(path), &st_ino, &buf)) {
- uerror("lstat", path);
- }
- - return stat_aux(1, &buf);
- + return stat_aux(1, st_ino, &buf);
- }
- CAMLprim value unix_fstat(value handle)
- @@ -348,7 +355,7 @@ CAMLprim value unix_fstat(value handle)
- win32_maperr(ERROR_ARITHMETIC_OVERFLOW);
- uerror("fstat", Nothing);
- }
- - return stat_aux(0, &buf);
- + return stat_aux(0, 0, &buf);
- }
- CAMLprim value unix_fstat_64(value handle)
- @@ -358,5 +365,5 @@ CAMLprim value unix_fstat_64(value handle)
- ret = _fstat64(win_CRT_fd_of_filedescr(handle), &buf);
- if (ret == -1) uerror("fstat", Nothing);
- - return stat_aux(1, &buf);
- + return stat_aux(1, 0, &buf);
- }
- --
- 2.4.4.windows.2
Add Comment
Please, Sign In to add comment