Artifact b1aa06c0f1dac277695d4529e5e976c65ab5678dcbb53a0304deaa8adc44b332:
- File
ext/misc/fileio.c
— part of check-in
[55e93f25]
at
2017-07-11 18:11:33
on branch trunk
— Fix harmless compiler warnings in the core.
(user:
drh
size: 2597)
[more...]
0000: 2f 2a 0a 2a 2a 20 32 30 31 34 2d 30 36 2d 31 33 /*.** 2014-06-13
0010: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
0020: 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
0030: 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
0040: 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
0050: 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
0060: 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
0070: 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
0080: 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
0090: 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
00a0: 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
00b0: 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
00c0: 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
00d0: 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
00e0: 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
00f0: 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
0100: 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
0110: 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
0120: 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
0130: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0140: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0150: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0160: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0170: 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ****.**.** This
0180: 53 51 4c 69 74 65 20 65 78 74 65 6e 73 69 6f 6e SQLite extension
0190: 20 69 6d 70 6c 65 6d 65 6e 74 73 20 53 51 4c 20 implements SQL
01a0: 66 75 6e 63 74 69 6f 6e 73 20 72 65 61 64 66 69 functions readfi
01b0: 6c 65 28 29 20 61 6e 64 0a 2a 2a 20 77 72 69 74 le() and.** writ
01c0: 65 66 69 6c 65 28 29 2e 0a 2a 2f 0a 23 69 6e 63 efile()..*/.#inc
01d0: 6c 75 64 65 20 22 73 71 6c 69 74 65 33 65 78 74 lude "sqlite3ext
01e0: 2e 68 22 0a 53 51 4c 49 54 45 5f 45 58 54 45 4e .h".SQLITE_EXTEN
01f0: 53 49 4f 4e 5f 49 4e 49 54 31 0a 23 69 6e 63 6c SION_INIT1.#incl
0200: 75 64 65 20 3c 73 74 64 69 6f 2e 68 3e 0a 0a 2f ude <stdio.h>../
0210: 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 *.** Implementat
0220: 69 6f 6e 20 6f 66 20 74 68 65 20 22 72 65 61 64 ion of the "read
0230: 66 69 6c 65 28 58 29 22 20 53 51 4c 20 66 75 6e file(X)" SQL fun
0240: 63 74 69 6f 6e 2e 20 20 54 68 65 20 65 6e 74 69 ction. The enti
0250: 72 65 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 6f 66 re content.** of
0260: 20 74 68 65 20 66 69 6c 65 20 6e 61 6d 65 64 20 the file named
0270: 58 20 69 73 20 72 65 61 64 20 61 6e 64 20 72 65 X is read and re
0280: 74 75 72 6e 65 64 20 61 73 20 61 20 42 4c 4f 42 turned as a BLOB
0290: 2e 20 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 . NULL is retur
02a0: 6e 65 64 0a 2a 2a 20 69 66 20 74 68 65 20 66 69 ned.** if the fi
02b0: 6c 65 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 le does not exis
02c0: 74 20 6f 72 20 69 73 20 75 6e 72 65 61 64 61 62 t or is unreadab
02d0: 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f le..*/.static vo
02e0: 69 64 20 72 65 61 64 66 69 6c 65 46 75 6e 63 28 id readfileFunc(
02f0: 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 . sqlite3_conte
0300: 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 xt *context,. i
0310: 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 nt argc,. sqlit
0320: 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a e3_value **argv.
0330: 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 ){. const char
0340: 2a 7a 4e 61 6d 65 3b 0a 20 20 46 49 4c 45 20 2a *zName;. FILE *
0350: 69 6e 3b 0a 20 20 6c 6f 6e 67 20 6e 49 6e 3b 0a in;. long nIn;.
0360: 20 20 76 6f 69 64 20 2a 70 42 75 66 3b 0a 0a 20 void *pBuf;..
0370: 20 28 76 6f 69 64 29 28 61 72 67 63 29 3b 20 20 (void)(argc);
0380: 2f 2a 20 55 6e 75 73 65 64 20 70 61 72 61 6d 65 /* Unused parame
0390: 74 65 72 20 2a 2f 0a 20 20 7a 4e 61 6d 65 20 3d ter */. zName =
03a0: 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 (const char*)sq
03b0: 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
03c0: 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28 (argv[0]);. if(
03d0: 20 7a 4e 61 6d 65 3d 3d 30 20 29 20 72 65 74 75 zName==0 ) retu
03e0: 72 6e 3b 0a 20 20 69 6e 20 3d 20 66 6f 70 65 6e rn;. in = fopen
03f0: 28 7a 4e 61 6d 65 2c 20 22 72 62 22 29 3b 0a 20 (zName, "rb");.
0400: 20 69 66 28 20 69 6e 3d 3d 30 20 29 20 72 65 74 if( in==0 ) ret
0410: 75 72 6e 3b 0a 20 20 66 73 65 65 6b 28 69 6e 2c urn;. fseek(in,
0420: 20 30 2c 20 53 45 45 4b 5f 45 4e 44 29 3b 0a 20 0, SEEK_END);.
0430: 20 6e 49 6e 20 3d 20 66 74 65 6c 6c 28 69 6e 29 nIn = ftell(in)
0440: 3b 0a 20 20 72 65 77 69 6e 64 28 69 6e 29 3b 0a ;. rewind(in);.
0450: 20 20 70 42 75 66 20 3d 20 73 71 6c 69 74 65 33 pBuf = sqlite3
0460: 5f 6d 61 6c 6c 6f 63 28 20 6e 49 6e 20 29 3b 0a _malloc( nIn );.
0470: 20 20 69 66 28 20 70 42 75 66 20 26 26 20 31 3d if( pBuf && 1=
0480: 3d 66 72 65 61 64 28 70 42 75 66 2c 20 6e 49 6e =fread(pBuf, nIn
0490: 2c 20 31 2c 20 69 6e 29 20 29 7b 0a 20 20 20 20 , 1, in) ){.
04a0: 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 sqlite3_result_b
04b0: 6c 6f 62 28 63 6f 6e 74 65 78 74 2c 20 70 42 75 lob(context, pBu
04c0: 66 2c 20 6e 49 6e 2c 20 73 71 6c 69 74 65 33 5f f, nIn, sqlite3_
04d0: 66 72 65 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a free);. }else{.
04e0: 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
04f0: 28 70 42 75 66 29 3b 0a 20 20 7d 0a 20 20 66 63 (pBuf);. }. fc
0500: 6c 6f 73 65 28 69 6e 29 3b 0a 7d 0a 0a 2f 2a 0a lose(in);.}../*.
0510: 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** Implementatio
0520: 6e 20 6f 66 20 74 68 65 20 22 77 72 69 74 65 66 n of the "writef
0530: 69 6c 65 28 58 2c 59 29 22 20 53 51 4c 20 66 75 ile(X,Y)" SQL fu
0540: 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 61 72 67 nction. The arg
0550: 75 6d 65 6e 74 20 59 0a 2a 2a 20 69 73 20 77 72 ument Y.** is wr
0560: 69 74 74 65 6e 20 69 6e 74 6f 20 66 69 6c 65 20 itten into file
0570: 58 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 20 6f X. The number o
0580: 66 20 62 79 74 65 73 20 77 72 69 74 74 65 6e 20 f bytes written
0590: 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 4f 72 is returned. Or
05a0: 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 .** NULL is retu
05b0: 72 6e 65 64 20 69 66 20 73 6f 6d 65 74 68 69 6e rned if somethin
05c0: 67 20 67 6f 65 73 20 77 72 6f 6e 67 2c 20 73 75 g goes wrong, su
05d0: 63 68 20 61 73 20 62 65 69 6e 67 20 75 6e 61 62 ch as being unab
05e0: 6c 65 20 74 6f 20 6f 70 65 6e 0a 2a 2a 20 66 69 le to open.** fi
05f0: 6c 65 20 58 20 66 6f 72 20 77 72 69 74 69 6e 67 le X for writing
0600: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
0610: 20 77 72 69 74 65 66 69 6c 65 46 75 6e 63 28 0a writefileFunc(.
0620: 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 sqlite3_contex
0630: 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e t *context,. in
0640: 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 t argc,. sqlite
0650: 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 3_value **argv.)
0660: 7b 0a 20 20 46 49 4c 45 20 2a 6f 75 74 3b 0a 20 {. FILE *out;.
0670: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b 0a const char *z;.
0680: 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 sqlite3_int64
0690: 72 63 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 rc;. const char
06a0: 20 2a 7a 46 69 6c 65 3b 0a 0a 20 20 28 76 6f 69 *zFile;.. (voi
06b0: 64 29 28 61 72 67 63 29 3b 20 20 2f 2a 20 55 6e d)(argc); /* Un
06c0: 75 73 65 64 20 70 61 72 61 6d 65 74 65 72 20 2a used parameter *
06d0: 2f 0a 20 20 7a 46 69 6c 65 20 3d 20 28 63 6f 6e /. zFile = (con
06e0: 73 74 20 63 68 61 72 2a 29 73 71 6c 69 74 65 33 st char*)sqlite3
06f0: 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 _value_text(argv
0700: 5b 30 5d 29 3b 0a 20 20 69 66 28 20 7a 46 69 6c [0]);. if( zFil
0710: 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 e==0 ) return;.
0720: 20 6f 75 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69 out = fopen(zFi
0730: 6c 65 2c 20 22 77 62 22 29 3b 0a 20 20 69 66 28 le, "wb");. if(
0740: 20 6f 75 74 3d 3d 30 20 29 20 72 65 74 75 72 6e out==0 ) return
0750: 3b 0a 20 20 7a 20 3d 20 28 63 6f 6e 73 74 20 63 ;. z = (const c
0760: 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c har*)sqlite3_val
0770: 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 31 5d 29 ue_blob(argv[1])
0780: 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a ;. if( z==0 ){.
0790: 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 7d 65 rc = 0;. }e
07a0: 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 66 77 lse{. rc = fw
07b0: 72 69 74 65 28 7a 2c 20 31 2c 20 73 71 6c 69 74 rite(z, 1, sqlit
07c0: 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 e3_value_bytes(a
07d0: 72 67 76 5b 31 5d 29 2c 20 6f 75 74 29 3b 0a 20 rgv[1]), out);.
07e0: 20 7d 0a 20 20 66 63 6c 6f 73 65 28 6f 75 74 29 }. fclose(out)
07f0: 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 ;. sqlite3_resu
0800: 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65 78 74 lt_int64(context
0810: 2c 20 72 63 29 3b 0a 7d 0a 0a 0a 23 69 66 64 65 , rc);.}...#ifde
0820: 66 20 5f 57 49 4e 33 32 0a 5f 5f 64 65 63 6c 73 f _WIN32.__decls
0830: 70 65 63 28 64 6c 6c 65 78 70 6f 72 74 29 0a 23 pec(dllexport).#
0840: 65 6e 64 69 66 0a 69 6e 74 20 73 71 6c 69 74 65 endif.int sqlite
0850: 33 5f 66 69 6c 65 69 6f 5f 69 6e 69 74 28 0a 20 3_fileio_init(.
0860: 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 sqlite3 *db, .
0870: 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 char **pzErrMsg
0880: 2c 20 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 , . const sqlit
0890: 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 20 e3_api_routines
08a0: 2a 70 41 70 69 0a 29 7b 0a 20 20 69 6e 74 20 72 *pApi.){. int r
08b0: 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
08c0: 20 53 51 4c 49 54 45 5f 45 58 54 45 4e 53 49 4f SQLITE_EXTENSIO
08d0: 4e 5f 49 4e 49 54 32 28 70 41 70 69 29 3b 0a 20 N_INIT2(pApi);.
08e0: 20 28 76 6f 69 64 29 70 7a 45 72 72 4d 73 67 3b (void)pzErrMsg;
08f0: 20 20 2f 2a 20 55 6e 75 73 65 64 20 70 61 72 61 /* Unused para
0900: 6d 65 74 65 72 20 2a 2f 0a 20 20 72 63 20 3d 20 meter */. rc =
0910: 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 sqlite3_create_f
0920: 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22 72 65 61 unction(db, "rea
0930: 64 66 69 6c 65 22 2c 20 31 2c 20 53 51 4c 49 54 dfile", 1, SQLIT
0940: 45 5f 55 54 46 38 2c 20 30 2c 0a 20 20 20 20 20 E_UTF8, 0,.
0950: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0960: 20 20 20 20 20 20 20 20 20 20 72 65 61 64 66 69 readfi
0970: 6c 65 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a 20 leFunc, 0, 0);.
0980: 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
0990: 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 OK ){. rc = s
09a0: 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 qlite3_create_fu
09b0: 6e 63 74 69 6f 6e 28 64 62 2c 20 22 77 72 69 74 nction(db, "writ
09c0: 65 66 69 6c 65 22 2c 20 32 2c 20 53 51 4c 49 54 efile", 2, SQLIT
09d0: 45 5f 55 54 46 38 2c 20 30 2c 0a 20 20 20 20 20 E_UTF8, 0,.
09e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
09f0: 20 20 20 20 20 20 20 20 20 20 20 20 77 72 69 74 writ
0a00: 65 66 69 6c 65 46 75 6e 63 2c 20 30 2c 20 30 29 efileFunc, 0, 0)
0a10: 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
0a20: 63 3b 0a 7d 0a c;.}.