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
|
/*****************************************************************************\
pcardext - Python extension for HP photocard services
(c) Copyright 2003-2015 HP Development Company, L.P.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Requires:
Python 2.2+
Author: Don Welch
\*****************************************************************************/
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <structmember.h>
#include "../fat.h"
/* Ref: PEP 353 (Python 2.5) */
#if PY_VERSION_HEX < 0x02050000
typedef int Py_ssize_t;
#define PY_SSIZE_T_MAX INT_MAX
#define PY_SSIZE_T_MIN INT_MIN
#endif
int verbose=0;
PyObject * readsectorFunc = NULL;
PyObject * writesectorFunc = NULL;
int ReadSector(int sector, int nsector, void *buf, int size)
{
PyObject * result;
char * result_str;
if( readsectorFunc )
{
if( nsector <= 0 || (nsector*FAT_HARDSECT) > size || nsector > FAT_BLKSIZE )
goto abort;
result = PyObject_CallFunction( readsectorFunc, "ii", sector, nsector );
if( result )
{
Py_ssize_t len = 0;
PyString_AsStringAndSize( result, &result_str, &len );
if( len < nsector*FAT_HARDSECT )
{
goto abort;
}
memcpy( buf, result_str, nsector*FAT_HARDSECT );
return 0;
}
}
abort:
return 1;
}
int WriteSector(int sector, int nsector, void *buf, int size )
{
PyObject * result;
if( writesectorFunc )
{
result = PyObject_CallFunction( writesectorFunc, "iis#", sector, nsector, buf, size );
return PyInt_AS_LONG( result );
}
return 1;
}
PyObject * pcardext_mount( PyObject * self, PyObject * args )
{
if( !PyArg_ParseTuple( args, "OO", &readsectorFunc, &writesectorFunc ) )
{
return Py_BuildValue( "i", 1 );
}
if( !PyCallable_Check( readsectorFunc ) || !PyCallable_Check( writesectorFunc ) )
{
return Py_BuildValue( "i", 2 );
}
Py_INCREF( readsectorFunc );
Py_INCREF( writesectorFunc );
int i = FatInit();
/*char buf[1024];
sprintf( buf, "print 'FatInit()=%d\n'", i );
PyRun_SimpleString( buf );*/
return Py_BuildValue( "i", i ); // ==0 ->OK, !=0 --> NG
}
PyObject * pcardext_df( PyObject * self, PyObject * args )
{
return Py_BuildValue( "i", FatFreeSpace() );
}
PyObject * pcardext_ls( PyObject * self, PyObject * args )
{
PyObject * file_list;
file_list = PyList_New((Py_ssize_t)0);
FILE_ATTRIBUTES fa;
FatDirBegin( &fa );
do
{
if( fa.Attr != 'x' )
PyList_Append( file_list, Py_BuildValue( "(sci)", fa.Name, fa.Attr, fa.Size ) );
} while( FatDirNext( &fa ) );
return file_list;
}
PyObject * pcardext_cp( PyObject * self, PyObject * args )
{
char * name;
int fileno = 0;
if( !PyArg_ParseTuple( args, "si", &name, &fileno ) )
{
return Py_BuildValue( "i", 0 );
}
return Py_BuildValue( "i", FatReadFile( name, fileno ) );
}
PyObject * pcardext_cd( PyObject * self, PyObject * args )
{
char * dir;
if( !PyArg_ParseTuple( args, "s", &dir ) )
{
return Py_BuildValue( "i", 0 );
}
FatSetCWD( dir );
return Py_BuildValue( "i", 1 );
}
PyObject * pcardext_rm( PyObject * self, PyObject * args )
{
char * name;
if( !PyArg_ParseTuple( args, "s", &name ) )
{
return Py_BuildValue( "i", 0 );
}
return Py_BuildValue( "i", FatDeleteFile( name ) );
}
PyObject * pcardext_umount( PyObject * self, PyObject * args )
{
return Py_BuildValue( "" );
}
PyObject * pcardext_info( PyObject * self, PyObject * args )
{
PHOTO_CARD_ATTRIBUTES pa;
FatDiskAttributes( &pa );
return Py_BuildValue( "(siiiiissi)", pa.OEMID, pa.BytesPerSector, pa.SectorsPerCluster, pa.ReservedSectors,
pa.RootEntries, pa.SectorsPerFat, pa.VolumeLabel, pa.SystemID,
pa.WriteProtect );
}
PyObject * pcardext_read( PyObject * self, PyObject * args )
{
char * name;
int offset = 0;
Py_ssize_t len = 0;
void * buffer;
if( !PyArg_ParseTuple( args, "sii", &name, &offset, &len ) )
{
return Py_BuildValue( "s", "" );
}
buffer = alloca( len );
if( FatReadFileExt( name, offset, len, buffer ) == len )
{
return PyString_FromStringAndSize( (char *)buffer, len );
}
else
{
return Py_BuildValue( "s", "" );
}
}
static PyMethodDef pcardext_methods[] =
{
{ "mount", (PyCFunction)pcardext_mount, METH_VARARGS },
{ "ls", (PyCFunction)pcardext_ls, METH_VARARGS },
{ "cp", (PyCFunction)pcardext_cp, METH_VARARGS },
{ "cd", (PyCFunction)pcardext_cd, METH_VARARGS },
{ "rm", (PyCFunction)pcardext_rm, METH_VARARGS },
{ "umount", (PyCFunction)pcardext_umount, METH_VARARGS },
{ "df", (PyCFunction)pcardext_df, METH_VARARGS },
{ "info", (PyCFunction)pcardext_info, METH_VARARGS },
{ "read", (PyCFunction)pcardext_read, METH_VARARGS },
{ NULL, NULL }
};
static char pcardext_documentation[] = "Python extension for HP photocard services";
void initpcardext( void )
{
PyObject * mod = Py_InitModule4( "pcardext", pcardext_methods,
pcardext_documentation, (PyObject*)NULL,
PYTHON_API_VERSION );
if (mod == NULL)
return;
}
|