~ubuntu-branches/ubuntu/gutsy/libapache2-mod-python/gutsy

« back to all changes in this revision

Viewing changes to src/finfoobject.c

  • Committer: Bazaar Package Importer
  • Author(s): Piotr Ożarowski
  • Date: 2007-04-12 20:52:05 UTC
  • mfrom: (1.2.4 upstream) (1.1.2 etch)
  • Revision ID: james.westby@ubuntu.com-20070412205205-j4qlsw3o4tl615iq
Tags: 3.3.1-1
* New upstream release
* Remove configure and mod_python.h files in clean rule to make the diff.gz
  file smaller
* Current Python version in libapache2-mod-pythonX.Y package name (Provides:
  field) filled in automatically.
* Added XS-Vcs-Browser field

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2004 Apache Software Foundation 
 
3
 * 
 
4
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 
5
 * may not use this file except in compliance with the License.  You
 
6
 * may obtain a copy of the License at
 
7
 *
 
8
 *      http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 
13
 * implied.  See the License for the specific language governing
 
14
 * permissions and limitations under the License.
 
15
 *
 
16
 * Originally developed by Gregory Trubetskoy.
 
17
 *
 
18
 *
 
19
 * finfoobject.c 
 
20
 *
 
21
 * $Id: finfoobject.c 466105 2006-10-20 13:28:02Z jgallacher $
 
22
 *
 
23
 */
 
24
 
 
25
#include "mod_python.h"
 
26
 
 
27
/**
 
28
 **     MpFinfo_FromFinfo
 
29
 **
 
30
 *      This routine creates a Python finfoobject given an Apache
 
31
 *      finfo pointer.
 
32
 *
 
33
 */
 
34
 
 
35
PyObject * MpFinfo_FromFinfo(apr_finfo_t *f)
 
36
{
 
37
    finfoobject *result;
 
38
 
 
39
    result = PyObject_New(finfoobject, &MpFinfo_Type);
 
40
    if (! result)
 
41
        return PyErr_NoMemory();
 
42
 
 
43
    result->finfo = f;
 
44
    result->pool = NULL;
 
45
 
 
46
    return (PyObject *)result;
 
47
}
 
48
 
 
49
/** 
 
50
 ** MpFinfo_New
 
51
 **
 
52
 *  This returns a new object of built-in type finfo.
 
53
 *
 
54
 *  NOTE: The apr_finfo_t gets greated in its own pool, which lives
 
55
 *  throught the life of the finfoobject.
 
56
 *
 
57
 */
 
58
 
 
59
PyObject * MpFinfo_New()
 
60
{
 
61
    finfoobject *f;
 
62
    apr_pool_t *p;
 
63
 
 
64
    /* XXX need second arg abort function to report mem error */
 
65
    apr_pool_create_ex(&p, NULL, NULL, NULL);
 
66
 
 
67
    f = (finfoobject *)MpFinfo_FromFinfo(apr_pcalloc(p, sizeof(apr_finfo_t)));
 
68
 
 
69
    /* remember the pointer to our own pool */
 
70
    f->pool = p;
 
71
 
 
72
    return (PyObject *)f;
 
73
}
 
74
 
 
75
/**
 
76
 ** finfo_dealloc
 
77
 **
 
78
 *      Frees finfo's memory
 
79
 */
 
80
 
 
81
static void finfo_dealloc(register finfoobject *self)
 
82
{  
 
83
    if (MpFinfo_Check(self)) {
 
84
        if (self->pool) 
 
85
            apr_pool_destroy(self->pool);
 
86
        PyObject_Del(self);
 
87
    }
 
88
    else
 
89
        self->ob_type->tp_free((PyObject *)self);
 
90
}
 
91
 
 
92
/**
 
93
 ** finfo_getattr
 
94
 **
 
95
 *  Get finfo object attributes
 
96
 *
 
97
 */
 
98
 
 
99
static PyObject * finfo_getattr(finfoobject *self, char *name)
 
100
{
 
101
    if (strcmp(name, "fname") == 0) {
 
102
        if (self->finfo->fname)
 
103
            return PyString_FromString(self->finfo->fname);
 
104
    }
 
105
    else if (strcmp(name, "filetype") == 0) {
 
106
        return PyInt_FromLong(self->finfo->filetype);
 
107
    }
 
108
    else if (strcmp(name, "valid") == 0) {
 
109
        if (self->finfo->filetype == APR_NOFILE) {
 
110
            Py_INCREF(Py_None);
 
111
            return Py_None;
 
112
        }
 
113
        return PyInt_FromLong(self->finfo->valid);
 
114
    }
 
115
    else if (strcmp(name, "protection") == 0) {
 
116
        if (self->finfo->filetype == APR_NOFILE) {
 
117
            Py_INCREF(Py_None);
 
118
            return Py_None;
 
119
        }
 
120
        if (self->finfo->valid & APR_FINFO_PROT)
 
121
            return PyInt_FromLong(self->finfo->protection);
 
122
    }
 
123
    else if (strcmp(name, "user") == 0) {
 
124
        if (self->finfo->filetype == APR_NOFILE) {
 
125
            Py_INCREF(Py_None);
 
126
            return Py_None;
 
127
        }
 
128
        if (self->finfo->valid & APR_FINFO_USER)
 
129
            return PyInt_FromLong(self->finfo->user);
 
130
    }
 
131
    else if (strcmp(name, "group") == 0) {
 
132
        if (self->finfo->filetype == APR_NOFILE) {
 
133
            Py_INCREF(Py_None);
 
134
            return Py_None;
 
135
        }
 
136
        if (self->finfo->valid & APR_FINFO_GROUP)
 
137
            return PyInt_FromLong(self->finfo->group);
 
138
    }
 
139
    else if (strcmp(name, "inode") == 0) {
 
140
        if (self->finfo->filetype == APR_NOFILE) {
 
141
            Py_INCREF(Py_None);
 
142
            return Py_None;
 
143
        }
 
144
        if (self->finfo->valid & APR_FINFO_INODE)
 
145
            return PyInt_FromLong(self->finfo->inode);
 
146
    }
 
147
    else if (strcmp(name, "device") == 0) {
 
148
        if (self->finfo->filetype == APR_NOFILE) {
 
149
            Py_INCREF(Py_None);
 
150
            return Py_None;
 
151
        }
 
152
        if (self->finfo->valid & APR_FINFO_DEV)
 
153
            return PyInt_FromLong(self->finfo->device);
 
154
    }
 
155
    else if (strcmp(name, "nlink") == 0) {
 
156
        if (self->finfo->filetype == APR_NOFILE) {
 
157
            Py_INCREF(Py_None);
 
158
            return Py_None;
 
159
        }
 
160
        if (self->finfo->valid & APR_FINFO_NLINK)
 
161
            return PyInt_FromLong(self->finfo->nlink);
 
162
    }
 
163
    else if (strcmp(name, "size") == 0) {
 
164
        if (self->finfo->filetype == APR_NOFILE) {
 
165
            Py_INCREF(Py_None);
 
166
            return Py_None;
 
167
        }
 
168
        if (self->finfo->valid & APR_FINFO_SIZE) {
 
169
            if (sizeof(apr_off_t) == sizeof(LONG_LONG)) {
 
170
                return PyLong_FromLongLong(self->finfo->size);
 
171
            }
 
172
            else {
 
173
                return PyLong_FromLong(self->finfo->size);
 
174
            }
 
175
        }
 
176
    }
 
177
    else if (strcmp(name, "atime") == 0) {
 
178
        if (self->finfo->filetype == APR_NOFILE) {
 
179
            Py_INCREF(Py_None);
 
180
            return Py_None;
 
181
        }
 
182
        if (self->finfo->valid & APR_FINFO_ATIME)
 
183
            return PyInt_FromLong(self->finfo->atime*0.000001);
 
184
    }
 
185
    else if (strcmp(name, "mtime") == 0) {
 
186
        if (self->finfo->filetype == APR_NOFILE) {
 
187
            Py_INCREF(Py_None);
 
188
            return Py_None;
 
189
        }
 
190
        if (self->finfo->valid & APR_FINFO_MTIME)
 
191
            return PyInt_FromLong(self->finfo->mtime*0.000001);
 
192
    }
 
193
    else if (strcmp(name, "ctime") == 0) {
 
194
        if (self->finfo->filetype == APR_NOFILE) {
 
195
            Py_INCREF(Py_None);
 
196
            return Py_None;
 
197
        }
 
198
        if (self->finfo->valid & APR_FINFO_CTIME)
 
199
            return PyInt_FromLong(self->finfo->ctime*0.000001);
 
200
    }
 
201
    else if (strcmp(name, "name") == 0) {
 
202
        if (self->finfo->filetype == APR_NOFILE) {
 
203
            Py_INCREF(Py_None);
 
204
            return Py_None;
 
205
        }
 
206
        if (self->finfo->valid & APR_FINFO_NAME)
 
207
            return PyString_FromString(self->finfo->name);
 
208
    }
 
209
    else {
 
210
        PyErr_Format(PyExc_AttributeError,
 
211
                     "class 'mp_finfo' has no attribute '%.400s'", name);
 
212
        return NULL;
 
213
    }
 
214
 
 
215
    Py_INCREF(Py_None);
 
216
    return Py_None;
 
217
}
 
218
 
 
219
static PyObject*
 
220
finfoseq_item(finfoobject *self, int i)
 
221
{
 
222
    if (i < 0 || i >= 12) {
 
223
        PyErr_SetString(PyExc_IndexError, "tuple index out of range");
 
224
        return NULL;
 
225
    }
 
226
 
 
227
    switch (i) {
 
228
        case 0: {
 
229
          return finfo_getattr(self, "protection");
 
230
        }
 
231
        case 1: {
 
232
            return finfo_getattr(self, "inode");
 
233
        }
 
234
        case 2: {
 
235
            return finfo_getattr(self, "device");
 
236
        }
 
237
        case 3: {
 
238
            return finfo_getattr(self, "nlink");
 
239
        }
 
240
        case 4: {
 
241
            return finfo_getattr(self, "user");
 
242
        }
 
243
        case 5: {
 
244
            return finfo_getattr(self, "group");
 
245
        }
 
246
        case 6: {
 
247
            return finfo_getattr(self, "size");
 
248
        }
 
249
        case 7: {
 
250
            return finfo_getattr(self, "atime");
 
251
        }
 
252
        case 8: {
 
253
            return finfo_getattr(self, "mtime");
 
254
        }
 
255
        case 9: {
 
256
            return finfo_getattr(self, "ctime");
 
257
        }
 
258
        case 10: {
 
259
            return finfo_getattr(self, "fname");
 
260
        }
 
261
        case 11: {
 
262
            return finfo_getattr(self, "name");
 
263
        }
 
264
        case 12: {
 
265
            return finfo_getattr(self, "filetype");
 
266
        }
 
267
    }
 
268
 
 
269
    Py_INCREF(Py_None);
 
270
    return Py_None;
 
271
}
 
272
 
 
273
static PySequenceMethods finfoseq_as_sequence = {
 
274
        0,
 
275
        0,                                      /* sq_concat */
 
276
        0,                                      /* sq_repeat */
 
277
        /* PYTHON 2.5: WARNING: 'intargfunc' must be replaced with 'ssizeargfunc' */
 
278
        (intargfunc)finfoseq_item,              /* sq_item */
 
279
        0,                                      /* sq_slice */
 
280
        0,                                      /* sq_ass_item */
 
281
        0,                                      /* sq_ass_slice */
 
282
        0,                                      /* sq_contains */
 
283
};
 
284
 
 
285
/**
 
286
 ** finfo_repr
 
287
 **
 
288
 *
 
289
 */
 
290
 
 
291
static PyObject *finfo_repr(finfoobject *self)
 
292
{
 
293
    PyObject *s = PyString_FromString("{");
 
294
    PyObject *t = NULL;
 
295
 
 
296
    PyString_ConcatAndDel(&s, PyString_FromString("'fname': "));
 
297
    t = finfo_getattr(self, "fname");
 
298
    PyString_ConcatAndDel(&s, PyObject_Repr(t));
 
299
    Py_XDECREF(t);
 
300
 
 
301
    PyString_ConcatAndDel(&s, PyString_FromString(", 'filetype': "));
 
302
    t = finfo_getattr(self, "filetype");
 
303
    PyString_ConcatAndDel(&s, PyObject_Repr(t));
 
304
    Py_XDECREF(t);
 
305
 
 
306
    PyString_ConcatAndDel(&s, PyString_FromString(", 'valid': "));
 
307
    t = finfo_getattr(self, "valid");
 
308
    PyString_ConcatAndDel(&s, PyObject_Repr(t));
 
309
    Py_XDECREF(t);
 
310
 
 
311
    PyString_ConcatAndDel(&s, PyString_FromString(", 'protection': "));
 
312
    t = finfo_getattr(self, "protection");
 
313
    PyString_ConcatAndDel(&s, PyObject_Repr(t));
 
314
    Py_XDECREF(t);
 
315
 
 
316
    PyString_ConcatAndDel(&s, PyString_FromString(", 'user': "));
 
317
    t = finfo_getattr(self, "user");
 
318
    PyString_ConcatAndDel(&s, PyObject_Repr(t));
 
319
    Py_XDECREF(t);
 
320
 
 
321
    PyString_ConcatAndDel(&s, PyString_FromString(", 'group': "));
 
322
    t = finfo_getattr(self, "group");
 
323
    PyString_ConcatAndDel(&s, PyObject_Repr(t));
 
324
    Py_XDECREF(t);
 
325
 
 
326
    PyString_ConcatAndDel(&s, PyString_FromString(", 'size': "));
 
327
    t = finfo_getattr(self, "size");
 
328
    PyString_ConcatAndDel(&s, PyObject_Repr(t));
 
329
    Py_XDECREF(t);
 
330
 
 
331
    PyString_ConcatAndDel(&s, PyString_FromString(", 'inode': "));
 
332
    t = finfo_getattr(self, "inode");
 
333
    PyString_ConcatAndDel(&s, PyObject_Repr(t));
 
334
    Py_XDECREF(t);
 
335
 
 
336
    PyString_ConcatAndDel(&s, PyString_FromString(", 'device': "));
 
337
    t = finfo_getattr(self, "device");
 
338
    PyString_ConcatAndDel(&s, PyObject_Repr(t));
 
339
    Py_XDECREF(t);
 
340
 
 
341
    PyString_ConcatAndDel(&s, PyString_FromString(", 'nlink': "));
 
342
    t = finfo_getattr(self, "nlink");
 
343
    PyString_ConcatAndDel(&s, PyObject_Repr(t));
 
344
    Py_XDECREF(t);
 
345
 
 
346
    PyString_ConcatAndDel(&s, PyString_FromString(", 'atime': "));
 
347
    t = finfo_getattr(self, "atime");
 
348
    PyString_ConcatAndDel(&s, PyObject_Repr(t));
 
349
    Py_XDECREF(t);
 
350
 
 
351
    PyString_ConcatAndDel(&s, PyString_FromString(", 'mtime': "));
 
352
    t = finfo_getattr(self, "mtime");
 
353
    PyString_ConcatAndDel(&s, PyObject_Repr(t));
 
354
    Py_XDECREF(t);
 
355
 
 
356
    PyString_ConcatAndDel(&s, PyString_FromString(", 'ctime': "));
 
357
    t = finfo_getattr(self, "ctime");
 
358
    PyString_ConcatAndDel(&s, PyObject_Repr(t));
 
359
    Py_XDECREF(t);
 
360
 
 
361
    PyString_ConcatAndDel(&s, PyString_FromString(", 'name': "));
 
362
    t = finfo_getattr(self, "name");
 
363
    PyString_ConcatAndDel(&s, PyObject_Repr(t));
 
364
    Py_XDECREF(t);
 
365
 
 
366
    PyString_ConcatAndDel(&s, PyString_FromString("}"));
 
367
 
 
368
    return s;
 
369
}
 
370
 
 
371
PyTypeObject MpFinfo_Type = {
 
372
    PyObject_HEAD_INIT(NULL)
 
373
    0,
 
374
    "mp_finfo",
 
375
    sizeof(finfoobject),
 
376
    0,
 
377
    (destructor)finfo_dealloc,          /* tp_dealloc */
 
378
    0,                                  /* tp_print */
 
379
    (getattrfunc)finfo_getattr,         /*tp_getattr*/
 
380
    0,                                  /*tp_setattr*/
 
381
    0,                                  /* tp_compare */
 
382
    (reprfunc)finfo_repr,               /*tp_repr*/
 
383
    0,                                  /* tp_as_number */
 
384
    &finfoseq_as_sequence,              /* tp_as_sequence */
 
385
    0,                                  /* tp_as_mapping */
 
386
    0,                                  /* tp_hash */
 
387
};