root/releases/pkgcore/0.2.10/src/chflags.c @ marienz%2540gentoo.org-20061129021619-h92kexkktibvv60m

Revision marienz%2540gentoo.org-20061129021619-h92kexkktibvv60m, 3.2 KB (checked in by Marien Zwart <marienz@…>, 2 years ago)

Improve error handling in cpython module initialization.

Line 
1/*
2 * Original author: Stephen Bennett <spb@gentoo.org>
3 * Modified by Marien Zwart <marienz@gentoo.org>
4 */
5
6#include "Python.h"
7
8#include <sys/stat.h>
9
10static const unsigned long problemflags = 0x00160016;
11
12static PyObject *
13chflags_lchflags(PyObject *self, PyObject *args)
14{
15    char *path = NULL;
16    int flags;
17    int res;
18
19    if (!PyArg_ParseTuple(args, "eti:lchflags",
20                          Py_FileSystemDefaultEncoding, &path, &flags)) {
21        return NULL;
22    }
23
24    res = lchflags(path, flags);
25
26    PyMem_Free(path);
27
28    if (res < 0) {
29        return PyErr_SetFromErrno(PyExc_OSError);
30    }
31
32    return PyInt_FromLong((long)res);
33}
34
35static PyObject *
36chflags_lhasproblems(PyObject *self, PyObject *args)
37{
38    char *path = NULL;
39    struct stat sb;
40    int res;
41
42    if (!PyArg_ParseTuple(args, "et:lhasproblems",
43                          Py_FileSystemDefaultEncoding, &path)) {
44        return NULL;
45    }
46
47    res = lstat(path, &sb);
48
49    PyMem_Free(path);
50
51    if (res < 0) {
52        return PyErr_SetFromErrno(PyExc_OSError);
53    }
54
55    return PyBool_FromLong(sb.st_flags & problemflags);
56}
57
58static PyObject *
59chflags_lgetflags(PyObject *self, PyObject *args)
60{
61    char *path = NULL;
62    struct stat sb;
63    int res;
64
65    if (!PyArg_ParseTuple(args, "et:lgetflags",
66                          Py_FileSystemDefaultEncoding, &path)) {
67        return NULL;
68    }
69
70    res = lstat(path, &sb);
71
72    PyMem_Free(path);
73
74    if (res < 0) {
75        return PyErr_SetFromErrno(PyExc_OSError);
76    }
77
78    return PyInt_FromLong((long)sb.st_flags);
79}
80
81PyDoc_STRVAR(
82    chflags_lchflags__doc__,
83    "lchflags(path, flags) -> None\n\
84Change the flags on path to equal flags.");
85
86PyDoc_STRVAR(
87    chflags_lgetflags__doc__,
88    "lgetflags(path) -> Integer\n\
89Returns the file flags on path.");
90
91PyDoc_STRVAR(
92    chflags_lhasproblems__doc__,
93    "lhasproblems(path) -> Integer\n\
94Returns True if path has any flags set that prevent write operations;\n\
95False otherwise.");
96
97static PyMethodDef chflags_methods[] = {
98    {"lchflags", chflags_lchflags, METH_VARARGS, chflags_lchflags__doc__},
99    {"lgetflags", chflags_lgetflags, METH_VARARGS, chflags_lgetflags__doc__},
100    {"lhasproblems", chflags_lhasproblems, METH_VARARGS,
101     chflags_lhasproblems__doc__},
102    {NULL}
103};
104
105PyDoc_STRVAR(
106    chflags__doc__,
107    "Provide some operations for manipulating FreeBSD's filesystem flags");
108
109PyMODINIT_FUNC
110init_chflags()
111{
112    PyObject *m = Py_InitModule3("chflags", chflags_methods, chflags__doc__);
113    if (!m)
114        return;
115
116#define addconst(name, value) \
117    if (PyModule_AddIntConstant(m, (name), (value)) == -1) \
118        return;
119
120    addconst("UF_SETTABLE",  UF_SETTABLE);
121    addconst("UF_NODUMP",    UF_NODUMP);
122    addconst("UF_IMMUTABLE", UF_IMMUTABLE);
123    addconst("UF_APPEND",    UF_APPEND);
124    addconst("UF_OPAQUE",    UF_OPAQUE);
125    addconst("UF_NOUNLINK",  UF_NOUNLINK);
126
127    addconst("SF_SETTABLE",  SF_SETTABLE);
128    addconst("SF_NODUMP",    SF_NODUMP);
129    addconst("SF_IMMUTABLE", SF_IMMUTABLE);
130    addconst("SF_APPEND",    SF_APPEND);
131    addconst("SF_OPAQUE",    SF_OPAQUE);
132    addconst("SF_NOUNLINK",  SF_NOUNLINK);
133    addconst("SF_SNAPSHOT",  SF_SNAPSHOT);
134
135    addconst("PROBLEM_FLAGS", problemflags);
136}
Note: See TracBrowser for help on using the browser.