stathelper.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* copied from Python-2.7.2/Modules/posixmodule.c */
  2. #include "structseq.h"
  3. #define STRUCT_STAT struct stat
  4. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  5. #define ST_BLKSIZE_IDX 13
  6. #else
  7. #define ST_BLKSIZE_IDX 12
  8. #endif
  9. #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
  10. #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
  11. #else
  12. #define ST_BLOCKS_IDX ST_BLKSIZE_IDX
  13. #endif
  14. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  15. #define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
  16. #else
  17. #define ST_RDEV_IDX ST_BLOCKS_IDX
  18. #endif
  19. #ifdef HAVE_STRUCT_STAT_ST_FLAGS
  20. #define ST_FLAGS_IDX (ST_RDEV_IDX+1)
  21. #else
  22. #define ST_FLAGS_IDX ST_RDEV_IDX
  23. #endif
  24. #ifdef HAVE_STRUCT_STAT_ST_GEN
  25. #define ST_GEN_IDX (ST_FLAGS_IDX+1)
  26. #else
  27. #define ST_GEN_IDX ST_FLAGS_IDX
  28. #endif
  29. #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
  30. #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
  31. #else
  32. #define ST_BIRTHTIME_IDX ST_GEN_IDX
  33. #endif
  34. static PyObject* posixmodule = NULL;
  35. static PyTypeObject* pStatResultType = NULL;
  36. static PyObject* import_posixmodule(void)
  37. {
  38. if (!posixmodule) {
  39. posixmodule = PyImport_ImportModule("posix");
  40. }
  41. return posixmodule;
  42. }
  43. static PyObject* import_StatResultType(void)
  44. {
  45. PyObject* p = NULL;
  46. if (!pStatResultType) {
  47. PyObject* module;
  48. module = import_posixmodule();
  49. if (module) {
  50. p = PyObject_GetAttrString(module, "stat_result");
  51. }
  52. }
  53. return p;
  54. }
  55. static void
  56. fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
  57. {
  58. PyObject *fval,*ival;
  59. #if SIZEOF_TIME_T > SIZEOF_LONG
  60. ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
  61. #else
  62. ival = PyInt_FromLong((long)sec);
  63. #endif
  64. if (!ival)
  65. return;
  66. fval = PyFloat_FromDouble(sec + 1e-9*nsec);
  67. PyStructSequence_SET_ITEM(v, index, ival);
  68. PyStructSequence_SET_ITEM(v, index+3, fval);
  69. }
  70. /* pack a system stat C structure into the Python stat tuple
  71. (used by posix_stat() and posix_fstat()) */
  72. static PyObject*
  73. _pystat_fromstructstat(STRUCT_STAT *st)
  74. {
  75. unsigned long ansec, mnsec, cnsec;
  76. PyObject *v;
  77. PyTypeObject* StatResultType = (PyTypeObject*)import_StatResultType();
  78. if (StatResultType == NULL) {
  79. return NULL;
  80. }
  81. v = PyStructSequence_New(StatResultType);
  82. if (v == NULL)
  83. return NULL;
  84. PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode));
  85. #ifdef HAVE_LARGEFILE_SUPPORT
  86. PyStructSequence_SET_ITEM(v, 1,
  87. PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
  88. #else
  89. PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino));
  90. #endif
  91. #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
  92. PyStructSequence_SET_ITEM(v, 2,
  93. PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
  94. #else
  95. PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev));
  96. #endif
  97. PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink));
  98. PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid));
  99. PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid));
  100. #ifdef HAVE_LARGEFILE_SUPPORT
  101. PyStructSequence_SET_ITEM(v, 6,
  102. PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
  103. #else
  104. PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size));
  105. #endif
  106. #if defined(HAVE_STAT_TV_NSEC)
  107. ansec = st->st_atim.tv_nsec;
  108. mnsec = st->st_mtim.tv_nsec;
  109. cnsec = st->st_ctim.tv_nsec;
  110. #elif defined(HAVE_STAT_TV_NSEC2)
  111. ansec = st->st_atimespec.tv_nsec;
  112. mnsec = st->st_mtimespec.tv_nsec;
  113. cnsec = st->st_ctimespec.tv_nsec;
  114. #elif defined(HAVE_STAT_NSEC)
  115. ansec = st->st_atime_nsec;
  116. mnsec = st->st_mtime_nsec;
  117. cnsec = st->st_ctime_nsec;
  118. #else
  119. ansec = mnsec = cnsec = 0;
  120. #endif
  121. fill_time(v, 7, st->st_atime, ansec);
  122. fill_time(v, 8, st->st_mtime, mnsec);
  123. fill_time(v, 9, st->st_ctime, cnsec);
  124. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  125. PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
  126. PyInt_FromLong((long)st->st_blksize));
  127. #endif
  128. #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
  129. PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
  130. PyInt_FromLong((long)st->st_blocks));
  131. #endif
  132. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  133. PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
  134. PyInt_FromLong((long)st->st_rdev));
  135. #endif
  136. #ifdef HAVE_STRUCT_STAT_ST_GEN
  137. PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
  138. PyInt_FromLong((long)st->st_gen));
  139. #endif
  140. #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
  141. {
  142. PyObject *val;
  143. unsigned long bsec,bnsec;
  144. bsec = (long)st->st_birthtime;
  145. #ifdef HAVE_STAT_TV_NSEC2
  146. bnsec = st->st_birthtimespec.tv_nsec;
  147. #else
  148. bnsec = 0;
  149. #endif
  150. val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
  151. PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
  152. val);
  153. }
  154. #endif
  155. #ifdef HAVE_STRUCT_STAT_ST_FLAGS
  156. PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
  157. PyInt_FromLong((long)st->st_flags));
  158. #endif
  159. if (PyErr_Occurred()) {
  160. Py_DECREF(v);
  161. return NULL;
  162. }
  163. return v;
  164. }