From: drh Date: Fri, 13 Jun 2014 13:43:25 +0000 (+0000) Subject: Add the fileio.c loadable extension, that implements readfile() and writefile() X-Git-Tag: version-3.8.6~119 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=40e75cb6f6dd3dfebb344dfd13255c21745a02e3;p=thirdparty%2Fsqlite.git Add the fileio.c loadable extension, that implements readfile() and writefile() SQL functions. FossilOrigin-Name: 0ca104d821d5841ab0754113be074c520cf07f23 --- diff --git a/ext/misc/fileio.c b/ext/misc/fileio.c new file mode 100644 index 0000000000..6ab073535a --- /dev/null +++ b/ext/misc/fileio.c @@ -0,0 +1,103 @@ +/* +** 2014-06-13 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This SQLite extension implements SQL functions readfile() and +** writefile(). +*/ +#include "sqlite3ext.h" +SQLITE_EXTENSION_INIT1 +#include + +/* +** Implementation of the "readfile(X)" SQL function. The entire content +** of the file named X is read and returned as a BLOB. NULL is returned +** if the file does not exist or is unreadable. +*/ +static void readfileFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + const char *zName; + FILE *in; + long nIn; + void *pBuf; + + zName = (const char*)sqlite3_value_text(argv[0]); + if( zName==0 ) return; + in = fopen(zName, "rb"); + if( in==0 ) return; + fseek(in, 0, SEEK_END); + nIn = ftell(in); + rewind(in); + pBuf = sqlite3_malloc( nIn ); + if( pBuf && 1==fread(pBuf, nIn, 1, in) ){ + sqlite3_result_blob(context, pBuf, nIn, sqlite3_free); + }else{ + sqlite3_free(pBuf); + } + fclose(in); +} + +/* +** Implementation of the "writefile(X,Y)" SQL function. The argument Y +** is written into file X. The number of bytes written is returned. Or +** NULL is returned if something goes wrong, such as being unable to open +** file X for writing. +*/ +static void writefileFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + FILE *out; + const char *z; + int n; + long rc; + const char *zFile; + + zFile = (const char*)sqlite3_value_text(argv[0]); + if( zFile==0 ) return; + out = fopen(zFile, "wb"); + if( out==0 ) return; + z = (const char*)sqlite3_value_blob(argv[1]); + if( z==0 ){ + n = 0; + rc = 0; + }else{ + n = sqlite3_value_bytes(argv[1]); + rc = fwrite(z, 1, n, out); + } + fclose(out); + sqlite3_result_int64(context, rc); +} + + +#ifdef _WIN32 +__declspec(dllexport) +#endif +int sqlite3_fileio_init( + sqlite3 *db, + char **pzErrMsg, + const sqlite3_api_routines *pApi +){ + int rc = SQLITE_OK; + SQLITE_EXTENSION_INIT2(pApi); + (void)pzErrMsg; /* Unused parameter */ + rc = sqlite3_create_function(db, "readfile", 1, SQLITE_UTF8, 0, + readfileFunc, 0, 0); + if( rc==SQLITE_OK ){ + rc = sqlite3_create_function(db, "writefile", 2, SQLITE_UTF8, 0, + writefileFunc, 0, 0); + } + return rc; +} diff --git a/manifest b/manifest index 87826c081e..0c69079fb3 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Add\san\sextension\sthat\simplements\scompress()\sand\suncompress()\sSQL\sfunctions. -D 2014-06-13T13:08:21.440 +C Add\sthe\sfileio.c\sloadable\sextension,\sthat\simplements\sreadfile()\sand\swritefile()\nSQL\sfunctions. +D 2014-06-13T13:43:25.488 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in dd2b1aba364ff9b05de41086f74407f285c57670 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@ -109,6 +109,7 @@ F ext/icu/sqliteicu.h 728867a802baa5a96de7495e9689a8e01715ef37 F ext/misc/amatch.c 678056a4bfcd83c4e82dea81d37543cd1d6dbee1 F ext/misc/closure.c 636024302cde41b2bf0c542f81c40c624cfb7012 F ext/misc/compress.c 76e45655f4046e756064ab10c62e18f2eb846b9f +F ext/misc/fileio.c 2927bf833edad966ed222f0552c0602ae473ca16 F ext/misc/fuzzer.c 136533c53cfce0957f0b48fa11dba27e21c5c01d F ext/misc/ieee754.c b0362167289170627659e84173f5d2e8fee8566e F ext/misc/nextchar.c 35c8b8baacb96d92abbb34a83a997b797075b342 @@ -1175,7 +1176,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4 F tool/warnings.sh 0abfd78ceb09b7f7c27c688c8e3fe93268a13b32 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f -P 2aeacf81df92b4fe5d1825c1dc1cd176b8af8bd9 -R d1737f084b807e547e6c83682c2e8d71 +P d5c17d1a423321f766616d82c9b27ef87c1d5afd +R 1805f6a0ae3bbffdcfbcff54f7b6c6b0 U drh -Z 8447f1843125715fcf2f15ddbc0454fa +Z 904e0ffbd1a81754412e5093211c823c diff --git a/manifest.uuid b/manifest.uuid index dd7a18f631..b70849785c 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -d5c17d1a423321f766616d82c9b27ef87c1d5afd \ No newline at end of file +0ca104d821d5841ab0754113be074c520cf07f23 \ No newline at end of file