sys-stat.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * <sys/stat.h> wrapper functions.
  3. *
  4. * Authors:
  5. * Jonathan Pryor ([email protected])
  6. *
  7. * Copyright (C) 2004-2006 Jonathan Pryor
  8. */
  9. #ifndef _GNU_SOURCE
  10. #define _GNU_SOURCE
  11. #endif /* ndef _GNU_SOURCE */
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #include <unistd.h>
  15. #include <fcntl.h>
  16. #include <errno.h>
  17. #include "map.h"
  18. #include "mph.h"
  19. G_BEGIN_DECLS
  20. int
  21. Mono_Posix_FromStat (struct Mono_Posix_Stat *from, void *_to)
  22. {
  23. struct stat *to = _to;
  24. memset (to, 0, sizeof(*to));
  25. to->st_dev = from->st_dev;
  26. to->st_ino = from->st_ino;
  27. unsigned int to_st_mode;
  28. if (Mono_Posix_FromFilePermissions (from->st_mode, &to_st_mode) != 0) {
  29. return -1;
  30. }
  31. to->st_mode = to_st_mode;
  32. to->st_nlink = from->st_nlink;
  33. to->st_uid = from->st_uid;
  34. to->st_gid = from->st_gid;
  35. to->st_rdev = from->st_rdev;
  36. to->st_size = from->st_size;
  37. to->st_blksize = from->st_blksize;
  38. to->st_blocks = from->st_blocks;
  39. to->st_atime = from->st_atime_;
  40. to->st_mtime = from->st_mtime_;
  41. to->st_ctime = from->st_ctime_;
  42. #ifdef HAVE_STRUCT_STAT_ST_ATIM
  43. to->st_atim.tv_nsec = from->st_atime_nsec;
  44. #endif
  45. #ifdef HAVE_STRUCT_STAT_ST_MTIM
  46. to->st_mtim.tv_nsec = from->st_mtime_nsec;
  47. #endif
  48. #ifdef HAVE_STRUCT_STAT_ST_CTIM
  49. to->st_ctim.tv_nsec = from->st_ctime_nsec;
  50. #endif
  51. return 0;
  52. }
  53. int
  54. Mono_Posix_ToStat (void *_from, struct Mono_Posix_Stat *to)
  55. {
  56. struct stat *from = _from;
  57. memset (to, 0, sizeof(*to));
  58. to->st_dev = from->st_dev;
  59. to->st_ino = from->st_ino;
  60. if (Mono_Posix_ToFilePermissions (from->st_mode, &to->st_mode) != 0) {
  61. return -1;
  62. }
  63. to->st_nlink = from->st_nlink;
  64. to->st_uid = from->st_uid;
  65. to->st_gid = from->st_gid;
  66. to->st_rdev = from->st_rdev;
  67. to->st_size = from->st_size;
  68. to->st_blksize = from->st_blksize;
  69. to->st_blocks = from->st_blocks;
  70. to->st_atime_ = from->st_atime;
  71. to->st_mtime_ = from->st_mtime;
  72. to->st_ctime_ = from->st_ctime;
  73. #ifdef HAVE_STRUCT_STAT_ST_ATIM
  74. to->st_atime_nsec = from->st_atim.tv_nsec;
  75. #endif
  76. #ifdef HAVE_STRUCT_STAT_ST_MTIM
  77. to->st_mtime_nsec = from->st_mtim.tv_nsec;
  78. #endif
  79. #ifdef HAVE_STRUCT_STAT_ST_CTIM
  80. to->st_ctime_nsec = from->st_ctim.tv_nsec;
  81. #endif
  82. return 0;
  83. }
  84. gint32
  85. Mono_Posix_Syscall_stat (const char *file_name, struct Mono_Posix_Stat *buf)
  86. {
  87. int r;
  88. struct stat _buf;
  89. if (buf == NULL) {
  90. errno = EFAULT;
  91. return -1;
  92. }
  93. r = stat (file_name, &_buf);
  94. if (r != -1 && Mono_Posix_ToStat (&_buf, buf) == -1)
  95. r = -1;
  96. return r;
  97. }
  98. gint32
  99. Mono_Posix_Syscall_fstat (int filedes, struct Mono_Posix_Stat *buf)
  100. {
  101. int r;
  102. struct stat _buf;
  103. if (buf == NULL) {
  104. errno = EFAULT;
  105. return -1;
  106. }
  107. r = fstat (filedes, &_buf);
  108. if (r != -1 && Mono_Posix_ToStat (&_buf, buf) == -1)
  109. r = -1;
  110. return r;
  111. }
  112. gint32
  113. Mono_Posix_Syscall_lstat (const char *file_name, struct Mono_Posix_Stat *buf)
  114. {
  115. int r;
  116. struct stat _buf;
  117. if (buf == NULL) {
  118. errno = EFAULT;
  119. return -1;
  120. }
  121. r = lstat (file_name, &_buf);
  122. if (r != -1 && Mono_Posix_ToStat (&_buf, buf) == -1)
  123. r = -1;
  124. return r;
  125. }
  126. #ifdef HAVE_FSTATAT
  127. gint32
  128. Mono_Posix_Syscall_fstatat (gint32 dirfd, const char *file_name, struct Mono_Posix_Stat *buf, gint32 flags)
  129. {
  130. int r;
  131. struct stat _buf;
  132. if (Mono_Posix_FromAtFlags (flags, &flags) == -1)
  133. return -1;
  134. if (buf == NULL) {
  135. errno = EFAULT;
  136. return -1;
  137. }
  138. r = fstatat (dirfd, file_name, &_buf, flags);
  139. if (r != -1 && Mono_Posix_ToStat (&_buf, buf) == -1)
  140. r = -1;
  141. return r;
  142. }
  143. #endif
  144. gint32
  145. Mono_Posix_Syscall_mknod (const char *pathname, guint32 mode, mph_dev_t dev)
  146. {
  147. if (Mono_Posix_FromFilePermissions (mode, &mode) == -1)
  148. return -1;
  149. return mknod (pathname, mode, dev);
  150. }
  151. #ifdef HAVE_MKNODAT
  152. gint32
  153. Mono_Posix_Syscall_mknodat (int dirfd, const char *pathname, guint32 mode, mph_dev_t dev)
  154. {
  155. if (Mono_Posix_FromFilePermissions (mode, &mode) == -1)
  156. return -1;
  157. return mknodat (dirfd, pathname, mode, dev);
  158. }
  159. #endif
  160. G_END_DECLS
  161. gint64
  162. Mono_Posix_Syscall_get_utime_now ()
  163. {
  164. #ifdef UTIME_NOW
  165. return UTIME_NOW;
  166. #else
  167. return -1;
  168. #endif
  169. }
  170. gint64
  171. Mono_Posix_Syscall_get_utime_omit ()
  172. {
  173. #ifdef UTIME_OMIT
  174. return UTIME_OMIT;
  175. #else
  176. return -1;
  177. #endif
  178. }
  179. static inline struct timespec*
  180. copy_utimens (struct timespec* to, struct Mono_Posix_Timespec *from)
  181. {
  182. if (from) {
  183. to[0].tv_sec = from[0].tv_sec;
  184. to[0].tv_nsec = from[0].tv_nsec;
  185. to[1].tv_sec = from[1].tv_sec;
  186. to[1].tv_nsec = from[1].tv_nsec;
  187. return to;
  188. }
  189. return NULL;
  190. }
  191. #ifdef HAVE_FUTIMENS
  192. gint32
  193. Mono_Posix_Syscall_futimens(int fd, struct Mono_Posix_Timespec *tv)
  194. {
  195. struct timespec _tv[2];
  196. struct timespec *ptv;
  197. ptv = copy_utimens (_tv, tv);
  198. return futimens (fd, ptv);
  199. }
  200. #endif /* def HAVE_FUTIMENS */
  201. #ifdef HAVE_UTIMENSAT
  202. gint32
  203. Mono_Posix_Syscall_utimensat(int dirfd, const char *pathname, struct Mono_Posix_Timespec *tv, int flags)
  204. {
  205. struct timespec _tv[2];
  206. struct timespec *ptv;
  207. ptv = copy_utimens (_tv, tv);
  208. return utimensat (dirfd, pathname, ptv, flags);
  209. }
  210. #endif /* def HAVE_UTIMENSAT */
  211. /*
  212. * vim: noexpandtab
  213. */