sys-statvfs.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * <sys/sendfile.h> wrapper functions.
  3. *
  4. * Authors:
  5. * Jonathan Pryor ([email protected])
  6. *
  7. * Copyright (C) 2004-2006 Jonathan Pryor
  8. */
  9. #include <errno.h>
  10. #include <string.h>
  11. #include "mph.h"
  12. #include "map.h"
  13. #ifdef HAVE_SYS_STATVFS_H
  14. #include <sys/statvfs.h>
  15. #endif /* def HAVE_SYS_STATVFS_H */
  16. #ifdef HAVE_GETFSSTAT
  17. #include <sys/param.h>
  18. #include <sys/ucred.h>
  19. #include <sys/mount.h>
  20. #include <unistd.h> /* for pathconf */
  21. #endif /* def HAVE_GETFSSTAT */
  22. G_BEGIN_DECLS
  23. #ifdef HAVE_SYS_STATVFS_H
  24. int
  25. Mono_Posix_ToStatvfs (void *_from, struct Mono_Posix_Statvfs *to)
  26. {
  27. struct statvfs *from = _from;
  28. to->f_bsize = from->f_bsize;
  29. to->f_frsize = from->f_frsize;
  30. to->f_blocks = from->f_blocks;
  31. to->f_bfree = from->f_bfree;
  32. to->f_bavail = from->f_bavail;
  33. to->f_files = from->f_files;
  34. to->f_ffree = from->f_ffree;
  35. to->f_favail = from->f_favail;
  36. to->f_fsid = from->f_fsid;
  37. to->f_namemax = from->f_namemax;
  38. if (Mono_Posix_ToMountFlags (from->f_flag, &to->f_flag) != 0)
  39. return -1;
  40. return 0;
  41. }
  42. int
  43. Mono_Posix_FromStatvfs (struct Mono_Posix_Statvfs *from, void *_to)
  44. {
  45. struct statvfs *to = _to;
  46. guint64 flag;
  47. to->f_bsize = from->f_bsize;
  48. to->f_frsize = from->f_frsize;
  49. to->f_blocks = from->f_blocks;
  50. to->f_bfree = from->f_bfree;
  51. to->f_bavail = from->f_bavail;
  52. to->f_files = from->f_files;
  53. to->f_ffree = from->f_ffree;
  54. to->f_favail = from->f_favail;
  55. to->f_fsid = from->f_fsid;
  56. to->f_namemax = from->f_namemax;
  57. if (Mono_Posix_FromMountFlags (from->f_flag, &flag) != 0)
  58. return -1;
  59. to->f_flag = flag;
  60. return 0;
  61. }
  62. #endif /* ndef HAVE_SYS_STATVFS_H */
  63. /*
  64. * System V-compatible definitions
  65. */
  66. #ifdef HAVE_STATVFS
  67. gint32
  68. Mono_Posix_Syscall_statvfs (const char *path, struct Mono_Posix_Statvfs *buf)
  69. {
  70. struct statvfs s;
  71. int r;
  72. if (buf == NULL) {
  73. errno = EFAULT;
  74. return -1;
  75. }
  76. if ((r = statvfs (path, &s)) == 0)
  77. r = Mono_Posix_ToStatvfs (&s, buf);
  78. return r;
  79. }
  80. #endif /* ndef HAVA_STATVFS */
  81. #ifdef HAVE_FSTATVFS
  82. gint32
  83. Mono_Posix_Syscall_fstatvfs (gint32 fd, struct Mono_Posix_Statvfs *buf)
  84. {
  85. struct statvfs s;
  86. int r;
  87. if (buf == NULL) {
  88. errno = EFAULT;
  89. return -1;
  90. }
  91. if ((r = fstatvfs (fd, &s)) == 0)
  92. r = Mono_Posix_ToStatvfs (&s, buf);
  93. return r;
  94. }
  95. #endif /* ndef HAVA_FSTATVFS */
  96. /*
  97. * BSD-compatible definitions.
  98. *
  99. * Linux also provides these, but are deprecated in favor of (f)statvfs.
  100. */
  101. #if (defined (HAVE_STATFS) || defined (HAVE_FSTATFS)) && !defined (HAVE_STATVFS)
  102. int
  103. Mono_Posix_ToStatvfs (void *_from, struct Mono_Posix_Statvfs *to)
  104. {
  105. struct statfs *from = _from;
  106. to->f_bsize = from->f_bsize;
  107. to->f_frsize = from->f_bsize;
  108. to->f_blocks = from->f_blocks;
  109. to->f_bfree = from->f_bfree;
  110. to->f_bavail = from->f_bavail;
  111. to->f_files = from->f_files;
  112. to->f_ffree = from->f_ffree;
  113. to->f_favail = from->f_ffree; /* OSX doesn't have f_avail */
  114. // from->f_fsid is an int32[2], to->f_fsid is a uint64,
  115. // so this shouldn't lose anything.
  116. memcpy (&to->f_fsid, &from->f_fsid, sizeof(to->f_fsid));
  117. if (Mono_Posix_ToMountFlags (from->f_flags, &to->f_flag) != 0)
  118. return -1;
  119. return 0;
  120. }
  121. int
  122. Mono_Posix_FromStatvfs (struct Mono_Posix_Statvfs *from, void *_to)
  123. {
  124. struct statfs *to = _to;
  125. guint64 flag;
  126. to->f_bsize = from->f_bsize;
  127. to->f_blocks = from->f_blocks;
  128. to->f_bfree = from->f_bfree;
  129. to->f_bavail = from->f_bavail;
  130. to->f_files = from->f_files;
  131. to->f_ffree = from->f_ffree;
  132. // from->f_fsid is an int32[2], to->f_fsid is a uint64,
  133. // so this shouldn't lose anything.
  134. memcpy (&to->f_fsid, &from->f_fsid, sizeof(to->f_fsid));
  135. if (Mono_Posix_FromMountFlags (from->f_flag, &flag) != 0)
  136. return -1;
  137. to->f_flags = flag;
  138. return 0;
  139. }
  140. static void
  141. set_namemax (const char *path, struct Mono_Posix_Statvfs *buf)
  142. {
  143. buf->f_namemax = pathconf (path, _PC_NAME_MAX);
  144. }
  145. static void
  146. set_fnamemax (int fd, struct Mono_Posix_Statvfs *buf)
  147. {
  148. buf->f_namemax = fpathconf (fd, _PC_NAME_MAX);
  149. }
  150. #endif /* (def HAVE_STATFS || def HAVE_FSTATFS) && !def HAVE_STATVFS */
  151. #if !defined (HAVE_STATVFS) && defined (HAVE_STATFS)
  152. gint32
  153. Mono_Posix_Syscall_statvfs (const char *path, struct Mono_Posix_Statvfs *buf)
  154. {
  155. struct statfs s;
  156. int r;
  157. if (buf == NULL) {
  158. errno = EFAULT;
  159. return -1;
  160. }
  161. if ((r = statfs (path, &s)) == 0 &&
  162. (r = Mono_Posix_ToStatvfs (&s, buf)) == 0) {
  163. set_namemax (path, buf);
  164. }
  165. return r;
  166. }
  167. #endif /* !def HAVE_STATVFS && def HAVE_STATFS */
  168. #if !defined (HAVE_STATVFS) && defined (HAVE_STATFS)
  169. gint32
  170. Mono_Posix_Syscall_fstatvfs (gint32 fd, struct Mono_Posix_Statvfs *buf)
  171. {
  172. struct statfs s;
  173. int r;
  174. if (buf == NULL) {
  175. errno = EFAULT;
  176. return -1;
  177. }
  178. if ((r = fstatfs (fd, &s)) == 0 &&
  179. (r = Mono_Posix_ToStatvfs (&s, buf)) == 0) {
  180. set_fnamemax (fd, buf);
  181. }
  182. return r;
  183. }
  184. #endif /* !def HAVE_FSTATVFS && def HAVE_STATFS */
  185. G_END_DECLS
  186. /*
  187. * vim: noexpandtab
  188. */