ia64/xen-unstable

changeset 12339:72579f0cdaf7

Use libfsimage for reading filesystem images.

Signed-off-by: John Levon <john.levon@sun.com>
author Tim Deegan <Tim.Deegan@xensource.com>
date Thu Nov 09 14:11:11 2006 +0000 (2006-11-09)
parents f54e29a3be1f
children 14dc20d98ee1
files tools/pygrub/setup.py tools/pygrub/src/fsimage/fsimage.c tools/pygrub/src/fsys/__init__.py tools/pygrub/src/fsys/ext2/__init__.py tools/pygrub/src/fsys/ext2/ext2module.c tools/pygrub/src/fsys/ext2/test.py tools/pygrub/src/fsys/reiser/__init__.py tools/pygrub/src/fsys/reiser/reisermodule.c tools/pygrub/src/pygrub
line diff
     1.1 --- a/tools/pygrub/setup.py	Thu Nov 09 14:09:53 2006 +0000
     1.2 +++ b/tools/pygrub/setup.py	Thu Nov 09 14:11:11 2006 +0000
     1.3 @@ -5,46 +5,25 @@ import sys
     1.4  
     1.5  extra_compile_args  = [ "-fno-strict-aliasing", "-Werror" ]
     1.6  
     1.7 -fsys_mods = []
     1.8 -fsys_pkgs = []
     1.9 +XEN_ROOT = "../.."
    1.10  
    1.11 -if os.path.exists("/usr/include/ext2fs/ext2_fs.h"):
    1.12 -    ext2defines = []
    1.13 -    cc = new_compiler()
    1.14 -    cc.add_library("ext2fs")
    1.15 -    if hasattr(cc, "has_function") and cc.has_function("ext2fs_open2"):
    1.16 -        ext2defines.append( ("HAVE_EXT2FS_OPEN2", None) )
    1.17 -    else:
    1.18 -        sys.stderr.write("WARNING: older version of e2fsprogs installed, not building full\n")
    1.19 -        sys.stderr.write("         disk support for ext2.\n")
    1.20 -        
    1.21 -    ext2 = Extension("grub.fsys.ext2._pyext2",
    1.22 -                     extra_compile_args = extra_compile_args,
    1.23 -                     libraries = ["ext2fs"],
    1.24 -                     define_macros = ext2defines,
    1.25 -                     sources = ["src/fsys/ext2/ext2module.c"])
    1.26 -    fsys_mods.append(ext2)
    1.27 -    fsys_pkgs.append("grub.fsys.ext2")
    1.28 +fsimage = Extension("fsimage",
    1.29 +    extra_compile_args = extra_compile_args,
    1.30 +    include_dirs = [ XEN_ROOT + "/tools/libfsimage/common/" ],
    1.31 +    library_dirs = [ XEN_ROOT + "/tools/libfsimage/common/" ],
    1.32 +    libraries = ["fsimage"],
    1.33 +    sources = ["src/fsimage/fsimage.c"])
    1.34  
    1.35 -if os.path.exists("/usr/include/reiserfs/reiserfs.h"):
    1.36 -    reiser = Extension("grub.fsys.reiser._pyreiser",
    1.37 -                     extra_compile_args = extra_compile_args,
    1.38 -                     libraries = ["reiserfs"],
    1.39 -                     sources = ["src/fsys/reiser/reisermodule.c"])
    1.40 -    fsys_mods.append(reiser)
    1.41 -    fsys_pkgs.append("grub.fsys.reiser")
    1.42 +pkgs = [ 'grub' ]
    1.43  
    1.44 -pkgs = ['grub', 'grub.fsys']
    1.45 -pkgs.extend(fsys_pkgs)
    1.46  setup(name='pygrub',
    1.47        version='0.3',
    1.48        description='Boot loader that looks a lot like grub for Xen',
    1.49        author='Jeremy Katz',
    1.50        author_email='katzj@redhat.com',
    1.51        license='GPL',
    1.52 -      package_dir={'grub': 'src'},
    1.53 +      package_dir={'grub': 'src', 'fsimage': 'src'},
    1.54        scripts = ["src/pygrub"],
    1.55        packages=pkgs,
    1.56 -      ext_modules = fsys_mods
    1.57 +      ext_modules = [ fsimage ]
    1.58        )
    1.59 -               
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/tools/pygrub/src/fsimage/fsimage.c	Thu Nov 09 14:11:11 2006 +0000
     2.3 @@ -0,0 +1,299 @@
     2.4 +/*
     2.5 + * Permission is hereby granted, free of charge, to any person obtaining a copy
     2.6 + * of this software and associated documentation files (the "Software"), to
     2.7 + * deal in the Software without restriction, including without limitation the
     2.8 + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
     2.9 + * sell copies of the Software, and to permit persons to whom the Software is
    2.10 + * furnished to do so, subject to the following conditions:
    2.11 + *
    2.12 + * The above copyright notice and this permission notice shall be included in
    2.13 + * all copies or substantial portions of the Software.
    2.14 + *
    2.15 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    2.16 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    2.17 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    2.18 + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    2.19 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    2.20 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    2.21 + * DEALINGS IN THE SOFTWARE.
    2.22 + *
    2.23 + * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
    2.24 + * Use is subject to license terms.
    2.25 + */
    2.26 +
    2.27 +#include <Python.h>
    2.28 +
    2.29 +#include <fsimage.h>
    2.30 +#include <stdlib.h>
    2.31 +
    2.32 +#if (PYTHON_API_VERSION >= 1011)
    2.33 +#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
    2.34 +#else
    2.35 +#define PY_PAD 0L,0L,0L,0L
    2.36 +#endif
    2.37 +
    2.38 +typedef struct fsimage_fs {
    2.39 +	PyObject_HEAD
    2.40 +	fsi_t *fs;
    2.41 +} fsimage_fs_t;
    2.42 +
    2.43 +typedef struct fsimage_file { 
    2.44 +	PyObject_HEAD
    2.45 +	fsimage_fs_t *fs;
    2.46 +	fsi_file_t *file;
    2.47 +} fsimage_file_t;
    2.48 +
    2.49 +struct foo {
    2.50 +	int ref;
    2.51 +	int size;
    2.52 +	long hash;
    2.53 +	int state;
    2.54 +};
    2.55 +
    2.56 +static PyObject *
    2.57 +fsimage_file_read(fsimage_file_t *file, PyObject *args, PyObject *kwargs)
    2.58 +{
    2.59 +	static char *kwlist[] = { "size", "offset", NULL };
    2.60 +	int bufsize;
    2.61 +	int size = 0;
    2.62 +	uint64_t offset = 0;
    2.63 +	ssize_t bytesread = 0;
    2.64 +	PyObject * buffer;
    2.65 +
    2.66 +	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iL", kwlist, 
    2.67 +	    &size, &offset))
    2.68 +		return (NULL);
    2.69 +
    2.70 +	bufsize = size ? size : 4096;
    2.71 +
    2.72 +	if ((buffer = PyString_FromStringAndSize(NULL, bufsize)) == NULL)
    2.73 +		return (NULL);
    2.74 + 
    2.75 +	while (1) {
    2.76 +		int err;
    2.77 +		void *buf = PyString_AS_STRING(buffer) + bytesread;
    2.78 +
    2.79 +		err = fsi_pread_file(file->file, buf, bufsize,
    2.80 +		    bytesread + offset);
    2.81 +			
    2.82 +		if (err == -1) {
    2.83 +			Py_DECREF(buffer);
    2.84 +			PyErr_SetFromErrno(PyExc_IOError);
    2.85 +			return (NULL);
    2.86 +		} else if (err == 0) {
    2.87 +			break;
    2.88 +		}
    2.89 +
    2.90 +		bytesread += err;
    2.91 +
    2.92 +		if (size != 0) {
    2.93 +			bufsize -= bytesread;
    2.94 +			if (bufsize == 0)
    2.95 +				break;
    2.96 +		} else {
    2.97 +			if (_PyString_Resize(&buffer, bytesread + bufsize) < 0)
    2.98 +				return (NULL);
    2.99 +		}
   2.100 +	}
   2.101 +
   2.102 +	_PyString_Resize(&buffer, bytesread);
   2.103 +	return (buffer);
   2.104 +}
   2.105 +
   2.106 +PyDoc_STRVAR(fsimage_file_read__doc__,
   2.107 +   "read(file, [size=size, offset=off])\n"
   2.108 +   "\n"
   2.109 +   "Read size bytes (or all bytes if not set) from the given "
   2.110 +   "file. If offset is specified as well, read from the given "
   2.111 +   "offset.\n");
   2.112 +
   2.113 +static struct PyMethodDef fsimage_file_methods[] = {
   2.114 +	{ "read", (PyCFunction) fsimage_file_read,
   2.115 +	    METH_VARARGS|METH_KEYWORDS, fsimage_file_read__doc__ },
   2.116 +	{ NULL, NULL, 0, NULL }	
   2.117 +};
   2.118 +
   2.119 +static PyObject *
   2.120 +fsimage_file_getattr(fsimage_file_t *file, char *name)
   2.121 +{
   2.122 +	return (Py_FindMethod(fsimage_file_methods, (PyObject *)file, name));
   2.123 +}
   2.124 +
   2.125 +static void
   2.126 +fsimage_file_dealloc(fsimage_file_t *file)
   2.127 +{
   2.128 +	if (file->file != NULL)
   2.129 +		fsi_close_file(file->file);
   2.130 +	Py_XDECREF(file->fs);
   2.131 +	PyMem_DEL(file);
   2.132 +}
   2.133 +
   2.134 +static char fsimage_file_type__doc__[] = "Filesystem image file";
   2.135 +PyTypeObject fsimage_file_type = {
   2.136 +	PyObject_HEAD_INIT(&PyType_Type)
   2.137 +	0,					/* ob_size */
   2.138 +	"fsimage.file",				/* tp_name */
   2.139 +	sizeof(fsimage_file_t),			/* tp_size */
   2.140 +	0,					/* tp_itemsize */
   2.141 +	(destructor) fsimage_file_dealloc, 	/* tp_dealloc */
   2.142 +	0,					/* tp_print */
   2.143 +	(getattrfunc) fsimage_file_getattr, 	/* tp_getattr */
   2.144 +	0,					/* tp_setattr */
   2.145 +	0,					/* tp_compare */
   2.146 +	0,					/* tp_repr */
   2.147 +	0,					/* tp_as_number */
   2.148 +	0,	 				/* tp_as_sequence */
   2.149 +	0,					/* tp_as_mapping */
   2.150 +	0,	   				/* tp_hash */
   2.151 +	0,					/* tp_call */
   2.152 +	0,					/* tp_str */
   2.153 +	0,					/* tp_getattro */
   2.154 +	0,					/* tp_setattro */
   2.155 +	0,					/* tp_as_buffer */
   2.156 +	Py_TPFLAGS_DEFAULT,			/* tp_flags */
   2.157 +	fsimage_file_type__doc__,
   2.158 +	PY_PAD
   2.159 +};
   2.160 +
   2.161 +static PyObject *
   2.162 +fsimage_fs_open_file(fsimage_fs_t *fs, PyObject *args, PyObject *kwargs)
   2.163 +{
   2.164 +	static char *kwlist[] = { "name", NULL };
   2.165 +	fsimage_file_t *file;
   2.166 +	char *name;
   2.167 +
   2.168 +	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &name))
   2.169 +		return (NULL);
   2.170 +
   2.171 +	file = (fsimage_file_t *)PyObject_NEW(fsimage_file_t, &fsimage_file_type);
   2.172 +
   2.173 +	if (file == NULL)
   2.174 +		return (NULL);
   2.175 +
   2.176 +	file->fs = fs;
   2.177 +
   2.178 +	Py_INCREF(file->fs);
   2.179 +	if ((file->file = fsi_open_file(fs->fs, name)) == NULL) {
   2.180 +		Py_DECREF(file->fs);
   2.181 +		file->fs = NULL;
   2.182 +		PyErr_SetFromErrno(PyExc_IOError);
   2.183 +		return (NULL);
   2.184 +	}
   2.185 +
   2.186 +	return ((PyObject *)file);
   2.187 +}
   2.188 +
   2.189 +static PyObject *
   2.190 +fsimage_fs_file_exists(fsimage_fs_t *fs, PyObject *args, PyObject *kwargs)
   2.191 +{
   2.192 +	static char *kwlist[] = { "name", NULL };
   2.193 +	char *name;
   2.194 +
   2.195 +	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &name))
   2.196 +		return (NULL);
   2.197 +
   2.198 +	if (fsi_file_exists(fs->fs, name)) {
   2.199 +		Py_INCREF(Py_True);
   2.200 +		return (Py_True);
   2.201 +	}
   2.202 +
   2.203 +	Py_INCREF(Py_False);
   2.204 +	return (Py_False);
   2.205 +}
   2.206 +
   2.207 +PyDoc_STRVAR(fsimage_fs_open_file__doc__,
   2.208 +   "open_file(fs, filename) - lookup name in the given fs and return the file");
   2.209 +PyDoc_STRVAR(fsimage_fs_file_exists__doc__,
   2.210 +   "file_exists(fs, name) - lookup name in the given fs and return "
   2.211 +   "True if it exists");
   2.212 +
   2.213 +static struct PyMethodDef fsimage_fs_methods[] = {
   2.214 +	{ "open_file", (PyCFunction) fsimage_fs_open_file,
   2.215 +	  METH_VARARGS|METH_KEYWORDS, fsimage_fs_open_file__doc__ },
   2.216 +	{ "file_exists", (PyCFunction) fsimage_fs_file_exists,
   2.217 +	  METH_VARARGS|METH_KEYWORDS, fsimage_fs_file_exists__doc__ },
   2.218 +	{ NULL, NULL, 0, NULL }	
   2.219 +};
   2.220 +
   2.221 +static PyObject *
   2.222 +fsimage_fs_getattr(fsimage_fs_t *fs, char *name)
   2.223 +{
   2.224 +	return (Py_FindMethod(fsimage_fs_methods, (PyObject *)fs, name));
   2.225 +}
   2.226 +
   2.227 +static void
   2.228 +fsimage_fs_dealloc (fsimage_fs_t *fs)
   2.229 +{
   2.230 +	if (fs->fs != NULL)
   2.231 +		fsi_close_fsimage(fs->fs);
   2.232 +	PyMem_DEL(fs);
   2.233 +}
   2.234 +
   2.235 +PyDoc_STRVAR(fsimage_fs_type__doc__, "Filesystem image");
   2.236 +
   2.237 +PyTypeObject fsimage_fs_type = {
   2.238 +	PyObject_HEAD_INIT(&PyType_Type)
   2.239 +	0,					/* ob_size */
   2.240 +	"fsimage.fs",				/* tp_name */
   2.241 +	sizeof(fsimage_fs_t),			/* tp_size */
   2.242 +	0,					/* tp_itemsize */
   2.243 +	(destructor) fsimage_fs_dealloc, 	/* tp_dealloc */
   2.244 +	0,					/* tp_print */
   2.245 +	(getattrfunc) fsimage_fs_getattr, 	/* tp_getattr */
   2.246 +	0,					/* tp_setattr */
   2.247 +	0,					/* tp_compare */
   2.248 +	0,					/* tp_repr */
   2.249 +	0,					/* tp_as_number */
   2.250 +	0,	 				/* tp_as_sequence */
   2.251 +	0,					/* tp_as_mapping */
   2.252 +	0,	   				/* tp_hash */
   2.253 +	0,					/* tp_call */
   2.254 +	0,					/* tp_str */
   2.255 +	0,					/* tp_getattro */
   2.256 +	0,					/* tp_setattro */
   2.257 +	0,					/* tp_as_buffer */
   2.258 +	Py_TPFLAGS_DEFAULT,			/* tp_flags */
   2.259 +	fsimage_fs_type__doc__,
   2.260 +	PY_PAD
   2.261 +};
   2.262 +
   2.263 +static PyObject *
   2.264 +fsimage_open(PyObject *o, PyObject *args, PyObject *kwargs)
   2.265 +{
   2.266 +	static char *kwlist[] = { "name", "offset", NULL };
   2.267 +	char * name;
   2.268 +	uint64_t offset = 0;
   2.269 +	fsimage_fs_t *fs;
   2.270 +
   2.271 +	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|L", kwlist, 
   2.272 +	    &name, &offset))
   2.273 +		return (NULL);
   2.274 +
   2.275 +	if ((fs = PyObject_NEW(fsimage_fs_t, &fsimage_fs_type)) == NULL)
   2.276 +		return (NULL);
   2.277 +
   2.278 +	if ((fs->fs = fsi_open_fsimage(name, offset)) == NULL) {
   2.279 +		PyErr_SetFromErrno(PyExc_IOError);
   2.280 +		return (NULL);
   2.281 +	}
   2.282 +
   2.283 +	return (PyObject *)fs;
   2.284 +}
   2.285 +
   2.286 +PyDoc_STRVAR(fsimage_open__doc__,
   2.287 +    "open(name, [offset=off]) - Open the given file as a filesystem image.\n"
   2.288 +    "\n"
   2.289 +    "name - name of file to open.\n"
   2.290 +    "offset - offset of file system within file image.\n");
   2.291 +
   2.292 +static struct PyMethodDef fsimage_module_methods[] = {
   2.293 +	{ "open", (PyCFunction)fsimage_open,
   2.294 +	    METH_VARARGS|METH_KEYWORDS, fsimage_open__doc__ },
   2.295 +	{ NULL, NULL, 0, NULL }
   2.296 +};
   2.297 +
   2.298 +PyMODINIT_FUNC
   2.299 +initfsimage(void)
   2.300 +{
   2.301 +	Py_InitModule("fsimage", fsimage_module_methods);
   2.302 +}
     3.1 --- a/tools/pygrub/src/fsys/__init__.py	Thu Nov 09 14:09:53 2006 +0000
     3.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.3 @@ -1,64 +0,0 @@
     3.4 -#
     3.5 -# Copyright 2005 Red Hat, Inc.
     3.6 -# Jeremy Katz <katzj@redhat.com>
     3.7 -#
     3.8 -# This software may be freely redistributed under the terms of the GNU
     3.9 -# general public license.
    3.10 -#
    3.11 -# You should have received a copy of the GNU General Public License
    3.12 -# along with this program; if not, write to the Free Software
    3.13 -# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    3.14 -#
    3.15 -
    3.16 -import os
    3.17 -import sys
    3.18 -
    3.19 -fstypes = {}
    3.20 -
    3.21 -def register_fstype(x):
    3.22 -    if x.name in fstypes.keys():
    3.23 -        return
    3.24 -    fstypes[x.name] = x
    3.25 -
    3.26 -class FileSystemType(object):
    3.27 -    """A simple representation for a file system that gives a fs name
    3.28 -    and a method for sniffing a file to see if it's of the given fstype."""
    3.29 -    def __init__(self):
    3.30 -        self.name = ""
    3.31 -
    3.32 -    def sniff_magic(self, fn, offset = 0):
    3.33 -        """Look at the filesystem at fn for the appropriate magic starting at
    3.34 -        offset offset."""
    3.35 -        raise RuntimeError, "sniff_magic not implemented"
    3.36 -
    3.37 -    def open_fs(self, fn, offset = 0):
    3.38 -        """Open the given filesystem and return a filesystem object."""
    3.39 -        raise RuntimeError, "open_fs not implemented"
    3.40 -
    3.41 -class FileSystem(object):
    3.42 -    def open(self, name, flags = 0, block_size = 0):
    3.43 -        """Open the fsys on name with given flags and block_size."""
    3.44 -        raise RuntimeError, "open not implemented"
    3.45 -
    3.46 -    def close(self):
    3.47 -        """Close the fsys."""
    3.48 -        raise RuntimeError, "close not implemented"
    3.49 -
    3.50 -    def open_file(self, file, flags = None):
    3.51 -        """Open the file 'name' with the given flags.  The returned object
    3.52 -        should look similar to a native file object."""
    3.53 -        raise RuntimeError, "open_file not implemented"
    3.54 -    
    3.55 -    def file_exist(self, file):
    3.56 -        """Check to see if the give file is existed.
    3.57 -        Return true if file existed, return false otherwise."""
    3.58 -        raise RuntimeError, "file_exist not implemented"
    3.59 -
    3.60 -mydir = sys.modules['grub.fsys'].__path__[0]
    3.61 -for f in os.listdir(mydir):
    3.62 -    if not os.path.isdir("%s/%s" %(mydir, f)):
    3.63 -        continue
    3.64 -    try:
    3.65 -        exec "import grub.fsys.%s" %(f,)        
    3.66 -    except ImportError, e:
    3.67 -        pass
     4.1 --- a/tools/pygrub/src/fsys/ext2/__init__.py	Thu Nov 09 14:09:53 2006 +0000
     4.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 @@ -1,38 +0,0 @@
     4.4 -# Copyright 2005 Red Hat, Inc.
     4.5 -# Jeremy Katz <katzj@redhat.com>
     4.6 -#
     4.7 -# This software may be freely redistributed under the terms of the GNU
     4.8 -# general public license.
     4.9 -#
    4.10 -# You should have received a copy of the GNU General Public License
    4.11 -# along with this program; if not, write to the Free Software
    4.12 -# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    4.13 -#
    4.14 -
    4.15 -from grub.fsys import register_fstype, FileSystemType
    4.16 -from _pyext2 import *
    4.17 -
    4.18 -import os, struct
    4.19 -
    4.20 -class Ext2FileSystemType(FileSystemType):
    4.21 -    def __init__(self):
    4.22 -        FileSystemType.__init__(self)
    4.23 -        self.name = "ext2"
    4.24 -
    4.25 -    def sniff_magic(self, fn, offset = 0):
    4.26 -        fd = os.open(fn, os.O_RDONLY)
    4.27 -        os.lseek(fd, offset, 0)
    4.28 -        buf = os.read(fd, 2048)
    4.29 -        os.close(fd)        
    4.30 -        if len(buf) > 1082 and \
    4.31 -               struct.unpack("<H", buf[1080:1082]) == (0xef53,):
    4.32 -            return True
    4.33 -        return False
    4.34 -
    4.35 -    def open_fs(self, fn, offset = 0):
    4.36 -        if not self.sniff_magic(fn, offset):
    4.37 -            raise ValueError, "Not an ext2 filesystem"
    4.38 -        return Ext2Fs(fn, offset = offset)
    4.39 -
    4.40 -register_fstype(Ext2FileSystemType())
    4.41 -
     5.1 --- a/tools/pygrub/src/fsys/ext2/ext2module.c	Thu Nov 09 14:09:53 2006 +0000
     5.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.3 @@ -1,387 +0,0 @@
     5.4 -/*
     5.5 - * ext2module.c - simple python binding for libext2fs
     5.6 - *
     5.7 - * Copyright 2005 Red Hat, Inc.
     5.8 - * Jeremy Katz <katzj@redhat.com>
     5.9 - *
    5.10 - * This software may be freely redistributed under the terms of the GNU
    5.11 - * general public license.
    5.12 - *
    5.13 - * You should have received a copy of the GNU General Public License
    5.14 - * along with this program; if not, write to the Free Software
    5.15 - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    5.16 - */
    5.17 -
    5.18 -#include <Python.h>
    5.19 -
    5.20 -#include <ext2fs/ext2fs.h>
    5.21 -#include <stdlib.h>
    5.22 -#include <stdio.h>
    5.23 -
    5.24 -#if (PYTHON_API_VERSION >= 1011)
    5.25 -#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
    5.26 -#else
    5.27 -#define PY_PAD 0L,0L,0L,0L
    5.28 -#endif
    5.29 -
    5.30 -
    5.31 -/* global error object */
    5.32 -PyObject *Ext2Error;
    5.33 -
    5.34 -typedef struct _Ext2Fs Ext2Fs;
    5.35 -struct _Ext2Fs {
    5.36 -    PyObject_HEAD;
    5.37 -    ext2_filsys fs;
    5.38 -};
    5.39 -
    5.40 -typedef struct _Ext2File Ext2File;
    5.41 -struct _Ext2File {
    5.42 -    PyObject_HEAD;
    5.43 -    ext2_file_t file;
    5.44 -};
    5.45 -
    5.46 -/* ext2 file object */
    5.47 -
    5.48 -static PyObject *
    5.49 -ext2_file_close (Ext2File *file, PyObject *args)
    5.50 -{
    5.51 -    if (file->file != NULL)
    5.52 -        ext2fs_file_close(file->file);
    5.53 -    Py_INCREF(Py_None);
    5.54 -    return Py_None;
    5.55 -}
    5.56 -
    5.57 -static PyObject *
    5.58 -ext2_file_read (Ext2File *file, PyObject *args)
    5.59 -{
    5.60 -    int err, size = 0;
    5.61 -    unsigned int n, total = 0;
    5.62 -    PyObject * buffer = NULL;
    5.63 -
    5.64 -    if (file->file == NULL) {
    5.65 -        PyErr_SetString(PyExc_ValueError, "Cannot read from closed file");
    5.66 -        return NULL;
    5.67 -    }
    5.68 -
    5.69 -    if (!PyArg_ParseTuple(args, "|i", &size))
    5.70 -        return NULL;
    5.71 -
    5.72 -    buffer = PyString_FromStringAndSize((char *) NULL, (size) ? size : 4096);
    5.73 -    if (buffer == NULL)
    5.74 -        return buffer;
    5.75 - 
    5.76 -    while (1) {
    5.77 -        err = ext2fs_file_read(file->file, PyString_AS_STRING(buffer) + total, 
    5.78 -                               (size) ? size : 4096, &n);
    5.79 -        if (err) {
    5.80 -            if (buffer != NULL) { Py_DECREF(buffer); }
    5.81 -            Py_DECREF(buffer);
    5.82 -            PyErr_SetString(PyExc_ValueError, "read error");
    5.83 -            return NULL;
    5.84 -        }
    5.85 -
    5.86 -        total += n;
    5.87 -        if (n == 0)
    5.88 -            break;
    5.89 -
    5.90 -        if (size && size == total)
    5.91 -            break;
    5.92 -
    5.93 -        if (!size) {
    5.94 -            _PyString_Resize(&buffer, total + 4096);
    5.95 -        }
    5.96 -    }
    5.97 -
    5.98 -    _PyString_Resize(&buffer, total);
    5.99 -    return buffer;
   5.100 -}
   5.101 -
   5.102 -static void
   5.103 -ext2_file_dealloc (Ext2File * file)
   5.104 -{
   5.105 -    if (file->file != NULL)
   5.106 -        ext2fs_file_close(file->file);
   5.107 -    PyMem_DEL(file);
   5.108 -}
   5.109 -
   5.110 -static struct PyMethodDef Ext2FileMethods[] = {
   5.111 -        { "close",
   5.112 -          (PyCFunction) ext2_file_close,
   5.113 -          METH_VARARGS, NULL },
   5.114 -        { "read",
   5.115 -          (PyCFunction) ext2_file_read,
   5.116 -          METH_VARARGS, NULL },
   5.117 -	{ NULL, NULL, 0, NULL }	
   5.118 -};
   5.119 -
   5.120 -static PyObject *
   5.121 -ext2_file_getattr (Ext2File * file, char * name)
   5.122 -{
   5.123 -        return Py_FindMethod (Ext2FileMethods, (PyObject *) file, name);
   5.124 -}
   5.125 -
   5.126 -static char Ext2FileType__doc__[] = "This is the ext2 filesystem object";
   5.127 -PyTypeObject Ext2FileType = {
   5.128 -	PyObject_HEAD_INIT(&PyType_Type)
   5.129 -	0,				/* ob_size */
   5.130 -	"Ext2File",			/* tp_name */
   5.131 -	sizeof(Ext2File),		/* tp_size */
   5.132 -	0,				/* tp_itemsize */
   5.133 -	(destructor) ext2_file_dealloc, 	/* tp_dealloc */
   5.134 -	0,				/* tp_print */
   5.135 -	(getattrfunc) ext2_file_getattr, 	/* tp_getattr */
   5.136 -	0,				/* tp_setattr */
   5.137 -	0,				/* tp_compare */
   5.138 -	0,				/* tp_repr */
   5.139 -	0,				/* tp_as_number */
   5.140 -	0,	 			/* tp_as_sequence */
   5.141 -	0,				/* tp_as_mapping */
   5.142 -	0,           			/* tp_hash */
   5.143 -	0,                		/* tp_call */
   5.144 -	0,                    		/* tp_str */
   5.145 -	0,				/* tp_getattro */
   5.146 -	0,				/* tp_setattro */
   5.147 -	0,				/* tp_as_buffer */
   5.148 -	0L,	       			/* tp_flags */
   5.149 -	Ext2FileType__doc__,
   5.150 -	PY_PAD
   5.151 -};
   5.152 -
   5.153 -static PyObject *
   5.154 -ext2_file_open (Ext2Fs *fs, char * name, int flags)
   5.155 -{
   5.156 -    int err;
   5.157 -    ext2_file_t f;
   5.158 -    ext2_ino_t ino;
   5.159 -    Ext2File * file;
   5.160 -
   5.161 -    file = (Ext2File *) PyObject_NEW(Ext2File, &Ext2FileType);
   5.162 -    file->file = NULL;
   5.163 -
   5.164 -    err = ext2fs_namei_follow(fs->fs, EXT2_ROOT_INO, EXT2_ROOT_INO, name, &ino);
   5.165 -    if (err) {
   5.166 -        PyErr_SetString(PyExc_ValueError, "unable to open file");
   5.167 -        return NULL;
   5.168 -    }
   5.169 -
   5.170 -    err = ext2fs_file_open(fs->fs, ino, flags, &f);
   5.171 -    if (err) {
   5.172 -        PyErr_SetString(PyExc_ValueError, "unable to open file");
   5.173 -        return NULL;
   5.174 -    }
   5.175 -
   5.176 -    file->file = f;
   5.177 -    return (PyObject *) file;
   5.178 -}
   5.179 -
   5.180 -static PyObject *
   5.181 -ext2_file_exist (Ext2Fs *fs, char * name)
   5.182 -{
   5.183 -    int err;
   5.184 -    ext2_ino_t ino;
   5.185 -    Ext2File * file;
   5.186 -
   5.187 -    file = (Ext2File *) PyObject_NEW(Ext2File, &Ext2FileType);
   5.188 -    file->file = NULL;
   5.189 -
   5.190 -    err = ext2fs_namei_follow(fs->fs, EXT2_ROOT_INO, EXT2_ROOT_INO, name, &ino);
   5.191 -    if (err) {
   5.192 -        Py_INCREF(Py_False);
   5.193 -        return Py_False;
   5.194 -    }
   5.195 -    Py_INCREF(Py_True);
   5.196 -    return Py_True;
   5.197 -}
   5.198 -
   5.199 -/* ext2fs object */
   5.200 -
   5.201 -static PyObject *
   5.202 -ext2_fs_close (Ext2Fs *fs, PyObject *args)
   5.203 -{
   5.204 -    if (fs->fs != NULL)
   5.205 -        ext2fs_close(fs->fs);
   5.206 -    Py_INCREF(Py_None);
   5.207 -    return Py_None;
   5.208 -}
   5.209 -
   5.210 -static PyObject *
   5.211 -ext2_fs_open (Ext2Fs *fs, PyObject *args, PyObject *kwargs)
   5.212 -{
   5.213 -    static char *kwlist[] = { "name", "flags", "superblock", 
   5.214 -                              "block_size", "offset", NULL };
   5.215 -    char * name;
   5.216 -    int flags = 0, superblock = 0, offset = 0, err;
   5.217 -    unsigned int block_size = 0;
   5.218 -    ext2_filsys efs;
   5.219 -#ifdef HAVE_EXT2FS_OPEN2
   5.220 -    char offsetopt[30];
   5.221 -#endif
   5.222 -
   5.223 -    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iiii", kwlist, 
   5.224 -                                     &name, &flags, &superblock, 
   5.225 -                                     &block_size, &offset))
   5.226 -        return NULL;
   5.227 -
   5.228 -    if (fs->fs != NULL) {
   5.229 -        PyErr_SetString(PyExc_ValueError, "already have an fs object");
   5.230 -        return NULL;
   5.231 -    }
   5.232 -
   5.233 -#ifdef HAVE_EXT2FS_OPEN2
   5.234 -    if (offset == 0) {
   5.235 -        offsetopt[0] = '\0';
   5.236 -    }
   5.237 -    else {
   5.238 -        snprintf(offsetopt, 29, "offset=%d", offset);
   5.239 -    }
   5.240 -
   5.241 -    err = ext2fs_open2(name, offsetopt, flags, superblock, block_size, 
   5.242 -                       unix_io_manager, &efs);
   5.243 -#else
   5.244 -    if (offset != 0) {
   5.245 -        PyErr_SetString(PyExc_ValueError, "offset argument not supported");
   5.246 -        return NULL;
   5.247 -    }
   5.248 -
   5.249 -    err = ext2fs_open(name, flags, superblock, block_size,
   5.250 -                      unix_io_manager, &efs);
   5.251 -#endif
   5.252 -    if (err) {
   5.253 -        PyErr_SetString(PyExc_ValueError, "unable to open filesystem");
   5.254 -        return NULL;
   5.255 -    }
   5.256 -
   5.257 -    fs->fs = efs;
   5.258 -
   5.259 -    Py_INCREF(Py_None);
   5.260 -    return Py_None;
   5.261 -}
   5.262 -
   5.263 -static PyObject *
   5.264 -ext2_fs_open_file (Ext2Fs *fs, PyObject *args, PyObject *kwargs)
   5.265 -{
   5.266 -    static char *kwlist[] = { "name", "flags", NULL };
   5.267 -    char * name;
   5.268 -    int flags = 0;
   5.269 -
   5.270 -    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|i", kwlist, 
   5.271 -                                     &name, &flags))
   5.272 -                                     return NULL;
   5.273 -
   5.274 -    return ext2_file_open(fs, name, flags);
   5.275 -}
   5.276 -
   5.277 -static PyObject *
   5.278 -ext2_fs_file_exist (Ext2Fs *fs, PyObject *args, PyObject *kwargs)
   5.279 -{
   5.280 -    static char *kwlist[] = { "name", NULL };
   5.281 -    char * name;
   5.282 -
   5.283 -    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &name))
   5.284 -                                     return NULL;
   5.285 -
   5.286 -    return ext2_file_exist(fs, name);
   5.287 -}
   5.288 -
   5.289 -static void
   5.290 -ext2_fs_dealloc (Ext2Fs * fs)
   5.291 -{
   5.292 -    if (fs->fs != NULL)
   5.293 -        ext2fs_close(fs->fs);
   5.294 -    PyMem_DEL(fs);
   5.295 -}
   5.296 -
   5.297 -static struct PyMethodDef Ext2FsMethods[] = {
   5.298 -        { "close",
   5.299 -          (PyCFunction) ext2_fs_close,
   5.300 -          METH_VARARGS, NULL },
   5.301 -        { "open",
   5.302 -          (PyCFunction) ext2_fs_open,
   5.303 -          METH_VARARGS|METH_KEYWORDS, NULL },
   5.304 -        { "open_file",
   5.305 -          (PyCFunction) ext2_fs_open_file,
   5.306 -          METH_VARARGS|METH_KEYWORDS, NULL },
   5.307 -        { "file_exist",
   5.308 -          (PyCFunction) ext2_fs_file_exist,
   5.309 -          METH_VARARGS|METH_KEYWORDS, NULL },
   5.310 -	{ NULL, NULL, 0, NULL }	
   5.311 -};
   5.312 -
   5.313 -static PyObject *
   5.314 -ext2_fs_getattr (Ext2Fs * fs, char * name)
   5.315 -{
   5.316 -        return Py_FindMethod (Ext2FsMethods, (PyObject *) fs, name);
   5.317 -}
   5.318 -
   5.319 -static char Ext2FsType__doc__[] = "This is the ext2 filesystem object";
   5.320 -PyTypeObject Ext2FsType = {
   5.321 -	PyObject_HEAD_INIT(&PyType_Type)
   5.322 -	0,				/* ob_size */
   5.323 -	"Ext2Fs",			/* tp_name */
   5.324 -	sizeof(Ext2Fs),		/* tp_size */
   5.325 -	0,				/* tp_itemsize */
   5.326 -	(destructor) ext2_fs_dealloc, 	/* tp_dealloc */
   5.327 -	0,				/* tp_print */
   5.328 -	(getattrfunc) ext2_fs_getattr, 	/* tp_getattr */
   5.329 -	0,				/* tp_setattr */
   5.330 -	0,				/* tp_compare */
   5.331 -	0,				/* tp_repr */
   5.332 -	0,				/* tp_as_number */
   5.333 -	0,	 			/* tp_as_sequence */
   5.334 -	0,				/* tp_as_mapping */
   5.335 -	0,           			/* tp_hash */
   5.336 -	0,                		/* tp_call */
   5.337 -	0,                    		/* tp_str */
   5.338 -	0,				/* tp_getattro */
   5.339 -	0,				/* tp_setattro */
   5.340 -	0,				/* tp_as_buffer */
   5.341 -	0L,	       			/* tp_flags */
   5.342 -	Ext2FsType__doc__,
   5.343 -	PY_PAD
   5.344 -};
   5.345 -
   5.346 -static PyObject *
   5.347 -ext2_fs_new(PyObject *o, PyObject *args, PyObject *kwargs) 
   5.348 -{
   5.349 -    static char *kwlist[] = { "name", "flags", "superblock", 
   5.350 -                              "block_size", "offset", NULL };
   5.351 -    char * name;
   5.352 -    int flags = 0, superblock = 0, offset;
   5.353 -    unsigned int block_size = 0;
   5.354 -    Ext2Fs *pfs;
   5.355 -
   5.356 -    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iiii", kwlist, 
   5.357 -                                     &name, &flags, &superblock, &block_size,
   5.358 -                                     &offset))
   5.359 -        return NULL;
   5.360 -
   5.361 -    pfs = (Ext2Fs *) PyObject_NEW(Ext2Fs, &Ext2FsType);
   5.362 -    if (pfs == NULL)
   5.363 -        return NULL;
   5.364 -    pfs->fs = NULL;
   5.365 -
   5.366 -    if (!ext2_fs_open(pfs, 
   5.367 -                      Py_BuildValue("siiii", name, flags, superblock, 
   5.368 -                                    block_size, offset), NULL))
   5.369 -        return NULL;
   5.370 -
   5.371 -    return (PyObject *)pfs;
   5.372 -}
   5.373 -
   5.374 -static struct PyMethodDef Ext2ModuleMethods[] = {
   5.375 -    { "Ext2Fs", (PyCFunction) ext2_fs_new, METH_VARARGS|METH_KEYWORDS, NULL },
   5.376 -    { NULL, NULL, 0, NULL }
   5.377 -};
   5.378 -
   5.379 -void init_pyext2(void) {
   5.380 -    PyObject *m;
   5.381 -
   5.382 -    m = Py_InitModule("_pyext2", Ext2ModuleMethods);
   5.383 -    /*
   5.384 -     * PyObject *d;
   5.385 -     * d = PyModule_GetDict(m);
   5.386 -     * o = PyObject_NEW(PyObject, yExt2FsConstructorType);
   5.387 -     * PyDict_SetItemString(d, "PyExt2Fs", o);
   5.388 -     * Py_DECREF(o);
   5.389 -     */
   5.390 -}
     6.1 --- a/tools/pygrub/src/fsys/ext2/test.py	Thu Nov 09 14:09:53 2006 +0000
     6.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.3 @@ -1,15 +0,0 @@
     6.4 -#!/usr/bin/python
     6.5 -
     6.6 -
     6.7 -import _pyext2
     6.8 -import struct, os, sys
     6.9 -
    6.10 -fs = _pyext2.Ext2Fs("test.img")
    6.11 -
    6.12 -f = fs.open_file("/boot/vmlinuz-2.6.11-1.1177_FC4")
    6.13 -buf = f.read()
    6.14 -o = open("vmlinuz", "wb+")
    6.15 -o.write(buf)
    6.16 -o.close()
    6.17 -
    6.18 -f.close()
     7.1 --- a/tools/pygrub/src/fsys/reiser/__init__.py	Thu Nov 09 14:09:53 2006 +0000
     7.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.3 @@ -1,40 +0,0 @@
     7.4 -# 
     7.5 -# Copyright (C) 2005 Nguyen Anh Quynh <aquynh@gmail.com>
     7.6 -#
     7.7 -# This software may be freely redistributed under the terms of the GNU
     7.8 -# general public license.
     7.9 -#
    7.10 -# You should have received a copy of the GNU General Public License
    7.11 -# along with this program; if not, write to the Free Software
    7.12 -# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    7.13 -#
    7.14 -
    7.15 -from grub.fsys import register_fstype, FileSystemType
    7.16 -from _pyreiser import *
    7.17 -
    7.18 -import os
    7.19 -
    7.20 -FSMAGIC2 = 'ReIsEr2'
    7.21 -FSMAGIC3 = 'ReIsEr3'
    7.22 -
    7.23 -class ReiserFileSystemType(FileSystemType):
    7.24 -    def __init__(self):
    7.25 -        FileSystemType.__init__(self)
    7.26 -        self.name = "reiser"
    7.27 -
    7.28 -    def sniff_magic(self, fn, offset = 0):
    7.29 -        fd = os.open(fn, os.O_RDONLY)
    7.30 -        os.lseek(fd, 0x10000, 0)
    7.31 -        buf = os.read(fd, 0x40)
    7.32 -        os.close(fd)
    7.33 -        if len(buf) == 0x40 and (buf[0x34:0x3B] in [FSMAGIC2, FSMAGIC3]) :
    7.34 -            return True
    7.35 -        return False
    7.36 -
    7.37 -    def open_fs(self, fn, offset = 0):
    7.38 -        if not self.sniff_magic(fn, offset):
    7.39 -            raise ValueError, "Not a reiserfs filesystem"
    7.40 -        return ReiserFs(fn)
    7.41 -
    7.42 -register_fstype(ReiserFileSystemType())
    7.43 -
     8.1 --- a/tools/pygrub/src/fsys/reiser/reisermodule.c	Thu Nov 09 14:09:53 2006 +0000
     8.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.3 @@ -1,345 +0,0 @@
     8.4 -/*
     8.5 - * reisermodule.c - simple python binding for libreiserfs{2,3}
     8.6 - *
     8.7 - * Copyright (C) 2005 Nguyen Anh Quynh <aquynh@gmail.com>
     8.8 - *
     8.9 - * This software may be freely redistributed under the terms of the GNU
    8.10 - * general public license.
    8.11 - *
    8.12 - * You should have received a copy of the GNU General Public License
    8.13 - * along with this program; if not, write to the Free Software
    8.14 - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    8.15 - */
    8.16 -
    8.17 -#include <Python.h>
    8.18 -
    8.19 -#include <fcntl.h>
    8.20 -#include <stdlib.h>
    8.21 -#include <stdio.h>
    8.22 -
    8.23 -#include <dal/file_dal.h>
    8.24 -#include <reiserfs/reiserfs.h>
    8.25 -
    8.26 -#if (PYTHON_API_VERSION >= 1011)
    8.27 -#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
    8.28 -#else
    8.29 -#define PY_PAD 0L,0L,0L,0L
    8.30 -#endif
    8.31 -
    8.32 -
    8.33 -/* global error object */
    8.34 -PyObject *ReiserError;
    8.35 -
    8.36 -typedef struct {
    8.37 -    PyObject_HEAD
    8.38 -    reiserfs_fs_t *fs;
    8.39 -	dal_t *dal;
    8.40 -} ReiserFs;
    8.41 -
    8.42 -typedef struct _ReiserFile ReiserFile;
    8.43 -struct _ReiserFile {
    8.44 -    PyObject_HEAD
    8.45 -    reiserfs_file_t *file;
    8.46 -};
    8.47 -
    8.48 -void file_dal_close(dal_t *dal) {
    8.49 -
    8.50 -	if (!dal) return;
    8.51 -
    8.52 -	close((int)(unsigned long)dal->dev);
    8.53 -	dal_free(dal);
    8.54 -}
    8.55 -
    8.56 -/* reiser file object */
    8.57 -
    8.58 -static PyObject *
    8.59 -reiser_file_close (ReiserFile *file, PyObject *args)
    8.60 -{
    8.61 -    if (file->file != NULL)
    8.62 -	{
    8.63 -        reiserfs_file_close(file->file);
    8.64 -		file->file = NULL;
    8.65 -	}
    8.66 -    Py_INCREF(Py_None);
    8.67 -    return Py_None;
    8.68 -}
    8.69 -
    8.70 -static PyObject *
    8.71 -reiser_file_read (ReiserFile *file, PyObject *args)
    8.72 -{
    8.73 -    int size = 0;
    8.74 -    size_t n, total = 0;
    8.75 -    PyObject * buffer = NULL;
    8.76 -
    8.77 -    if (file->file == NULL) {
    8.78 -        PyErr_SetString(PyExc_ValueError, "Cannot read from closed file");
    8.79 -        return NULL;
    8.80 -    }
    8.81 -
    8.82 -    if (!PyArg_ParseTuple(args, "|i", &size))
    8.83 -        return NULL;
    8.84 -
    8.85 -    buffer = PyString_FromStringAndSize((char *) NULL, (size) ? size : 4096);
    8.86 -    if (buffer == NULL)
    8.87 -        return buffer;
    8.88 - 
    8.89 -    while (1) {
    8.90 -        n = reiserfs_file_read(file->file, PyString_AS_STRING(buffer) + total, 
    8.91 -                               (size) ? size : 4096);
    8.92 -        if (n == 0)
    8.93 -            break;
    8.94 -
    8.95 -        total += n;
    8.96 -
    8.97 -        if (size && size == total)
    8.98 -            break;
    8.99 -
   8.100 -        if (!size) {
   8.101 -            _PyString_Resize(&buffer, total + 4096);
   8.102 -        }
   8.103 -    }
   8.104 -
   8.105 -    _PyString_Resize(&buffer, total);
   8.106 -    return buffer;
   8.107 -}
   8.108 -
   8.109 -static void
   8.110 -reiser_file_dealloc (ReiserFile * file)
   8.111 -{
   8.112 -    if (file->file != NULL) {
   8.113 -        reiserfs_file_close(file->file);
   8.114 -		file->file = NULL;
   8.115 -	}
   8.116 -	PyObject_DEL(file);
   8.117 -}
   8.118 -
   8.119 -static struct PyMethodDef ReiserFileMethods[] = {
   8.120 -	{ "close", (PyCFunction) reiser_file_close, METH_VARARGS, NULL },
   8.121 -	{ "read", (PyCFunction) reiser_file_read, METH_VARARGS, NULL },
   8.122 -	{ NULL, NULL, 0, NULL }	
   8.123 -};
   8.124 -
   8.125 -static PyObject *
   8.126 -reiser_file_getattr (ReiserFile * file, char * name)
   8.127 -{
   8.128 -	return Py_FindMethod (ReiserFileMethods, (PyObject *) file, name);
   8.129 -}
   8.130 -
   8.131 -static char ReiserFileType__doc__[] = "This is the reiser filesystem object";
   8.132 -PyTypeObject ReiserFileType = {
   8.133 -	PyObject_HEAD_INIT(&PyType_Type)
   8.134 -	0,				/* ob_size */
   8.135 -	"ReiserFile",			/* tp_name */
   8.136 -	sizeof(ReiserFile),		/* tp_size */
   8.137 -	0,				/* tp_itemsize */
   8.138 -	(destructor) reiser_file_dealloc, 	/* tp_dealloc */
   8.139 -	0,				/* tp_print */
   8.140 -	(getattrfunc) reiser_file_getattr, 	/* tp_getattr */
   8.141 -	0,				/* tp_setattr */
   8.142 -	0,				/* tp_compare */
   8.143 -	0,				/* tp_repr */
   8.144 -	0,				/* tp_as_number */
   8.145 -	0,	 			/* tp_as_sequence */
   8.146 -	0,				/* tp_as_mapping */
   8.147 -	0,           	/* tp_hash */
   8.148 -	0,             	/* tp_call */
   8.149 -	0,             	/* tp_str */
   8.150 -	0,				/* tp_getattro */
   8.151 -	0,				/* tp_setattro */
   8.152 -	0,				/* tp_as_buffer */
   8.153 -	Py_TPFLAGS_DEFAULT,	       		/* tp_flags */
   8.154 -	ReiserFileType__doc__,
   8.155 -	PY_PAD
   8.156 -};
   8.157 -
   8.158 -static PyObject *
   8.159 -reiser_file_open (ReiserFs *fs, char *name, int flags)
   8.160 -{
   8.161 -    ReiserFile *file;
   8.162 -    reiserfs_file_t *f;
   8.163 -
   8.164 -    file = (ReiserFile *) PyObject_NEW(ReiserFile, &ReiserFileType);
   8.165 -
   8.166 -    f = reiserfs_file_open(fs->fs, name, flags);
   8.167 -    file->file = f;
   8.168 -    
   8.169 -    if (!f) {
   8.170 -        PyErr_SetString(PyExc_ValueError, "unable to open file");
   8.171 -        return NULL;
   8.172 -    }
   8.173 -
   8.174 -    return (PyObject *) file;
   8.175 -}
   8.176 -
   8.177 -static PyObject *
   8.178 -reiser_file_exist (ReiserFs *fs, char *name)
   8.179 -{
   8.180 -    reiserfs_file_t *f;
   8.181 -
   8.182 -    f = reiserfs_file_open(fs->fs, name, O_RDONLY);
   8.183 -
   8.184 -	if (!f) {
   8.185 -		Py_INCREF(Py_False);
   8.186 -		return Py_False;
   8.187 -	}
   8.188 -	reiserfs_file_close(f);
   8.189 -    Py_INCREF(Py_True);
   8.190 -    return Py_True;
   8.191 -}
   8.192 -
   8.193 -/* reiserfs object */
   8.194 -
   8.195 -static PyObject *
   8.196 -reiser_fs_close (ReiserFs *fs, PyObject *args)
   8.197 -{
   8.198 -    if (fs->fs != NULL)
   8.199 -    {
   8.200 -        reiserfs_fs_close(fs->fs);
   8.201 -        file_dal_close(fs->dal);
   8.202 -		fs->fs = NULL;
   8.203 -    }
   8.204 -    Py_INCREF(Py_None);
   8.205 -    return Py_None;
   8.206 -}
   8.207 -
   8.208 -static PyObject *
   8.209 -reiser_fs_open (ReiserFs *fs, PyObject *args)
   8.210 -{
   8.211 -    char *name;
   8.212 -	size_t block_size = DEFAULT_BLOCK_SIZE;
   8.213 -    dal_t *dal;
   8.214 -    reiserfs_fs_t *rfs;
   8.215 -
   8.216 -    if (!PyArg_ParseTuple(args, "s|i", &name, &block_size))
   8.217 -        return NULL;
   8.218 -
   8.219 -    if (fs->fs != NULL) {
   8.220 -        PyErr_SetString(PyExc_ValueError, "already have an fs object");
   8.221 -        return NULL;
   8.222 -    }
   8.223 -
   8.224 -    if (!(dal = file_dal_open(name, block_size, O_RDONLY))) {
   8.225 -        PyErr_SetString(PyExc_ValueError, "Couldn't create device abstraction");
   8.226 -        return NULL;    
   8.227 -    }
   8.228 -    
   8.229 -    if (!(rfs = reiserfs_fs_open_fast(dal, dal))) {
   8.230 -		file_dal_close(dal);
   8.231 -        PyErr_SetString(PyExc_ValueError, "unable to open file");
   8.232 -        return NULL;
   8.233 -    }
   8.234 -    
   8.235 -    fs->fs = rfs;
   8.236 -	fs->dal = dal;
   8.237 -
   8.238 -    Py_INCREF(Py_None);
   8.239 -    return Py_None;
   8.240 -}
   8.241 -
   8.242 -static PyObject *
   8.243 -reiser_fs_open_file (ReiserFs *fs, PyObject *args)
   8.244 -{
   8.245 -    char *name;
   8.246 -    int flags = 0;
   8.247 -
   8.248 -    if (!PyArg_ParseTuple(args, "s|i", &name, &flags))
   8.249 -		return NULL;
   8.250 -
   8.251 -    return reiser_file_open(fs, name, flags);
   8.252 -}
   8.253 -
   8.254 -static PyObject *
   8.255 -reiser_fs_file_exist (ReiserFs *fs, PyObject *args)
   8.256 -{
   8.257 -    char * name;
   8.258 -
   8.259 -    if (!PyArg_ParseTuple(args, "s", &name))
   8.260 -        return NULL;
   8.261 -
   8.262 -    return reiser_file_exist(fs, name);
   8.263 -}
   8.264 -
   8.265 -static void
   8.266 -reiser_fs_dealloc (ReiserFs * fs)
   8.267 -{
   8.268 -    if (fs->fs != NULL)
   8.269 -	{
   8.270 -        reiserfs_fs_close(fs->fs);
   8.271 -		file_dal_close(fs->dal);
   8.272 -		fs->fs = NULL;
   8.273 -	}
   8.274 -	PyObject_DEL(fs);
   8.275 -}
   8.276 -
   8.277 -static struct PyMethodDef ReiserFsMethods[] = {
   8.278 -	{ "close", (PyCFunction) reiser_fs_close, METH_VARARGS, NULL },
   8.279 -	{ "open", (PyCFunction) reiser_fs_open, METH_VARARGS, NULL },
   8.280 -	{ "open_file", (PyCFunction) reiser_fs_open_file, METH_VARARGS, NULL },
   8.281 -	{ "file_exist", (PyCFunction) reiser_fs_file_exist, METH_VARARGS, NULL },
   8.282 -	{ NULL, NULL, 0, NULL }	
   8.283 -};
   8.284 -
   8.285 -static PyObject *
   8.286 -reiser_fs_getattr (ReiserFs * fs, char * name)
   8.287 -{
   8.288 -        return Py_FindMethod (ReiserFsMethods, (PyObject *) fs, name);
   8.289 -}
   8.290 -
   8.291 -static char ReiserFsType__doc__[] = "This is the reiser filesystem object";
   8.292 -
   8.293 -PyTypeObject ReiserFsType = {
   8.294 -	PyObject_HEAD_INIT(&PyType_Type)
   8.295 -	0,				/* ob_size */
   8.296 -	"ReiserFs",		/* tp_name */
   8.297 -	sizeof(ReiserFs),		/* tp_size */
   8.298 -	0,				/* tp_itemsize */
   8.299 -	(destructor) reiser_fs_dealloc, 	/* tp_dealloc */
   8.300 -	0,				/* tp_print */
   8.301 -	(getattrfunc) reiser_fs_getattr, 	/* tp_getattr */
   8.302 -	0,				/* tp_setattr */
   8.303 -	0,				/* tp_compare */
   8.304 -	0,				/* tp_repr */
   8.305 -	0,				/* tp_as_number */
   8.306 -	0,	 			/* tp_as_sequence */
   8.307 -	0,				/* tp_as_mapping */
   8.308 -	0,           	/* tp_hash */
   8.309 -	0,             	/* tp_call */
   8.310 -	0,             	/* tp_str */
   8.311 -	0,				/* tp_getattro */
   8.312 -	0,				/* tp_setattro */
   8.313 -	0,				/* tp_as_buffer */
   8.314 -	Py_TPFLAGS_DEFAULT,			/* tp_flags */
   8.315 -	ReiserFsType__doc__,
   8.316 -	PY_PAD
   8.317 -};
   8.318 -
   8.319 -static PyObject *
   8.320 -reiser_fs_new(PyObject *o, PyObject *args) 
   8.321 -{
   8.322 -    char *name;
   8.323 -	size_t block_size = DEFAULT_BLOCK_SIZE;
   8.324 -    ReiserFs *pfs;
   8.325 -    
   8.326 -    if (!PyArg_ParseTuple(args, "s|i", &name, &block_size))
   8.327 -        return NULL;
   8.328 -    
   8.329 -    pfs = (ReiserFs *) PyObject_NEW(ReiserFs, &ReiserFsType);
   8.330 -    if (pfs == NULL)
   8.331 -        return NULL;
   8.332 -
   8.333 -    pfs->fs = NULL;
   8.334 -    
   8.335 -    if (!reiser_fs_open(pfs, Py_BuildValue("si", name, block_size)))
   8.336 -        return NULL;
   8.337 -    
   8.338 -    return (PyObject *)pfs;
   8.339 -}
   8.340 -
   8.341 -static struct PyMethodDef ReiserModuleMethods[] = {
   8.342 -    { "ReiserFs", (PyCFunction) reiser_fs_new, METH_VARARGS},
   8.343 -    { NULL, NULL, 0}
   8.344 -};
   8.345 -
   8.346 -void init_pyreiser(void) {
   8.347 -    Py_InitModule("_pyreiser", ReiserModuleMethods);
   8.348 -}
     9.1 --- a/tools/pygrub/src/pygrub	Thu Nov 09 14:09:53 2006 +0000
     9.2 +++ b/tools/pygrub/src/pygrub	Thu Nov 09 14:11:11 2006 +0000
     9.3 @@ -22,8 +22,8 @@ import getopt
     9.4  
     9.5  sys.path = [ '/usr/lib/python' ] + sys.path
     9.6  
     9.7 +import fsimage
     9.8  import grub.GrubConf
     9.9 -import grub.fsys
    9.10  
    9.11  PYGRUB_VER = 0.5
    9.12  
    9.13 @@ -313,25 +313,21 @@ class Grub:
    9.14                  raise RuntimeError, "Unable to find active partition on disk"
    9.15  
    9.16          # open the image and read the grub config
    9.17 -        fs = None
    9.18 -        for fstype in grub.fsys.fstypes.values():
    9.19 -            if fstype.sniff_magic(fn, offset):
    9.20 -                fs = fstype.open_fs(fn, offset)
    9.21 -                break
    9.22 +        fs = fsimage.open(fn, offset)
    9.23  
    9.24          if fs is not None:
    9.25              grubfile = None
    9.26              for f in ("/boot/grub/menu.lst", "/boot/grub/grub.conf",
    9.27                        "/grub/menu.lst", "/grub/grub.conf"):
    9.28 -                if fs.file_exist(f):
    9.29 +                if fs.file_exists(f):
    9.30                      grubfile = f
    9.31                      break
    9.32              if grubfile is None:
    9.33                  raise RuntimeError, "we couldn't find grub config file in the image provided."
    9.34              f = fs.open_file(grubfile)
    9.35              buf = f.read()
    9.36 -            f.close()
    9.37 -            fs.close()
    9.38 +            del f
    9.39 +            del fs
    9.40              # then parse the grub config
    9.41              self.cf.parse(buf)
    9.42          else:
    9.43 @@ -511,14 +507,7 @@ if __name__ == "__main__":
    9.44              raise RuntimeError, "Unable to find active partition on disk"
    9.45  
    9.46      # read the kernel and initrd onto the hostfs
    9.47 -    fs = None
    9.48 -    for fstype in grub.fsys.fstypes.values():
    9.49 -        if fstype.sniff_magic(file, offset):
    9.50 -            fs = fstype.open_fs(file, offset)
    9.51 -            break
    9.52 -
    9.53 -    if fs is None:
    9.54 -        raise RuntimeError, "Unable to open filesystem"
    9.55 +    fs = fsimage.open(file, offset)
    9.56  
    9.57      kernel = fs.open_file(img.kernel[1],).read()
    9.58      (tfd, fn) = tempfile.mkstemp(prefix="vmlinuz.", dir="/var/lib/xen")