From a6f6a1bb6ff2b876047173be7cec28a1e1faaaa4 Mon Sep 17 00:00:00 2001 From: charles Date: Fri, 9 Oct 2009 06:12:26 +0000 Subject: [PATCH] Change from SWIG to ctypes... much easier --- direct/python/Makefile | 24 +++++++---- direct/python/README.txt | 62 +++++++++++++++++++++++++++++ direct/python/yaffs_python_helper.c | 47 ++++++++++++++++++++++ direct/python/yaffs_swig_helper.c | 23 ----------- 4 files changed, 125 insertions(+), 31 deletions(-) create mode 100644 direct/python/README.txt create mode 100644 direct/python/yaffs_python_helper.c delete mode 100644 direct/python/yaffs_swig_helper.c diff --git a/direct/python/Makefile b/direct/python/Makefile index 5f24f97..f5ca02c 100644 --- a/direct/python/Makefile +++ b/direct/python/Makefile @@ -14,7 +14,7 @@ # # NB Warning this Makefile does not include header dependencies. # -# $Id: Makefile,v 1.2 2009-10-08 23:11:12 charles Exp $ +# $Id: Makefile,v 1.3 2009-10-09 06:12:26 charles Exp $ #EXTRA_COMPILE_FLAGS = -DYAFFS_IGNORE_TAGS_ECC @@ -36,10 +36,10 @@ COMMONTESTOBJS = yaffscfg2k.o yaffs_ecc.o yaffs_fileem.o yaffs_fileem2k.o yaffsf yaffs_norif1.o ynorsim.o -YAFFSTESTOBJS = $(COMMONTESTOBJS) yaffs_wrap.o yaffs_swig_helper.o +YAFFSSWIGOBJS = $(COMMONTESTOBJS) yaffs_wrap.o yaffs_python_helper.o +YAFFSLIBOBJS = $(COMMONTESTOBJS) yaffs_python_helper.o -ALLOBJS = $(sort $(YAFFSTESTOBJS)) YAFFSSYMLINKS = devextras.h yaffs_ecc.c yaffs_ecc.h yaffs_guts.c yaffs_guts.h yaffsinterface.h yportenv.h yaffs_tagscompat.c yaffs_tagscompat.h \ yaffs_packedtags1.c yaffs_packedtags1.h yaffs_packedtags2.c yaffs_packedtags2.h yaffs_nandemul2k.h \ @@ -56,7 +56,7 @@ YAFFSDIRECTSYMLINKS = yaffscfg2k.c yaffs_fileem2k.c yaffsfs.c yaffs_flashif.h y SYMLINKS = $(YAFFSSYMLINKS) $(YAFFSDIRECTSYMLINKS) -all: _yaffs.so +all: _yaffs.so libyaffsfs.so yaffs_wrap.c: yaffs.i yaffsfs_for_swig.h swig -python yaffs.i @@ -71,7 +71,10 @@ yaffsfs_for_swig.h: yaffsfs.h sed -e "s/struct yaffs_stat/struct yaffs_stat_struct/g" \ > $@ -$(ALLOBJS): %.o: %.c +$(YAFFSLIBOBJS): %.o: %.c + gcc -c $(CFLAGS) -o $@ $< + +yaffs_wrap.o: %.o: %.c gcc -c $(CFLAGS) -I/usr/include/python2.6/ -o $@ $< @@ -83,13 +86,18 @@ $(YAFFSDIRECTSYMLINKS): -_yaffs.so: $(SYMLINKS) yaffs_wrap.c $(ALLOBJS) - gcc -shared $(ALLOBJS) -o $@ +_yaffs.so: $(SYMLINKS) yaffs_wrap.c $(YAFFSSWIGOBJS) + gcc -shared $(YAFFSSWIGOBJS) -o $@ + +libyaffsfs.so: $(SYMLINKS) $(YAFFSLIBOBJS) + gcc -shared $(YAFFSLIBOBJS) -o $@ clean: - rm -f _yaffs.so yaffs.py yaffs.pyc yaffs_wrap.c $(ALLOBJS) core $(YAFFSSYMLINKS) $(YAFFSDIRECTSYMLINKS) + rm -f _yaffs.so yaffs.py yaffs.pyc yaffs_wrap.c $(YAFFSLIBOBJS) yaffs_wrap.o core $(YAFFSSYMLINKS) $(YAFFSDIRECTSYMLINKS) rm -f *.c.* rm -f yaffsfs_for_swig.h + rm -f libyaffsfs.so + diff --git a/direct/python/README.txt b/direct/python/README.txt new file mode 100644 index 0000000..a6706e1 --- /dev/null +++ b/direct/python/README.txt @@ -0,0 +1,62 @@ +Using python with yaffsfs +~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Herewith a brief session showing yaffs direct being accessed from python. + +I tried SWIG first, but ctypes turned out to be way, way easier... + +Thanks to the people on NZPUG mailing list that helped with the exercise. + +Build libyaffsfs.so with the accompanying Makefile. + +$ make libyaffsfs.so + +$ python +Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41) +[GCC 4.3.3] on linux2 +Type "help", "copyright", "credits" or "license" for more information. +>>> from ctypes import * +>>> cdll.LoadLibrary("./libyaffsfs.so") + +>>> y = CDLL("./libyaffsfs.so") +>>> y.yaffs_StartUp() +0 +>>> y.yaffs_mount("/yaffs2") +yaffs: Mounting /yaffs2 +yaffs: yaffs_GutsInitialise() +yaffs_ScanBackwards starts intstartblk 1 intendblk 256... +0 blocks to be sorted... +...done +yaffs_ScanBackwards ends + +Block summary +0 blocks have illegal states +Unknown 0 blocks +Needs scanning 0 blocks +Scanning 0 blocks +Empty 256 blocks +Allocating 0 blocks +Full 0 blocks +Dirty 0 blocks +Checkpoint 0 blocks +Collecting 0 blocks +Dead 0 blocks + +yaffs: yaffs_GutsInitialise() done. + +0 +>>> y.yaffs_open("/yaffs2/xx",66,0666) +yaffs: Tnodes added +Allocated block 1, seq 4097, 255 left +0 +>>> y.yaffs_write(0,"abc",3) +3 +>>> y.yaffs_lseek(0,0,0) +0 +>>> b = create_string_buffer("",100) +>>> y.yaffs_read(0,b,100) +3 +>>> b.value +'abc' +>>> + diff --git a/direct/python/yaffs_python_helper.c b/direct/python/yaffs_python_helper.c new file mode 100644 index 0000000..aa63d5e --- /dev/null +++ b/direct/python/yaffs_python_helper.c @@ -0,0 +1,47 @@ + +/* + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system. + * + * Copyright (C) 2002-2007 Aleph One Ltd. + * for Toby Churchill Ltd and Brightstar Engineering + * + * Created by Charles Manning + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + */ + +/* + * These are some dangly bits that need to be built to wrap up the rest of the yaffs test harness. + * + * + * This is also the place where extra debugging stuff might end up. + * + */ + +#include "yaffsfs.h" + +int simulate_power_failure; +int random_seed; + +int yaffs_print_constants(void) +{ + printf( "O_CREAT........%d\n",O_CREAT); + printf( "O_RDONLY.......%d\n",O_RDONLY); + printf( "O_WRONLY.......%d\n",O_WRONLY); + printf( "O_RDWR.........%d\n",O_RDWR); + printf( "O_TRUNC........%d\n",O_TRUNC); + + return 0; +} + +int yaffs_O_CREAT(void) { return O_CREAT;} +int yaffs_O_RDONLY(void) { return O_RDONLY;} +int yaffs_O_WRONLY(void) { return O_WRONLY;} +int yaffs_O_RDWR(void) { return O_RDWR;} +int yaffs_O_TRUNC(void) { return O_TRUNC;} + +int yaffs_set_trace(unsigned int tm) { return yaffs_traceMask=tm; } +int yaffs_get_trace(void) { return yaffs_traceMask; } diff --git a/direct/python/yaffs_swig_helper.c b/direct/python/yaffs_swig_helper.c deleted file mode 100644 index c803a55..0000000 --- a/direct/python/yaffs_swig_helper.c +++ /dev/null @@ -1,23 +0,0 @@ -/* - * These are some dangly bits that need to be built to wrap up the rest of the yaffs test harness - * in SWIG. - * - * This is also the place where extra debugging stuff might end up. - * - */ - -#include "yaffsfs.h" - -int simulate_power_failure; -int random_seed; - -int yaffs_print_constants(void) -{ - printf( "O_CREAT........%d\n",O_CREAT); - printf( "O_RDONLY.......%d\n",O_RDONLY); - printf( "O_WRONLY.......%d\n",O_WRONLY); - printf( "O_RDWR.........%d\n",O_RDWR); - printf( "O_TRUNC........%d\n",O_TRUNC); - - return 0; -} -- 2.30.2