0000: 2f 2a 0a 2a 2a 20 43 6f 70 79 72 69 67 68 74 20 /*.** Copyright
0010: 28 63 29 20 32 30 30 38 20 44 2e 20 52 69 63 68 (c) 2008 D. Rich
0020: 61 72 64 20 48 69 70 70 0a 2a 2a 0a 2a 2a 20 54 ard Hipp.**.** T
0030: 68 69 73 20 70 72 6f 67 72 61 6d 20 69 73 20 66 his program is f
0040: 72 65 65 20 73 6f 66 74 77 61 72 65 3b 20 79 6f ree software; yo
0050: 75 20 63 61 6e 20 72 65 64 69 73 74 72 69 62 75 u can redistribu
0060: 74 65 20 69 74 20 61 6e 64 2f 6f 72 0a 2a 2a 20 te it and/or.**
0070: 6d 6f 64 69 66 79 20 69 74 20 75 6e 64 65 72 20 modify it under
0080: 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 65 the terms of the
0090: 20 47 4e 55 20 47 65 6e 65 72 61 6c 20 50 75 62 GNU General Pub
00a0: 6c 69 63 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 76 lic.** License v
00b0: 65 72 73 69 6f 6e 20 32 20 61 73 20 70 75 62 6c ersion 2 as publ
00c0: 69 73 68 65 64 20 62 79 20 74 68 65 20 46 72 65 ished by the Fre
00d0: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64 e Software Found
00e0: 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 ation..**.** Thi
00f0: 73 20 70 72 6f 67 72 61 6d 20 69 73 20 64 69 73 s program is dis
0100: 74 72 69 62 75 74 65 64 20 69 6e 20 74 68 65 20 tributed in the
0110: 68 6f 70 65 20 74 68 61 74 20 69 74 20 77 69 6c hope that it wil
0120: 6c 20 62 65 20 75 73 65 66 75 6c 2c 0a 2a 2a 20 l be useful,.**
0130: 62 75 74 20 57 49 54 48 4f 55 54 20 41 4e 59 20 but WITHOUT ANY
0140: 57 41 52 52 41 4e 54 59 3b 20 77 69 74 68 6f 75 WARRANTY; withou
0150: 74 20 65 76 65 6e 20 74 68 65 20 69 6d 70 6c 69 t even the impli
0160: 65 64 20 77 61 72 72 61 6e 74 79 20 6f 66 0a 2a ed warranty of.*
0170: 2a 20 4d 45 52 43 48 41 4e 54 41 42 49 4c 49 54 * MERCHANTABILIT
0180: 59 20 6f 72 20 46 49 54 4e 45 53 53 20 46 4f 52 Y or FITNESS FOR
0190: 20 41 20 50 41 52 54 49 43 55 4c 41 52 20 50 55 A PARTICULAR PU
01a0: 52 50 4f 53 45 2e 20 20 53 65 65 20 74 68 65 20 RPOSE. See the
01b0: 47 4e 55 0a 2a 2a 20 47 65 6e 65 72 61 6c 20 50 GNU.** General P
01c0: 75 62 6c 69 63 20 4c 69 63 65 6e 73 65 20 66 6f ublic License fo
01d0: 72 20 6d 6f 72 65 20 64 65 74 61 69 6c 73 2e 0a r more details..
01e0: 2a 2a 20 0a 2a 2a 20 59 6f 75 20 73 68 6f 75 6c ** .** You shoul
01f0: 64 20 68 61 76 65 20 72 65 63 65 69 76 65 64 20 d have received
0200: 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 47 4e a copy of the GN
0210: 55 20 47 65 6e 65 72 61 6c 20 50 75 62 6c 69 63 U General Public
0220: 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 61 6c 6f 6e .** License alon
0230: 67 20 77 69 74 68 20 74 68 69 73 20 6c 69 62 72 g with this libr
0240: 61 72 79 3b 20 69 66 20 6e 6f 74 2c 20 77 72 69 ary; if not, wri
0250: 74 65 20 74 6f 20 74 68 65 0a 2a 2a 20 46 72 65 te to the.** Fre
0260: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64 e Software Found
0270: 61 74 69 6f 6e 2c 20 49 6e 63 2e 2c 20 35 39 20 ation, Inc., 59
0280: 54 65 6d 70 6c 65 20 50 6c 61 63 65 20 2d 20 53 Temple Place - S
0290: 75 69 74 65 20 33 33 30 2c 0a 2a 2a 20 42 6f 73 uite 330,.** Bos
02a0: 74 6f 6e 2c 20 4d 41 20 20 30 32 31 31 31 2d 31 ton, MA 02111-1
02b0: 33 30 37 2c 20 55 53 41 2e 0a 2a 2a 0a 2a 2a 20 307, USA..**.**
02c0: 41 75 74 68 6f 72 20 63 6f 6e 74 61 63 74 20 69 Author contact i
02d0: 6e 66 6f 72 6d 61 74 69 6f 6e 3a 0a 2a 2a 20 20 nformation:.**
02e0: 20 64 72 68 40 68 77 61 63 69 2e 63 6f 6d 0a 2a drh@hwaci.com.*
02f0: 2a 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e 68 * http://www.h
0300: 77 61 63 69 2e 63 6f 6d 2f 64 72 68 2f 0a 2a 2a waci.com/drh/.**
0310: 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
0320: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0330: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0340: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0350: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0360: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
0370: 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 contains code t
0380: 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 o implement the
0390: 66 69 6c 65 20 62 72 6f 77 73 65 72 20 77 65 62 file browser web
03a0: 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 23 interface..*/.#
03b0: 69 6e 63 6c 75 64 65 20 22 63 6f 6e 66 69 67 2e include "config.
03c0: 68 22 0a 23 69 6e 63 6c 75 64 65 20 22 62 72 6f h".#include "bro
03d0: 77 73 65 2e 68 22 0a 23 69 6e 63 6c 75 64 65 20 wse.h".#include
03e0: 3c 61 73 73 65 72 74 2e 68 3e 0a 0a 2f 2a 0a 2a <assert.h>../*.*
03f0: 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 69 6d * This is the im
0400: 70 6c 65 6d 65 6e 74 69 6f 6e 20 6f 66 20 74 68 plemention of th
0410: 65 20 22 70 61 74 68 65 6c 65 6d 65 6e 74 28 58 e "pathelement(X
0420: 2c 4e 29 22 20 53 51 4c 20 66 75 6e 63 74 69 6f ,N)" SQL functio
0430: 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 58 20 69 73 n..**.** If X is
0440: 20 61 20 75 6e 69 78 2d 6c 69 6b 65 20 70 61 74 a unix-like pat
0450: 68 6e 61 6d 65 20 28 77 69 74 68 20 22 2f 22 20 hname (with "/"
0460: 73 65 70 61 72 61 74 6f 72 73 29 20 61 6e 64 20 separators) and
0470: 4e 20 69 73 20 61 6e 0a 2a 2a 20 69 6e 74 65 67 N is an.** integ
0480: 65 72 2c 20 74 68 65 6e 20 73 6b 69 70 20 74 68 er, then skip th
0490: 65 20 69 6e 69 74 69 61 6c 20 4e 20 63 68 61 72 e initial N char
04a0: 61 63 74 65 72 73 20 6f 66 20 58 20 61 6e 64 20 acters of X and
04b0: 72 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20 6e 61 return the.** na
04c0: 6d 65 20 6f 66 20 74 68 65 20 70 61 74 68 20 63 me of the path c
04d0: 6f 6d 70 6f 6e 65 6e 74 20 74 68 61 74 20 62 65 omponent that be
04e0: 67 69 6e 73 20 6f 6e 20 74 68 65 20 4e 2b 31 74 gins on the N+1t
04f0: 68 20 63 68 61 72 61 63 74 65 72 0a 2a 2a 20 28 h character.** (
0500: 6e 75 6d 62 65 72 65 64 20 66 72 6f 6d 20 30 29 numbered from 0)
0510: 2e 20 20 49 66 20 74 68 65 20 70 61 74 68 20 63 . If the path c
0520: 6f 6d 70 6f 6e 65 6e 74 20 69 73 20 61 20 64 69 omponent is a di
0530: 72 65 63 74 6f 72 79 20 28 69 66 0a 2a 2a 20 69 rectory (if.** i
0540: 74 20 69 73 20 66 6f 6c 6c 6f 77 65 64 20 62 79 t is followed by
0550: 20 6f 74 68 65 72 20 70 61 74 68 20 63 6f 6d 70 other path comp
0560: 6f 6e 65 6e 74 73 29 20 74 68 65 6e 20 70 72 65 onents) then pre
0570: 70 65 6e 64 20 22 2f 22 2e 0a 2a 2a 0a 2a 2a 20 pend "/"..**.**
0580: 45 78 61 6d 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 Examples:.**.**
0590: 20 20 20 20 20 70 61 74 68 65 6c 65 6d 65 6e 74 pathelement
05a0: 28 27 61 62 63 2f 70 71 72 2f 78 79 7a 27 2c 20 ('abc/pqr/xyz',
05b0: 34 29 20 20 2d 3e 20 20 27 2f 70 71 72 27 0a 2a 4) -> '/pqr'.*
05c0: 2a 20 20 20 20 20 20 70 61 74 68 65 6c 65 6d 65 * patheleme
05d0: 6e 74 28 27 61 62 63 2f 70 71 72 27 2c 20 34 29 nt('abc/pqr', 4)
05e0: 20 20 20 20 20 20 2d 3e 20 20 27 70 71 72 27 0a -> 'pqr'.
05f0: 2a 2a 20 20 20 20 20 20 70 61 74 68 65 6c 65 6d ** pathelem
0600: 65 6e 74 28 27 61 62 63 2f 70 71 72 2f 78 79 7a ent('abc/pqr/xyz
0610: 27 2c 20 30 29 20 20 2d 3e 20 20 27 2f 61 62 63 ', 0) -> '/abc
0620: 27 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 '.*/.static void
0630: 20 70 61 74 68 65 6c 65 6d 65 6e 74 46 75 6e 63 pathelementFunc
0640: 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 (. sqlite3_cont
0650: 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 ext *context,.
0660: 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 int argc,. sqli
0670: 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 te3_value **argv
0680: 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 .){. const unsi
0690: 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a 20 20 gned char *z;.
06a0: 69 6e 74 20 6c 65 6e 2c 20 6e 2c 20 69 3b 0a 20 int len, n, i;.
06b0: 20 63 68 61 72 20 2a 7a 4f 75 74 3b 0a 0a 20 20 char *zOut;..
06c0: 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 32 20 assert( argc==2
06d0: 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 );. z = sqlite3
06e0: 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 _value_text(argv
06f0: 5b 30 5d 29 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 [0]);. if( z==0
0700: 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6c 65 6e ) return;. len
0710: 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 = sqlite3_value
0720: 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b _bytes(argv[0]);
0730: 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 . n = sqlite3_v
0740: 61 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b 31 5d alue_int(argv[1]
0750: 29 3b 0a 20 20 69 66 28 20 6c 65 6e 3c 3d 6e 20 );. if( len<=n
0760: 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 ) return;. if(
0770: 6e 3e 30 20 26 26 20 7a 5b 6e 2d 31 5d 21 3d 27 n>0 && z[n-1]!='
0780: 2f 27 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 /' ) return;. f
0790: 6f 72 28 69 3d 6e 3b 20 69 3c 6c 65 6e 20 26 26 or(i=n; i<len &&
07a0: 20 7a 5b 69 5d 21 3d 27 2f 27 3b 20 69 2b 2b 29 z[i]!='/'; i++)
07b0: 7b 7d 0a 20 20 69 66 28 20 69 3d 3d 6c 65 6e 20 {}. if( i==len
07c0: 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 ){. sqlite3_r
07d0: 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 esult_text(conte
07e0: 78 74 2c 20 28 63 68 61 72 2a 29 26 7a 5b 6e 5d xt, (char*)&z[n]
07f0: 2c 20 6c 65 6e 2d 6e 2c 20 53 51 4c 49 54 45 5f , len-n, SQLITE_
0800: 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 65 TRANSIENT);. }e
0810: 6c 73 65 7b 0a 20 20 20 20 7a 4f 75 74 20 3d 20 lse{. zOut =
0820: 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 sqlite3_mprintf(
0830: 22 2f 25 2e 2a 73 22 2c 20 69 2d 6e 2c 20 26 7a "/%.*s", i-n, &z
0840: 5b 6e 5d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 [n]);. sqlite
0850: 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 3_result_text(co
0860: 6e 74 65 78 74 2c 20 7a 4f 75 74 2c 20 69 2d 6e ntext, zOut, i-n
0870: 2b 31 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 +1, sqlite3_free
0880: 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a );. }.}.../*.**
0890: 20 57 45 42 50 41 47 45 3a 20 64 69 72 0a 2a 2a WEBPAGE: dir.**
08a0: 0a 2a 2a 20 51 75 65 72 79 20 70 61 72 61 6d 65 .** Query parame
08b0: 74 65 72 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 64 ters:.**.** d
08c0: 3d 50 41 54 48 20 20 20 20 20 20 20 20 44 69 72 =PATH Dir
08d0: 65 63 74 6f 72 79 20 74 6f 20 64 69 73 70 6c 61 ectory to displa
08e0: 79 2e 20 20 52 65 71 75 69 72 65 64 2e 0a 2a 2f y. Required..*/
08f0: 0a 76 6f 69 64 20 70 61 67 65 5f 64 69 72 28 76 .void page_dir(v
0900: 6f 69 64 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 oid){. const ch
0910: 61 72 20 2a 7a 44 20 3d 20 50 28 22 64 22 29 3b ar *zD = P("d");
0920: 0a 20 20 69 6e 74 20 6d 78 4c 65 6e 3b 0a 20 20 . int mxLen;.
0930: 69 6e 74 20 6e 43 6f 6c 2c 20 6e 52 6f 77 3b 0a int nCol, nRow;.
0940: 20 20 69 6e 74 20 63 6e 74 2c 20 69 3b 0a 20 20 int cnt, i;.
0950: 63 68 61 72 20 2a 7a 50 72 65 66 69 78 3b 0a 20 char *zPrefix;.
0960: 20 53 74 6d 74 20 71 3b 0a 0a 20 20 6c 6f 67 69 Stmt q;.. logi
0970: 6e 5f 63 68 65 63 6b 5f 63 72 65 64 65 6e 74 69 n_check_credenti
0980: 61 6c 73 28 29 3b 0a 20 20 69 66 28 20 21 67 2e als();. if( !g.
0990: 6f 6b 48 69 73 74 6f 72 79 20 29 7b 20 6c 6f 67 okHistory ){ log
09a0: 69 6e 5f 6e 65 65 64 65 64 28 29 3b 20 72 65 74 in_needed(); ret
09b0: 75 72 6e 3b 20 7d 0a 20 20 73 74 79 6c 65 5f 68 urn; }. style_h
09c0: 65 61 64 65 72 28 22 46 69 6c 65 20 4c 69 73 74 eader("File List
09d0: 22 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 ");. sqlite3_cr
09e0: 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 67 2e eate_function(g.
09f0: 64 62 2c 20 22 70 61 74 68 65 6c 65 6d 65 6e 74 db, "pathelement
0a00: 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 ", 2, SQLITE_UTF
0a10: 38 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 8, 0,.
0a20: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0a30: 70 61 74 68 65 6c 65 6d 65 6e 74 46 75 6e 63 2c pathelementFunc,
0a40: 20 30 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 49 66 0, 0);.. /* If
0a50: 20 74 68 65 20 64 3d 20 70 61 72 61 6d 65 74 65 the d= paramete
0a60: 72 20 69 73 20 61 6e 20 65 6d 70 74 79 20 73 74 r is an empty st
0a70: 72 69 6e 67 2c 20 6d 61 6b 65 20 69 74 20 61 20 ring, make it a
0a80: 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a NULL pointer */.
0a90: 20 20 69 66 28 20 7a 44 20 26 26 20 73 74 72 6c if( zD && strl
0aa0: 65 6e 28 7a 44 29 3d 3d 30 20 29 7b 20 7a 44 20 en(zD)==0 ){ zD
0ab0: 3d 20 30 3b 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6d = 0; }.. /* Com
0ac0: 70 75 74 65 20 74 68 65 20 74 69 74 6c 65 20 6f pute the title o
0ad0: 66 20 74 68 65 20 70 61 67 65 20 2a 2f 20 20 0a f the page */ .
0ae0: 20 20 69 66 28 20 7a 44 20 29 7b 0a 20 20 20 20 if( zD ){.
0af0: 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 20 20 63 68 int i, j;. ch
0b00: 61 72 20 2a 7a 43 6f 70 79 3b 0a 20 20 20 20 42 ar *zCopy;. B
0b10: 6c 6f 62 20 74 69 74 6c 65 3b 0a 0a 20 20 20 20 lob title;..
0b20: 62 6c 6f 62 5f 7a 65 72 6f 28 26 74 69 74 6c 65 blob_zero(&title
0b30: 29 3b 0a 20 20 20 20 7a 43 6f 70 79 20 3d 20 73 );. zCopy = s
0b40: 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 qlite3_mprintf("
0b50: 25 73 2f 22 2c 20 7a 44 29 3b 0a 20 20 20 20 62 %s/", zD);. b
0b60: 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 74 69 74 lob_appendf(&tit
0b70: 6c 65 2c 0a 20 20 20 20 20 20 20 22 46 69 6c 65 le,. "File
0b80: 73 20 69 6e 20 64 69 72 65 63 74 6f 72 79 20 3c s in directory <
0b90: 61 20 68 72 65 66 3d 5c 22 25 73 2f 64 69 72 5c a href=\"%s/dir\
0ba0: 22 3e 3c 69 3e 72 6f 6f 74 3c 2f 69 3e 3c 2f 61 "><i>root</i></a
0bb0: 3e 22 2c 0a 20 20 20 20 20 20 20 67 2e 7a 42 61 >",. g.zBa
0bc0: 73 65 55 52 4c 0a 20 20 20 20 29 3b 0a 20 20 20 seURL. );.
0bd0: 20 66 6f 72 28 69 3d 30 3b 20 7a 44 5b 69 5d 3b for(i=0; zD[i];
0be0: 20 69 3d 6a 29 7b 0a 20 20 20 20 20 20 66 6f 72 i=j){. for
0bf0: 28 6a 3d 69 3b 20 7a 44 5b 6a 5d 20 26 26 20 7a (j=i; zD[j] && z
0c00: 44 5b 6a 5d 21 3d 27 2f 27 3b 20 6a 2b 2b 29 7b D[j]!='/'; j++){
0c10: 7d 0a 20 20 20 20 20 20 69 66 28 20 7a 44 5b 6a }. if( zD[j
0c20: 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 43 6f ] ){. zCo
0c30: 70 79 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 20 py[j] = 0;.
0c40: 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 blob_appendf(
0c50: 26 74 69 74 6c 65 2c 20 22 2f 3c 61 20 68 72 65 &title, "/<a hre
0c60: 66 3d 5c 22 25 73 2f 64 69 72 3f 64 3d 25 54 5c f=\"%s/dir?d=%T\
0c70: 22 3e 25 68 3c 2f 61 3e 22 2c 20 0a 20 20 20 20 ">%h</a>", .
0c80: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0c90: 20 67 2e 7a 42 61 73 65 55 52 4c 2c 20 7a 43 6f g.zBaseURL, zCo
0ca0: 70 79 2c 20 26 7a 43 6f 70 79 5b 69 5d 29 3b 0a py, &zCopy[i]);.
0cb0: 20 20 20 20 20 20 20 20 7a 43 6f 70 79 5b 6a 5d zCopy[j]
0cc0: 20 3d 20 27 2f 27 3b 0a 20 20 20 20 20 20 7d 65 = '/';. }e
0cd0: 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 62 6c 6f lse{. blo
0ce0: 62 5f 61 70 70 65 6e 64 66 28 26 74 69 74 6c 65 b_appendf(&title
0cf0: 2c 20 22 2f 25 68 22 2c 20 26 7a 43 6f 70 79 5b , "/%h", &zCopy[
0d00: 69 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 i]);. }.
0d10: 20 20 20 77 68 69 6c 65 28 20 7a 44 5b 6a 5d 3d while( zD[j]=
0d20: 3d 27 2f 27 20 29 7b 20 6a 2b 2b 3b 20 7d 0a 20 ='/' ){ j++; }.
0d30: 20 20 20 7d 0a 20 20 20 20 40 20 3c 68 32 3e 25 }. @ <h2>%
0d40: 73 28 62 6c 6f 62 5f 73 74 72 28 26 74 69 74 6c s(blob_str(&titl
0d50: 65 29 29 3c 2f 68 32 3e 0a 20 20 20 20 62 6c 6f e))</h2>. blo
0d60: 62 5f 72 65 73 65 74 28 26 74 69 74 6c 65 29 3b b_reset(&title);
0d70: 0a 20 20 20 20 7a 50 72 65 66 69 78 20 3d 20 7a . zPrefix = z
0d80: 43 6f 70 79 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 Copy;. }else{.
0d90: 20 20 20 40 20 3c 68 32 3e 46 69 6c 65 73 20 69 @ <h2>Files i
0da0: 6e 20 74 68 65 20 74 6f 70 2d 6c 65 76 65 6c 20 n the top-level
0db0: 64 69 72 65 63 74 6f 72 79 3c 2f 68 32 3e 0a 20 directory</h2>.
0dc0: 20 20 20 7a 50 72 65 66 69 78 20 3d 20 22 22 3b zPrefix = "";
0dd0: 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6d 70 75 . }.. /* Compu
0de0: 74 65 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 te the temporary
0df0: 20 74 61 62 6c 65 20 22 6c 6f 63 61 6c 66 69 6c table "localfil
0e00: 65 73 22 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 es" containing t
0e10: 68 65 20 6e 61 6d 65 73 0a 20 20 2a 2a 20 6f 66 he names. ** of
0e20: 20 61 6c 6c 20 66 69 6c 65 73 20 61 6e 64 20 73 all files and s
0e30: 75 62 64 69 72 65 63 74 6f 72 69 65 73 20 69 6e ubdirectories in
0e40: 20 74 68 65 20 7a 44 5b 5d 20 64 69 72 65 63 74 the zD[] direct
0e50: 6f 72 79 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a ory. . **. **
0e60: 20 53 75 62 64 69 72 65 63 74 6f 72 79 20 6e 61 Subdirectory na
0e70: 6d 65 73 20 62 65 67 69 6e 20 77 69 74 68 20 22 mes begin with "
0e80: 2f 22 2e 20 20 54 68 69 73 20 63 61 75 73 65 73 /". This causes
0e90: 20 74 68 65 6d 20 74 6f 20 73 6f 72 74 0a 20 20 them to sort.
0ea0: 2a 2a 20 66 69 72 73 74 20 61 6e 64 20 69 74 20 ** first and it
0eb0: 61 6c 73 6f 20 67 69 76 65 73 20 75 73 20 61 6e also gives us an
0ec0: 20 65 61 73 79 20 77 61 79 20 74 6f 20 64 69 73 easy way to dis
0ed0: 74 69 6e 67 75 69 73 68 20 66 69 6c 65 73 0a 20 tinguish files.
0ee0: 20 2a 2a 20 66 72 6f 6d 20 64 69 72 65 63 74 6f ** from directo
0ef0: 72 69 65 73 20 69 6e 20 74 68 65 20 6c 6f 6f 70 ries in the loop
0f00: 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2e 0a 20 that follows..
0f10: 20 2a 2f 0a 20 20 69 66 28 20 7a 44 20 29 7b 0a */. if( zD ){.
0f20: 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 db_multi_exe
0f30: 63 28 0a 20 20 20 20 20 20 20 22 43 52 45 41 54 c(. "CREAT
0f40: 45 20 54 45 4d 50 20 54 41 42 4c 45 20 6c 6f 63 E TEMP TABLE loc
0f50: 61 6c 66 69 6c 65 73 28 78 20 55 4e 49 51 55 45 alfiles(x UNIQUE
0f60: 20 4e 4f 54 20 4e 55 4c 4c 29 3b 22 0a 20 20 20 NOT NULL);".
0f70: 20 20 20 20 22 49 4e 53 45 52 54 20 4f 52 20 49 "INSERT OR I
0f80: 47 4e 4f 52 45 20 49 4e 54 4f 20 6c 6f 63 61 6c GNORE INTO local
0f90: 66 69 6c 65 73 20 22 0a 20 20 20 20 20 20 20 22 files ". "
0fa0: 20 20 53 45 4c 45 43 54 20 70 61 74 68 65 6c 65 SELECT pathele
0fb0: 6d 65 6e 74 28 6e 61 6d 65 2c 25 64 29 20 46 52 ment(name,%d) FR
0fc0: 4f 4d 20 66 69 6c 65 6e 61 6d 65 22 0a 20 20 20 OM filename".
0fd0: 20 20 20 20 22 20 20 20 57 48 45 52 45 20 2b 6e " WHERE +n
0fe0: 61 6d 65 20 47 4c 4f 42 20 27 25 71 2f 2a 27 22 ame GLOB '%q/*'"
0ff0: 2c 0a 20 20 20 20 20 20 20 73 74 72 6c 65 6e 28 ,. strlen(
1000: 7a 44 29 2b 31 2c 20 7a 44 0a 20 20 20 20 29 3b zD)+1, zD. );
1010: 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 62 . }else{. db
1020: 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20 _multi_exec(.
1030: 20 20 20 20 22 43 52 45 41 54 45 20 54 45 4d 50 "CREATE TEMP
1040: 20 54 41 42 4c 45 20 6c 6f 63 61 6c 66 69 6c 65 TABLE localfile
1050: 73 28 78 20 55 4e 49 51 55 45 20 4e 4f 54 20 4e s(x UNIQUE NOT N
1060: 55 4c 4c 29 3b 22 0a 20 20 20 20 20 20 20 22 49 ULL);". "I
1070: 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 NSERT OR IGNORE
1080: 49 4e 54 4f 20 6c 6f 63 61 6c 66 69 6c 65 73 20 INTO localfiles
1090: 22 0a 20 20 20 20 20 20 20 22 20 20 53 45 4c 45 ". " SELE
10a0: 43 54 20 70 61 74 68 65 6c 65 6d 65 6e 74 28 6e CT pathelement(n
10b0: 61 6d 65 2c 30 29 20 46 52 4f 4d 20 66 69 6c 65 ame,0) FROM file
10c0: 6e 61 6d 65 22 0a 20 20 20 20 29 3b 0a 20 20 7d name". );. }
10d0: 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 .. /* Generate
10e0: 61 20 6d 75 6c 74 69 2d 63 6f 6c 75 6d 6e 20 74 a multi-column t
10f0: 61 62 6c 65 20 6c 69 73 74 69 6e 67 20 74 68 65 able listing the
1100: 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 7a 44 5b contents of zD[
1110: 5d 0a 20 20 2a 2a 20 64 69 72 65 63 74 6f 72 79 ]. ** directory
1120: 2e 0a 20 20 2a 2f 0a 20 20 6d 78 4c 65 6e 20 3d .. */. mxLen =
1130: 20 64 62 5f 69 6e 74 28 31 32 2c 20 22 53 45 4c db_int(12, "SEL
1140: 45 43 54 20 6d 61 78 28 6c 65 6e 67 74 68 28 78 ECT max(length(x
1150: 29 29 20 46 52 4f 4d 20 6c 6f 63 61 6c 66 69 6c )) FROM localfil
1160: 65 73 22 29 3b 0a 20 20 63 6e 74 20 3d 20 64 62 es");. cnt = db
1170: 5f 69 6e 74 28 30 2c 20 22 53 45 4c 45 43 54 20 _int(0, "SELECT
1180: 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 6c 6f count(*) FROM lo
1190: 63 61 6c 66 69 6c 65 73 22 29 3b 0a 20 20 6e 43 calfiles");. nC
11a0: 6f 6c 20 3d 20 34 3b 0a 20 20 6e 52 6f 77 20 3d ol = 4;. nRow =
11b0: 20 28 63 6e 74 2b 6e 43 6f 6c 2d 31 29 2f 6e 43 (cnt+nCol-1)/nC
11c0: 6f 6c 3b 0a 20 20 64 62 5f 70 72 65 70 61 72 65 ol;. db_prepare
11d0: 28 26 71 2c 20 22 53 45 4c 45 43 54 20 78 20 46 (&q, "SELECT x F
11e0: 52 4f 4d 20 6c 6f 63 61 6c 66 69 6c 65 73 20 4f ROM localfiles O
11f0: 52 44 45 52 20 42 59 20 78 22 29 3b 0a 20 20 40 RDER BY x");. @
1200: 20 3c 74 61 62 6c 65 20 62 6f 72 64 65 72 3d 22 <table border="
1210: 30 22 20 77 69 64 74 68 3d 22 31 30 30 25 25 22 0" width="100%%"
1220: 3e 3c 74 72 3e 3c 74 64 20 76 61 6c 69 67 6e 3d ><tr><td valign=
1230: 22 74 6f 70 22 20 77 69 64 74 68 3d 22 32 35 25 "top" width="25%
1240: 25 22 3e 0a 20 20 69 20 3d 20 30 3b 0a 20 20 77 %">. i = 0;. w
1250: 68 69 6c 65 28 20 64 62 5f 73 74 65 70 28 26 71 hile( db_step(&q
1260: 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b )==SQLITE_ROW ){
1270: 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 . const char
1280: 2a 7a 46 4e 61 6d 65 3b 0a 20 20 20 20 69 66 28 *zFName;. if(
1290: 20 69 3d 3d 6e 52 6f 77 20 29 7b 0a 20 20 20 20 i==nRow ){.
12a0: 20 20 40 20 3c 2f 74 64 3e 3c 74 64 20 76 61 6c @ </td><td val
12b0: 69 67 6e 3d 22 74 6f 70 22 20 77 69 64 74 68 3d ign="top" width=
12c0: 22 32 35 25 25 22 3e 0a 20 20 20 20 20 20 69 20 "25%%">. i
12d0: 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 = 0;. }. i
12e0: 2b 2b 3b 0a 20 20 20 20 7a 46 4e 61 6d 65 20 3d ++;. zFName =
12f0: 20 64 62 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 db_column_text(
1300: 26 71 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 &q, 0);. if(
1310: 7a 46 4e 61 6d 65 5b 30 5d 3d 3d 27 2f 27 20 29 zFName[0]=='/' )
1320: 7b 0a 20 20 20 20 20 20 7a 46 4e 61 6d 65 2b 2b {. zFName++
1330: 3b 0a 20 20 20 20 20 20 40 20 3c 6c 69 3e 3c 61 ;. @ <li><a
1340: 20 68 72 65 66 3d 22 25 73 28 67 2e 7a 42 61 73 href="%s(g.zBas
1350: 65 55 52 4c 29 2f 64 69 72 3f 64 3d 25 54 28 7a eURL)/dir?d=%T(z
1360: 50 72 65 66 69 78 29 25 54 28 7a 46 4e 61 6d 65 Prefix)%T(zFName
1370: 29 22 3e 0a 20 20 20 20 20 20 40 20 20 20 20 20 )">. @
1380: 25 68 28 7a 46 4e 61 6d 65 29 2f 3c 2f 61 3e 3c %h(zFName)/</a><
1390: 2f 6c 69 3e 0a 20 20 20 20 7d 65 6c 73 65 7b 0a /li>. }else{.
13a0: 20 20 20 20 20 20 40 20 3c 6c 69 3e 3c 61 20 68 @ <li><a h
13b0: 72 65 66 3d 22 25 73 28 67 2e 7a 42 61 73 65 55 ref="%s(g.zBaseU
13c0: 52 4c 29 2f 66 69 6e 66 6f 3f 6e 61 6d 65 3d 25 RL)/finfo?name=%
13d0: 54 28 7a 50 72 65 66 69 78 29 25 54 28 7a 46 4e T(zPrefix)%T(zFN
13e0: 61 6d 65 29 22 3e 0a 20 20 20 20 20 20 40 20 20 ame)">. @
13f0: 20 20 20 25 68 28 7a 46 4e 61 6d 65 29 3c 2f 61 %h(zFName)</a
1400: 3e 3c 2f 6c 69 3e 0a 20 20 20 20 7d 0a 20 20 7d ></li>. }. }
1410: 0a 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 . db_finalize(&
1420: 71 29 3b 0a 20 20 40 20 3c 2f 74 64 3e 3c 2f 74 q);. @ </td></t
1430: 72 3e 3c 2f 74 61 62 6c 65 3e 0a 20 20 73 74 79 r></table>. sty
1440: 6c 65 5f 66 6f 6f 74 65 72 28 29 3b 0a 7d 0a le_footer();.}.