[34] | 1 | /* |
---|
| 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: |
---|
| 8 | * |
---|
| 9 | * The above copyright notice and this permission notice shall be included in |
---|
| 10 | * all copies or substantial portions of the Software. |
---|
| 11 | * |
---|
| 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. |
---|
| 19 | * |
---|
| 20 | * Copyright 2006 Sun Microsystems, Inc. All rights reserved. |
---|
| 21 | * Use is subject to license terms. |
---|
| 22 | */ |
---|
| 23 | |
---|
| 24 | #include <Python.h> |
---|
| 25 | |
---|
| 26 | #include <fsimage.h> |
---|
| 27 | #include <stdlib.h> |
---|
| 28 | |
---|
| 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 |
---|
| 31 | #else |
---|
| 32 | #define PY_PAD 0L,0L,0L,0L |
---|
| 33 | #endif |
---|
| 34 | |
---|
| 35 | typedef struct fsimage_fs { |
---|
| 36 | PyObject_HEAD |
---|
| 37 | fsi_t *fs; |
---|
| 38 | } fsimage_fs_t; |
---|
| 39 | |
---|
| 40 | typedef struct fsimage_file { |
---|
| 41 | PyObject_HEAD |
---|
| 42 | fsimage_fs_t *fs; |
---|
| 43 | fsi_file_t *file; |
---|
| 44 | } fsimage_file_t; |
---|
| 45 | |
---|
| 46 | struct foo { |
---|
| 47 | int ref; |
---|
| 48 | int size; |
---|
| 49 | long hash; |
---|
| 50 | int state; |
---|
| 51 | }; |
---|
| 52 | |
---|
| 53 | static PyObject * |
---|
| 54 | fsimage_file_read(fsimage_file_t *file, PyObject *args, PyObject *kwargs) |
---|
| 55 | { |
---|
| 56 | static char *kwlist[] = { "size", "offset", NULL }; |
---|
| 57 | int bufsize; |
---|
| 58 | int size = 0; |
---|
| 59 | uint64_t offset = 0; |
---|
| 60 | ssize_t bytesread = 0; |
---|
| 61 | PyObject * buffer; |
---|
| 62 | |
---|
| 63 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iL", kwlist, |
---|
| 64 | &size, &offset)) |
---|
| 65 | return (NULL); |
---|
| 66 | |
---|
| 67 | bufsize = size ? size : 4096; |
---|
| 68 | |
---|
| 69 | if ((buffer = PyString_FromStringAndSize(NULL, bufsize)) == NULL) |
---|
| 70 | return (NULL); |
---|
| 71 | |
---|
| 72 | while (1) { |
---|
| 73 | int err; |
---|
| 74 | void *buf = PyString_AS_STRING(buffer) + bytesread; |
---|
| 75 | |
---|
| 76 | err = fsi_pread_file(file->file, buf, bufsize, |
---|
| 77 | bytesread + offset); |
---|
| 78 | |
---|
| 79 | if (err == -1) { |
---|
| 80 | Py_DECREF(buffer); |
---|
| 81 | PyErr_SetFromErrno(PyExc_IOError); |
---|
| 82 | return (NULL); |
---|
| 83 | } else if (err == 0) { |
---|
| 84 | break; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | bytesread += err; |
---|
| 88 | |
---|
| 89 | if (size != 0) { |
---|
| 90 | bufsize -= bytesread; |
---|
| 91 | if (bufsize == 0) |
---|
| 92 | break; |
---|
| 93 | } else { |
---|
| 94 | if (_PyString_Resize(&buffer, bytesread + bufsize) < 0) |
---|
| 95 | return (NULL); |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | _PyString_Resize(&buffer, bytesread); |
---|
| 100 | return (buffer); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | PyDoc_STRVAR(fsimage_file_read__doc__, |
---|
| 104 | "read(file, [size=size, offset=off])\n" |
---|
| 105 | "\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 " |
---|
| 108 | "offset.\n"); |
---|
| 109 | |
---|
| 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 } |
---|
| 114 | }; |
---|
| 115 | |
---|
| 116 | static PyObject * |
---|
| 117 | fsimage_file_getattr(fsimage_file_t *file, char *name) |
---|
| 118 | { |
---|
| 119 | return (Py_FindMethod(fsimage_file_methods, (PyObject *)file, name)); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | static void |
---|
| 123 | fsimage_file_dealloc(fsimage_file_t *file) |
---|
| 124 | { |
---|
| 125 | if (file->file != NULL) |
---|
| 126 | fsi_close_file(file->file); |
---|
| 127 | Py_XDECREF(file->fs); |
---|
| 128 | PyObject_DEL(file); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | static char fsimage_file_type__doc__[] = "Filesystem image file"; |
---|
| 132 | PyTypeObject fsimage_file_type = { |
---|
| 133 | PyObject_HEAD_INIT(&PyType_Type) |
---|
| 134 | 0, /* ob_size */ |
---|
| 135 | "fsimage.file", /* tp_name */ |
---|
| 136 | sizeof(fsimage_file_t), /* tp_size */ |
---|
| 137 | 0, /* tp_itemsize */ |
---|
| 138 | (destructor) fsimage_file_dealloc, /* tp_dealloc */ |
---|
| 139 | 0, /* tp_print */ |
---|
| 140 | (getattrfunc) fsimage_file_getattr, /* tp_getattr */ |
---|
| 141 | 0, /* tp_setattr */ |
---|
| 142 | 0, /* tp_compare */ |
---|
| 143 | 0, /* tp_repr */ |
---|
| 144 | 0, /* tp_as_number */ |
---|
| 145 | 0, /* tp_as_sequence */ |
---|
| 146 | 0, /* tp_as_mapping */ |
---|
| 147 | 0, /* tp_hash */ |
---|
| 148 | 0, /* tp_call */ |
---|
| 149 | 0, /* tp_str */ |
---|
| 150 | 0, /* tp_getattro */ |
---|
| 151 | 0, /* tp_setattro */ |
---|
| 152 | 0, /* tp_as_buffer */ |
---|
| 153 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
---|
| 154 | fsimage_file_type__doc__, |
---|
| 155 | PY_PAD |
---|
| 156 | }; |
---|
| 157 | |
---|
| 158 | static PyObject * |
---|
| 159 | fsimage_fs_open_file(fsimage_fs_t *fs, PyObject *args, PyObject *kwargs) |
---|
| 160 | { |
---|
| 161 | static char *kwlist[] = { "name", NULL }; |
---|
| 162 | fsimage_file_t *file; |
---|
| 163 | char *name; |
---|
| 164 | |
---|
| 165 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &name)) |
---|
| 166 | return (NULL); |
---|
| 167 | |
---|
| 168 | file = (fsimage_file_t *)PyObject_NEW(fsimage_file_t, &fsimage_file_type); |
---|
| 169 | |
---|
| 170 | if (file == NULL) |
---|
| 171 | return (NULL); |
---|
| 172 | |
---|
| 173 | file->fs = fs; |
---|
| 174 | |
---|
| 175 | Py_INCREF(file->fs); |
---|
| 176 | if ((file->file = fsi_open_file(fs->fs, name)) == NULL) { |
---|
| 177 | Py_DECREF(file->fs); |
---|
| 178 | file->fs = NULL; |
---|
| 179 | PyErr_SetFromErrno(PyExc_IOError); |
---|
| 180 | return (NULL); |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | return ((PyObject *)file); |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | static PyObject * |
---|
| 187 | fsimage_fs_file_exists(fsimage_fs_t *fs, PyObject *args, PyObject *kwargs) |
---|
| 188 | { |
---|
| 189 | static char *kwlist[] = { "name", NULL }; |
---|
| 190 | char *name; |
---|
| 191 | |
---|
| 192 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &name)) |
---|
| 193 | return (NULL); |
---|
| 194 | |
---|
| 195 | if (fsi_file_exists(fs->fs, name)) { |
---|
| 196 | Py_INCREF(Py_True); |
---|
| 197 | return (Py_True); |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | Py_INCREF(Py_False); |
---|
| 201 | return (Py_False); |
---|
| 202 | } |
---|
| 203 | |
---|
| 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"); |
---|
| 209 | |
---|
| 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 } |
---|
| 216 | }; |
---|
| 217 | |
---|
| 218 | static PyObject * |
---|
| 219 | fsimage_fs_getattr(fsimage_fs_t *fs, char *name) |
---|
| 220 | { |
---|
| 221 | return (Py_FindMethod(fsimage_fs_methods, (PyObject *)fs, name)); |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | static void |
---|
| 225 | fsimage_fs_dealloc (fsimage_fs_t *fs) |
---|
| 226 | { |
---|
| 227 | if (fs->fs != NULL) |
---|
| 228 | fsi_close_fsimage(fs->fs); |
---|
| 229 | PyObject_DEL(fs); |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | PyDoc_STRVAR(fsimage_fs_type__doc__, "Filesystem image"); |
---|
| 233 | |
---|
| 234 | PyTypeObject fsimage_fs_type = { |
---|
| 235 | PyObject_HEAD_INIT(&PyType_Type) |
---|
| 236 | 0, /* ob_size */ |
---|
| 237 | "fsimage.fs", /* tp_name */ |
---|
| 238 | sizeof(fsimage_fs_t), /* tp_size */ |
---|
| 239 | 0, /* tp_itemsize */ |
---|
| 240 | (destructor) fsimage_fs_dealloc, /* tp_dealloc */ |
---|
| 241 | 0, /* tp_print */ |
---|
| 242 | (getattrfunc) fsimage_fs_getattr, /* tp_getattr */ |
---|
| 243 | 0, /* tp_setattr */ |
---|
| 244 | 0, /* tp_compare */ |
---|
| 245 | 0, /* tp_repr */ |
---|
| 246 | 0, /* tp_as_number */ |
---|
| 247 | 0, /* tp_as_sequence */ |
---|
| 248 | 0, /* tp_as_mapping */ |
---|
| 249 | 0, /* tp_hash */ |
---|
| 250 | 0, /* tp_call */ |
---|
| 251 | 0, /* tp_str */ |
---|
| 252 | 0, /* tp_getattro */ |
---|
| 253 | 0, /* tp_setattro */ |
---|
| 254 | 0, /* tp_as_buffer */ |
---|
| 255 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
---|
| 256 | fsimage_fs_type__doc__, |
---|
| 257 | PY_PAD |
---|
| 258 | }; |
---|
| 259 | |
---|
| 260 | static PyObject * |
---|
| 261 | fsimage_open(PyObject *o, PyObject *args, PyObject *kwargs) |
---|
| 262 | { |
---|
| 263 | static char *kwlist[] = { "name", "offset", "options", NULL }; |
---|
| 264 | char *name; |
---|
| 265 | char *options = NULL; |
---|
| 266 | uint64_t offset = 0; |
---|
| 267 | fsimage_fs_t *fs; |
---|
| 268 | |
---|
| 269 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|Ls", kwlist, |
---|
| 270 | &name, &offset, &options)) |
---|
| 271 | return (NULL); |
---|
| 272 | |
---|
| 273 | if ((fs = PyObject_NEW(fsimage_fs_t, &fsimage_fs_type)) == NULL) |
---|
| 274 | return (NULL); |
---|
| 275 | |
---|
| 276 | if ((fs->fs = fsi_open_fsimage(name, offset, options)) == NULL) { |
---|
| 277 | PyErr_SetFromErrno(PyExc_IOError); |
---|
| 278 | return (NULL); |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | return (PyObject *)fs; |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | PyDoc_STRVAR(fsimage_open__doc__, |
---|
| 285 | "open(name, [offset=off]) - Open the given file as a filesystem image.\n" |
---|
| 286 | "\n" |
---|
| 287 | "name - name of file to open.\n" |
---|
| 288 | "offset - offset of file system within file image.\n" |
---|
| 289 | "options - mount options string.\n"); |
---|
| 290 | |
---|
| 291 | static struct PyMethodDef fsimage_module_methods[] = { |
---|
| 292 | { "open", (PyCFunction)fsimage_open, |
---|
| 293 | METH_VARARGS|METH_KEYWORDS, fsimage_open__doc__ }, |
---|
| 294 | { NULL, NULL, 0, NULL } |
---|
| 295 | }; |
---|
| 296 | |
---|
| 297 | PyMODINIT_FUNC |
---|
| 298 | initfsimage(void) |
---|
| 299 | { |
---|
| 300 | Py_InitModule("fsimage", fsimage_module_methods); |
---|
| 301 | } |
---|