blob: a301fa8840c57a733f80cff1815b10f79c8fc7bd [file] [log] [blame]
Christian Heimesc77d9f32013-06-22 21:05:02 +02001/* stat.h interface
2 *
3 * The module defines all S_IF*, S_I*, UF_*, SF_* and ST_* constants to
4 * sensible default values as well as defines S_IS*() macros in order to keep
5 * backward compatibility with the old stat.py module.
6 *
7 * New constants and macros such as S_IFDOOR / S_ISDOOR() are always defined
8 * as int 0.
9 *
10 * NOTE: POSIX only defines the values of the S_I* permission bits.
11 *
12 */
13
14#define PY_SSIZE_T_CLEAN
15#include "Python.h"
16
17#ifdef __cplusplus
18extern "C" {
19#endif
20
21#ifdef HAVE_SYS_TYPES_H
22#include <sys/types.h>
23#endif /* HAVE_SYS_TYPES_H */
24
25#ifdef HAVE_SYS_STAT_H
26#include <sys/stat.h>
27#endif /* HAVE_SYS_STAT_H */
28
Victor Stinnerc6ebd162013-06-23 01:49:42 +020029#ifdef MS_WINDOWS
30typedef unsigned short mode_t;
31#endif
32
Christian Heimesc77d9f32013-06-22 21:05:02 +020033/* From Python's stat.py */
34#ifndef S_IMODE
35# define S_IMODE 07777
36#endif
37
38/* S_IFXXX constants (file types)
39 *
40 * Only the names are defined by POSIX but not their value. All common file
41 * types seems to have the same numeric value on all platforms, though.
Christian Heimes5b2f1842013-06-24 00:13:14 +020042 *
43 * pyport.h guarantees S_IFMT, S_IFDIR, S_IFCHR, S_IFREG and S_IFLNK
Christian Heimesc77d9f32013-06-22 21:05:02 +020044 */
Christian Heimesc77d9f32013-06-22 21:05:02 +020045
46#ifndef S_IFBLK
47# define S_IFBLK 0060000
48#endif
49
Christian Heimesc77d9f32013-06-22 21:05:02 +020050#ifndef S_IFIFO
51# define S_IFIFO 0010000
52#endif
53
Christian Heimesc77d9f32013-06-22 21:05:02 +020054#ifndef S_IFSOCK
55# define S_IFSOCK 0140000
56#endif
57
58#ifndef S_IFDOOR
59# define S_IFDOOR 0
60#endif
61
62#ifndef S_IFPORT
63# define S_IFPORT 0
64#endif
65
66#ifndef S_IFWHT
67# define S_IFWHT 0
68#endif
69
70
Christian Heimes5b2f1842013-06-24 00:13:14 +020071/* S_ISXXX()
72 * pyport.h defines S_ISDIR(), S_ISREG() and S_ISCHR()
73 */
Christian Heimesc77d9f32013-06-22 21:05:02 +020074
75#ifndef S_ISBLK
Christian Heimes858c9472013-06-23 22:57:02 +020076# define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)
Christian Heimesc77d9f32013-06-22 21:05:02 +020077#endif
78
Christian Heimesc77d9f32013-06-22 21:05:02 +020079#ifndef S_ISFIFO
Christian Heimes858c9472013-06-23 22:57:02 +020080# define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
Christian Heimesc77d9f32013-06-22 21:05:02 +020081#endif
82
83#ifndef S_ISLNK
Christian Heimes858c9472013-06-23 22:57:02 +020084# define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
Christian Heimesc77d9f32013-06-22 21:05:02 +020085#endif
86
87#ifndef S_ISSOCK
Christian Heimes858c9472013-06-23 22:57:02 +020088# define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
Christian Heimesc77d9f32013-06-22 21:05:02 +020089#endif
90
91#ifndef S_ISDOOR
92# define S_ISDOOR(mode) 0
93#endif
94
95#ifndef S_ISPORT
96# define S_ISPORT(mode) 0
97#endif
98
99#ifndef S_ISWHT
100# define S_ISWHT(mode) 0
101#endif
102
103
104/* S_I* file permission
105 *
106 * The permission bit value are defined by POSIX standards.
107 */
108#ifndef S_ISUID
109# define S_ISUID 04000
110#endif
111
112#ifndef S_ISGID
113# define S_ISGID 02000
114#endif
115
116/* what is S_ENFMT? */
117#ifndef S_ENFMT
118# define S_ENFMT S_ISGID
119#endif
120
121#ifndef S_ISVTX
122# define S_ISVTX 01000
123#endif
124
125#ifndef S_IREAD
126# define S_IREAD 00400
127#endif
128
129#ifndef S_IWRITE
130# define S_IWRITE 00200
131#endif
132
133#ifndef S_IEXEC
134# define S_IEXEC 00100
135#endif
136
137#ifndef S_IRWXU
138# define S_IRWXU 00700
139#endif
140
141#ifndef S_IRUSR
142# define S_IRUSR 00400
143#endif
144
145#ifndef S_IWUSR
146# define S_IWUSR 00200
147#endif
148
149#ifndef S_IXUSR
150# define S_IXUSR 00100
151#endif
152
153#ifndef S_IRWXG
154# define S_IRWXG 00070
155#endif
156
157#ifndef S_IRGRP
158# define S_IRGRP 00040
159#endif
160
161#ifndef S_IWGRP
162# define S_IWGRP 00020
163#endif
164
165#ifndef S_IXGRP
166# define S_IXGRP 00010
167#endif
168
169#ifndef S_IRWXO
170# define S_IRWXO 00007
171#endif
172
173#ifndef S_IROTH
174# define S_IROTH 00004
175#endif
176
177#ifndef S_IWOTH
178# define S_IWOTH 00002
179#endif
180
181#ifndef S_IXOTH
182# define S_IXOTH 00001
183#endif
184
185
186/* Names for file flags */
187#ifndef UF_NODUMP
188# define UF_NODUMP 0x00000001
189#endif
190
191#ifndef UF_IMMUTABLE
192# define UF_IMMUTABLE 0x00000002
193#endif
194
195#ifndef UF_APPEND
196# define UF_APPEND 0x00000004
197#endif
198
199#ifndef UF_OPAQUE
200# define UF_OPAQUE 0x00000008
201#endif
202
203#ifndef UF_NOUNLINK
204# define UF_NOUNLINK 0x00000010
205#endif
206
207#ifndef UF_COMPRESSED
208# define UF_COMPRESSED 0x00000020
209#endif
210
211#ifndef UF_HIDDEN
212# define UF_HIDDEN 0x00008000
213#endif
214
215#ifndef SF_ARCHIVED
216# define SF_ARCHIVED 0x00010000
217#endif
218
219#ifndef SF_IMMUTABLE
220# define SF_IMMUTABLE 0x00020000
221#endif
222
223#ifndef SF_APPEND
224# define SF_APPEND 0x00040000
225#endif
226
227#ifndef SF_NOUNLINK
228# define SF_NOUNLINK 0x00100000
229#endif
230
231#ifndef SF_SNAPSHOT
232# define SF_SNAPSHOT 0x00200000
233#endif
234
Victor Stinner1ce46d92013-06-23 22:57:43 +0200235static mode_t
236_PyLong_AsMode_t(PyObject *op)
237{
238 unsigned long value;
239 mode_t mode;
240
241 value = PyLong_AsUnsignedLong(op);
242 if ((value == (unsigned long)-1) && PyErr_Occurred())
243 return (mode_t)-1;
244
245 mode = (mode_t)value;
246 if ((unsigned long)mode != value) {
247 PyErr_SetString(PyExc_OverflowError, "mode out of range");
248 return (mode_t)-1;
249 }
250 return mode;
251}
252
Christian Heimesc77d9f32013-06-22 21:05:02 +0200253
254#define stat_S_ISFUNC(isfunc, doc) \
255 static PyObject * \
256 stat_ ##isfunc (PyObject *self, PyObject *omode) \
257 { \
Victor Stinner1ce46d92013-06-23 22:57:43 +0200258 mode_t mode = _PyLong_AsMode_t(omode); \
259 if ((mode == (mode_t)-1) && PyErr_Occurred()) \
Christian Heimesc77d9f32013-06-22 21:05:02 +0200260 return NULL; \
Christian Heimesc77d9f32013-06-22 21:05:02 +0200261 return PyBool_FromLong(isfunc(mode)); \
262 } \
263 PyDoc_STRVAR(stat_ ## isfunc ## _doc, doc)
264
265stat_S_ISFUNC(S_ISDIR,
266 "S_ISDIR(mode) -> bool\n\n"
267 "Return True if mode is from a directory.");
268
269stat_S_ISFUNC(S_ISCHR,
270 "S_ISCHR(mode) -> bool\n\n"
271 "Return True if mode is from a character special device file.");
272
273stat_S_ISFUNC(S_ISBLK,
274 "S_ISBLK(mode) -> bool\n\n"
275 "Return True if mode is from a block special device file.");
276
277stat_S_ISFUNC(S_ISREG,
278 "S_ISREG(mode) -> bool\n\n"
279 "Return True if mode is from a regular file.");
280
281stat_S_ISFUNC(S_ISFIFO,
282 "S_ISFIFO(mode) -> bool\n\n"
283 "Return True if mode is from a FIFO (named pipe).");
284
285stat_S_ISFUNC(S_ISLNK,
286 "S_ISLNK(mode) -> bool\n\n"
287 "Return True if mode is from a symbolic link.");
288
289stat_S_ISFUNC(S_ISSOCK,
290 "S_ISSOCK(mode) -> bool\n\n"
291 "Return True if mode is from a socket.");
292
293stat_S_ISFUNC(S_ISDOOR,
294 "S_ISDOOR(mode) -> bool\n\n"
295 "Return True if mode is from a door.");
296
297stat_S_ISFUNC(S_ISPORT,
298 "S_ISPORT(mode) -> bool\n\n"
299 "Return True if mode is from an event port.");
300
301stat_S_ISFUNC(S_ISWHT,
302 "S_ISWHT(mode) -> bool\n\n"
303 "Return True if mode is from a whiteout.");
304
305
306PyDoc_STRVAR(stat_S_IMODE_doc,
307"Return the portion of the file's mode that can be set by os.chmod().");
308
309static PyObject *
310stat_S_IMODE(PyObject *self, PyObject *omode)
311{
Victor Stinner1ce46d92013-06-23 22:57:43 +0200312 mode_t mode = _PyLong_AsMode_t(omode);
313 if ((mode == (mode_t)-1) && PyErr_Occurred())
Christian Heimesc77d9f32013-06-22 21:05:02 +0200314 return NULL;
Christian Heimesc77d9f32013-06-22 21:05:02 +0200315 return PyLong_FromUnsignedLong(mode & S_IMODE);
316}
317
318
319PyDoc_STRVAR(stat_S_IFMT_doc,
320"Return the portion of the file's mode that describes the file type.");
321
322static PyObject *
323stat_S_IFMT(PyObject *self, PyObject *omode)
324{
Victor Stinner1ce46d92013-06-23 22:57:43 +0200325 mode_t mode = _PyLong_AsMode_t(omode);
326 if ((mode == (mode_t)-1) && PyErr_Occurred())
Christian Heimesc77d9f32013-06-22 21:05:02 +0200327 return NULL;
Christian Heimesc77d9f32013-06-22 21:05:02 +0200328 return PyLong_FromUnsignedLong(mode & S_IFMT);
329}
330
331/* file type chars according to
332 http://en.wikibooks.org/wiki/C_Programming/POSIX_Reference/sys/stat.h */
333
334static char
335filetype(mode_t mode)
336{
337 /* common cases first */
338 if (S_ISREG(mode)) return '-';
339 if (S_ISDIR(mode)) return 'd';
340 if (S_ISLNK(mode)) return 'l';
341 /* special files */
342 if (S_ISBLK(mode)) return 'b';
343 if (S_ISCHR(mode)) return 'c';
344 if (S_ISFIFO(mode)) return 'p';
345 if (S_ISSOCK(mode)) return 's';
346 /* non-standard types */
347 if (S_ISDOOR(mode)) return 'D';
348 if (S_ISPORT(mode)) return 'P';
349 if (S_ISWHT(mode)) return 'w';
350 /* unknown */
351 return '?';
352}
353
354static void
355fileperm(mode_t mode, char *buf)
356{
357 buf[0] = mode & S_IRUSR ? 'r' : '-';
358 buf[1] = mode & S_IWUSR ? 'w' : '-';
359 if (mode & S_ISUID) {
360 buf[2] = mode & S_IXUSR ? 's' : 'S';
361 } else {
362 buf[2] = mode & S_IXUSR ? 'x' : '-';
363 }
364 buf[3] = mode & S_IRGRP ? 'r' : '-';
365 buf[4] = mode & S_IWGRP ? 'w' : '-';
366 if (mode & S_ISGID) {
367 buf[5] = mode & S_IXGRP ? 's' : 'S';
368 } else {
369 buf[5] = mode & S_IXGRP ? 'x' : '-';
370 }
371 buf[6] = mode & S_IROTH ? 'r' : '-';
372 buf[7] = mode & S_IWOTH ? 'w' : '-';
373 if (mode & S_ISVTX) {
374 buf[8] = mode & S_IXOTH ? 't' : 'T';
375 } else {
376 buf[8] = mode & S_IXOTH ? 'x' : '-';
377 }
378}
379
380PyDoc_STRVAR(stat_filemode_doc,
381"Convert a file's mode to a string of the form '-rwxrwxrwx'");
382
383static PyObject *
384stat_filemode(PyObject *self, PyObject *omode)
385{
386 char buf[10];
Victor Stinner1ce46d92013-06-23 22:57:43 +0200387 mode_t mode;
Christian Heimesc77d9f32013-06-22 21:05:02 +0200388
Victor Stinner1ce46d92013-06-23 22:57:43 +0200389 mode = _PyLong_AsMode_t(omode);
390 if ((mode == (mode_t)-1) && PyErr_Occurred())
Christian Heimesc77d9f32013-06-22 21:05:02 +0200391 return NULL;
Christian Heimesc77d9f32013-06-22 21:05:02 +0200392
393 buf[0] = filetype(mode);
394 fileperm(mode, &buf[1]);
395 return PyUnicode_FromStringAndSize(buf, 10);
396}
397
398
399static PyMethodDef stat_methods[] = {
400 {"S_ISDIR", stat_S_ISDIR, METH_O, stat_S_ISDIR_doc},
401 {"S_ISCHR", stat_S_ISCHR, METH_O, stat_S_ISCHR_doc},
402 {"S_ISBLK", stat_S_ISBLK, METH_O, stat_S_ISBLK_doc},
403 {"S_ISREG", stat_S_ISREG, METH_O, stat_S_ISREG_doc},
404 {"S_ISFIFO", stat_S_ISFIFO, METH_O, stat_S_ISFIFO_doc},
405 {"S_ISLNK", stat_S_ISLNK, METH_O, stat_S_ISLNK_doc},
406 {"S_ISSOCK", stat_S_ISSOCK, METH_O, stat_S_ISSOCK_doc},
407 {"S_ISDOOR", stat_S_ISDOOR, METH_O, stat_S_ISDOOR_doc},
408 {"S_ISPORT", stat_S_ISPORT, METH_O, stat_S_ISPORT_doc},
409 {"S_ISWHT", stat_S_ISWHT, METH_O, stat_S_ISWHT_doc},
410 {"S_IMODE", stat_S_IMODE, METH_O, stat_S_IMODE_doc},
411 {"S_IFMT", stat_S_IFMT, METH_O, stat_S_IFMT_doc},
412 {"filemode", stat_filemode, METH_O, stat_filemode_doc},
413 {NULL, NULL} /* sentinel */
414};
415
416
417PyDoc_STRVAR(module_doc,
418"S_IFMT_: file type bits\n\
419S_IFDIR: directory\n\
420S_IFCHR: character device\n\
421S_IFBLK: block device\n\
422S_IFREG: regular file\n\
423S_IFIFO: fifo (named pipe)\n\
424S_IFLNK: symbolic link\n\
425S_IFSOCK: socket file\n\
426S_IFDOOR: door\n\
427S_IFPORT: event port\n\
428S_IFWHT: whiteout\n\
429\n"
430
431"S_ISUID: set UID bit\n\
432S_ISGID: set GID bit\n\
433S_ENFMT: file locking enforcement\n\
434S_ISVTX: sticky bit\n\
435S_IREAD: Unix V7 synonym for S_IRUSR\n\
436S_IWRITE: Unix V7 synonym for S_IWUSR\n\
437S_IEXEC: Unix V7 synonym for S_IXUSR\n\
438S_IRWXU: mask for owner permissions\n\
439S_IRUSR: read by owner\n\
440S_IWUSR: write by owner\n\
441S_IXUSR: execute by owner\n\
442S_IRWXG: mask for group permissions\n\
443S_IRGRP: read by group\n\
444S_IWGRP: write by group\n\
445S_IXGRP: execute by group\n\
446S_IRWXO: mask for others (not in group) permissions\n\
447S_IROTH: read by others\n\
448S_IWOTH: write by others\n\
449S_IXOTH: execute by others\n\
450\n"
451
452"UF_NODUMP: do not dump file\n\
453UF_IMMUTABLE: file may not be changed\n\
454UF_APPEND: file may only be appended to\n\
455UF_OPAQUE: directory is opaque when viewed through a union stack\n\
456UF_NOUNLINK: file may not be renamed or deleted\n\
457UF_COMPRESSED: OS X: file is hfs-compressed\n\
458UF_HIDDEN: OS X: file should not be displayed\n\
459SF_ARCHIVED: file may be archived\n\
460SF_IMMUTABLE: file may not be changed\n\
461SF_APPEND: file may only be appended to\n\
462SF_NOUNLINK: file may not be renamed or deleted\n\
463SF_SNAPSHOT: file is a snapshot file\n\
464\n"
465
466"ST_MODE\n\
467ST_INO\n\
468ST_DEV\n\
469ST_NLINK\n\
470ST_UID\n\
471ST_GID\n\
472ST_SIZE\n\
473ST_ATIME\n\
474ST_MTIME\n\
475ST_CTIME\n\
476");
477
478
479static struct PyModuleDef statmodule = {
480 PyModuleDef_HEAD_INIT,
481 "_stat",
482 module_doc,
483 -1,
484 stat_methods,
485 NULL,
486 NULL,
487 NULL,
488 NULL
489};
490
491PyMODINIT_FUNC
492PyInit__stat(void)
493{
494 PyObject *m;
495 m = PyModule_Create(&statmodule);
496 if (m == NULL)
497 return NULL;
498
499 if (PyModule_AddIntMacro(m, S_IFDIR)) return NULL;
500 if (PyModule_AddIntMacro(m, S_IFCHR)) return NULL;
501 if (PyModule_AddIntMacro(m, S_IFBLK)) return NULL;
502 if (PyModule_AddIntMacro(m, S_IFREG)) return NULL;
503 if (PyModule_AddIntMacro(m, S_IFIFO)) return NULL;
504 if (PyModule_AddIntMacro(m, S_IFLNK)) return NULL;
505 if (PyModule_AddIntMacro(m, S_IFSOCK)) return NULL;
506 if (PyModule_AddIntMacro(m, S_IFDOOR)) return NULL;
507 if (PyModule_AddIntMacro(m, S_IFPORT)) return NULL;
508 if (PyModule_AddIntMacro(m, S_IFWHT)) return NULL;
509
510 if (PyModule_AddIntMacro(m, S_ISUID)) return NULL;
511 if (PyModule_AddIntMacro(m, S_ISGID)) return NULL;
512 if (PyModule_AddIntMacro(m, S_ISVTX)) return NULL;
513 if (PyModule_AddIntMacro(m, S_ENFMT)) return NULL;
514
515 if (PyModule_AddIntMacro(m, S_IREAD)) return NULL;
516 if (PyModule_AddIntMacro(m, S_IWRITE)) return NULL;
517 if (PyModule_AddIntMacro(m, S_IEXEC)) return NULL;
518
519 if (PyModule_AddIntMacro(m, S_IRWXU)) return NULL;
520 if (PyModule_AddIntMacro(m, S_IRUSR)) return NULL;
521 if (PyModule_AddIntMacro(m, S_IWUSR)) return NULL;
522 if (PyModule_AddIntMacro(m, S_IXUSR)) return NULL;
523
524 if (PyModule_AddIntMacro(m, S_IRWXG)) return NULL;
525 if (PyModule_AddIntMacro(m, S_IRGRP)) return NULL;
526 if (PyModule_AddIntMacro(m, S_IWGRP)) return NULL;
527 if (PyModule_AddIntMacro(m, S_IXGRP)) return NULL;
528
529 if (PyModule_AddIntMacro(m, S_IRWXO)) return NULL;
530 if (PyModule_AddIntMacro(m, S_IROTH)) return NULL;
531 if (PyModule_AddIntMacro(m, S_IWOTH)) return NULL;
532 if (PyModule_AddIntMacro(m, S_IXOTH)) return NULL;
533
534 if (PyModule_AddIntMacro(m, UF_NODUMP)) return NULL;
535 if (PyModule_AddIntMacro(m, UF_IMMUTABLE)) return NULL;
536 if (PyModule_AddIntMacro(m, UF_APPEND)) return NULL;
537 if (PyModule_AddIntMacro(m, UF_OPAQUE)) return NULL;
538 if (PyModule_AddIntMacro(m, UF_NOUNLINK)) return NULL;
539 if (PyModule_AddIntMacro(m, UF_COMPRESSED)) return NULL;
540 if (PyModule_AddIntMacro(m, UF_HIDDEN)) return NULL;
541 if (PyModule_AddIntMacro(m, SF_ARCHIVED)) return NULL;
542 if (PyModule_AddIntMacro(m, SF_IMMUTABLE)) return NULL;
543 if (PyModule_AddIntMacro(m, SF_APPEND)) return NULL;
544 if (PyModule_AddIntMacro(m, SF_NOUNLINK)) return NULL;
545 if (PyModule_AddIntMacro(m, SF_SNAPSHOT)) return NULL;
546
547 if (PyModule_AddIntConstant(m, "ST_MODE", 0)) return NULL;
548 if (PyModule_AddIntConstant(m, "ST_INO", 1)) return NULL;
549 if (PyModule_AddIntConstant(m, "ST_DEV", 2)) return NULL;
550 if (PyModule_AddIntConstant(m, "ST_NLINK", 3)) return NULL;
551 if (PyModule_AddIntConstant(m, "ST_UID", 4)) return NULL;
552 if (PyModule_AddIntConstant(m, "ST_GID", 5)) return NULL;
553 if (PyModule_AddIntConstant(m, "ST_SIZE", 6)) return NULL;
554 if (PyModule_AddIntConstant(m, "ST_ATIME", 7)) return NULL;
555 if (PyModule_AddIntConstant(m, "ST_MTIME", 8)) return NULL;
556 if (PyModule_AddIntConstant(m, "ST_CTIME", 9)) return NULL;
557
558 return m;
559}
560
561#ifdef __cplusplus
562}
563#endif