ia64/xen-unstable

view tools/pygrub/src/fsys/reiser/reisermodule.c @ 7350:43b40ae7904c

Better cast for ptr->int in residerfs module.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Wed Oct 12 11:13:00 2005 +0100 (2005-10-12)
parents 4b93736d64f7
children
line source
1 /*
2 * reisermodule.c - simple python binding for libreiserfs{2,3}
3 *
4 * Copyright (C) 2005 Nguyen Anh Quynh <aquynh@gmail.com>
5 *
6 * This software may be freely redistributed under the terms of the GNU
7 * general public license.
8 *
9 * You should have received a copy of the GNU General Public License
10 * along with this program; if not, write to the Free Software
11 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
12 */
14 #include <Python.h>
16 #include <fcntl.h>
17 #include <stdlib.h>
18 #include <stdio.h>
20 #include <dal/file_dal.h>
21 #include <reiserfs/reiserfs.h>
23 #if (PYTHON_API_VERSION >= 1011)
24 #define PY_PAD 0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L
25 #else
26 #define PY_PAD 0L,0L,0L,0L
27 #endif
30 /* global error object */
31 PyObject *ReiserError;
33 typedef struct {
34 PyObject_HEAD
35 reiserfs_fs_t *fs;
36 dal_t *dal;
37 } ReiserFs;
39 typedef struct _ReiserFile ReiserFile;
40 struct _ReiserFile {
41 PyObject_HEAD
42 reiserfs_file_t *file;
43 };
45 void file_dal_close(dal_t *dal) {
47 if (!dal) return;
49 close((int)(unsigned long)dal->dev);
50 dal_free(dal);
51 }
53 /* reiser file object */
55 static PyObject *
56 reiser_file_close (ReiserFile *file, PyObject *args)
57 {
58 if (file->file != NULL)
59 {
60 reiserfs_file_close(file->file);
61 file->file = NULL;
62 }
63 Py_INCREF(Py_None);
64 return Py_None;
65 }
67 static PyObject *
68 reiser_file_read (ReiserFile *file, PyObject *args)
69 {
70 int size = 0;
71 size_t n, total = 0;
72 PyObject * buffer = NULL;
74 if (file->file == NULL) {
75 PyErr_SetString(PyExc_ValueError, "Cannot read from closed file");
76 return NULL;
77 }
79 if (!PyArg_ParseTuple(args, "|i", &size))
80 return NULL;
82 buffer = PyString_FromStringAndSize((char *) NULL, (size) ? size : 4096);
83 if (buffer == NULL)
84 return buffer;
86 while (1) {
87 n = reiserfs_file_read(file->file, PyString_AS_STRING(buffer) + total,
88 (size) ? size : 4096);
89 if (n == 0)
90 break;
92 total += n;
94 if (size && size == total)
95 break;
97 if (!size) {
98 _PyString_Resize(&buffer, total + 4096);
99 }
100 }
102 _PyString_Resize(&buffer, total);
103 return buffer;
104 }
106 static void
107 reiser_file_dealloc (ReiserFile * file)
108 {
109 if (file->file != NULL) {
110 reiserfs_file_close(file->file);
111 file->file = NULL;
112 }
113 PyObject_DEL(file);
114 }
116 static struct PyMethodDef ReiserFileMethods[] = {
117 { "close", (PyCFunction) reiser_file_close, METH_VARARGS, NULL },
118 { "read", (PyCFunction) reiser_file_read, METH_VARARGS, NULL },
119 { NULL, NULL, 0, NULL }
120 };
122 static PyObject *
123 reiser_file_getattr (ReiserFile * file, char * name)
124 {
125 return Py_FindMethod (ReiserFileMethods, (PyObject *) file, name);
126 }
128 static char ReiserFileType__doc__[] = "This is the reiser filesystem object";
129 PyTypeObject ReiserFileType = {
130 PyObject_HEAD_INIT(&PyType_Type)
131 0, /* ob_size */
132 "ReiserFile", /* tp_name */
133 sizeof(ReiserFile), /* tp_size */
134 0, /* tp_itemsize */
135 (destructor) reiser_file_dealloc, /* tp_dealloc */
136 0, /* tp_print */
137 (getattrfunc) reiser_file_getattr, /* tp_getattr */
138 0, /* tp_setattr */
139 0, /* tp_compare */
140 0, /* tp_repr */
141 0, /* tp_as_number */
142 0, /* tp_as_sequence */
143 0, /* tp_as_mapping */
144 0, /* tp_hash */
145 0, /* tp_call */
146 0, /* tp_str */
147 0, /* tp_getattro */
148 0, /* tp_setattro */
149 0, /* tp_as_buffer */
150 Py_TPFLAGS_DEFAULT, /* tp_flags */
151 ReiserFileType__doc__,
152 PY_PAD
153 };
155 static PyObject *
156 reiser_file_open (ReiserFs *fs, char *name, int flags)
157 {
158 ReiserFile *file;
159 reiserfs_file_t *f;
161 file = (ReiserFile *) PyObject_NEW(ReiserFile, &ReiserFileType);
163 f = reiserfs_file_open(fs->fs, name, flags);
164 file->file = f;
166 if (!f) {
167 PyErr_SetString(PyExc_ValueError, "unable to open file");
168 return NULL;
169 }
171 return (PyObject *) file;
172 }
174 static PyObject *
175 reiser_file_exist (ReiserFs *fs, char *name)
176 {
177 reiserfs_file_t *f;
179 f = reiserfs_file_open(fs->fs, name, O_RDONLY);
181 if (!f) {
182 Py_INCREF(Py_False);
183 return Py_False;
184 }
185 reiserfs_file_close(f);
186 Py_INCREF(Py_True);
187 return Py_True;
188 }
190 /* reiserfs object */
192 static PyObject *
193 reiser_fs_close (ReiserFs *fs, PyObject *args)
194 {
195 if (fs->fs != NULL)
196 {
197 reiserfs_fs_close(fs->fs);
198 file_dal_close(fs->dal);
199 fs->fs = NULL;
200 }
201 Py_INCREF(Py_None);
202 return Py_None;
203 }
205 static PyObject *
206 reiser_fs_open (ReiserFs *fs, PyObject *args)
207 {
208 char *name;
209 size_t block_size = DEFAULT_BLOCK_SIZE;
210 dal_t *dal;
211 reiserfs_fs_t *rfs;
213 if (!PyArg_ParseTuple(args, "s|i", &name, &block_size))
214 return NULL;
216 if (fs->fs != NULL) {
217 PyErr_SetString(PyExc_ValueError, "already have an fs object");
218 return NULL;
219 }
221 if (!(dal = file_dal_open(name, block_size, O_RDONLY))) {
222 PyErr_SetString(PyExc_ValueError, "Couldn't create device abstraction");
223 return NULL;
224 }
226 if (!(rfs = reiserfs_fs_open_fast(dal, dal))) {
227 file_dal_close(dal);
228 PyErr_SetString(PyExc_ValueError, "unable to open file");
229 return NULL;
230 }
232 fs->fs = rfs;
233 fs->dal = dal;
235 Py_INCREF(Py_None);
236 return Py_None;
237 }
239 static PyObject *
240 reiser_fs_open_file (ReiserFs *fs, PyObject *args)
241 {
242 char *name;
243 int flags = 0;
245 if (!PyArg_ParseTuple(args, "s|i", &name, &flags))
246 return NULL;
248 return reiser_file_open(fs, name, flags);
249 }
251 static PyObject *
252 reiser_fs_file_exist (ReiserFs *fs, PyObject *args)
253 {
254 char * name;
256 if (!PyArg_ParseTuple(args, "s", &name))
257 return NULL;
259 return reiser_file_exist(fs, name);
260 }
262 static void
263 reiser_fs_dealloc (ReiserFs * fs)
264 {
265 if (fs->fs != NULL)
266 {
267 reiserfs_fs_close(fs->fs);
268 file_dal_close(fs->dal);
269 fs->fs = NULL;
270 }
271 PyObject_DEL(fs);
272 }
274 static struct PyMethodDef ReiserFsMethods[] = {
275 { "close", (PyCFunction) reiser_fs_close, METH_VARARGS, NULL },
276 { "open", (PyCFunction) reiser_fs_open, METH_VARARGS, NULL },
277 { "open_file", (PyCFunction) reiser_fs_open_file, METH_VARARGS, NULL },
278 { "file_exist", (PyCFunction) reiser_fs_file_exist, METH_VARARGS, NULL },
279 { NULL, NULL, 0, NULL }
280 };
282 static PyObject *
283 reiser_fs_getattr (ReiserFs * fs, char * name)
284 {
285 return Py_FindMethod (ReiserFsMethods, (PyObject *) fs, name);
286 }
288 static char ReiserFsType__doc__[] = "This is the reiser filesystem object";
290 PyTypeObject ReiserFsType = {
291 PyObject_HEAD_INIT(&PyType_Type)
292 0, /* ob_size */
293 "ReiserFs", /* tp_name */
294 sizeof(ReiserFs), /* tp_size */
295 0, /* tp_itemsize */
296 (destructor) reiser_fs_dealloc, /* tp_dealloc */
297 0, /* tp_print */
298 (getattrfunc) reiser_fs_getattr, /* tp_getattr */
299 0, /* tp_setattr */
300 0, /* tp_compare */
301 0, /* tp_repr */
302 0, /* tp_as_number */
303 0, /* tp_as_sequence */
304 0, /* tp_as_mapping */
305 0, /* tp_hash */
306 0, /* tp_call */
307 0, /* tp_str */
308 0, /* tp_getattro */
309 0, /* tp_setattro */
310 0, /* tp_as_buffer */
311 Py_TPFLAGS_DEFAULT, /* tp_flags */
312 ReiserFsType__doc__,
313 PY_PAD
314 };
316 static PyObject *
317 reiser_fs_new(PyObject *o, PyObject *args)
318 {
319 char *name;
320 size_t block_size = DEFAULT_BLOCK_SIZE;
321 ReiserFs *pfs;
323 if (!PyArg_ParseTuple(args, "s|i", &name, &block_size))
324 return NULL;
326 pfs = (ReiserFs *) PyObject_NEW(ReiserFs, &ReiserFsType);
327 if (pfs == NULL)
328 return NULL;
330 pfs->fs = NULL;
332 if (!reiser_fs_open(pfs, Py_BuildValue("si", name, block_size)))
333 return NULL;
335 return (PyObject *)pfs;
336 }
338 static struct PyMethodDef ReiserModuleMethods[] = {
339 { "ReiserFs", (PyCFunction) reiser_fs_new, METH_VARARGS},
340 { NULL, NULL, 0}
341 };
343 void init_pyreiser(void) {
344 Py_InitModule("_pyreiser", ReiserModuleMethods);
345 }