123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- /* copied from Python-2.7.2/Modules/posixmodule.c */
- #include "structseq.h"
- #define STRUCT_STAT struct stat
- #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
- #define ST_BLKSIZE_IDX 13
- #else
- #define ST_BLKSIZE_IDX 12
- #endif
- #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
- #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
- #else
- #define ST_BLOCKS_IDX ST_BLKSIZE_IDX
- #endif
- #ifdef HAVE_STRUCT_STAT_ST_RDEV
- #define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
- #else
- #define ST_RDEV_IDX ST_BLOCKS_IDX
- #endif
- #ifdef HAVE_STRUCT_STAT_ST_FLAGS
- #define ST_FLAGS_IDX (ST_RDEV_IDX+1)
- #else
- #define ST_FLAGS_IDX ST_RDEV_IDX
- #endif
- #ifdef HAVE_STRUCT_STAT_ST_GEN
- #define ST_GEN_IDX (ST_FLAGS_IDX+1)
- #else
- #define ST_GEN_IDX ST_FLAGS_IDX
- #endif
- #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
- #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
- #else
- #define ST_BIRTHTIME_IDX ST_GEN_IDX
- #endif
- static PyObject* posixmodule = NULL;
- static PyTypeObject* pStatResultType = NULL;
- static PyObject* import_posixmodule(void)
- {
- if (!posixmodule) {
- posixmodule = PyImport_ImportModule("posix");
- }
- return posixmodule;
- }
- static PyObject* import_StatResultType(void)
- {
- PyObject* p = NULL;
- if (!pStatResultType) {
- PyObject* module;
- module = import_posixmodule();
- if (module) {
- p = PyObject_GetAttrString(module, "stat_result");
- }
- }
- return p;
- }
- static void
- fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
- {
- PyObject *fval,*ival;
- #if SIZEOF_TIME_T > SIZEOF_LONG
- ival = PyLong_FromLongLong((PY_LONG_LONG)sec);
- #else
- ival = PyInt_FromLong((long)sec);
- #endif
- if (!ival)
- return;
- fval = PyFloat_FromDouble(sec + 1e-9*nsec);
- PyStructSequence_SET_ITEM(v, index, ival);
- PyStructSequence_SET_ITEM(v, index+3, fval);
- }
- /* pack a system stat C structure into the Python stat tuple
- (used by posix_stat() and posix_fstat()) */
- static PyObject*
- _pystat_fromstructstat(STRUCT_STAT *st)
- {
- unsigned long ansec, mnsec, cnsec;
- PyObject *v;
- PyTypeObject* StatResultType = (PyTypeObject*)import_StatResultType();
- if (StatResultType == NULL) {
- return NULL;
- }
- v = PyStructSequence_New(StatResultType);
- if (v == NULL)
- return NULL;
- PyStructSequence_SET_ITEM(v, 0, PyInt_FromLong((long)st->st_mode));
- #ifdef HAVE_LARGEFILE_SUPPORT
- PyStructSequence_SET_ITEM(v, 1,
- PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
- #else
- PyStructSequence_SET_ITEM(v, 1, PyInt_FromLong((long)st->st_ino));
- #endif
- #if defined(HAVE_LONG_LONG) && !defined(MS_WINDOWS)
- PyStructSequence_SET_ITEM(v, 2,
- PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
- #else
- PyStructSequence_SET_ITEM(v, 2, PyInt_FromLong((long)st->st_dev));
- #endif
- PyStructSequence_SET_ITEM(v, 3, PyInt_FromLong((long)st->st_nlink));
- PyStructSequence_SET_ITEM(v, 4, PyInt_FromLong((long)st->st_uid));
- PyStructSequence_SET_ITEM(v, 5, PyInt_FromLong((long)st->st_gid));
- #ifdef HAVE_LARGEFILE_SUPPORT
- PyStructSequence_SET_ITEM(v, 6,
- PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
- #else
- PyStructSequence_SET_ITEM(v, 6, PyInt_FromLong(st->st_size));
- #endif
- #if defined(HAVE_STAT_TV_NSEC)
- ansec = st->st_atim.tv_nsec;
- mnsec = st->st_mtim.tv_nsec;
- cnsec = st->st_ctim.tv_nsec;
- #elif defined(HAVE_STAT_TV_NSEC2)
- ansec = st->st_atimespec.tv_nsec;
- mnsec = st->st_mtimespec.tv_nsec;
- cnsec = st->st_ctimespec.tv_nsec;
- #elif defined(HAVE_STAT_NSEC)
- ansec = st->st_atime_nsec;
- mnsec = st->st_mtime_nsec;
- cnsec = st->st_ctime_nsec;
- #else
- ansec = mnsec = cnsec = 0;
- #endif
- fill_time(v, 7, st->st_atime, ansec);
- fill_time(v, 8, st->st_mtime, mnsec);
- fill_time(v, 9, st->st_ctime, cnsec);
- #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
- PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
- PyInt_FromLong((long)st->st_blksize));
- #endif
- #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
- PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
- PyInt_FromLong((long)st->st_blocks));
- #endif
- #ifdef HAVE_STRUCT_STAT_ST_RDEV
- PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
- PyInt_FromLong((long)st->st_rdev));
- #endif
- #ifdef HAVE_STRUCT_STAT_ST_GEN
- PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
- PyInt_FromLong((long)st->st_gen));
- #endif
- #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
- {
- PyObject *val;
- unsigned long bsec,bnsec;
- bsec = (long)st->st_birthtime;
- #ifdef HAVE_STAT_TV_NSEC2
- bnsec = st->st_birthtimespec.tv_nsec;
- #else
- bnsec = 0;
- #endif
- val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
- PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
- val);
- }
- #endif
- #ifdef HAVE_STRUCT_STAT_ST_FLAGS
- PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
- PyInt_FromLong((long)st->st_flags));
- #endif
- if (PyErr_Occurred()) {
- Py_DECREF(v);
- return NULL;
- }
- return v;
- }
|