} } } code = attribute_data_to_stat(&info, result); if (code < 0) return code; /* Set IFEXEC if it is an .exe, .bat, ... */ dot = wcsrchr(path, '.'); if (dot) { if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 || _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0) result->st_mode |= 0111; } return code;} static intwin32_fstat(int file_number, struct win32_stat *result){ BY_HANDLE_FILE_INFORMATION info; HANDLE h; int type; h = (HANDLE)_get_osfhandle(file_number); /* Protocol violation: we explicitly clear errno, instead of setting it to a POSIX error. Callers should use GetLastError. */ errno = 0; if (h == INVALID_HANDLE_VALUE) { /* This is really a C library error (invalid file handle). We set the Win32 error to the closes one matching. */ SetLastError(ERROR_INVALID_HANDLE); return -1; } memset(result, 0, sizeof(*result)); type = GetFileType(h); if (type == FILE_TYPE_UNKNOWN) { DWORD error = GetLastError(); if (error != 0) { return -1; } /* else: valid but unknown file */ } if (type != FILE_TYPE_DISK) { if (type == FILE_TYPE_CHAR) result->st_mode = _S_IFCHR; else if (type == FILE_TYPE_PIPE) result->st_mode = _S_IFIFO; return 0; } if (!GetFileInformationByHandle(h, &info)) { return -1; } /* similar to stat() */ result->st_mode = attributes_to_mode(info.dwFileAttributes); result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow; FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); /* specific to fstat() */ result->st_nlink = info.nNumberOfLinks; result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; return 0;} #endif /* MS_WINDOWS */ PyDoc_STRVAR(stat_result__doc__,"stat_result: Result from stat or lstat.\n\n\This object may be accessed either as a tuple of\n\ (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\\n\Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\or st_flags, they are available as attributes only.\n\\n\See os.stat for more information."); static PyStructSequence_Field stat_result_fields[] = { {"st_mode", "protection bits"}, {"st_ino", "inode"}, {"st_dev", "device"}, {"st_nlink", "number of hard links"}, {"st_uid", "user ID of owner"}, {"st_gid", "group ID of owner"}, {"st_size", "total size, in bytes"}, /* The NULL is replaced with PyStructSequence_UnnamedField later. */ {NULL, "integer time of last access"}, {NULL, "integer time of last modification"}, {NULL, "integer time of last change"}, {"st_atime", "time of last access"}, {"st_mtime", "time of last modification"}, {"st_ctime", "time of last change"},#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE {"st_blksize", "blocksize for filesystem I/O"},#endif#ifdef HAVE_STRUCT_STAT_ST_BLOCKS {"st_blocks", "number of blocks allocated"},#endif#ifdef HAVE_STRUCT_STAT_ST_RDEV {"st_rdev", "device type (if inode device)"},#endif#ifdef HAVE_STRUCT_STAT_ST_FLAGS {"st_flags", "user defined flags for file"},#endif#ifdef HAVE_STRUCT_STAT_ST_GEN {"st_gen", "generation number"},#endif#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME {"st_birthtime", "time of creation"},#endif {0}}; #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 PyStructSequence_Desc stat_result_desc = { "stat_result", /* name */ stat_result__doc__, /* doc */ stat_result_fields, 10}; PyDoc_STRVAR(statvfs_result__doc__,"statvfs_result: Result from statvfs or fstatvfs.\n\n\This object may be accessed either as a tuple of\n\ (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\\n\See os.statvfs for more information."); static PyStructSequence_Field statvfs_result_fields[] = { {"f_bsize", }, {"f_frsize", }, {"f_blocks", }, {"f_bfree", }, {"f_bavail", }, {"f_files", }, {"f_ffree", }, {"f_favail", }, {"f_flag", }, {"f_namemax",}, {0}}; static PyStructSequence_Desc statvfs_result_desc = { "statvfs_result", /* name */ statvfs_result__doc__, /* doc */ statvfs_result_fields, 10}; static int initialized;static PyTypeObject StatResultType;static PyTypeObject StatVFSResultType;static newfunc structseq_new; static PyObject *statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds){ PyStructSequence *result; int i; result = (PyStructSequence*)structseq_new(type, args, kwds); if (!result) return NULL; /* If we have been initialized from a tuple, st_?time might be set to None. Initialize it from the int slots. */ for (i = 7; i <= 9; i++) { if (result->ob_item[i+3] == Py_None) { Py_DECREF(Py_None); Py_INCREF(result->ob_item[i]); result->ob_item[i+3] = result->ob_item[i]; } } return (PyObject*)result;} /* If true, st_?time is float. */static int _stat_float_times = 1; PyDoc_STRVAR(stat_float_times__doc__,"stat_float_times([newval]) -> oldval\n\n\Determine whether os.[lf]stat represents time stamps as float objects.\n\If newval is True, future calls to stat() return floats, if it is False,\n\future calls return ints. \n\If newval is omitted, return the current setting.\n"); static PyObject*stat_float_times(PyObject* self, PyObject *args){ int newval = -1; if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) return NULL; if (newval == -1) /* Return old value */ return PyBool_FromLong(_stat_float_times); _stat_float_times = newval; Py_INCREF(Py_None); return Py_None;} static voidfill_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; if (_stat_float_times) { fval = PyFloat_FromDouble(sec + 1e-9*nsec); } else { fval = ival; Py_INCREF(fval); } 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 = 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 if (_stat_float_times) { val = PyFloat_FromDouble(bsec + 1e-9*bnsec); } else { val = PyInt_FromLong((long)bsec); } 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;} #ifdef MS_WINDOWS /* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\, where / can be used in place of \ and the trailing slash is optional. Both SERVER and SHARE must have at least one character.*/ #define ISSLASHA(c) ((c) == '\\' || (c) == '/')#define ISSLASHW(c) ((c) == L'\\' || (c) == L'/')#ifndef ARRAYSIZE#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))#endif static BOOLIsUNCRootA(char *path, int pathlen){ #define ISSLASH ISSLASHA int i, share; if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) /* minimum UNCRoot is \\x\y */ return FALSE; for (i = 2; i < pathlen ; i++) if (ISSLASH(path[i])) break; if (i == 2 || i == pathlen) /* do not allow \\\SHARE or \\SERVER */ return FALSE; share = i+1; for (i = share; i < pathlen; i++) if (ISSLASH(path[i])) break; return (i != share && (i == pathlen || i == pathlen-1)); #undef ISSLASH} #ifdef Py_WIN_WIDE_FILENAMESstatic BOOLIsUNCRootW(Py_UNICODE *path, int pathlen){ #define ISSLASH ISSLASHW int i, share; if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) /* minimum UNCRoot is \\x\y */ return FALSE; for (i = 2; i < pathlen ; i++) if (ISSLASH(path[i])) break; if (i == 2 || i == pathlen) /* do not allow \\\SHARE or \\SERVER */ return FALSE; share = i+1; for (i = share; i < pathlen; i++) if (ISSLASH(path[i])) break; return (i != share && (i == pathlen || i == pathlen-1)); #undef ISSLASH}#endif /* Py_WIN_WIDE_FILENAMES */#endif /* MS_WINDOWS */ static PyObject *posix_do_stat(PyObject *self, PyObject *args, char *format,#ifdef __VMS int (*statfunc)(const char *, STRUCT_STAT *, ...),#else int (*statfunc)(const char *, STRUCT_STAT *),#endif char *wformat, int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)){ STRUCT_STAT st; char *path = NULL; /* pass this to stat; do not free() it */ char *pathfree = NULL; /* this memory must be free'd */ int res; PyObject *result; #ifdef Py_WIN_WIDE_FILENAMES /* If on wide-character-capable OS see if argument is Unicode and if so use wide API. */ if (unicode_file_names()) { PyUnicodeObject *po; if (PyArg_ParseTuple(args, wformat, &po)) { Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); Py_BEGIN_ALLOW_THREADS /* PyUnicode_AS_UNICODE result OK without thread lock as it is a simple dereference. */ res = wstatfunc(wpath, &st); Py_END_ALLOW_THREADS if (res != 0) return win32_error_unicode("stat", wpath); return _pystat_fromstructstat(&st); } /* Drop the argument parsing error as narrow strings are also valid. */ PyErr_Clear(); }#endif if (!PyArg_ParseTuple(args, format, Py_FileSystemDefaultEncoding, &path)) return NULL; pathfree = path; Py_BEGIN_ALLOW_THREADS res = (*statfunc)(path, &st); Py_END_ALLOW_THREADS if (res != 0) {#ifdef MS_WINDOWS result = win32_error("stat", pathfree);#else result = posix_error_with_filename(pathfree);#endif } else result = _pystat_fromstructstat(&st); PyMem_Free(pathfree); return result;} /* POSIX methods */ PyDoc_STRVAR(posix_access__doc__,"access(path, mode) -> True if granted, False otherwise\n\n\Use the real uid/gid to test for access to a path. Note that most\n\operations will use the effective uid/gid, therefore this routine can\n\be used in a suid/sgid environment to test if the invoking user has the\n\Lines 1001–1500 of 8756. Previous lines Load more
} } } code = attribute_data_to_stat(&info, result); if (code < 0) return code; /* Set IFEXEC if it is an .exe, .bat, ... */ dot = wcsrchr(path, '.'); if (dot) { if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 || _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0) result->st_mode |= 0111; } return code; } static int win32_fstat(int file_number, struct win32_stat *result) { BY_HANDLE_FILE_INFORMATION info; HANDLE h; int type; h = (HANDLE)_get_osfhandle(file_number); /* Protocol violation: we explicitly clear errno, instead of setting it to a POSIX error. Callers should use GetLastError. */ errno = 0; if (h == INVALID_HANDLE_VALUE) { /* This is really a C library error (invalid file handle). We set the Win32 error to the closes one matching. */ SetLastError(ERROR_INVALID_HANDLE); return -1; } memset(result, 0, sizeof(*result)); type = GetFileType(h); if (type == FILE_TYPE_UNKNOWN) { DWORD error = GetLastError(); if (error != 0) { return -1; } /* else: valid but unknown file */ } if (type != FILE_TYPE_DISK) { if (type == FILE_TYPE_CHAR) result->st_mode = _S_IFCHR; else if (type == FILE_TYPE_PIPE) result->st_mode = _S_IFIFO; return 0; } if (!GetFileInformationByHandle(h, &info)) { return -1; } /* similar to stat() */ result->st_mode = attributes_to_mode(info.dwFileAttributes); result->st_size = (((__int64)info.nFileSizeHigh)<<32) + info.nFileSizeLow; FILE_TIME_to_time_t_nsec(&info.ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); FILE_TIME_to_time_t_nsec(&info.ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); FILE_TIME_to_time_t_nsec(&info.ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); /* specific to fstat() */ result->st_nlink = info.nNumberOfLinks; result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; return 0; } #endif /* MS_WINDOWS */ PyDoc_STRVAR(stat_result__doc__, "stat_result: Result from stat or lstat.\n\n\ This object may be accessed either as a tuple of\n\ (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ \n\ Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ or st_flags, they are available as attributes only.\n\ \n\ See os.stat for more information."); static PyStructSequence_Field stat_result_fields[] = { {"st_mode", "protection bits"}, {"st_ino", "inode"}, {"st_dev", "device"}, {"st_nlink", "number of hard links"}, {"st_uid", "user ID of owner"}, {"st_gid", "group ID of owner"}, {"st_size", "total size, in bytes"}, /* The NULL is replaced with PyStructSequence_UnnamedField later. */ {NULL, "integer time of last access"}, {NULL, "integer time of last modification"}, {NULL, "integer time of last change"}, {"st_atime", "time of last access"}, {"st_mtime", "time of last modification"}, {"st_ctime", "time of last change"}, #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE {"st_blksize", "blocksize for filesystem I/O"}, #endif #ifdef HAVE_STRUCT_STAT_ST_BLOCKS {"st_blocks", "number of blocks allocated"}, #endif #ifdef HAVE_STRUCT_STAT_ST_RDEV {"st_rdev", "device type (if inode device)"}, #endif #ifdef HAVE_STRUCT_STAT_ST_FLAGS {"st_flags", "user defined flags for file"}, #endif #ifdef HAVE_STRUCT_STAT_ST_GEN {"st_gen", "generation number"}, #endif #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME {"st_birthtime", "time of creation"}, #endif {0} }; #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 PyStructSequence_Desc stat_result_desc = { "stat_result", /* name */ stat_result__doc__, /* doc */ stat_result_fields, 10 }; PyDoc_STRVAR(statvfs_result__doc__, "statvfs_result: Result from statvfs or fstatvfs.\n\n\ This object may be accessed either as a tuple of\n\ (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\ \n\ See os.statvfs for more information."); static PyStructSequence_Field statvfs_result_fields[] = { {"f_bsize", }, {"f_frsize", }, {"f_blocks", }, {"f_bfree", }, {"f_bavail", }, {"f_files", }, {"f_ffree", }, {"f_favail", }, {"f_flag", }, {"f_namemax",}, {0} }; static PyStructSequence_Desc statvfs_result_desc = { "statvfs_result", /* name */ statvfs_result__doc__, /* doc */ statvfs_result_fields, 10 }; static int initialized; static PyTypeObject StatResultType; static PyTypeObject StatVFSResultType; static newfunc structseq_new; static PyObject * statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PyStructSequence *result; int i; result = (PyStructSequence*)structseq_new(type, args, kwds); if (!result) return NULL; /* If we have been initialized from a tuple, st_?time might be set to None. Initialize it from the int slots. */ for (i = 7; i <= 9; i++) { if (result->ob_item[i+3] == Py_None) { Py_DECREF(Py_None); Py_INCREF(result->ob_item[i]); result->ob_item[i+3] = result->ob_item[i]; } } return (PyObject*)result; } /* If true, st_?time is float. */ static int _stat_float_times = 1; PyDoc_STRVAR(stat_float_times__doc__, "stat_float_times([newval]) -> oldval\n\n\ Determine whether os.[lf]stat represents time stamps as float objects.\n\ If newval is True, future calls to stat() return floats, if it is False,\n\ future calls return ints. \n\ If newval is omitted, return the current setting.\n"); static PyObject* stat_float_times(PyObject* self, PyObject *args) { int newval = -1; if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval)) return NULL; if (newval == -1) /* Return old value */ return PyBool_FromLong(_stat_float_times); _stat_float_times = newval; Py_INCREF(Py_None); return Py_None; } 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; if (_stat_float_times) { fval = PyFloat_FromDouble(sec + 1e-9*nsec); } else { fval = ival; Py_INCREF(fval); } 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 = 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 if (_stat_float_times) { val = PyFloat_FromDouble(bsec + 1e-9*bnsec); } else { val = PyInt_FromLong((long)bsec); } 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; } #ifdef MS_WINDOWS /* IsUNCRoot -- test whether the supplied path is of the form \\SERVER\SHARE\, where / can be used in place of \ and the trailing slash is optional. Both SERVER and SHARE must have at least one character. */ #define ISSLASHA(c) ((c) == '\\' || (c) == '/') #define ISSLASHW(c) ((c) == L'\\' || (c) == L'/') #ifndef ARRAYSIZE #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0])) #endif static BOOL IsUNCRootA(char *path, int pathlen) { #define ISSLASH ISSLASHA int i, share; if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) /* minimum UNCRoot is \\x\y */ return FALSE; for (i = 2; i < pathlen ; i++) if (ISSLASH(path[i])) break; if (i == 2 || i == pathlen) /* do not allow \\\SHARE or \\SERVER */ return FALSE; share = i+1; for (i = share; i < pathlen; i++) if (ISSLASH(path[i])) break; return (i != share && (i == pathlen || i == pathlen-1)); #undef ISSLASH } #ifdef Py_WIN_WIDE_FILENAMES static BOOL IsUNCRootW(Py_UNICODE *path, int pathlen) { #define ISSLASH ISSLASHW int i, share; if (pathlen < 5 || !ISSLASH(path[0]) || !ISSLASH(path[1])) /* minimum UNCRoot is \\x\y */ return FALSE; for (i = 2; i < pathlen ; i++) if (ISSLASH(path[i])) break; if (i == 2 || i == pathlen) /* do not allow \\\SHARE or \\SERVER */ return FALSE; share = i+1; for (i = share; i < pathlen; i++) if (ISSLASH(path[i])) break; return (i != share && (i == pathlen || i == pathlen-1)); #undef ISSLASH } #endif /* Py_WIN_WIDE_FILENAMES */ #endif /* MS_WINDOWS */ static PyObject * posix_do_stat(PyObject *self, PyObject *args, char *format, #ifdef __VMS int (*statfunc)(const char *, STRUCT_STAT *, ...), #else int (*statfunc)(const char *, STRUCT_STAT *), #endif char *wformat, int (*wstatfunc)(const Py_UNICODE *, STRUCT_STAT *)) { STRUCT_STAT st; char *path = NULL; /* pass this to stat; do not free() it */ char *pathfree = NULL; /* this memory must be free'd */ int res; PyObject *result; #ifdef Py_WIN_WIDE_FILENAMES /* If on wide-character-capable OS see if argument is Unicode and if so use wide API. */ if (unicode_file_names()) { PyUnicodeObject *po; if (PyArg_ParseTuple(args, wformat, &po)) { Py_UNICODE *wpath = PyUnicode_AS_UNICODE(po); Py_BEGIN_ALLOW_THREADS /* PyUnicode_AS_UNICODE result OK without thread lock as it is a simple dereference. */ res = wstatfunc(wpath, &st); Py_END_ALLOW_THREADS if (res != 0) return win32_error_unicode("stat", wpath); return _pystat_fromstructstat(&st); } /* Drop the argument parsing error as narrow strings are also valid. */ PyErr_Clear(); } #endif if (!PyArg_ParseTuple(args, format, Py_FileSystemDefaultEncoding, &path)) return NULL; pathfree = path; Py_BEGIN_ALLOW_THREADS res = (*statfunc)(path, &st); Py_END_ALLOW_THREADS if (res != 0) { #ifdef MS_WINDOWS result = win32_error("stat", pathfree); #else result = posix_error_with_filename(pathfree); #endif } else result = _pystat_fromstructstat(&st); PyMem_Free(pathfree); return result; } /* POSIX methods */ PyDoc_STRVAR(posix_access__doc__, "access(path, mode) -> True if granted, False otherwise\n\n\ Use the real uid/gid to test for access to a path. Note that most\n\ operations will use the effective uid/gid, therefore this routine can\n\ be used in a suid/sgid environment to test if the invoking user has the\n\