aboutsummaryrefslogtreecommitdiffstats
path: root/tools/pygrub/src/fsys/ext2/ext2module.c
blob: 57f1a83eaf47a8417d7812bc9af12a7814e6f824 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
 * ext2module.c - simple python binding for libext2fs
 *
 * Copyright 2005 Red Hat, Inc.
 * Jeremy Katz <katzj@redhat.com>
 *
 * This software may be freely redistributed under the terms of the GNU
 * general public license.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <Python.h>

#include <ext2fs/ext2fs.h>
#include <stdlib.h>
#include <stdio.h>

#if (PYTHON_API_VERSION >= 1011)
#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
#else
#define PY_PAD 0L,0L,0L,0L
#endif


/* global error object */
PyObject *Ext2Error;

typedef struct _Ext2Fs Ext2Fs;
struct _Ext2Fs {
    PyObject_HEAD;
    ext2_filsys fs;
};

typedef struct _Ext2File Ext2File;
struct _Ext2File {
    PyObject_HEAD;
    ext2_file_t file;
};

/* ext2 file object */

static PyObject *
ext2_file_close (Ext2File *file, PyObject *args)
{
    if (file->file != NULL)
        ext2fs_file_close(file->file);
    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *
ext2_file_read (Ext2File *file, PyObject *args)
{
    int err, size = 0;
    unsigned int n, total = 0;
    PyObject * buffer = NULL;

    if (file->file == NULL) {
        PyErr_SetString(PyExc_ValueError, "Cannot read from closed file");
        return NULL;
    }

    if (!PyArg_ParseTuple(args, "|i", &size))
        return NULL;

    buffer = PyString_FromStringAndSize((char *) NULL, (size) ? size : 4096);
    if (buffer == NULL)
        return buffer;
 
    while (1) {
        err = ext2fs_file_read(file->file, PyString_AS_STRING(buffer) + total, 
                               (size) ? size : 4096, &n);
        if (err) {
            if (buffer != NULL) { Py_DECREF(buffer); }
            Py_DECREF(buffer);
            PyErr_SetString(PyExc_ValueError, "read error");
            return NULL;
        }

        total += n;
        if (n == 0)
            break;

        if (size && size == total)
            break;

        if (!size) {
            _PyString_Resize(&buffer, total + 4096);
        }
    }

    _PyString_Resize(&buffer, total);
    return buffer;
}

static void
ext2_file_dealloc (Ext2File * file)
{
    if (file->file != NULL)
        ext2fs_file_close(file->file);
    PyMem_DEL(file);
}

static struct PyMethodDef Ext2FileMethods[] = {
        { "close",
          (PyCFunction) ext2_file_close,
          METH_VARARGS, NULL },
        { "read",
          (PyCFunction) ext2_file_read,
          METH_VARARGS, NULL },
	{ NULL, NULL, 0, NULL }	
};

static PyObject *
ext2_file_getattr (Ext2File * file, char * name)
{
        return Py_FindMethod (Ext2FileMethods, (PyObject *) file, name);
}

static char Ext2FileType__doc__[] = "This is the ext2 filesystem object";
PyTypeObject Ext2FileType = {
	PyObject_HEAD_INIT(&PyType_Type)
	0,				/* ob_size */
	"Ext2File",			/* tp_name */
	sizeof(Ext2File),		/* tp_size */
	0,				/* tp_itemsize */
	(destructor) ext2_file_dealloc, 	/* tp_dealloc */
	0,				/* tp_print */
	(getattrfunc) ext2_file_getattr, 	/* tp_getattr */
	0,				/* tp_setattr */
	0,				/* tp_compare */
	0,				/* tp_repr */
	0,				/* tp_as_number */
	0,	 			/* tp_as_sequence */
	0,				/* tp_as_mapping */
	0,           			/* tp_hash */
	0,                		/* tp_call */
	0,                    		/* tp_str */
	0,				/* tp_getattro */
	0,				/* tp_setattro */
	0,				/* tp_as_buffer */
	0L,	       			/* tp_flags */
	Ext2FileType__doc__,
	PY_PAD
};

static PyObject *
ext2_file_open (Ext2Fs *fs, char * name, int flags)
{
    int err;
    ext2_file_t f;
    ext2_ino_t ino;
    Ext2File * file;

    file = (Ext2File *) PyObject_NEW(Ext2File, &Ext2FileType);
    file->file = NULL;

    err = ext2fs_namei_follow(fs->fs, EXT2_ROOT_INO, EXT2_ROOT_INO, name, &ino);
    if (err) {
        PyErr_SetString(PyExc_ValueError, "unable to open file");
        return NULL;
    }

    err = ext2fs_file_open(fs->fs, ino, flags, &f);
    if (err) {
        PyErr_SetString(PyExc_ValueError, "unable to open file");
        return NULL;
    }

    file->file = f;
    return (PyObject *) file;
}

static PyObject *
ext2_file_exist (Ext2Fs *fs, char * name)
{
    int err;
    ext2_ino_t ino;
    Ext2File * file;

    file = (Ext2File *) PyObject_NEW(Ext2File, &Ext2FileType);
    file->file = NULL;

    err = ext2fs_namei_follow(fs->fs, EXT2_ROOT_INO, EXT2_ROOT_INO, name, &ino);
    if (err) {
        Py_INCREF(Py_False);
        return Py_False;
    }
    Py_INCREF(Py_True);
    return Py_True;
}

/* ext2fs object */

static PyObject *
ext2_fs_close (Ext2Fs *fs, PyObject *args)
{
    if (fs->fs != NULL)
        ext2fs_close(fs->fs);
    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *
ext2_fs_open (Ext2Fs *fs, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "name", "flags", "superblock", 
                              "block_size", "offset", NULL };
    char * name;
    int flags = 0, superblock = 0, offset = 0, err;
    unsigned int block_size = 0;
    ext2_filsys efs;
#ifdef HAVE_EXT2FS_OPEN2
    char offsetopt[30];
#endif

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iiii", kwlist, 
                                     &name, &flags, &superblock, 
                                     &block_size, &offset))
        return NULL;

    if (fs->fs != NULL) {
        PyErr_SetString(PyExc_ValueError, "already have an fs object");
        return NULL;
    }

#ifdef HAVE_EXT2FS_OPEN2
    if (offset == 0) {
        offsetopt[0] = '\0';
    }
    else {
        snprintf(offsetopt, 29, "offset=%d", offset);
    }

    err = ext2fs_open2(name, offsetopt, flags, superblock, block_size, 
                       unix_io_manager, &efs);
#else
    if (offset != 0) {
        PyErr_SetString(PyExc_ValueError, "offset argument not supported");
        return NULL;
    }

    err = ext2fs_open(name, flags, superblock, block_size,
                      unix_io_manager, &efs);
#endif
    if (err) {
        PyErr_SetString(PyExc_ValueError, "unable to open filesystem");
        return NULL;
    }

    fs->fs = efs;

    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *
ext2_fs_open_file (Ext2Fs *fs, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "name", "flags", NULL };
    char * name;
    int flags = 0;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|i", kwlist, 
                                     &name, &flags))
                                     return NULL;

    return ext2_file_open(fs, name, flags);
}

static PyObject *
ext2_fs_file_exist (Ext2Fs *fs, PyObject *args, PyObject *kwargs)
{
    static char *kwlist[] = { "name", NULL };
    char * name;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &name))
                                     return NULL;

    return ext2_file_exist(fs, name);
}

static void
ext2_fs_dealloc (Ext2Fs * fs)
{
    if (fs->fs != NULL)
        ext2fs_close(fs->fs);
    PyMem_DEL(fs);
}

static struct PyMethodDef Ext2FsMethods[] = {
        { "close",
          (PyCFunction) ext2_fs_close,
          METH_VARARGS, NULL },
        { "open",
          (PyCFunction) ext2_fs_open,
          METH_VARARGS|METH_KEYWORDS, NULL },
        { "open_file",
          (PyCFunction) ext2_fs_open_file,
          METH_VARARGS|METH_KEYWORDS, NULL },
        { "file_exist",
          (PyCFunction) ext2_fs_file_exist,
          METH_VARARGS|METH_KEYWORDS, NULL },
	{ NULL, NULL, 0, NULL }	
};

static PyObject *
ext2_fs_getattr (Ext2Fs * fs, char * name)
{
        return Py_FindMethod (Ext2FsMethods, (PyObject *) fs, name);
}

static char Ext2FsType__doc__[] = "This is the ext2 filesystem object";
PyTypeObject Ext2FsType = {
	PyObject_HEAD_INIT(&PyType_Type)
	0,				/* ob_size */
	"Ext2Fs",			/* tp_name */
	sizeof(Ext2Fs),		/* tp_size */
	0,				/* tp_itemsize */
	(destructor) ext2_fs_dealloc, 	/* tp_dealloc */
	0,				/* tp_print */
	(getattrfunc) ext2_fs_getattr, 	/* tp_getattr */
	0,				/* tp_setattr */
	0,				/* tp_compare */
	0,				/* tp_repr */
	0,				/* tp_as_number */
	0,	 			/* tp_as_sequence */
	0,				/* tp_as_mapping */
	0,           			/* tp_hash */
	0,                		/* tp_call */
	0,                    		/* tp_str */
	0,				/* tp_getattro */
	0,				/* tp_setattro */
	0,				/* tp_as_buffer */
	0L,	       			/* tp_flags */
	Ext2FsType__doc__,
	PY_PAD
};

static PyObject *
ext2_fs_new(PyObject *o, PyObject *args, PyObject *kwargs) 
{
    static char *kwlist[] = { "name", "flags", "superblock", 
                              "block_size", "offset", NULL };
    char * name;
    int flags = 0, superblock = 0, offset;
    unsigned int block_size = 0;
    Ext2Fs *pfs;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iiii", kwlist, 
                                     &name, &flags, &superblock, &block_size,
                                     &offset))
        return NULL;

    pfs = (Ext2Fs *) PyObject_NEW(Ext2Fs, &Ext2FsType);
    if (pfs == NULL)
        return NULL;
    pfs->fs = NULL;

    if (!ext2_fs_open(pfs, 
                      Py_BuildValue("siiii", name, flags, superblock, 
                                    block_size, offset), NULL))
        return NULL;

    return (PyObject *)pfs;
}

static struct PyMethodDef Ext2ModuleMethods[] = {
    { "Ext2Fs", (PyCFunction) ext2_fs_new, METH_VARARGS|METH_KEYWORDS, NULL },
    { NULL, NULL, 0, NULL }
};

void init_pyext2(void) {
    PyObject *m;

    m = Py_InitModule("_pyext2", Ext2ModuleMethods);
    /*
     * PyObject *d;
     * d = PyModule_GetDict(m);
     * o = PyObject_NEW(PyObject, yExt2FsConstructorType);
     * PyDict_SetItemString(d, "PyExt2Fs", o);
     * Py_DECREF(o);
     */
}