Guest User

Untitled

a guest
Feb 16th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  1. From 7d85b872fd714590a158c9ae47a8bb53d5571c54 Mon Sep 17 00:00:00 2001
  2. From: David Allsopp <david.allsopp@metastack.com>
  3. Date: Thu, 30 Jul 2015 16:05:33 +0200
  4. Subject: [PATCH] Populate st_ino for Unix.stat/lstat on Windows
  5.  
  6. ---
  7. otherlibs/win32unix/stat.c | 71 +++++++++++++++++++++++++---------------------
  8. 1 file changed, 39 insertions(+), 32 deletions(-)
  9.  
  10. diff --git a/otherlibs/win32unix/stat.c b/otherlibs/win32unix/stat.c
  11. index 4d910e1..50da744 100644
  12. --- a/otherlibs/win32unix/stat.c
  13. +++ b/otherlibs/win32unix/stat.c
  14. @@ -46,14 +46,14 @@ static int file_kind_table[] = {
  15. S_IFREG, S_IFDIR, S_IFCHR, S_IFBLK, S_IFLNK, S_IFIFO, S_IFSOCK
  16. };
  17.  
  18. -static value stat_aux(int use_64, struct _stat64 *buf)
  19. +static value stat_aux(int use_64, __int64 st_ino, struct _stat64 *buf)
  20. {
  21. CAMLparam0 ();
  22. CAMLlocal1 (v);
  23.  
  24. v = caml_alloc (12, 0);
  25. Store_field (v, 0, Val_int (buf->st_dev));
  26. - Store_field (v, 1, Val_int (buf->st_ino));
  27. + Store_field (v, 1, Val_int (st_ino ? st_ino & Max_long : buf->st_ino));
  28. Store_field (v, 2, cst_to_constr (buf->st_mode & S_IFMT, file_kind_table,
  29. sizeof(file_kind_table) / sizeof(int), 0));
  30. Store_field (v, 3, Val_int(buf->st_mode & 07777));
  31. @@ -149,32 +149,16 @@ int IsRootUNCName(char* path)
  32. return 0;
  33. }
  34.  
  35. -static int do_stat(int do_lstat, int use_64, char* path, mlsize_t l, struct _stat64* res)
  36. +static int do_stat(int do_lstat, int use_64, char* path, mlsize_t l, __int64* st_ino, struct _stat64* res)
  37. {
  38. BY_HANDLE_FILE_INFORMATION info;
  39. - int drive, i;
  40. + int i;
  41. char* ptr;
  42. char c;
  43. HANDLE h;
  44. unsigned short mode;
  45. int is_symlink = 0;
  46.  
  47. - /*
  48. - * Get the drive number
  49. - */
  50. - if (l > 1 && path[1] == ':') {
  51. - if (path[2]) {
  52. - drive = tolower(*path) - 'a';
  53. - }
  54. - else {
  55. - errno = ENOENT;
  56. - return 0;
  57. - }
  58. - }
  59. - else {
  60. - drive = _getdrive() - 1;
  61. - }
  62. -
  63. caml_enter_blocking_section();
  64. 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);
  65. caml_leave_blocking_section();
  66. @@ -199,6 +183,23 @@ static int do_stat(int do_lstat, int use_64, char* path, mlsize_t l, struct _sta
  67. info.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
  68. res->st_size = 0;
  69. res->st_nlink = 1;
  70. +
  71. + /*
  72. + * Get the drive number
  73. + */
  74. + if (l > 1 && path[1] == ':') {
  75. + if (path[2]) {
  76. + res->st_dev = tolower(*path) - 'a';
  77. + }
  78. + else {
  79. + errno = ENOENT;
  80. + return 0;
  81. + }
  82. + }
  83. + else {
  84. + res->st_dev = _getdrive() - 1;
  85. + }
  86. +
  87. /*
  88. * January 1, 1980 UTC
  89. */
  90. @@ -283,9 +284,11 @@ static int do_stat(int do_lstat, int use_64, char* path, mlsize_t l, struct _sta
  91. }
  92.  
  93. /*
  94. - * Note MS CRT (still) puts st_nlink = 1
  95. + * Note MS CRT (still) puts st_nlink = 1 and gives st_ino = 0
  96. */
  97. res->st_nlink = info.nNumberOfLinks;
  98. + res->st_dev = info.dwVolumeSerialNumber;
  99. + *st_ino = ((__int64)(info.nFileIndexHigh)) << 32 | ((__int64)info.nFileIndexLow);
  100. }
  101.  
  102. 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);
  103. @@ -296,7 +299,7 @@ static int do_stat(int do_lstat, int use_64, char* path, mlsize_t l, struct _sta
  104. mode |= (mode & 0700) >> 6;
  105. res->st_mode = mode;
  106. res->st_uid = res->st_gid = res->st_ino = 0;
  107. - res->st_rdev = res->st_dev = drive;
  108. + res->st_rdev = res->st_dev;
  109.  
  110. return 1;
  111. }
  112. @@ -304,37 +307,41 @@ static int do_stat(int do_lstat, int use_64, char* path, mlsize_t l, struct _sta
  113. CAMLprim value unix_stat(value path)
  114. {
  115. struct _stat64 buf;
  116. - if (!do_stat(0, 0, String_val(path), caml_string_length(path), &buf)) {
  117. + __int64 st_ino;
  118. + if (!do_stat(0, 0, String_val(path), caml_string_length(path), &st_ino, &buf)) {
  119. uerror("stat", path);
  120. }
  121. - return stat_aux(0, &buf);
  122. + return stat_aux(0, st_ino, &buf);
  123. }
  124.  
  125. CAMLprim value unix_stat_64(value path)
  126. {
  127. struct _stat64 buf;
  128. - if (!do_stat(0, 1, String_val(path), caml_string_length(path), &buf)) {
  129. + __int64 st_ino;
  130. + if (!do_stat(0, 1, String_val(path), caml_string_length(path), &st_ino, &buf)) {
  131. uerror("stat", path);
  132. }
  133. - return stat_aux(1, &buf);
  134. + return stat_aux(1, st_ino, &buf);
  135. }
  136.  
  137. CAMLprim value unix_lstat(value path)
  138. {
  139. struct _stat64 buf;
  140. - if (!do_stat(1, 0, String_val(path), caml_string_length(path), &buf)) {
  141. + __int64 st_ino;
  142. + if (!do_stat(1, 0, String_val(path), caml_string_length(path), &st_ino, &buf)) {
  143. uerror("lstat", path);
  144. }
  145. - return stat_aux(0, &buf);
  146. + return stat_aux(0, st_ino, &buf);
  147. }
  148.  
  149. CAMLprim value unix_lstat_64(value path)
  150. {
  151. struct _stat64 buf;
  152. - if (!do_stat(1, 1, String_val(path), caml_string_length(path), &buf)) {
  153. + __int64 st_ino;
  154. + if (!do_stat(1, 1, String_val(path), caml_string_length(path), &st_ino, &buf)) {
  155. uerror("lstat", path);
  156. }
  157. - return stat_aux(1, &buf);
  158. + return stat_aux(1, st_ino, &buf);
  159. }
  160.  
  161. CAMLprim value unix_fstat(value handle)
  162. @@ -348,7 +355,7 @@ CAMLprim value unix_fstat(value handle)
  163. win32_maperr(ERROR_ARITHMETIC_OVERFLOW);
  164. uerror("fstat", Nothing);
  165. }
  166. - return stat_aux(0, &buf);
  167. + return stat_aux(0, 0, &buf);
  168. }
  169.  
  170. CAMLprim value unix_fstat_64(value handle)
  171. @@ -358,5 +365,5 @@ CAMLprim value unix_fstat_64(value handle)
  172.  
  173. ret = _fstat64(win_CRT_fd_of_filedescr(handle), &buf);
  174. if (ret == -1) uerror("fstat", Nothing);
  175. - return stat_aux(1, &buf);
  176. + return stat_aux(1, 0, &buf);
  177. }
  178. --
  179. 2.4.4.windows.2
Add Comment
Please, Sign In to add comment