annotate mercurial/osutil.c @ 31601:8e7feaad2d8d

logtoprocess: use lowercase for docstring title
author Jun Wu <quark@fb.com>
date Thu, 23 Mar 2017 21:16:55 -0700
parents 2d501fb60b2d
children 2243ba216f66
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
1 /*
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
2 osutil.c - native operating system services
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
3
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
4 Copyright 2007 Matt Mackall and others
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
5
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
6 This software may be used and distributed according to the terms of
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
7 the GNU General Public License, incorporated herein by reference.
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
8 */
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
9
5463
3b204881f959 osutil: use fdopendir instead of dirfd
Bryan O'Sullivan <bos@serpentine.com>
parents: 5457
diff changeset
10 #define _ATFILE_SOURCE
5397
11caa374f497 osutil.c: include Python.h before the other headers
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5396
diff changeset
11 #include <Python.h>
8330
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
12 #include <fcntl.h>
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
13 #include <stdio.h>
27473
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
14 #include <stdlib.h>
8330
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
15 #include <string.h>
14873
f79d47813b8b osutil: emulate os.listdir's OSError for long names (issue2898)
Matt Mackall <mpm@selenic.com>
parents: 13748
diff changeset
16 #include <errno.h>
8330
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
17
7056
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
18 #ifdef _WIN32
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 9353
diff changeset
19 #include <windows.h>
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 9353
diff changeset
20 #include <io.h>
7056
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
21 #else
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 9353
diff changeset
22 #include <dirent.h>
27473
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
23 #include <sys/socket.h>
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 9353
diff changeset
24 #include <sys/stat.h>
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 9353
diff changeset
25 #include <sys/types.h>
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 9353
diff changeset
26 #include <unistd.h>
31563
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
27 #ifdef HAVE_LINUX_MAGIC_H
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
28 #include <linux/magic.h>
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
29 #endif
31597
2d501fb60b2d osutil: report fstype for BSD and OSX
Jun Wu <quark@fb.com>
parents: 31564
diff changeset
30 #ifdef HAVE_BSD_STATFS
31563
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
31 #include <sys/mount.h>
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
32 #include <sys/param.h>
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
33 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
34 #ifdef HAVE_SYS_VFS_H
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
35 #include <sys/vfs.h>
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
36 #endif
7056
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
37 #endif
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
38
24461
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
39 #ifdef __APPLE__
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
40 #include <sys/attr.h>
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
41 #include <sys/vnode.h>
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
42 #endif
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
43
11359
4eaacccbb2ca osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
44 #include "util.h"
4eaacccbb2ca osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
45
9353
3ac42ca1f3e6 osutil: fix compilation with -ansi
Sebastien Binet <binet@cern.ch>
parents: 8723
diff changeset
46 /* some platforms lack the PATH_MAX definition (eg. GNU/Hurd) */
8722
48da69ff79bd Some platforms lack the PATH_MAX definition (eg. GNU/Hurd).
Arne Babenhauserheide <bab@draketo.de>
parents: 7190
diff changeset
47 #ifndef PATH_MAX
48da69ff79bd Some platforms lack the PATH_MAX definition (eg. GNU/Hurd).
Arne Babenhauserheide <bab@draketo.de>
parents: 7190
diff changeset
48 #define PATH_MAX 4096
48da69ff79bd Some platforms lack the PATH_MAX definition (eg. GNU/Hurd).
Arne Babenhauserheide <bab@draketo.de>
parents: 7190
diff changeset
49 #endif
48da69ff79bd Some platforms lack the PATH_MAX definition (eg. GNU/Hurd).
Arne Babenhauserheide <bab@draketo.de>
parents: 7190
diff changeset
50
7056
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
51 #ifdef _WIN32
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
52 /*
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
53 stat struct compatible with hg expectations
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
54 Mercurial only uses st_mode, st_size and st_mtime
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
55 the rest is kept to minimize changes between implementations
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
56 */
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
57 struct hg_stat {
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
58 int st_dev;
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
59 int st_mode;
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
60 int st_nlink;
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
61 __int64 st_size;
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
62 int st_mtime;
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
63 int st_ctime;
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
64 };
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
65 struct listdir_stat {
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
66 PyObject_HEAD
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
67 struct hg_stat st;
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
68 };
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
69 #else
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
70 struct listdir_stat {
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
71 PyObject_HEAD
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
72 struct stat st;
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
73 };
7056
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
74 #endif
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
75
30111
a989fa78dafa osutil: use PyLongObject on Python 3 for listdir_slot
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30110
diff changeset
76 #ifdef IS_PY3K
a989fa78dafa osutil: use PyLongObject on Python 3 for listdir_slot
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30110
diff changeset
77 #define listdir_slot(name) \
a989fa78dafa osutil: use PyLongObject on Python 3 for listdir_slot
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30110
diff changeset
78 static PyObject *listdir_stat_##name(PyObject *self, void *x) \
a989fa78dafa osutil: use PyLongObject on Python 3 for listdir_slot
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30110
diff changeset
79 { \
a989fa78dafa osutil: use PyLongObject on Python 3 for listdir_slot
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30110
diff changeset
80 return PyLong_FromLong(((struct listdir_stat *)self)->st.name); \
a989fa78dafa osutil: use PyLongObject on Python 3 for listdir_slot
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30110
diff changeset
81 }
a989fa78dafa osutil: use PyLongObject on Python 3 for listdir_slot
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30110
diff changeset
82 #else
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
83 #define listdir_slot(name) \
7190
aecea6934fdd Some additional space/tab cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 7136
diff changeset
84 static PyObject *listdir_stat_##name(PyObject *self, void *x) \
aecea6934fdd Some additional space/tab cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 7136
diff changeset
85 { \
aecea6934fdd Some additional space/tab cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 7136
diff changeset
86 return PyInt_FromLong(((struct listdir_stat *)self)->st.name); \
aecea6934fdd Some additional space/tab cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 7136
diff changeset
87 }
30111
a989fa78dafa osutil: use PyLongObject on Python 3 for listdir_slot
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30110
diff changeset
88 #endif
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
89
5431
a7c832abd29c Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents: 5430
diff changeset
90 listdir_slot(st_dev)
a7c832abd29c Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents: 5430
diff changeset
91 listdir_slot(st_mode)
a7c832abd29c Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents: 5430
diff changeset
92 listdir_slot(st_nlink)
7056
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
93 #ifdef _WIN32
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
94 static PyObject *listdir_stat_st_size(PyObject *self, void *x)
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
95 {
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
96 return PyLong_FromLongLong(
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
97 (PY_LONG_LONG)((struct listdir_stat *)self)->st.st_size);
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
98 }
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
99 #else
5431
a7c832abd29c Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents: 5430
diff changeset
100 listdir_slot(st_size)
7056
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
101 #endif
5431
a7c832abd29c Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents: 5430
diff changeset
102 listdir_slot(st_mtime)
a7c832abd29c Fix build error with Sun C compiler.
Bryan O'Sullivan <bos@serpentine.com>
parents: 5430
diff changeset
103 listdir_slot(st_ctime)
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
104
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
105 static struct PyGetSetDef listdir_stat_getsets[] = {
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
106 {"st_dev", listdir_stat_st_dev, 0, 0, 0},
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
107 {"st_mode", listdir_stat_st_mode, 0, 0, 0},
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
108 {"st_nlink", listdir_stat_st_nlink, 0, 0, 0},
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
109 {"st_size", listdir_stat_st_size, 0, 0, 0},
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
110 {"st_mtime", listdir_stat_st_mtime, 0, 0, 0},
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
111 {"st_ctime", listdir_stat_st_ctime, 0, 0, 0},
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
112 {0, 0, 0, 0, 0}
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
113 };
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
114
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
115 static PyObject *listdir_stat_new(PyTypeObject *t, PyObject *a, PyObject *k)
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
116 {
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
117 return t->tp_alloc(t, 0);
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
118 }
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
119
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
120 static void listdir_stat_dealloc(PyObject *o)
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
121 {
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
122 o->ob_type->tp_free(o);
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
123 }
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
124
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
125 static PyTypeObject listdir_stat_type = {
11359
4eaacccbb2ca osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
126 PyVarObject_HEAD_INIT(NULL, 0)
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
127 "osutil.stat", /*tp_name*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
128 sizeof(struct listdir_stat), /*tp_basicsize*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
129 0, /*tp_itemsize*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
130 (destructor)listdir_stat_dealloc, /*tp_dealloc*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
131 0, /*tp_print*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
132 0, /*tp_getattr*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
133 0, /*tp_setattr*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
134 0, /*tp_compare*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
135 0, /*tp_repr*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
136 0, /*tp_as_number*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
137 0, /*tp_as_sequence*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
138 0, /*tp_as_mapping*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
139 0, /*tp_hash */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
140 0, /*tp_call*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
141 0, /*tp_str*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
142 0, /*tp_getattro*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
143 0, /*tp_setattro*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
144 0, /*tp_as_buffer*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
145 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
146 "stat objects", /* tp_doc */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
147 0, /* tp_traverse */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
148 0, /* tp_clear */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
149 0, /* tp_richcompare */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
150 0, /* tp_weaklistoffset */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
151 0, /* tp_iter */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
152 0, /* tp_iternext */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
153 0, /* tp_methods */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
154 0, /* tp_members */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
155 listdir_stat_getsets, /* tp_getset */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
156 0, /* tp_base */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
157 0, /* tp_dict */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
158 0, /* tp_descr_get */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
159 0, /* tp_descr_set */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
160 0, /* tp_dictoffset */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
161 0, /* tp_init */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
162 0, /* tp_alloc */
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
163 listdir_stat_new, /* tp_new */
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
164 };
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
165
7056
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
166 #ifdef _WIN32
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
167
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
168 static int to_python_time(const FILETIME *tm)
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
169 {
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
170 /* number of seconds between epoch and January 1 1601 */
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
171 const __int64 a0 = (__int64)134774L * (__int64)24L * (__int64)3600L;
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
172 /* conversion factor from 100ns to 1s */
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
173 const __int64 a1 = 10000000;
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
174 /* explicit (int) cast to suspend compiler warnings */
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
175 return (int)((((__int64)tm->dwHighDateTime << 32)
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
176 + tm->dwLowDateTime) / a1 - a0);
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
177 }
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
178
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
179 static PyObject *make_item(const WIN32_FIND_DATAA *fd, int wantstat)
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
180 {
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
181 PyObject *py_st;
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
182 struct hg_stat *stp;
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
183
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
184 int kind = (fd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
185 ? _S_IFDIR : _S_IFREG;
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
186
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
187 if (!wantstat)
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
188 return Py_BuildValue("si", fd->cFileName, kind);
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
189
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
190 py_st = PyObject_CallObject((PyObject *)&listdir_stat_type, NULL);
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
191 if (!py_st)
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
192 return NULL;
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
193
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
194 stp = &((struct listdir_stat *)py_st)->st;
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
195 /*
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
196 use kind as st_mode
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
197 rwx bits on Win32 are meaningless
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
198 and Hg does not use them anyway
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
199 */
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
200 stp->st_mode = kind;
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
201 stp->st_mtime = to_python_time(&fd->ftLastWriteTime);
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
202 stp->st_ctime = to_python_time(&fd->ftCreationTime);
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
203 if (kind == _S_IFREG)
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 9353
diff changeset
204 stp->st_size = ((__int64)fd->nFileSizeHigh << 32)
7056
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
205 + fd->nFileSizeLow;
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
206 return Py_BuildValue("siN", fd->cFileName,
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
207 kind, py_st);
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
208 }
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
209
7098
8a5c88c7e97b osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7059
diff changeset
210 static PyObject *_listdir(char *path, int plen, int wantstat, char *skip)
7056
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
211 {
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
212 PyObject *rval = NULL; /* initialize - return value */
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
213 PyObject *list;
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
214 HANDLE fh;
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
215 WIN32_FIND_DATAA fd;
7098
8a5c88c7e97b osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7059
diff changeset
216 char *pattern;
7056
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
217
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
218 /* build the path + \* pattern string */
31468
b9dd03ed564f osutil: use Python memory allocator in _listdir
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30409
diff changeset
219 pattern = PyMem_Malloc(plen + 3); /* path + \* + \0 */
7056
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
220 if (!pattern) {
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
221 PyErr_NoMemory();
7098
8a5c88c7e97b osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7059
diff changeset
222 goto error_nomem;
7056
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
223 }
28593
e60c492a0d9b osutil: stop using strcpy
Augie Fackler <augie@google.com>
parents: 27970
diff changeset
224 memcpy(pattern, path, plen);
7056
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
225
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
226 if (plen > 0) {
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
227 char c = path[plen-1];
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
228 if (c != ':' && c != '/' && c != '\\')
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
229 pattern[plen++] = '\\';
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
230 }
28593
e60c492a0d9b osutil: stop using strcpy
Augie Fackler <augie@google.com>
parents: 27970
diff changeset
231 pattern[plen++] = '*';
e60c492a0d9b osutil: stop using strcpy
Augie Fackler <augie@google.com>
parents: 27970
diff changeset
232 pattern[plen] = '\0';
7056
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
233
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
234 fh = FindFirstFileA(pattern, &fd);
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
235 if (fh == INVALID_HANDLE_VALUE) {
7059
6a76cf980999 Improve error handling in osutil.c
Petr Kodl <petrkodl@gmail.com>
parents: 7056
diff changeset
236 PyErr_SetFromWindowsErrWithFilename(GetLastError(), path);
7056
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
237 goto error_file;
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
238 }
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
239
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
240 list = PyList_New(0);
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
241 if (!list)
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
242 goto error_list;
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
243
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
244 do {
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
245 PyObject *item;
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
246
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
247 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
248 if (!strcmp(fd.cFileName, ".")
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
249 || !strcmp(fd.cFileName, ".."))
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
250 continue;
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
251
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
252 if (skip && !strcmp(fd.cFileName, skip)) {
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
253 rval = PyList_New(0);
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
254 goto error;
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
255 }
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
256 }
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
257
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
258 item = make_item(&fd, wantstat);
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
259 if (!item)
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
260 goto error;
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
261
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
262 if (PyList_Append(list, item)) {
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
263 Py_XDECREF(item);
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
264 goto error;
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
265 }
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
266
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
267 Py_XDECREF(item);
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
268 } while (FindNextFileA(fh, &fd));
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
269
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
270 if (GetLastError() != ERROR_NO_MORE_FILES) {
7059
6a76cf980999 Improve error handling in osutil.c
Petr Kodl <petrkodl@gmail.com>
parents: 7056
diff changeset
271 PyErr_SetFromWindowsErrWithFilename(GetLastError(), path);
7056
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
272 goto error;
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
273 }
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
274
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
275 rval = list;
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
276 Py_XINCREF(rval);
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
277 error:
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
278 Py_XDECREF(list);
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
279 error_list:
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
280 FindClose(fh);
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
281 error_file:
31468
b9dd03ed564f osutil: use Python memory allocator in _listdir
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30409
diff changeset
282 PyMem_Free(pattern);
7098
8a5c88c7e97b osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7059
diff changeset
283 error_nomem:
7056
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
284 return rval;
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
285 }
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
286
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
287 #else
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
288
7031
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
289 int entkind(struct dirent *ent)
5425
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
290 {
7031
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
291 #ifdef DT_REG
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
292 switch (ent->d_type) {
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
293 case DT_REG: return S_IFREG;
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
294 case DT_DIR: return S_IFDIR;
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
295 case DT_LNK: return S_IFLNK;
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
296 case DT_BLK: return S_IFBLK;
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
297 case DT_CHR: return S_IFCHR;
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
298 case DT_FIFO: return S_IFIFO;
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
299 case DT_SOCK: return S_IFSOCK;
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
300 }
5463
3b204881f959 osutil: use fdopendir instead of dirfd
Bryan O'Sullivan <bos@serpentine.com>
parents: 5457
diff changeset
301 #endif
7033
892d27fb04a5 osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents: 7031
diff changeset
302 return -1;
5425
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
303 }
830f6e280c90 osutils: pull file stat loop into its own function
Matt Mackall <mpm@selenic.com>
parents: 5424
diff changeset
304
18019
e248bff2d8dd osutil: factor out creation and init of listdir_stat
Bryan O'Sullivan <bryano@fb.com>
parents: 16747
diff changeset
305 static PyObject *makestat(const struct stat *st)
e248bff2d8dd osutil: factor out creation and init of listdir_stat
Bryan O'Sullivan <bryano@fb.com>
parents: 16747
diff changeset
306 {
18021
9b05b31b413c osutil: fix tab damage
Bryan O'Sullivan <bryano@fb.com>
parents: 18019
diff changeset
307 PyObject *stat;
18019
e248bff2d8dd osutil: factor out creation and init of listdir_stat
Bryan O'Sullivan <bryano@fb.com>
parents: 16747
diff changeset
308
18021
9b05b31b413c osutil: fix tab damage
Bryan O'Sullivan <bryano@fb.com>
parents: 18019
diff changeset
309 stat = PyObject_CallObject((PyObject *)&listdir_stat_type, NULL);
9b05b31b413c osutil: fix tab damage
Bryan O'Sullivan <bryano@fb.com>
parents: 18019
diff changeset
310 if (stat)
9b05b31b413c osutil: fix tab damage
Bryan O'Sullivan <bryano@fb.com>
parents: 18019
diff changeset
311 memcpy(&((struct listdir_stat *)stat)->st, st, sizeof(*st));
9b05b31b413c osutil: fix tab damage
Bryan O'Sullivan <bryano@fb.com>
parents: 18019
diff changeset
312 return stat;
18019
e248bff2d8dd osutil: factor out creation and init of listdir_stat
Bryan O'Sullivan <bryano@fb.com>
parents: 16747
diff changeset
313 }
e248bff2d8dd osutil: factor out creation and init of listdir_stat
Bryan O'Sullivan <bryano@fb.com>
parents: 16747
diff changeset
314
24460
73477e755cd2 osutil._listdir: rename to _listdir_stat
Siddharth Agarwal <sid0@fb.com>
parents: 23966
diff changeset
315 static PyObject *_listdir_stat(char *path, int pathlen, int keepstat,
73477e755cd2 osutil._listdir: rename to _listdir_stat
Siddharth Agarwal <sid0@fb.com>
parents: 23966
diff changeset
316 char *skip)
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
317 {
23962
1f3b94e8dc40 osutil: fix leak of stat in makestat when Py_BuildValue fails
Augie Fackler <augie@google.com>
parents: 23961
diff changeset
318 PyObject *list, *elem, *stat = NULL, *ret = NULL;
7098
8a5c88c7e97b osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7059
diff changeset
319 char fullpath[PATH_MAX + 10];
7136
d834ed27199f _listdir only uses dfd if AT_SYMLINK_NOFOLLOW is defined
Brendan Cully <brendan@kublai.com>
parents: 7098
diff changeset
320 int kind, err;
7031
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
321 struct stat st;
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
322 struct dirent *ent;
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
323 DIR *dir;
7136
d834ed27199f _listdir only uses dfd if AT_SYMLINK_NOFOLLOW is defined
Brendan Cully <brendan@kublai.com>
parents: 7098
diff changeset
324 #ifdef AT_SYMLINK_NOFOLLOW
d834ed27199f _listdir only uses dfd if AT_SYMLINK_NOFOLLOW is defined
Brendan Cully <brendan@kublai.com>
parents: 7098
diff changeset
325 int dfd = -1;
d834ed27199f _listdir only uses dfd if AT_SYMLINK_NOFOLLOW is defined
Brendan Cully <brendan@kublai.com>
parents: 7098
diff changeset
326 #endif
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
327
7059
6a76cf980999 Improve error handling in osutil.c
Petr Kodl <petrkodl@gmail.com>
parents: 7056
diff changeset
328 if (pathlen >= PATH_MAX) {
14873
f79d47813b8b osutil: emulate os.listdir's OSError for long names (issue2898)
Matt Mackall <mpm@selenic.com>
parents: 13748
diff changeset
329 errno = ENAMETOOLONG;
f79d47813b8b osutil: emulate os.listdir's OSError for long names (issue2898)
Matt Mackall <mpm@selenic.com>
parents: 13748
diff changeset
330 PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
7098
8a5c88c7e97b osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7059
diff changeset
331 goto error_value;
7059
6a76cf980999 Improve error handling in osutil.c
Petr Kodl <petrkodl@gmail.com>
parents: 7056
diff changeset
332 }
7031
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
333 strncpy(fullpath, path, PATH_MAX);
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
334 fullpath[pathlen] = '/';
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
335
7033
892d27fb04a5 osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents: 7031
diff changeset
336 #ifdef AT_SYMLINK_NOFOLLOW
892d27fb04a5 osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents: 7031
diff changeset
337 dfd = open(path, O_RDONLY);
892d27fb04a5 osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents: 7031
diff changeset
338 if (dfd == -1) {
892d27fb04a5 osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents: 7031
diff changeset
339 PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
7098
8a5c88c7e97b osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7059
diff changeset
340 goto error_value;
7033
892d27fb04a5 osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents: 7031
diff changeset
341 }
892d27fb04a5 osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents: 7031
diff changeset
342 dir = fdopendir(dfd);
892d27fb04a5 osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents: 7031
diff changeset
343 #else
892d27fb04a5 osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents: 7031
diff changeset
344 dir = opendir(path);
892d27fb04a5 osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents: 7031
diff changeset
345 #endif
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
346 if (!dir) {
7031
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
347 PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
348 goto error_dir;
23961
bc851e2851b1 osutil.c: clean up space before a tab
Augie Fackler <augie@google.com>
parents: 18027
diff changeset
349 }
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
350
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
351 list = PyList_New(0);
7031
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
352 if (!list)
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
353 goto error_list;
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
354
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
355 while ((ent = readdir(dir))) {
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
356 if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
357 continue;
5416
ca890c0c3f1f osutil.c: style fix - delete trailing end-of-line spaces
Giorgos Keramidas <keramida@ceid.upatras.gr>
parents: 5398
diff changeset
358
7031
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
359 kind = entkind(ent);
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
360 if (kind == -1 || keepstat) {
7033
892d27fb04a5 osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents: 7031
diff changeset
361 #ifdef AT_SYMLINK_NOFOLLOW
892d27fb04a5 osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents: 7031
diff changeset
362 err = fstatat(dfd, ent->d_name, &st,
892d27fb04a5 osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents: 7031
diff changeset
363 AT_SYMLINK_NOFOLLOW);
892d27fb04a5 osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents: 7031
diff changeset
364 #else
892d27fb04a5 osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents: 7031
diff changeset
365 strncpy(fullpath + pathlen + 1, ent->d_name,
892d27fb04a5 osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents: 7031
diff changeset
366 PATH_MAX - pathlen);
24462
40b05303ac32 osutil: mark end of string with null char, not 0
Siddharth Agarwal <sid0@fb.com>
parents: 24461
diff changeset
367 fullpath[PATH_MAX] = '\0';
7033
892d27fb04a5 osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents: 7031
diff changeset
368 err = lstat(fullpath, &st);
892d27fb04a5 osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents: 7031
diff changeset
369 #endif
7031
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
370 if (err == -1) {
16747
6476a21337a6 osutil: handle deletion race with readdir/stat (issue3463)
Matt Mackall <mpm@selenic.com>
parents: 15098
diff changeset
371 /* race with file deletion? */
6476a21337a6 osutil: handle deletion race with readdir/stat (issue3463)
Matt Mackall <mpm@selenic.com>
parents: 15098
diff changeset
372 if (errno == ENOENT)
6476a21337a6 osutil: handle deletion race with readdir/stat (issue3463)
Matt Mackall <mpm@selenic.com>
parents: 15098
diff changeset
373 continue;
7031
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
374 strncpy(fullpath + pathlen + 1, ent->d_name,
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
375 PATH_MAX - pathlen);
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
376 fullpath[PATH_MAX] = 0;
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
377 PyErr_SetFromErrnoWithFilename(PyExc_OSError,
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
378 fullpath);
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
379 goto error;
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
380 }
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
381 kind = st.st_mode & S_IFMT;
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
382 }
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
383
7034
0d513661d6c2 listdir: add support for aborting if a certain path is found
Matt Mackall <mpm@selenic.com>
parents: 7033
diff changeset
384 /* quit early? */
0d513661d6c2 listdir: add support for aborting if a certain path is found
Matt Mackall <mpm@selenic.com>
parents: 7033
diff changeset
385 if (skip && kind == S_IFDIR && !strcmp(ent->d_name, skip)) {
0d513661d6c2 listdir: add support for aborting if a certain path is found
Matt Mackall <mpm@selenic.com>
parents: 7033
diff changeset
386 ret = PyList_New(0);
0d513661d6c2 listdir: add support for aborting if a certain path is found
Matt Mackall <mpm@selenic.com>
parents: 7033
diff changeset
387 goto error;
0d513661d6c2 listdir: add support for aborting if a certain path is found
Matt Mackall <mpm@selenic.com>
parents: 7033
diff changeset
388 }
0d513661d6c2 listdir: add support for aborting if a certain path is found
Matt Mackall <mpm@selenic.com>
parents: 7033
diff changeset
389
7031
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
390 if (keepstat) {
18019
e248bff2d8dd osutil: factor out creation and init of listdir_stat
Bryan O'Sullivan <bryano@fb.com>
parents: 16747
diff changeset
391 stat = makestat(&st);
7031
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
392 if (!stat)
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
393 goto error;
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
394 elem = Py_BuildValue("siN", ent->d_name, kind, stat);
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
395 } else
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
396 elem = Py_BuildValue("si", ent->d_name, kind);
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
397 if (!elem)
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
398 goto error;
23962
1f3b94e8dc40 osutil: fix leak of stat in makestat when Py_BuildValue fails
Augie Fackler <augie@google.com>
parents: 23961
diff changeset
399 stat = NULL;
7031
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
400
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
401 PyList_Append(list, elem);
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
402 Py_DECREF(elem);
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
403 }
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
404
7031
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
405 ret = list;
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
406 Py_INCREF(ret);
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
407
7031
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
408 error:
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
409 Py_DECREF(list);
23962
1f3b94e8dc40 osutil: fix leak of stat in makestat when Py_BuildValue fails
Augie Fackler <augie@google.com>
parents: 23961
diff changeset
410 Py_XDECREF(stat);
7031
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
411 error_list:
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
412 closedir(dir);
31471
95be8b7181d3 osutil: fix potential wrong fd close
Jun Wu <quark@fb.com>
parents: 31468
diff changeset
413 /* closedir also closes its dirfd */
95be8b7181d3 osutil: fix potential wrong fd close
Jun Wu <quark@fb.com>
parents: 31468
diff changeset
414 goto error_value;
7031
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
415 error_dir:
7033
892d27fb04a5 osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents: 7031
diff changeset
416 #ifdef AT_SYMLINK_NOFOLLOW
892d27fb04a5 osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents: 7031
diff changeset
417 close(dfd);
892d27fb04a5 osutil: fix some braindamage
Matt Mackall <mpm@selenic.com>
parents: 7031
diff changeset
418 #endif
7098
8a5c88c7e97b osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7059
diff changeset
419 error_value:
7031
19e8d034932e osutil: major listdir cleanup
Matt Mackall <mpm@selenic.com>
parents: 7022
diff changeset
420 return ret;
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
421 }
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
422
24461
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
423 #ifdef __APPLE__
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
424
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
425 typedef struct {
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
426 u_int32_t length;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
427 attrreference_t name;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
428 fsobj_type_t obj_type;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
429 struct timespec mtime;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
430 #if __LITTLE_ENDIAN__
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
431 mode_t access_mask;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
432 uint16_t padding;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
433 #else
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
434 uint16_t padding;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
435 mode_t access_mask;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
436 #endif
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
437 off_t size;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
438 } __attribute__((packed)) attrbuf_entry;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
439
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
440 int attrkind(attrbuf_entry *entry)
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
441 {
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
442 switch (entry->obj_type) {
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
443 case VREG: return S_IFREG;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
444 case VDIR: return S_IFDIR;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
445 case VLNK: return S_IFLNK;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
446 case VBLK: return S_IFBLK;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
447 case VCHR: return S_IFCHR;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
448 case VFIFO: return S_IFIFO;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
449 case VSOCK: return S_IFSOCK;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
450 }
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
451 return -1;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
452 }
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
453
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
454 /* get these many entries at a time */
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
455 #define LISTDIR_BATCH_SIZE 50
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
456
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
457 static PyObject *_listdir_batch(char *path, int pathlen, int keepstat,
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
458 char *skip, bool *fallback)
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
459 {
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
460 PyObject *list, *elem, *stat = NULL, *ret = NULL;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
461 int kind, err;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
462 unsigned long index;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
463 unsigned int count, old_state, new_state;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
464 bool state_seen = false;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
465 attrbuf_entry *entry;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
466 /* from the getattrlist(2) man page: a path can be no longer than
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
467 (NAME_MAX * 3 + 1) bytes. Also, "The getattrlist() function will
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
468 silently truncate attribute data if attrBufSize is too small." So
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
469 pass in a buffer big enough for the worst case. */
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
470 char attrbuf[LISTDIR_BATCH_SIZE * (sizeof(attrbuf_entry) + NAME_MAX * 3 + 1)];
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
471 unsigned int basep_unused;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
472
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
473 struct stat st;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
474 int dfd = -1;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
475
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
476 /* these must match the attrbuf_entry struct, otherwise you'll end up
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
477 with garbage */
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
478 struct attrlist requested_attr = {0};
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
479 requested_attr.bitmapcount = ATTR_BIT_MAP_COUNT;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
480 requested_attr.commonattr = (ATTR_CMN_NAME | ATTR_CMN_OBJTYPE |
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
481 ATTR_CMN_MODTIME | ATTR_CMN_ACCESSMASK);
27877
f6d1e92fdf8c mac: ignore resource fork when checking file sizes
Matt Mackall <mpm@selenic.com>
parents: 27473
diff changeset
482 requested_attr.fileattr = ATTR_FILE_DATALENGTH;
24461
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
483
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
484 *fallback = false;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
485
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
486 if (pathlen >= PATH_MAX) {
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
487 errno = ENAMETOOLONG;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
488 PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
489 goto error_value;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
490 }
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
491
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
492 dfd = open(path, O_RDONLY);
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
493 if (dfd == -1) {
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
494 PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
495 goto error_value;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
496 }
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
497
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
498 list = PyList_New(0);
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
499 if (!list)
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
500 goto error_dir;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
501
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
502 do {
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
503 count = LISTDIR_BATCH_SIZE;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
504 err = getdirentriesattr(dfd, &requested_attr, &attrbuf,
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
505 sizeof(attrbuf), &count, &basep_unused,
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
506 &new_state, 0);
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
507 if (err < 0) {
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
508 if (errno == ENOTSUP) {
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
509 /* We're on a filesystem that doesn't support
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
510 getdirentriesattr. Fall back to the
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
511 stat-based implementation. */
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
512 *fallback = true;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
513 } else
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
514 PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
515 goto error;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
516 }
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
517
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
518 if (!state_seen) {
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
519 old_state = new_state;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
520 state_seen = true;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
521 } else if (old_state != new_state) {
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
522 /* There's an edge case with getdirentriesattr. Consider
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
523 the following initial list of files:
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
524
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
525 a
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
526 b
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
527 <--
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
528 c
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
529 d
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
530
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
531 If the iteration is paused at the arrow, and b is
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
532 deleted before it is resumed, getdirentriesattr will
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
533 not return d at all! Ordinarily we're expected to
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
534 restart the iteration from the beginning. To avoid
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
535 getting stuck in a retry loop here, fall back to
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
536 stat. */
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
537 *fallback = true;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
538 goto error;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
539 }
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
540
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
541 entry = (attrbuf_entry *)attrbuf;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
542
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
543 for (index = 0; index < count; index++) {
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
544 char *filename = ((char *)&entry->name) +
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
545 entry->name.attr_dataoffset;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
546
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
547 if (!strcmp(filename, ".") || !strcmp(filename, ".."))
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
548 continue;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
549
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
550 kind = attrkind(entry);
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
551 if (kind == -1) {
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
552 PyErr_Format(PyExc_OSError,
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
553 "unknown object type %u for file "
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
554 "%s%s!",
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
555 entry->obj_type, path, filename);
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
556 goto error;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
557 }
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
558
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
559 /* quit early? */
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
560 if (skip && kind == S_IFDIR && !strcmp(filename, skip)) {
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
561 ret = PyList_New(0);
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
562 goto error;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
563 }
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
564
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
565 if (keepstat) {
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
566 /* from the getattrlist(2) man page: "Only the
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
567 permission bits ... are valid". */
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
568 st.st_mode = (entry->access_mask & ~S_IFMT) | kind;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
569 st.st_mtime = entry->mtime.tv_sec;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
570 st.st_size = entry->size;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
571 stat = makestat(&st);
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
572 if (!stat)
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
573 goto error;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
574 elem = Py_BuildValue("siN", filename, kind, stat);
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
575 } else
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
576 elem = Py_BuildValue("si", filename, kind);
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
577 if (!elem)
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
578 goto error;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
579 stat = NULL;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
580
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
581 PyList_Append(list, elem);
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
582 Py_DECREF(elem);
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
583
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
584 entry = (attrbuf_entry *)((char *)entry + entry->length);
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
585 }
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
586 } while (err == 0);
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
587
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
588 ret = list;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
589 Py_INCREF(ret);
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
590
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
591 error:
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
592 Py_DECREF(list);
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
593 Py_XDECREF(stat);
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
594 error_dir:
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
595 close(dfd);
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
596 error_value:
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
597 return ret;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
598 }
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
599
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
600 #endif /* __APPLE__ */
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
601
24460
73477e755cd2 osutil._listdir: rename to _listdir_stat
Siddharth Agarwal <sid0@fb.com>
parents: 23966
diff changeset
602 static PyObject *_listdir(char *path, int pathlen, int keepstat, char *skip)
73477e755cd2 osutil._listdir: rename to _listdir_stat
Siddharth Agarwal <sid0@fb.com>
parents: 23966
diff changeset
603 {
24461
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
604 #ifdef __APPLE__
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
605 PyObject *ret;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
606 bool fallback = false;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
607
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
608 ret = _listdir_batch(path, pathlen, keepstat, skip, &fallback);
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
609 if (ret != NULL || !fallback)
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
610 return ret;
05ccfe6763f1 osutil: use getdirentriesattr on OS X if possible
Siddharth Agarwal <sid0@fb.com>
parents: 24460
diff changeset
611 #endif
24460
73477e755cd2 osutil._listdir: rename to _listdir_stat
Siddharth Agarwal <sid0@fb.com>
parents: 23966
diff changeset
612 return _listdir_stat(path, pathlen, keepstat, skip);
73477e755cd2 osutil._listdir: rename to _listdir_stat
Siddharth Agarwal <sid0@fb.com>
parents: 23966
diff changeset
613 }
73477e755cd2 osutil._listdir: rename to _listdir_stat
Siddharth Agarwal <sid0@fb.com>
parents: 23966
diff changeset
614
18026
ddc0323db78b osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents: 18021
diff changeset
615 static PyObject *statfiles(PyObject *self, PyObject *args)
ddc0323db78b osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents: 18021
diff changeset
616 {
18027
4ca434500dbf osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents: 18026
diff changeset
617 PyObject *names, *stats;
4ca434500dbf osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents: 18026
diff changeset
618 Py_ssize_t i, count;
18026
ddc0323db78b osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents: 18021
diff changeset
619
18027
4ca434500dbf osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents: 18026
diff changeset
620 if (!PyArg_ParseTuple(args, "O:statfiles", &names))
4ca434500dbf osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents: 18026
diff changeset
621 return NULL;
18026
ddc0323db78b osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents: 18021
diff changeset
622
18027
4ca434500dbf osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents: 18026
diff changeset
623 count = PySequence_Length(names);
4ca434500dbf osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents: 18026
diff changeset
624 if (count == -1) {
4ca434500dbf osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents: 18026
diff changeset
625 PyErr_SetString(PyExc_TypeError, "not a sequence");
4ca434500dbf osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents: 18026
diff changeset
626 return NULL;
4ca434500dbf osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents: 18026
diff changeset
627 }
18026
ddc0323db78b osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents: 18021
diff changeset
628
18027
4ca434500dbf osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents: 18026
diff changeset
629 stats = PyList_New(count);
4ca434500dbf osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents: 18026
diff changeset
630 if (stats == NULL)
4ca434500dbf osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents: 18026
diff changeset
631 return NULL;
18026
ddc0323db78b osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents: 18021
diff changeset
632
18027
4ca434500dbf osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents: 18026
diff changeset
633 for (i = 0; i < count; i++) {
23966
2d2c0a8eeeb8 osutil: fix memory leak of PyBytes of path in statfiles
Augie Fackler <augie@google.com>
parents: 23962
diff changeset
634 PyObject *stat, *pypath;
18027
4ca434500dbf osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents: 18026
diff changeset
635 struct stat st;
4ca434500dbf osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents: 18026
diff changeset
636 int ret, kind;
4ca434500dbf osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents: 18026
diff changeset
637 char *path;
18026
ddc0323db78b osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents: 18021
diff changeset
638
26983
f9f2f29ce023 osutil: make statfiles check for interrupts periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 26982
diff changeset
639 /* With a large file count or on a slow filesystem,
f9f2f29ce023 osutil: make statfiles check for interrupts periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 26982
diff changeset
640 don't block signals for long (issue4878). */
f9f2f29ce023 osutil: make statfiles check for interrupts periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 26982
diff changeset
641 if ((i % 1000) == 999 && PyErr_CheckSignals() == -1)
f9f2f29ce023 osutil: make statfiles check for interrupts periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 26982
diff changeset
642 goto bail;
f9f2f29ce023 osutil: make statfiles check for interrupts periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 26982
diff changeset
643
23966
2d2c0a8eeeb8 osutil: fix memory leak of PyBytes of path in statfiles
Augie Fackler <augie@google.com>
parents: 23962
diff changeset
644 pypath = PySequence_GetItem(names, i);
2d2c0a8eeeb8 osutil: fix memory leak of PyBytes of path in statfiles
Augie Fackler <augie@google.com>
parents: 23962
diff changeset
645 if (!pypath)
26982
fa6685ea7ad8 osutil: don't leak on statfiles error
Bryan O'Sullivan <bos@serpentine.com>
parents: 24462
diff changeset
646 goto bail;
30098
301ef65e8ebb osutil: convert PyString* to PyBytes*
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28593
diff changeset
647 path = PyBytes_AsString(pypath);
18027
4ca434500dbf osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents: 18026
diff changeset
648 if (path == NULL) {
23966
2d2c0a8eeeb8 osutil: fix memory leak of PyBytes of path in statfiles
Augie Fackler <augie@google.com>
parents: 23962
diff changeset
649 Py_DECREF(pypath);
18027
4ca434500dbf osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents: 18026
diff changeset
650 PyErr_SetString(PyExc_TypeError, "not a string");
4ca434500dbf osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents: 18026
diff changeset
651 goto bail;
4ca434500dbf osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents: 18026
diff changeset
652 }
4ca434500dbf osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents: 18026
diff changeset
653 ret = lstat(path, &st);
23966
2d2c0a8eeeb8 osutil: fix memory leak of PyBytes of path in statfiles
Augie Fackler <augie@google.com>
parents: 23962
diff changeset
654 Py_DECREF(pypath);
18027
4ca434500dbf osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents: 18026
diff changeset
655 kind = st.st_mode & S_IFMT;
4ca434500dbf osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents: 18026
diff changeset
656 if (ret != -1 && (kind == S_IFREG || kind == S_IFLNK)) {
4ca434500dbf osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents: 18026
diff changeset
657 stat = makestat(&st);
4ca434500dbf osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents: 18026
diff changeset
658 if (stat == NULL)
4ca434500dbf osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents: 18026
diff changeset
659 goto bail;
4ca434500dbf osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents: 18026
diff changeset
660 PyList_SET_ITEM(stats, i, stat);
4ca434500dbf osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents: 18026
diff changeset
661 } else {
4ca434500dbf osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents: 18026
diff changeset
662 Py_INCREF(Py_None);
4ca434500dbf osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents: 18026
diff changeset
663 PyList_SET_ITEM(stats, i, Py_None);
4ca434500dbf osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents: 18026
diff changeset
664 }
4ca434500dbf osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents: 18026
diff changeset
665 }
18026
ddc0323db78b osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents: 18021
diff changeset
666
18027
4ca434500dbf osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents: 18026
diff changeset
667 return stats;
18026
ddc0323db78b osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents: 18021
diff changeset
668
ddc0323db78b osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents: 18021
diff changeset
669 bail:
18027
4ca434500dbf osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents: 18026
diff changeset
670 Py_DECREF(stats);
4ca434500dbf osutil: tab damage, how i hate thee
Bryan O'Sullivan <bryano@fb.com>
parents: 18026
diff changeset
671 return NULL;
18026
ddc0323db78b osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents: 18021
diff changeset
672 }
ddc0323db78b osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents: 18021
diff changeset
673
27473
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
674 /*
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
675 * recvfds() simply does not release GIL during blocking io operation because
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
676 * command server is known to be single-threaded.
27970
3d23700cf4a9 osutil: disable compilation of recvfds() on unsupported platforms
Yuya Nishihara <yuya@tcha.org>
parents: 27877
diff changeset
677 *
3d23700cf4a9 osutil: disable compilation of recvfds() on unsupported platforms
Yuya Nishihara <yuya@tcha.org>
parents: 27877
diff changeset
678 * Old systems such as Solaris don't provide CMSG_LEN, msg_control, etc.
3d23700cf4a9 osutil: disable compilation of recvfds() on unsupported platforms
Yuya Nishihara <yuya@tcha.org>
parents: 27877
diff changeset
679 * Currently, recvfds() is not supported on these platforms.
27473
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
680 */
27970
3d23700cf4a9 osutil: disable compilation of recvfds() on unsupported platforms
Yuya Nishihara <yuya@tcha.org>
parents: 27877
diff changeset
681 #ifdef CMSG_LEN
27473
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
682
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
683 static ssize_t recvfdstobuf(int sockfd, int **rfds, void *cbuf, size_t cbufsize)
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
684 {
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
685 char dummy[1];
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
686 struct iovec iov = {dummy, sizeof(dummy)};
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
687 struct msghdr msgh = {0};
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
688 struct cmsghdr *cmsg;
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
689
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
690 msgh.msg_iov = &iov;
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
691 msgh.msg_iovlen = 1;
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
692 msgh.msg_control = cbuf;
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
693 msgh.msg_controllen = (socklen_t)cbufsize;
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
694 if (recvmsg(sockfd, &msgh, 0) < 0)
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
695 return -1;
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
696
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
697 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg;
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
698 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
699 if (cmsg->cmsg_level != SOL_SOCKET ||
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
700 cmsg->cmsg_type != SCM_RIGHTS)
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
701 continue;
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
702 *rfds = (int *)CMSG_DATA(cmsg);
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
703 return (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
704 }
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
705
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
706 *rfds = cbuf;
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
707 return 0;
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
708 }
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
709
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
710 static PyObject *recvfds(PyObject *self, PyObject *args)
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
711 {
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
712 int sockfd;
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
713 int *rfds = NULL;
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
714 ssize_t rfdscount, i;
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
715 char cbuf[256];
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
716 PyObject *rfdslist = NULL;
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
717
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
718 if (!PyArg_ParseTuple(args, "i", &sockfd))
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
719 return NULL;
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
720
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
721 rfdscount = recvfdstobuf(sockfd, &rfds, cbuf, sizeof(cbuf));
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
722 if (rfdscount < 0)
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
723 return PyErr_SetFromErrno(PyExc_OSError);
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
724
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
725 rfdslist = PyList_New(rfdscount);
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
726 if (!rfdslist)
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
727 goto bail;
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
728 for (i = 0; i < rfdscount; i++) {
30110
79f438f5dd02 osutil: use PyLongObject in recvfds
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30098
diff changeset
729 PyObject *obj = PyLong_FromLong(rfds[i]);
27473
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
730 if (!obj)
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
731 goto bail;
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
732 PyList_SET_ITEM(rfdslist, i, obj);
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
733 }
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
734 return rfdslist;
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
735
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
736 bail:
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
737 Py_XDECREF(rfdslist);
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
738 return NULL;
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
739 }
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
740
27970
3d23700cf4a9 osutil: disable compilation of recvfds() on unsupported platforms
Yuya Nishihara <yuya@tcha.org>
parents: 27877
diff changeset
741 #endif /* CMSG_LEN */
30409
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
742
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
743 #if defined(HAVE_SETPROCTITLE)
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
744 /* setproctitle is the first choice - available in FreeBSD */
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
745 #define SETPROCNAME_USE_SETPROCTITLE
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
746 #elif (defined(__linux__) || defined(__APPLE__)) && PY_MAJOR_VERSION == 2
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
747 /* rewrite the argv buffer in place - works in Linux and OS X. Py_GetArgcArgv
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
748 * in Python 3 returns the copied wchar_t **argv, thus unsupported. */
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
749 #define SETPROCNAME_USE_ARGVREWRITE
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
750 #else
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
751 #define SETPROCNAME_USE_NONE
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
752 #endif
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
753
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
754 #ifndef SETPROCNAME_USE_NONE
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
755 static PyObject *setprocname(PyObject *self, PyObject *args)
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
756 {
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
757 const char *name = NULL;
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
758 if (!PyArg_ParseTuple(args, "s", &name))
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
759 return NULL;
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
760
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
761 #if defined(SETPROCNAME_USE_SETPROCTITLE)
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
762 setproctitle("%s", name);
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
763 #elif defined(SETPROCNAME_USE_ARGVREWRITE)
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
764 {
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
765 static char *argvstart = NULL;
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
766 static size_t argvsize = 0;
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
767 if (argvstart == NULL) {
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
768 int argc = 0, i;
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
769 char **argv = NULL;
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
770 char *argvend;
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
771 extern void Py_GetArgcArgv(int *argc, char ***argv);
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
772 Py_GetArgcArgv(&argc, &argv);
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
773
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
774 /* Check the memory we can use. Typically, argv[i] and
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
775 * argv[i + 1] are continuous. */
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
776 argvend = argvstart = argv[0];
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
777 for (i = 0; i < argc; ++i) {
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
778 if (argv[i] > argvend || argv[i] < argvstart)
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
779 break; /* not continuous */
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
780 size_t len = strlen(argv[i]);
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
781 argvend = argv[i] + len + 1 /* '\0' */;
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
782 }
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
783 if (argvend > argvstart) /* sanity check */
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
784 argvsize = argvend - argvstart;
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
785 }
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
786
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
787 if (argvstart && argvsize > 1) {
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
788 int n = snprintf(argvstart, argvsize, "%s", name);
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
789 if (n >= 0 && (size_t)n < argvsize)
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
790 memset(argvstart + n, 0, argvsize - n);
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
791 }
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
792 }
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
793 #endif
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
794
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
795 Py_RETURN_NONE;
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
796 }
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
797 #endif /* ndef SETPROCNAME_USE_NONE */
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
798
31563
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
799 #ifdef HAVE_STATFS
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
800 /* given a directory path, return filesystem type (best-effort), or None */
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
801 const char *getfstype(const char *path) {
31597
2d501fb60b2d osutil: report fstype for BSD and OSX
Jun Wu <quark@fb.com>
parents: 31564
diff changeset
802 #ifdef HAVE_BSD_STATFS
2d501fb60b2d osutil: report fstype for BSD and OSX
Jun Wu <quark@fb.com>
parents: 31564
diff changeset
803 /* need to return a string field */
2d501fb60b2d osutil: report fstype for BSD and OSX
Jun Wu <quark@fb.com>
parents: 31564
diff changeset
804 static struct statfs buf;
2d501fb60b2d osutil: report fstype for BSD and OSX
Jun Wu <quark@fb.com>
parents: 31564
diff changeset
805 #else
31563
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
806 struct statfs buf;
31597
2d501fb60b2d osutil: report fstype for BSD and OSX
Jun Wu <quark@fb.com>
parents: 31564
diff changeset
807 #endif
31563
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
808 int r;
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
809 memset(&buf, 0, sizeof(buf));
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
810 r = statfs(path, &buf);
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
811 if (r != 0)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
812 return NULL;
31597
2d501fb60b2d osutil: report fstype for BSD and OSX
Jun Wu <quark@fb.com>
parents: 31564
diff changeset
813 #ifdef HAVE_BSD_STATFS
2d501fb60b2d osutil: report fstype for BSD and OSX
Jun Wu <quark@fb.com>
parents: 31564
diff changeset
814 /* BSD or OSX provides a f_fstypename field */
2d501fb60b2d osutil: report fstype for BSD and OSX
Jun Wu <quark@fb.com>
parents: 31564
diff changeset
815 return buf.f_fstypename;
2d501fb60b2d osutil: report fstype for BSD and OSX
Jun Wu <quark@fb.com>
parents: 31564
diff changeset
816 #endif
31563
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
817 /* Begin of Linux filesystems */
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
818 #ifdef ADFS_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
819 if (buf.f_type == ADFS_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
820 return "adfs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
821 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
822 #ifdef AFFS_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
823 if (buf.f_type == AFFS_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
824 return "affs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
825 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
826 #ifdef BDEVFS_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
827 if (buf.f_type == BDEVFS_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
828 return "bdevfs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
829 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
830 #ifdef BEFS_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
831 if (buf.f_type == BEFS_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
832 return "befs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
833 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
834 #ifdef BFS_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
835 if (buf.f_type == BFS_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
836 return "bfs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
837 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
838 #ifdef BINFMTFS_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
839 if (buf.f_type == BINFMTFS_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
840 return "binfmtfs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
841 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
842 #ifdef BTRFS_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
843 if (buf.f_type == BTRFS_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
844 return "btrfs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
845 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
846 #ifdef CGROUP_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
847 if (buf.f_type == CGROUP_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
848 return "cgroup";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
849 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
850 #ifdef CIFS_MAGIC_NUMBER
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
851 if (buf.f_type == CIFS_MAGIC_NUMBER)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
852 return "cifs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
853 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
854 #ifdef CODA_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
855 if (buf.f_type == CODA_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
856 return "coda";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
857 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
858 #ifdef COH_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
859 if (buf.f_type == COH_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
860 return "coh";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
861 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
862 #ifdef CRAMFS_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
863 if (buf.f_type == CRAMFS_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
864 return "cramfs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
865 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
866 #ifdef DEBUGFS_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
867 if (buf.f_type == DEBUGFS_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
868 return "debugfs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
869 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
870 #ifdef DEVFS_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
871 if (buf.f_type == DEVFS_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
872 return "devfs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
873 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
874 #ifdef DEVPTS_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
875 if (buf.f_type == DEVPTS_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
876 return "devpts";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
877 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
878 #ifdef EFIVARFS_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
879 if (buf.f_type == EFIVARFS_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
880 return "efivarfs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
881 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
882 #ifdef EFS_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
883 if (buf.f_type == EFS_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
884 return "efs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
885 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
886 #ifdef EXT_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
887 if (buf.f_type == EXT_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
888 return "ext";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
889 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
890 #ifdef EXT2_OLD_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
891 if (buf.f_type == EXT2_OLD_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
892 return "ext2";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
893 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
894 #ifdef EXT2_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
895 if (buf.f_type == EXT2_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
896 return "ext2";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
897 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
898 #ifdef EXT3_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
899 if (buf.f_type == EXT3_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
900 return "ext3";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
901 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
902 #ifdef EXT4_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
903 if (buf.f_type == EXT4_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
904 return "ext4";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
905 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
906 #ifdef FUSE_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
907 if (buf.f_type == FUSE_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
908 return "fuse";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
909 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
910 #ifdef FUTEXFS_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
911 if (buf.f_type == FUTEXFS_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
912 return "futexfs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
913 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
914 #ifdef HFS_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
915 if (buf.f_type == HFS_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
916 return "hfs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
917 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
918 #ifdef HOSTFS_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
919 if (buf.f_type == HOSTFS_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
920 return "hostfs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
921 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
922 #ifdef HPFS_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
923 if (buf.f_type == HPFS_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
924 return "hpfs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
925 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
926 #ifdef HUGETLBFS_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
927 if (buf.f_type == HUGETLBFS_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
928 return "hugetlbfs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
929 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
930 #ifdef ISOFS_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
931 if (buf.f_type == ISOFS_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
932 return "isofs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
933 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
934 #ifdef JFFS2_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
935 if (buf.f_type == JFFS2_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
936 return "jffs2";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
937 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
938 #ifdef JFS_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
939 if (buf.f_type == JFS_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
940 return "jfs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
941 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
942 #ifdef MINIX_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
943 if (buf.f_type == MINIX_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
944 return "minix";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
945 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
946 #ifdef MINIX2_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
947 if (buf.f_type == MINIX2_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
948 return "minix2";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
949 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
950 #ifdef MINIX3_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
951 if (buf.f_type == MINIX3_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
952 return "minix3";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
953 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
954 #ifdef MQUEUE_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
955 if (buf.f_type == MQUEUE_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
956 return "mqueue";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
957 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
958 #ifdef MSDOS_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
959 if (buf.f_type == MSDOS_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
960 return "msdos";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
961 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
962 #ifdef NCP_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
963 if (buf.f_type == NCP_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
964 return "ncp";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
965 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
966 #ifdef NFS_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
967 if (buf.f_type == NFS_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
968 return "nfs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
969 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
970 #ifdef NILFS_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
971 if (buf.f_type == NILFS_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
972 return "nilfs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
973 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
974 #ifdef NTFS_SB_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
975 if (buf.f_type == NTFS_SB_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
976 return "ntfs-sb";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
977 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
978 #ifdef OCFS2_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
979 if (buf.f_type == OCFS2_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
980 return "ocfs2";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
981 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
982 #ifdef OPENPROM_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
983 if (buf.f_type == OPENPROM_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
984 return "openprom";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
985 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
986 #ifdef PIPEFS_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
987 if (buf.f_type == PIPEFS_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
988 return "pipefs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
989 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
990 #ifdef PROC_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
991 if (buf.f_type == PROC_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
992 return "proc";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
993 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
994 #ifdef PSTOREFS_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
995 if (buf.f_type == PSTOREFS_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
996 return "pstorefs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
997 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
998 #ifdef QNX4_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
999 if (buf.f_type == QNX4_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1000 return "qnx4";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1001 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1002 #ifdef QNX6_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1003 if (buf.f_type == QNX6_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1004 return "qnx6";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1005 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1006 #ifdef RAMFS_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1007 if (buf.f_type == RAMFS_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1008 return "ramfs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1009 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1010 #ifdef REISERFS_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1011 if (buf.f_type == REISERFS_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1012 return "reiserfs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1013 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1014 #ifdef ROMFS_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1015 if (buf.f_type == ROMFS_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1016 return "romfs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1017 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1018 #ifdef SELINUX_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1019 if (buf.f_type == SELINUX_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1020 return "selinux";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1021 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1022 #ifdef SMACK_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1023 if (buf.f_type == SMACK_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1024 return "smack";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1025 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1026 #ifdef SMB_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1027 if (buf.f_type == SMB_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1028 return "smb";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1029 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1030 #ifdef SOCKFS_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1031 if (buf.f_type == SOCKFS_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1032 return "sockfs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1033 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1034 #ifdef SQUASHFS_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1035 if (buf.f_type == SQUASHFS_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1036 return "squashfs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1037 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1038 #ifdef SYSFS_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1039 if (buf.f_type == SYSFS_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1040 return "sysfs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1041 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1042 #ifdef SYSV2_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1043 if (buf.f_type == SYSV2_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1044 return "sysv2";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1045 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1046 #ifdef SYSV4_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1047 if (buf.f_type == SYSV4_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1048 return "sysv4";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1049 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1050 #ifdef TMPFS_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1051 if (buf.f_type == TMPFS_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1052 return "tmpfs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1053 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1054 #ifdef UDF_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1055 if (buf.f_type == UDF_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1056 return "udf";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1057 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1058 #ifdef UFS_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1059 if (buf.f_type == UFS_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1060 return "ufs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1061 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1062 #ifdef USBDEVICE_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1063 if (buf.f_type == USBDEVICE_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1064 return "usbdevice";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1065 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1066 #ifdef V9FS_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1067 if (buf.f_type == V9FS_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1068 return "v9fs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1069 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1070 #ifdef VXFS_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1071 if (buf.f_type == VXFS_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1072 return "vxfs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1073 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1074 #ifdef XENFS_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1075 if (buf.f_type == XENFS_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1076 return "xenfs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1077 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1078 #ifdef XENIX_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1079 if (buf.f_type == XENIX_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1080 return "xenix";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1081 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1082 #ifdef XFS_SUPER_MAGIC
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1083 if (buf.f_type == XFS_SUPER_MAGIC)
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1084 return "xfs";
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1085 #endif
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1086 /* End of Linux filesystems */
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1087 return NULL;
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1088 }
31564
102f291807c9 osutil: export a "getfstype" method
Jun Wu <quark@fb.com>
parents: 31563
diff changeset
1089
102f291807c9 osutil: export a "getfstype" method
Jun Wu <quark@fb.com>
parents: 31563
diff changeset
1090 static PyObject *pygetfstype(PyObject *self, PyObject *args)
102f291807c9 osutil: export a "getfstype" method
Jun Wu <quark@fb.com>
parents: 31563
diff changeset
1091 {
102f291807c9 osutil: export a "getfstype" method
Jun Wu <quark@fb.com>
parents: 31563
diff changeset
1092 const char *path = NULL;
102f291807c9 osutil: export a "getfstype" method
Jun Wu <quark@fb.com>
parents: 31563
diff changeset
1093 if (!PyArg_ParseTuple(args, "s", &path))
102f291807c9 osutil: export a "getfstype" method
Jun Wu <quark@fb.com>
parents: 31563
diff changeset
1094 return NULL;
102f291807c9 osutil: export a "getfstype" method
Jun Wu <quark@fb.com>
parents: 31563
diff changeset
1095
102f291807c9 osutil: export a "getfstype" method
Jun Wu <quark@fb.com>
parents: 31563
diff changeset
1096 const char *type = getfstype(path);
102f291807c9 osutil: export a "getfstype" method
Jun Wu <quark@fb.com>
parents: 31563
diff changeset
1097 if (type == NULL)
102f291807c9 osutil: export a "getfstype" method
Jun Wu <quark@fb.com>
parents: 31563
diff changeset
1098 Py_RETURN_NONE;
102f291807c9 osutil: export a "getfstype" method
Jun Wu <quark@fb.com>
parents: 31563
diff changeset
1099
102f291807c9 osutil: export a "getfstype" method
Jun Wu <quark@fb.com>
parents: 31563
diff changeset
1100 PyObject *result = Py_BuildValue("s", type);
102f291807c9 osutil: export a "getfstype" method
Jun Wu <quark@fb.com>
parents: 31563
diff changeset
1101 return result;
102f291807c9 osutil: export a "getfstype" method
Jun Wu <quark@fb.com>
parents: 31563
diff changeset
1102 }
31563
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1103 #endif /* def HAVE_STATFS */
cc3ec3027695 osutil: add a C function getting filesystem type
Jun Wu <quark@fb.com>
parents: 31471
diff changeset
1104
7056
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
1105 #endif /* ndef _WIN32 */
2c1f18b88b6a osutil: implementation for Win32
Petr Kodl <petrkodl@gmail.com>
parents: 7034
diff changeset
1106
7098
8a5c88c7e97b osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7059
diff changeset
1107 static PyObject *listdir(PyObject *self, PyObject *args, PyObject *kwargs)
8a5c88c7e97b osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7059
diff changeset
1108 {
8a5c88c7e97b osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7059
diff changeset
1109 PyObject *statobj = NULL; /* initialize - optional arg */
8a5c88c7e97b osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7059
diff changeset
1110 PyObject *skipobj = NULL; /* initialize - optional arg */
8a5c88c7e97b osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7059
diff changeset
1111 char *path, *skip = NULL;
8a5c88c7e97b osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7059
diff changeset
1112 int wantstat, plen;
8a5c88c7e97b osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7059
diff changeset
1113
8a5c88c7e97b osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7059
diff changeset
1114 static char *kwlist[] = {"path", "stat", "skip", NULL};
8a5c88c7e97b osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7059
diff changeset
1115
8a5c88c7e97b osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7059
diff changeset
1116 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|OO:listdir",
8a5c88c7e97b osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7059
diff changeset
1117 kwlist, &path, &plen, &statobj, &skipobj))
8a5c88c7e97b osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7059
diff changeset
1118 return NULL;
8a5c88c7e97b osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7059
diff changeset
1119
8a5c88c7e97b osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7059
diff changeset
1120 wantstat = statobj && PyObject_IsTrue(statobj);
8a5c88c7e97b osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7059
diff changeset
1121
8a5c88c7e97b osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7059
diff changeset
1122 if (skipobj && skipobj != Py_None) {
11359
4eaacccbb2ca osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
1123 skip = PyBytes_AsString(skipobj);
7098
8a5c88c7e97b osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7059
diff changeset
1124 if (!skip)
8a5c88c7e97b osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7059
diff changeset
1125 return NULL;
8a5c88c7e97b osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7059
diff changeset
1126 }
8a5c88c7e97b osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7059
diff changeset
1127
8a5c88c7e97b osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7059
diff changeset
1128 return _listdir(path, plen, wantstat, skip);
8a5c88c7e97b osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7059
diff changeset
1129 }
8a5c88c7e97b osutil.c: refactor argument parsing, allow skip=None being passed
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7059
diff changeset
1130
8330
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1131 #ifdef _WIN32
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1132 static PyObject *posixfile(PyObject *self, PyObject *args, PyObject *kwds)
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1133 {
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1134 static char *kwlist[] = {"name", "mode", "buffering", NULL};
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1135 PyObject *file_obj = NULL;
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1136 char *name = NULL;
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1137 char *mode = "rb";
8597
dc1b9e22f288 osutil: silence uninitialized variable warning
Patrick Mezard <pmezard@gmail.com>
parents: 8330
diff changeset
1138 DWORD access = 0;
8330
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1139 DWORD creation;
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1140 HANDLE handle;
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1141 int fd, flags = 0;
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1142 int bufsize = -1;
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1143 char m0, m1, m2;
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1144 char fpmode[4];
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1145 int fppos = 0;
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1146 int plus;
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1147 FILE *fp;
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1148
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1149 if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:posixfile", kwlist,
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1150 Py_FileSystemDefaultEncoding,
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1151 &name, &mode, &bufsize))
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1152 return NULL;
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1153
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1154 m0 = mode[0];
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1155 m1 = m0 ? mode[1] : '\0';
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1156 m2 = m1 ? mode[2] : '\0';
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1157 plus = m1 == '+' || m2 == '+';
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1158
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1159 fpmode[fppos++] = m0;
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1160 if (m1 == 'b' || m2 == 'b') {
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1161 flags = _O_BINARY;
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1162 fpmode[fppos++] = 'b';
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1163 }
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1164 else
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1165 flags = _O_TEXT;
13273
764441ecbf2e osutil: treat open modes 'w' and 'a' as 'w+' and 'a+' in posixfile
Adrian Buehlmann <adrian@cadifra.com>
parents: 11359
diff changeset
1166 if (m0 == 'r' && !plus) {
764441ecbf2e osutil: treat open modes 'w' and 'a' as 'w+' and 'a+' in posixfile
Adrian Buehlmann <adrian@cadifra.com>
parents: 11359
diff changeset
1167 flags |= _O_RDONLY;
764441ecbf2e osutil: treat open modes 'w' and 'a' as 'w+' and 'a+' in posixfile
Adrian Buehlmann <adrian@cadifra.com>
parents: 11359
diff changeset
1168 access = GENERIC_READ;
764441ecbf2e osutil: treat open modes 'w' and 'a' as 'w+' and 'a+' in posixfile
Adrian Buehlmann <adrian@cadifra.com>
parents: 11359
diff changeset
1169 } else {
764441ecbf2e osutil: treat open modes 'w' and 'a' as 'w+' and 'a+' in posixfile
Adrian Buehlmann <adrian@cadifra.com>
parents: 11359
diff changeset
1170 /*
764441ecbf2e osutil: treat open modes 'w' and 'a' as 'w+' and 'a+' in posixfile
Adrian Buehlmann <adrian@cadifra.com>
parents: 11359
diff changeset
1171 work around http://support.microsoft.com/kb/899149 and
764441ecbf2e osutil: treat open modes 'w' and 'a' as 'w+' and 'a+' in posixfile
Adrian Buehlmann <adrian@cadifra.com>
parents: 11359
diff changeset
1172 set _O_RDWR for 'w' and 'a', even if mode has no '+'
764441ecbf2e osutil: treat open modes 'w' and 'a' as 'w+' and 'a+' in posixfile
Adrian Buehlmann <adrian@cadifra.com>
parents: 11359
diff changeset
1173 */
8330
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1174 flags |= _O_RDWR;
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1175 access = GENERIC_READ | GENERIC_WRITE;
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1176 fpmode[fppos++] = '+';
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1177 }
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1178 fpmode[fppos++] = '\0';
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1179
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1180 switch (m0) {
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1181 case 'r':
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1182 creation = OPEN_EXISTING;
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1183 break;
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1184 case 'w':
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1185 creation = CREATE_ALWAYS;
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1186 break;
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1187 case 'a':
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1188 creation = OPEN_ALWAYS;
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1189 flags |= _O_APPEND;
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1190 break;
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1191 default:
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1192 PyErr_Format(PyExc_ValueError,
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1193 "mode string must begin with one of 'r', 'w', "
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1194 "or 'a', not '%c'", m0);
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1195 goto bail;
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1196 }
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1197
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1198 handle = CreateFile(name, access,
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1199 FILE_SHARE_READ | FILE_SHARE_WRITE |
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1200 FILE_SHARE_DELETE,
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1201 NULL,
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1202 creation,
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1203 FILE_ATTRIBUTE_NORMAL,
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1204 0);
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1205
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1206 if (handle == INVALID_HANDLE_VALUE) {
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1207 PyErr_SetFromWindowsErrWithFilename(GetLastError(), name);
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1208 goto bail;
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1209 }
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1210
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 9353
diff changeset
1211 fd = _open_osfhandle((intptr_t)handle, flags);
11359
4eaacccbb2ca osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
1212
8330
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1213 if (fd == -1) {
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1214 CloseHandle(handle);
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1215 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1216 goto bail;
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1217 }
11359
4eaacccbb2ca osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
1218 #ifndef IS_PY3K
8330
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1219 fp = _fdopen(fd, fpmode);
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1220 if (fp == NULL) {
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1221 _close(fd);
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1222 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1223 goto bail;
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1224 }
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1225
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1226 file_obj = PyFile_FromFile(fp, name, mode, fclose);
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1227 if (file_obj == NULL) {
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1228 fclose(fp);
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1229 goto bail;
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1230 }
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1231
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1232 PyFile_SetBufSize(file_obj, bufsize);
11359
4eaacccbb2ca osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
1233 #else
4eaacccbb2ca osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
1234 file_obj = PyFile_FromFd(fd, name, mode, bufsize, NULL, NULL, NULL, 1);
4eaacccbb2ca osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
1235 if (file_obj == NULL)
4eaacccbb2ca osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
1236 goto bail;
4eaacccbb2ca osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
1237 #endif
8330
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1238 bail:
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1239 PyMem_Free(name);
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1240 return file_obj;
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1241 }
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1242 #endif
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1243
13734
16118b4859a1 util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13302
diff changeset
1244 #ifdef __APPLE__
13748
26f8844d1757 osutil: replace #import with #include, and add a check for it
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13736
diff changeset
1245 #include <ApplicationServices/ApplicationServices.h>
13734
16118b4859a1 util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13302
diff changeset
1246
16118b4859a1 util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13302
diff changeset
1247 static PyObject *isgui(PyObject *self)
16118b4859a1 util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13302
diff changeset
1248 {
13736
f3c4421e121c osutil: fix up check-code issues
Matt Mackall <mpm@selenic.com>
parents: 13734
diff changeset
1249 CFDictionaryRef dict = CGSessionCopyCurrentDictionary();
13734
16118b4859a1 util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13302
diff changeset
1250
13736
f3c4421e121c osutil: fix up check-code issues
Matt Mackall <mpm@selenic.com>
parents: 13734
diff changeset
1251 if (dict != NULL) {
f3c4421e121c osutil: fix up check-code issues
Matt Mackall <mpm@selenic.com>
parents: 13734
diff changeset
1252 CFRelease(dict);
15094
258eee414ab7 osutil: avoid accidentally destroying the True object in isgui (issue2937)
Steve Streeting <steve@stevestreeting.com>
parents: 13748
diff changeset
1253 Py_RETURN_TRUE;
13736
f3c4421e121c osutil: fix up check-code issues
Matt Mackall <mpm@selenic.com>
parents: 13734
diff changeset
1254 } else {
15094
258eee414ab7 osutil: avoid accidentally destroying the True object in isgui (issue2937)
Steve Streeting <steve@stevestreeting.com>
parents: 13748
diff changeset
1255 Py_RETURN_FALSE;
13736
f3c4421e121c osutil: fix up check-code issues
Matt Mackall <mpm@selenic.com>
parents: 13734
diff changeset
1256 }
13734
16118b4859a1 util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13302
diff changeset
1257 }
16118b4859a1 util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13302
diff changeset
1258 #endif
16118b4859a1 util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13302
diff changeset
1259
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
1260 static char osutil_doc[] = "Native operating system services.";
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
1261
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
1262 static PyMethodDef methods[] = {
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
1263 {"listdir", (PyCFunction)listdir, METH_VARARGS | METH_KEYWORDS,
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
1264 "list a directory\n"},
8330
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1265 #ifdef _WIN32
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1266 {"posixfile", (PyCFunction)posixfile, METH_VARARGS | METH_KEYWORDS,
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1267 "Open a file with POSIX-like semantics.\n"
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1268 "On error, this function may raise either a WindowsError or an IOError."},
18026
ddc0323db78b osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents: 18021
diff changeset
1269 #else
ddc0323db78b osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents: 18021
diff changeset
1270 {"statfiles", (PyCFunction)statfiles, METH_VARARGS | METH_KEYWORDS,
ddc0323db78b osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents: 18021
diff changeset
1271 "stat a series of files or symlinks\n"
ddc0323db78b osutil: write a C implementation of statfiles for unix
Bryan O'Sullivan <bryano@fb.com>
parents: 18021
diff changeset
1272 "Returns None for non-existent entries and entries of other types.\n"},
27970
3d23700cf4a9 osutil: disable compilation of recvfds() on unsupported platforms
Yuya Nishihara <yuya@tcha.org>
parents: 27877
diff changeset
1273 #ifdef CMSG_LEN
27473
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
1274 {"recvfds", (PyCFunction)recvfds, METH_VARARGS,
34a37a2e03e6 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
Yuya Nishihara <yuya@tcha.org>
parents: 26983
diff changeset
1275 "receive list of file descriptors via socket\n"},
8330
7de68012f86e Windows: improve performance via buffered I/O
Bryan O'Sullivan <bos@serpentine.com>
parents: 7190
diff changeset
1276 #endif
30409
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
1277 #ifndef SETPROCNAME_USE_NONE
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
1278 {"setprocname", (PyCFunction)setprocname, METH_VARARGS,
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
1279 "set process title (best-effort)\n"},
27970
3d23700cf4a9 osutil: disable compilation of recvfds() on unsupported platforms
Yuya Nishihara <yuya@tcha.org>
parents: 27877
diff changeset
1280 #endif
31564
102f291807c9 osutil: export a "getfstype" method
Jun Wu <quark@fb.com>
parents: 31563
diff changeset
1281 #ifdef HAVE_STATFS
102f291807c9 osutil: export a "getfstype" method
Jun Wu <quark@fb.com>
parents: 31563
diff changeset
1282 {"getfstype", (PyCFunction)pygetfstype, METH_VARARGS,
102f291807c9 osutil: export a "getfstype" method
Jun Wu <quark@fb.com>
parents: 31563
diff changeset
1283 "get filesystem type (best-effort)\n"},
102f291807c9 osutil: export a "getfstype" method
Jun Wu <quark@fb.com>
parents: 31563
diff changeset
1284 #endif
30409
0852161588c6 osutil: implement setprocname to set process title for some platforms
Jun Wu <quark@fb.com>
parents: 30111
diff changeset
1285 #endif /* ndef _WIN32 */
13734
16118b4859a1 util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13302
diff changeset
1286 #ifdef __APPLE__
13736
f3c4421e121c osutil: fix up check-code issues
Matt Mackall <mpm@selenic.com>
parents: 13734
diff changeset
1287 {
f3c4421e121c osutil: fix up check-code issues
Matt Mackall <mpm@selenic.com>
parents: 13734
diff changeset
1288 "isgui", (PyCFunction)isgui, METH_NOARGS,
f3c4421e121c osutil: fix up check-code issues
Matt Mackall <mpm@selenic.com>
parents: 13734
diff changeset
1289 "Is a CoreGraphics session available?"
f3c4421e121c osutil: fix up check-code issues
Matt Mackall <mpm@selenic.com>
parents: 13734
diff changeset
1290 },
13734
16118b4859a1 util: add Mac-specific check whether we're in a GUI session (issue2553)
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 13302
diff changeset
1291 #endif
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
1292 {NULL, NULL}
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
1293 };
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
1294
11359
4eaacccbb2ca osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
1295 #ifdef IS_PY3K
4eaacccbb2ca osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
1296 static struct PyModuleDef osutil_module = {
4eaacccbb2ca osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
1297 PyModuleDef_HEAD_INIT,
4eaacccbb2ca osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
1298 "osutil",
4eaacccbb2ca osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
1299 osutil_doc,
4eaacccbb2ca osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
1300 -1,
4eaacccbb2ca osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
1301 methods
4eaacccbb2ca osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
1302 };
4eaacccbb2ca osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
1303
4eaacccbb2ca osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
1304 PyMODINIT_FUNC PyInit_osutil(void)
4eaacccbb2ca osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
1305 {
4eaacccbb2ca osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
1306 if (PyType_Ready(&listdir_stat_type) < 0)
4eaacccbb2ca osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
1307 return NULL;
4eaacccbb2ca osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
1308
4eaacccbb2ca osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
1309 return PyModule_Create(&osutil_module);
4eaacccbb2ca osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
1310 }
4eaacccbb2ca osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
1311 #else
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
1312 PyMODINIT_FUNC initosutil(void)
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
1313 {
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
1314 if (PyType_Ready(&listdir_stat_type) == -1)
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
1315 return;
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
1316
5421
9b5d626be8ba osutil: cleanups
Matt Mackall <mpm@selenic.com>
parents: 5416
diff changeset
1317 Py_InitModule3("osutil", methods, osutil_doc);
5396
5105b119edd2 Add osutil module, containing a listdir function.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
1318 }
11359
4eaacccbb2ca osutil.c: Support for py3k added.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
1319 #endif