2
* Permission is hereby granted, free of charge, to any person obtaining a copy
3
* of this software and associated documentation files (the "Software"), to
4
* deal in the Software without restriction, including without limitation the
5
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6
* sell copies of the Software, and to permit persons to whom the Software is
7
* furnished to do so, subject to the following conditions:
9
* The above copyright notice and this permission notice shall be included in
10
* all copies or substantial portions of the Software.
12
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18
* DEALINGS IN THE SOFTWARE.
20
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
21
* Use is subject to license terms.
29
#if (PYTHON_API_VERSION >= 1011)
30
#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
32
#define PY_PAD 0L,0L,0L,0L
35
typedef struct fsimage_fs {
40
typedef struct fsimage_file {
54
fsimage_file_read(fsimage_file_t *file, PyObject *args, PyObject *kwargs)
56
static char *kwlist[] = { "size", "offset", NULL };
60
ssize_t bytesread = 0;
63
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iL", kwlist,
67
bufsize = size ? size : 4096;
69
if ((buffer = PyString_FromStringAndSize(NULL, bufsize)) == NULL)
74
void *buf = PyString_AS_STRING(buffer) + bytesread;
76
err = fsi_pread_file(file->file, buf, bufsize,
81
PyErr_SetFromErrno(PyExc_IOError);
83
} else if (err == 0) {
94
if (_PyString_Resize(&buffer, bytesread + bufsize) < 0)
99
_PyString_Resize(&buffer, bytesread);
103
PyDoc_STRVAR(fsimage_file_read__doc__,
104
"read(file, [size=size, offset=off])\n"
106
"Read size bytes (or all bytes if not set) from the given "
107
"file. If offset is specified as well, read from the given "
110
static struct PyMethodDef fsimage_file_methods[] = {
111
{ "read", (PyCFunction) fsimage_file_read,
112
METH_VARARGS|METH_KEYWORDS, fsimage_file_read__doc__ },
113
{ NULL, NULL, 0, NULL }
117
fsimage_file_getattr(fsimage_file_t *file, char *name)
119
return (Py_FindMethod(fsimage_file_methods, (PyObject *)file, name));
123
fsimage_file_dealloc(fsimage_file_t *file)
125
if (file->file != NULL)
126
fsi_close_file(file->file);
127
Py_XDECREF(file->fs);
131
static char fsimage_file_type__doc__[] = "Filesystem image file";
132
PyTypeObject fsimage_file_type = {
133
PyObject_HEAD_INIT(&PyType_Type)
135
"fsimage.file", /* tp_name */
136
sizeof(fsimage_file_t), /* tp_size */
138
(destructor) fsimage_file_dealloc, /* tp_dealloc */
140
(getattrfunc) fsimage_file_getattr, /* tp_getattr */
144
0, /* tp_as_number */
145
0, /* tp_as_sequence */
146
0, /* tp_as_mapping */
152
0, /* tp_as_buffer */
153
Py_TPFLAGS_DEFAULT, /* tp_flags */
154
fsimage_file_type__doc__,
159
fsimage_fs_open_file(fsimage_fs_t *fs, PyObject *args, PyObject *kwargs)
161
static char *kwlist[] = { "name", NULL };
162
fsimage_file_t *file;
165
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &name))
168
file = (fsimage_file_t *)PyObject_NEW(fsimage_file_t, &fsimage_file_type);
176
if ((file->file = fsi_open_file(fs->fs, name)) == NULL) {
179
PyErr_SetFromErrno(PyExc_IOError);
183
return ((PyObject *)file);
187
fsimage_fs_file_exists(fsimage_fs_t *fs, PyObject *args, PyObject *kwargs)
189
static char *kwlist[] = { "name", NULL };
192
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &name))
195
if (fsi_file_exists(fs->fs, name)) {
204
PyDoc_STRVAR(fsimage_fs_open_file__doc__,
205
"open_file(fs, filename) - lookup name in the given fs and return the file");
206
PyDoc_STRVAR(fsimage_fs_file_exists__doc__,
207
"file_exists(fs, name) - lookup name in the given fs and return "
208
"True if it exists");
210
static struct PyMethodDef fsimage_fs_methods[] = {
211
{ "open_file", (PyCFunction) fsimage_fs_open_file,
212
METH_VARARGS|METH_KEYWORDS, fsimage_fs_open_file__doc__ },
213
{ "file_exists", (PyCFunction) fsimage_fs_file_exists,
214
METH_VARARGS|METH_KEYWORDS, fsimage_fs_file_exists__doc__ },
215
{ NULL, NULL, 0, NULL }
219
fsimage_fs_getattr(fsimage_fs_t *fs, char *name)
221
return (Py_FindMethod(fsimage_fs_methods, (PyObject *)fs, name));
225
fsimage_fs_dealloc (fsimage_fs_t *fs)
228
fsi_close_fsimage(fs->fs);
232
PyDoc_STRVAR(fsimage_fs_type__doc__, "Filesystem image");
234
PyTypeObject fsimage_fs_type = {
235
PyObject_HEAD_INIT(&PyType_Type)
237
"fsimage.fs", /* tp_name */
238
sizeof(fsimage_fs_t), /* tp_size */
240
(destructor) fsimage_fs_dealloc, /* tp_dealloc */
242
(getattrfunc) fsimage_fs_getattr, /* tp_getattr */
246
0, /* tp_as_number */
247
0, /* tp_as_sequence */
248
0, /* tp_as_mapping */
254
0, /* tp_as_buffer */
255
Py_TPFLAGS_DEFAULT, /* tp_flags */
256
fsimage_fs_type__doc__,
261
fsimage_open(PyObject *o, PyObject *args, PyObject *kwargs)
263
static char *kwlist[] = { "name", "offset", "options", NULL };
265
char *options = NULL;
269
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|Ls", kwlist,
270
&name, &offset, &options))
273
if ((fs = PyObject_NEW(fsimage_fs_t, &fsimage_fs_type)) == NULL)
276
if ((fs->fs = fsi_open_fsimage(name, offset, options)) == NULL) {
277
PyErr_SetFromErrno(PyExc_IOError);
281
return (PyObject *)fs;
285
fsimage_getbootstring(PyObject *o, PyObject *args)
291
if (!PyArg_ParseTuple(args, "O", &fs))
294
fsi = ((fsimage_fs_t *)fs)->fs;
295
bootstring = fsi_fs_bootstring(fsi);
297
return Py_BuildValue("s", bootstring);
300
PyDoc_STRVAR(fsimage_open__doc__,
301
"open(name, [offset=off]) - Open the given file as a filesystem image.\n"
303
"name - name of file to open.\n"
304
"offset - offset of file system within file image.\n"
305
"options - mount options string.\n");
307
PyDoc_STRVAR(fsimage_getbootstring__doc__,
308
"getbootstring(fs) - Return the boot string needed for this file system "
309
"or NULL if none is needed.\n");
311
static struct PyMethodDef fsimage_module_methods[] = {
312
{ "open", (PyCFunction)fsimage_open,
313
METH_VARARGS|METH_KEYWORDS, fsimage_open__doc__ },
314
{ "getbootstring", (PyCFunction)fsimage_getbootstring,
315
METH_VARARGS, fsimage_getbootstring__doc__ },
316
{ NULL, NULL, 0, NULL }
322
Py_InitModule("fsimage", fsimage_module_methods);