2
* Copyright 2004 Apache Software Foundation
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
16
* Originally developed by Gregory Trubetskoy.
21
* $Id: finfoobject.c 466105 2006-10-20 13:28:02Z jgallacher $
25
#include "mod_python.h"
30
* This routine creates a Python finfoobject given an Apache
35
PyObject * MpFinfo_FromFinfo(apr_finfo_t *f)
39
result = PyObject_New(finfoobject, &MpFinfo_Type);
41
return PyErr_NoMemory();
46
return (PyObject *)result;
52
* This returns a new object of built-in type finfo.
54
* NOTE: The apr_finfo_t gets greated in its own pool, which lives
55
* throught the life of the finfoobject.
59
PyObject * MpFinfo_New()
64
/* XXX need second arg abort function to report mem error */
65
apr_pool_create_ex(&p, NULL, NULL, NULL);
67
f = (finfoobject *)MpFinfo_FromFinfo(apr_pcalloc(p, sizeof(apr_finfo_t)));
69
/* remember the pointer to our own pool */
78
* Frees finfo's memory
81
static void finfo_dealloc(register finfoobject *self)
83
if (MpFinfo_Check(self)) {
85
apr_pool_destroy(self->pool);
89
self->ob_type->tp_free((PyObject *)self);
95
* Get finfo object attributes
99
static PyObject * finfo_getattr(finfoobject *self, char *name)
101
if (strcmp(name, "fname") == 0) {
102
if (self->finfo->fname)
103
return PyString_FromString(self->finfo->fname);
105
else if (strcmp(name, "filetype") == 0) {
106
return PyInt_FromLong(self->finfo->filetype);
108
else if (strcmp(name, "valid") == 0) {
109
if (self->finfo->filetype == APR_NOFILE) {
113
return PyInt_FromLong(self->finfo->valid);
115
else if (strcmp(name, "protection") == 0) {
116
if (self->finfo->filetype == APR_NOFILE) {
120
if (self->finfo->valid & APR_FINFO_PROT)
121
return PyInt_FromLong(self->finfo->protection);
123
else if (strcmp(name, "user") == 0) {
124
if (self->finfo->filetype == APR_NOFILE) {
128
if (self->finfo->valid & APR_FINFO_USER)
129
return PyInt_FromLong(self->finfo->user);
131
else if (strcmp(name, "group") == 0) {
132
if (self->finfo->filetype == APR_NOFILE) {
136
if (self->finfo->valid & APR_FINFO_GROUP)
137
return PyInt_FromLong(self->finfo->group);
139
else if (strcmp(name, "inode") == 0) {
140
if (self->finfo->filetype == APR_NOFILE) {
144
if (self->finfo->valid & APR_FINFO_INODE)
145
return PyInt_FromLong(self->finfo->inode);
147
else if (strcmp(name, "device") == 0) {
148
if (self->finfo->filetype == APR_NOFILE) {
152
if (self->finfo->valid & APR_FINFO_DEV)
153
return PyInt_FromLong(self->finfo->device);
155
else if (strcmp(name, "nlink") == 0) {
156
if (self->finfo->filetype == APR_NOFILE) {
160
if (self->finfo->valid & APR_FINFO_NLINK)
161
return PyInt_FromLong(self->finfo->nlink);
163
else if (strcmp(name, "size") == 0) {
164
if (self->finfo->filetype == APR_NOFILE) {
168
if (self->finfo->valid & APR_FINFO_SIZE) {
169
if (sizeof(apr_off_t) == sizeof(LONG_LONG)) {
170
return PyLong_FromLongLong(self->finfo->size);
173
return PyLong_FromLong(self->finfo->size);
177
else if (strcmp(name, "atime") == 0) {
178
if (self->finfo->filetype == APR_NOFILE) {
182
if (self->finfo->valid & APR_FINFO_ATIME)
183
return PyInt_FromLong(self->finfo->atime*0.000001);
185
else if (strcmp(name, "mtime") == 0) {
186
if (self->finfo->filetype == APR_NOFILE) {
190
if (self->finfo->valid & APR_FINFO_MTIME)
191
return PyInt_FromLong(self->finfo->mtime*0.000001);
193
else if (strcmp(name, "ctime") == 0) {
194
if (self->finfo->filetype == APR_NOFILE) {
198
if (self->finfo->valid & APR_FINFO_CTIME)
199
return PyInt_FromLong(self->finfo->ctime*0.000001);
201
else if (strcmp(name, "name") == 0) {
202
if (self->finfo->filetype == APR_NOFILE) {
206
if (self->finfo->valid & APR_FINFO_NAME)
207
return PyString_FromString(self->finfo->name);
210
PyErr_Format(PyExc_AttributeError,
211
"class 'mp_finfo' has no attribute '%.400s'", name);
220
finfoseq_item(finfoobject *self, int i)
222
if (i < 0 || i >= 12) {
223
PyErr_SetString(PyExc_IndexError, "tuple index out of range");
229
return finfo_getattr(self, "protection");
232
return finfo_getattr(self, "inode");
235
return finfo_getattr(self, "device");
238
return finfo_getattr(self, "nlink");
241
return finfo_getattr(self, "user");
244
return finfo_getattr(self, "group");
247
return finfo_getattr(self, "size");
250
return finfo_getattr(self, "atime");
253
return finfo_getattr(self, "mtime");
256
return finfo_getattr(self, "ctime");
259
return finfo_getattr(self, "fname");
262
return finfo_getattr(self, "name");
265
return finfo_getattr(self, "filetype");
273
static PySequenceMethods finfoseq_as_sequence = {
277
/* PYTHON 2.5: WARNING: 'intargfunc' must be replaced with 'ssizeargfunc' */
278
(intargfunc)finfoseq_item, /* sq_item */
281
0, /* sq_ass_slice */
291
static PyObject *finfo_repr(finfoobject *self)
293
PyObject *s = PyString_FromString("{");
296
PyString_ConcatAndDel(&s, PyString_FromString("'fname': "));
297
t = finfo_getattr(self, "fname");
298
PyString_ConcatAndDel(&s, PyObject_Repr(t));
301
PyString_ConcatAndDel(&s, PyString_FromString(", 'filetype': "));
302
t = finfo_getattr(self, "filetype");
303
PyString_ConcatAndDel(&s, PyObject_Repr(t));
306
PyString_ConcatAndDel(&s, PyString_FromString(", 'valid': "));
307
t = finfo_getattr(self, "valid");
308
PyString_ConcatAndDel(&s, PyObject_Repr(t));
311
PyString_ConcatAndDel(&s, PyString_FromString(", 'protection': "));
312
t = finfo_getattr(self, "protection");
313
PyString_ConcatAndDel(&s, PyObject_Repr(t));
316
PyString_ConcatAndDel(&s, PyString_FromString(", 'user': "));
317
t = finfo_getattr(self, "user");
318
PyString_ConcatAndDel(&s, PyObject_Repr(t));
321
PyString_ConcatAndDel(&s, PyString_FromString(", 'group': "));
322
t = finfo_getattr(self, "group");
323
PyString_ConcatAndDel(&s, PyObject_Repr(t));
326
PyString_ConcatAndDel(&s, PyString_FromString(", 'size': "));
327
t = finfo_getattr(self, "size");
328
PyString_ConcatAndDel(&s, PyObject_Repr(t));
331
PyString_ConcatAndDel(&s, PyString_FromString(", 'inode': "));
332
t = finfo_getattr(self, "inode");
333
PyString_ConcatAndDel(&s, PyObject_Repr(t));
336
PyString_ConcatAndDel(&s, PyString_FromString(", 'device': "));
337
t = finfo_getattr(self, "device");
338
PyString_ConcatAndDel(&s, PyObject_Repr(t));
341
PyString_ConcatAndDel(&s, PyString_FromString(", 'nlink': "));
342
t = finfo_getattr(self, "nlink");
343
PyString_ConcatAndDel(&s, PyObject_Repr(t));
346
PyString_ConcatAndDel(&s, PyString_FromString(", 'atime': "));
347
t = finfo_getattr(self, "atime");
348
PyString_ConcatAndDel(&s, PyObject_Repr(t));
351
PyString_ConcatAndDel(&s, PyString_FromString(", 'mtime': "));
352
t = finfo_getattr(self, "mtime");
353
PyString_ConcatAndDel(&s, PyObject_Repr(t));
356
PyString_ConcatAndDel(&s, PyString_FromString(", 'ctime': "));
357
t = finfo_getattr(self, "ctime");
358
PyString_ConcatAndDel(&s, PyObject_Repr(t));
361
PyString_ConcatAndDel(&s, PyString_FromString(", 'name': "));
362
t = finfo_getattr(self, "name");
363
PyString_ConcatAndDel(&s, PyObject_Repr(t));
366
PyString_ConcatAndDel(&s, PyString_FromString("}"));
371
PyTypeObject MpFinfo_Type = {
372
PyObject_HEAD_INIT(NULL)
377
(destructor)finfo_dealloc, /* tp_dealloc */
379
(getattrfunc)finfo_getattr, /*tp_getattr*/
382
(reprfunc)finfo_repr, /*tp_repr*/
383
0, /* tp_as_number */
384
&finfoseq_as_sequence, /* tp_as_sequence */
385
0, /* tp_as_mapping */