Debian Codegraph @

ext/misc/fileio.c

raw
1 /*
2 ** 2014-06-13
3 **
4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
6 **
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
10 **
11 ******************************************************************************
12 **
13 ** This SQLite extension implements SQL functions readfile() and
14 ** writefile(), and eponymous virtual type "fsdir".
15 **
16 ** WRITEFILE(FILE, DATA [, MODE [, MTIME]]):
17 **
18 ** If neither of the optional arguments is present, then this UDF
19 ** function writes blob DATA to file FILE. If successful, the number
20 ** of bytes written is returned. If an error occurs, NULL is returned.
21 **
22 ** If the first option argument - MODE - is present, then it must
23 ** be passed an integer value that corresponds to a POSIX mode
24 ** value (file type + permissions, as returned in the stat.st_mode
25 ** field by the stat() system call). Three types of files may
26 ** be written/created:
27 **
28 ** regular files: (mode & 0170000)==0100000
29 ** symbolic links: (mode & 0170000)==0120000
30 ** directories: (mode & 0170000)==0040000
31 **
32 ** For a directory, the DATA is ignored. For a symbolic link, it is
33 ** interpreted as text and used as the target of the link. For a
34 ** regular file, it is interpreted as a blob and written into the
35 ** named file. Regardless of the type of file, its permissions are
36 ** set to (mode & 0777) before returning.
37 **
38 ** If the optional MTIME argument is present, then it is interpreted
39 ** as an integer - the number of seconds since the unix epoch. The
40 ** modification-time of the target file is set to this value before
41 ** returning.
42 **
43 ** If five or more arguments are passed to this function and an
44 ** error is encountered, an exception is raised.
45 **
46 ** READFILE(FILE):
47 **
48 ** Read and return the contents of file FILE (type blob) from disk.
49 **
50 ** FSDIR:
51 **
52 ** Used as follows:
53 **
54 ** SELECT * FROM fsdir($path [, $dir]);
55 **
56 ** Parameter $path is an absolute or relative pathname. If the file that it
57 ** refers to does not exist, it is an error. If the path refers to a regular
58 ** file or symbolic link, it returns a single row. Or, if the path refers
59 ** to a directory, it returns one row for the directory, and one row for each
60 ** file within the hierarchy rooted at $path.
61 **
62 ** Each row has the following columns:
63 **
64 ** name: Path to file or directory (text value).
65 ** mode: Value of stat.st_mode for directory entry (an integer).
66 ** mtime: Value of stat.st_mtime for directory entry (an integer).
67 ** data: For a regular file, a blob containing the file data. For a
68 ** symlink, a text value containing the text of the link. For a
69 ** directory, NULL.
70 ** level: Directory hierarchy level. Topmost is 1.
71 **
72 ** If a non-NULL value is specified for the optional $dir parameter and
73 ** $path is a relative path, then $path is interpreted relative to $dir.
74 ** And the paths returned in the "name" column of the table are also
75 ** relative to directory $dir.
76 */
77 #include "sqlite3ext.h"
78 SQLITE_EXTENSION_INIT1
79 #include <stdio.h>
80 #include <string.h>
81 #include <assert.h>
82
83 #include <sys/types.h>
84 #include <sys/stat.h>
85 #include <fcntl.h>
86 #if !defined(_WIN32) && !defined(WIN32)
87 # include <unistd.h>
88 # include <dirent.h>
89 # include <utime.h>
90 # include <sys/time.h>
91 # define STRUCT_STAT struct stat
92 # include <limits.h>
93 # include <stdlib.h>
94 #else
95 # include "windirent.h"
96 # include <direct.h>
97 # define STRUCT_STAT struct _stat
98 # define chmod(path,mode) fileio_chmod(path,mode)
99 # define mkdir(path,mode) fileio_mkdir(path)
100 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
101 extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
102 #endif
103 #include <time.h>
104 #include <errno.h>
105
106 /* When used as part of the CLI, the sqlite3_stdio.h module will have
107 ** been included before this one. In that case use the sqlite3_stdio.h
108 ** #defines. If not, create our own for fopen().
109 */
110 #ifndef _SQLITE3_STDIO_H_
111 # define sqlite3_fopen fopen
112 #endif
113
114 /*
115 ** Structure of the fsdir() table-valued function
116 */
117 /* 0 1 2 3 4 5 6 */
118 #define FSDIR_SCHEMA "(name,mode,mtime,data,level,path HIDDEN,dir HIDDEN)"
119
120 #define FSDIR_COLUMN_NAME 0 /* Name of the file */
121 #define FSDIR_COLUMN_MODE 1 /* Access mode */
122 #define FSDIR_COLUMN_MTIME 2 /* Last modification time */
123 #define FSDIR_COLUMN_DATA 3 /* File content */
124 #define FSDIR_COLUMN_LEVEL 4 /* Level. Topmost is 1 */
125 #define FSDIR_COLUMN_PATH 5 /* Path to top of search */
126 #define FSDIR_COLUMN_DIR 6 /* Path is relative to this directory */
127
128 /*
129 ** UTF8 chmod() function for Windows
130 */
131 #if defined(_WIN32) || defined(WIN32)
132 static int fileio_chmod(const char *zPath, int pmode){
133 int rc;
134 wchar_t *b1 = sqlite3_win32_utf8_to_unicode(zPath);
135 if( b1==0 ) return -1;
136 rc = _wchmod(b1, pmode);
137 sqlite3_free(b1);
138 return rc;
139 }
140 #endif
141
142 /*
143 ** UTF8 mkdir() function for Windows
144 */
145 #if defined(_WIN32) || defined(WIN32)
146 static int fileio_mkdir(const char *zPath){
147 int rc;
148 wchar_t *b1 = sqlite3_win32_utf8_to_unicode(zPath);
149 if( b1==0 ) return -1;
150 rc = _wmkdir(b1);
151 sqlite3_free(b1);
152 return rc;
153 }
154 #endif
155
156
157 /*
158 ** Set the result stored by context ctx to a blob containing the
159 ** contents of file zName. Or, leave the result unchanged (NULL)
160 ** if the file does not exist or is unreadable.
161 **
162 ** If the file exceeds the SQLite blob size limit, through an
163 ** SQLITE_TOOBIG error.
164 **
165 ** Throw an SQLITE_IOERR if there are difficulties pulling the file
166 ** off of disk.
167 */
168 static void readFileContents(sqlite3_context *ctx, const char *zName){
169 FILE *in;
170 sqlite3_int64 nIn;
171 void *pBuf;
172 sqlite3 *db;
173 int mxBlob;
174
175 in = sqlite3_fopen(zName, "rb");
176 if( in==0 ){
177 /* File does not exist or is unreadable. Leave the result set to NULL. */
178 return;
179 }
180 fseek(in, 0, SEEK_END);
181 nIn = ftell(in);
182 rewind(in);
183 db = sqlite3_context_db_handle(ctx);
184 mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1);
185 if( nIn>mxBlob ){
186 sqlite3_result_error_code(ctx, SQLITE_TOOBIG);
187 fclose(in);
188 return;
189 }
190 pBuf = sqlite3_malloc64( nIn ? nIn : 1 );
191 if( pBuf==0 ){
192 sqlite3_result_error_nomem(ctx);
193 fclose(in);
194 return;
195 }
196 if( nIn==(sqlite3_int64)fread(pBuf, 1, (size_t)nIn, in) ){
197 sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free);
198 }else{
199 sqlite3_result_error_code(ctx, SQLITE_IOERR);
200 sqlite3_free(pBuf);
201 }
202 fclose(in);
203 }
204
205 /*
206 ** Implementation of the "readfile(X)" SQL function. The entire content
207 ** of the file named X is read and returned as a BLOB. NULL is returned
208 ** if the file does not exist or is unreadable.
209 */
210 static void readfileFunc(
211 sqlite3_context *context,
212 int argc,
213 sqlite3_value **argv
214 ){
215 const char *zName;
216 (void)(argc); /* Unused parameter */
217 zName = (const char*)sqlite3_value_text(argv[0]);
218 if( zName==0 ) return;
219 readFileContents(context, zName);
220 }
221
222 /*
223 ** Set the error message contained in context ctx to the results of
224 ** vprintf(zFmt, ...).
225 */
226 static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
227 char *zMsg = 0;
228 va_list ap;
229 va_start(ap, zFmt);
230 zMsg = sqlite3_vmprintf(zFmt, ap);
231 sqlite3_result_error(ctx, zMsg, -1);
232 sqlite3_free(zMsg);
233 va_end(ap);
234 }
235
236 #if defined(_WIN32)
237 /*
238 ** This function is designed to convert a Win32 FILETIME structure into the
239 ** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC).
240 */
241 static sqlite3_uint64 fileTimeToUnixTime(
242 LPFILETIME pFileTime
243 ){
244 SYSTEMTIME epochSystemTime;
245 ULARGE_INTEGER epochIntervals;
246 FILETIME epochFileTime;
247 ULARGE_INTEGER fileIntervals;
248
249 memset(&epochSystemTime, 0, sizeof(SYSTEMTIME));
250 epochSystemTime.wYear = 1970;
251 epochSystemTime.wMonth = 1;
252 epochSystemTime.wDay = 1;
253 SystemTimeToFileTime(&epochSystemTime, &epochFileTime);
254 epochIntervals.LowPart = epochFileTime.dwLowDateTime;
255 epochIntervals.HighPart = epochFileTime.dwHighDateTime;
256
257 fileIntervals.LowPart = pFileTime->dwLowDateTime;
258 fileIntervals.HighPart = pFileTime->dwHighDateTime;
259
260 return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000;
261 }
262 #endif /* _WIN32 */
263
264 /*
265 ** This function is used in place of stat(). On Windows, special handling
266 ** is required in order for the included time to be returned as UTC. On all
267 ** other systems, this function simply calls stat().
268 */
269 static int fileStat(
270 const char *zPath,
271 STRUCT_STAT *pStatBuf
272 ){
273 #if defined(_WIN32)
274 int rc;
275 wchar_t *b1 = sqlite3_win32_utf8_to_unicode(zPath);
276 if( b1==0 ) return 1;
277 rc = _wstat(b1, pStatBuf);
278 if( rc==0 ){
279 HANDLE hFindFile;
280 WIN32_FIND_DATAW fd;
281 memset(&fd, 0, sizeof(WIN32_FIND_DATAW));
282 hFindFile = FindFirstFileW(b1, &fd);
283 if( hFindFile!=NULL ){
284 pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime);
285 pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime);
286 pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime);
287 FindClose(hFindFile);
288 }
289 }
290 sqlite3_free(b1);
291 return rc;
292 #else
293 return stat(zPath, pStatBuf);
294 #endif
295 }
296
297 /*
298 ** This function is used in place of lstat(). On Windows, special handling
299 ** is required in order for the included time to be returned as UTC. On all
300 ** other systems, this function simply calls lstat().
301 */
302 static int fileLinkStat(
303 const char *zPath,
304 STRUCT_STAT *pStatBuf
305 ){
306 #if defined(_WIN32)
307 return fileStat(zPath, pStatBuf);
308 #else
309 return lstat(zPath, pStatBuf);
310 #endif
311 }
312
313 /*
314 ** Argument zFile is the name of a file that will be created and/or written
315 ** by SQL function writefile(). This function ensures that the directory
316 ** zFile will be written to exists, creating it if required. The permissions
317 ** for any path components created by this function are set in accordance
318 ** with the current umask.
319 **
320 ** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
321 ** SQLITE_OK is returned if the directory is successfully created, or
322 ** SQLITE_ERROR otherwise.
323 */
324 static int makeDirectory(
325 const char *zFile
326 ){
327 char *zCopy = sqlite3_mprintf("%s", zFile);
328 int rc = SQLITE_OK;
329
330 if( zCopy==0 ){
331 rc = SQLITE_NOMEM;
332 }else{
333 int nCopy = (int)strlen(zCopy);
334 int i = 1;
335
336 while( rc==SQLITE_OK ){
337 STRUCT_STAT sStat;
338 int rc2;
339
340 for(; zCopy[i]!='/' && i<nCopy; i++);
341 if( i==nCopy ) break;
342 zCopy[i] = '\0';
343
344 rc2 = fileStat(zCopy, &sStat);
345 if( rc2!=0 ){
346 if( mkdir(zCopy, 0777) ) rc = SQLITE_ERROR;
347 }else{
348 if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
349 }
350 zCopy[i] = '/';
351 i++;
352 }
353
354 sqlite3_free(zCopy);
355 }
356
357 return rc;
358 }
359
360 /*
361 ** This function does the work for the writefile() UDF. Refer to
362 ** header comments at the top of this file for details.
363 */
364 static int writeFile(
365 sqlite3_context *pCtx, /* Context to return bytes written in */
366 const char *zFile, /* File to write */
367 sqlite3_value *pData, /* Data to write */
368 mode_t mode, /* MODE parameter passed to writefile() */
369 sqlite3_int64 mtime /* MTIME parameter (or -1 to not set time) */
370 ){
371 if( zFile==0 ) return 1;
372 #if !defined(_WIN32) && !defined(WIN32)
373 if( S_ISLNK(mode) ){
374 const char *zTo = (const char*)sqlite3_value_text(pData);
375 if( zTo==0 ) return 1;
376 unlink(zFile);
377 if( symlink(zTo, zFile)<0 ) return 1;
378 }else
379 #endif
380 {
381 if( S_ISDIR(mode) ){
382 if( mkdir(zFile, mode) ){
383 /* The mkdir() call to create the directory failed. This might not
384 ** be an error though - if there is already a directory at the same
385 ** path and either the permissions already match or can be changed
386 ** to do so using chmod(), it is not an error. */
387 STRUCT_STAT sStat;
388 if( errno!=EEXIST
389 || 0!=fileStat(zFile, &sStat)
390 || !S_ISDIR(sStat.st_mode)
391 || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777))
392 ){
393 return 1;
394 }
395 }
396 }else{
397 sqlite3_int64 nWrite = 0;
398 const char *z;
399 int rc = 0;
400 FILE *out = sqlite3_fopen(zFile, "wb");
401 if( out==0 ) return 1;
402 z = (const char*)sqlite3_value_blob(pData);
403 if( z ){
404 sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
405 nWrite = sqlite3_value_bytes(pData);
406 if( nWrite!=n ){
407 rc = 1;
408 }
409 }
410 fclose(out);
411 if( rc==0 && mode && chmod(zFile, mode & 0777) ){
412 rc = 1;
413 }
414 if( rc ) return 2;
415 sqlite3_result_int64(pCtx, nWrite);
416 }
417 }
418
419 if( mtime>=0 ){
420 #if defined(_WIN32)
421 /* Windows */
422 FILETIME lastAccess;
423 FILETIME lastWrite;
424 SYSTEMTIME currentTime;
425 LONGLONG intervals;
426 HANDLE hFile;
427 LPWSTR zUnicodeName;
428 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
429
430 GetSystemTime(&currentTime);
431 SystemTimeToFileTime(&currentTime, &lastAccess);
432 intervals = (mtime*10000000) + 116444736000000000;
433 lastWrite.dwLowDateTime = (DWORD)intervals;
434 lastWrite.dwHighDateTime = intervals >> 32;
435 zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile);
436 if( zUnicodeName==0 ){
437 return 1;
438 }
439 hFile = CreateFileW(
440 zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
441 FILE_FLAG_BACKUP_SEMANTICS, NULL
442 );
443 sqlite3_free(zUnicodeName);
444 if( hFile!=INVALID_HANDLE_VALUE ){
445 BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
446 CloseHandle(hFile);
447 return !bResult;
448 }else{
449 return 1;
450 }
451 #elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */
452 /* Recent unix */
453 struct timespec times[2];
454 times[0].tv_nsec = times[1].tv_nsec = 0;
455 times[0].tv_sec = time(0);
456 times[1].tv_sec = mtime;
457 if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){
458 return 1;
459 }
460 #else
461 /* Legacy unix.
462 **
463 ** Do not use utimes() on a symbolic link - it sees through the link and
464 ** modifies the timestamps on the target. Or fails if the target does
465 ** not exist. */
466 if( 0==S_ISLNK(mode) ){
467 struct timeval times[2];
468 times[0].tv_usec = times[1].tv_usec = 0;
469 times[0].tv_sec = time(0);
470 times[1].tv_sec = mtime;
471 if( utimes(zFile, times) ){
472 return 1;
473 }
474 }
475 #endif
476 }
477
478 return 0;
479 }
480
481 /*
482 ** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function.
483 ** Refer to header comments at the top of this file for details.
484 */
485 static void writefileFunc(
486 sqlite3_context *context,
487 int argc,
488 sqlite3_value **argv
489 ){
490 const char *zFile;
491 mode_t mode = 0;
492 int res;
493 sqlite3_int64 mtime = -1;
494
495 if( argc<2 || argc>4 ){
496 sqlite3_result_error(context,
497 "wrong number of arguments to function writefile()", -1
498 );
499 return;
500 }
501
502 zFile = (const char*)sqlite3_value_text(argv[0]);
503 if( zFile==0 ) return;
504 if( argc>=3 ){
505 mode = (mode_t)sqlite3_value_int(argv[2]);
506 }
507 if( argc==4 ){
508 mtime = sqlite3_value_int64(argv[3]);
509 }
510
511 res = writeFile(context, zFile, argv[1], mode, mtime);
512 if( res==1 && errno==ENOENT ){
513 if( makeDirectory(zFile)==SQLITE_OK ){
514 res = writeFile(context, zFile, argv[1], mode, mtime);
515 }
516 }
517
518 if( argc>2 && res!=0 ){
519 if( S_ISLNK(mode) ){
520 ctxErrorMsg(context, "failed to create symlink: %s", zFile);
521 }else if( S_ISDIR(mode) ){
522 ctxErrorMsg(context, "failed to create directory: %s", zFile);
523 }else{
524 ctxErrorMsg(context, "failed to write file: %s", zFile);
525 }
526 }
527 }
528
529 /*
530 ** SQL function: lsmode(MODE)
531 **
532 ** Given a numberic st_mode from stat(), convert it into a human-readable
533 ** text string in the style of "ls -l".
534 */
535 static void lsModeFunc(
536 sqlite3_context *context,
537 int argc,
538 sqlite3_value **argv
539 ){
540 int i;
541 int iMode = sqlite3_value_int(argv[0]);
542 char z[16];
543 (void)argc;
544 if( S_ISLNK(iMode) ){
545 z[0] = 'l';
546 }else if( S_ISREG(iMode) ){
547 z[0] = '-';
548 }else if( S_ISDIR(iMode) ){
549 z[0] = 'd';
550 }else{
551 z[0] = '?';
552 }
553 for(i=0; i<3; i++){
554 int m = (iMode >> ((2-i)*3));
555 char *a = &z[1 + i*3];
556 a[0] = (m & 0x4) ? 'r' : '-';
557 a[1] = (m & 0x2) ? 'w' : '-';
558 a[2] = (m & 0x1) ? 'x' : '-';
559 }
560 z[10] = '\0';
561 sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
562 }
563
564 #ifndef SQLITE_OMIT_VIRTUALTABLE
565
566 /*
567 ** Cursor type for recursively iterating through a directory structure.
568 */
569 typedef struct fsdir_cursor fsdir_cursor;
570 typedef struct FsdirLevel FsdirLevel;
571
572 struct FsdirLevel {
573 DIR *pDir; /* From opendir() */
574 char *zDir; /* Name of directory (nul-terminated) */
575 };
576
577 struct fsdir_cursor {
578 sqlite3_vtab_cursor base; /* Base class - must be first */
579
580 int nLvl; /* Number of entries in aLvl[] array */
581 int mxLvl; /* Maximum level */
582 int iLvl; /* Index of current entry */
583 FsdirLevel *aLvl; /* Hierarchy of directories being traversed */
584
585 const char *zBase;
586 int nBase;
587
588 STRUCT_STAT sStat; /* Current lstat() results */
589 char *zPath; /* Path to current entry */
590 sqlite3_int64 iRowid; /* Current rowid */
591 };
592
593 typedef struct fsdir_tab fsdir_tab;
594 struct fsdir_tab {
595 sqlite3_vtab base; /* Base class - must be first */
596 };
597
598 /*
599 ** Construct a new fsdir virtual table object.
600 */
601 static int fsdirConnect(
602 sqlite3 *db,
603 void *pAux,
604 int argc, const char *const*argv,
605 sqlite3_vtab **ppVtab,
606 char **pzErr
607 ){
608 fsdir_tab *pNew = 0;
609 int rc;
610 (void)pAux;
611 (void)argc;
612 (void)argv;
613 (void)pzErr;
614 rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA);
615 if( rc==SQLITE_OK ){
616 pNew = (fsdir_tab*)sqlite3_malloc64( sizeof(*pNew) );
617 if( pNew==0 ) return SQLITE_NOMEM;
618 memset(pNew, 0, sizeof(*pNew));
619 sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
620 }
621 *ppVtab = (sqlite3_vtab*)pNew;
622 return rc;
623 }
624
625 /*
626 ** This method is the destructor for fsdir vtab objects.
627 */
628 static int fsdirDisconnect(sqlite3_vtab *pVtab){
629 sqlite3_free(pVtab);
630 return SQLITE_OK;
631 }
632
633 /*
634 ** Constructor for a new fsdir_cursor object.
635 */
636 static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
637 fsdir_cursor *pCur;
638 (void)p;
639 pCur = sqlite3_malloc64( sizeof(*pCur) );
640 if( pCur==0 ) return SQLITE_NOMEM;
641 memset(pCur, 0, sizeof(*pCur));
642 pCur->iLvl = -1;
643 *ppCursor = &pCur->base;
644 return SQLITE_OK;
645 }
646
647 /*
648 ** Reset a cursor back to the state it was in when first returned
649 ** by fsdirOpen().
650 */
651 static void fsdirResetCursor(fsdir_cursor *pCur){
652 int i;
653 for(i=0; i<=pCur->iLvl; i++){
654 FsdirLevel *pLvl = &pCur->aLvl[i];
655 if( pLvl->pDir ) closedir(pLvl->pDir);
656 sqlite3_free(pLvl->zDir);
657 }
658 sqlite3_free(pCur->zPath);
659 sqlite3_free(pCur->aLvl);
660 pCur->aLvl = 0;
661 pCur->zPath = 0;
662 pCur->zBase = 0;
663 pCur->nBase = 0;
664 pCur->nLvl = 0;
665 pCur->iLvl = -1;
666 pCur->iRowid = 1;
667 }
668
669 /*
670 ** Destructor for an fsdir_cursor.
671 */
672 static int fsdirClose(sqlite3_vtab_cursor *cur){
673 fsdir_cursor *pCur = (fsdir_cursor*)cur;
674
675 fsdirResetCursor(pCur);
676 sqlite3_free(pCur);
677 return SQLITE_OK;
678 }
679
680 /*
681 ** Set the error message for the virtual table associated with cursor
682 ** pCur to the results of vprintf(zFmt, ...).
683 */
684 static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){
685 va_list ap;
686 va_start(ap, zFmt);
687 pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
688 va_end(ap);
689 }
690
691
692 /*
693 ** Advance an fsdir_cursor to its next row of output.
694 */
695 static int fsdirNext(sqlite3_vtab_cursor *cur){
696 fsdir_cursor *pCur = (fsdir_cursor*)cur;
697 mode_t m = pCur->sStat.st_mode;
698
699 pCur->iRowid++;
700 if( S_ISDIR(m) && pCur->iLvl+3<pCur->mxLvl ){
701 /* Descend into this directory */
702 int iNew = pCur->iLvl + 1;
703 FsdirLevel *pLvl;
704 if( iNew>=pCur->nLvl ){
705 int nNew = iNew+1;
706 sqlite3_int64 nByte = nNew*sizeof(FsdirLevel);
707 FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc64(pCur->aLvl, nByte);
708 if( aNew==0 ) return SQLITE_NOMEM;
709 memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl));
710 pCur->aLvl = aNew;
711 pCur->nLvl = nNew;
712 }
713 pCur->iLvl = iNew;
714 pLvl = &pCur->aLvl[iNew];
715
716 pLvl->zDir = pCur->zPath;
717 pCur->zPath = 0;
718 pLvl->pDir = opendir(pLvl->zDir);
719 if( pLvl->pDir==0 ){
720 fsdirSetErrmsg(pCur, "cannot read directory: %s", pLvl->zDir);
721 return SQLITE_ERROR;
722 }
723 }
724
725 while( pCur->iLvl>=0 ){
726 FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl];
727 struct dirent *pEntry = readdir(pLvl->pDir);
728 if( pEntry ){
729 if( pEntry->d_name[0]=='.' ){
730 if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue;
731 if( pEntry->d_name[1]=='\0' ) continue;
732 }
733 sqlite3_free(pCur->zPath);
734 pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name);
735 if( pCur->zPath==0 ) return SQLITE_NOMEM;
736 if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
737 fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
738 return SQLITE_ERROR;
739 }
740 return SQLITE_OK;
741 }
742 closedir(pLvl->pDir);
743 sqlite3_free(pLvl->zDir);
744 pLvl->pDir = 0;
745 pLvl->zDir = 0;
746 pCur->iLvl--;
747 }
748
749 /* EOF */
750 sqlite3_free(pCur->zPath);
751 pCur->zPath = 0;
752 return SQLITE_OK;
753 }
754
755 /*
756 ** Return values of columns for the row at which the series_cursor
757 ** is currently pointing.
758 */
759 static int fsdirColumn(
760 sqlite3_vtab_cursor *cur, /* The cursor */
761 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
762 int i /* Which column to return */
763 ){
764 fsdir_cursor *pCur = (fsdir_cursor*)cur;
765 switch( i ){
766 case FSDIR_COLUMN_NAME: {
767 sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT);
768 break;
769 }
770
771 case FSDIR_COLUMN_MODE:
772 sqlite3_result_int64(ctx, pCur->sStat.st_mode);
773 break;
774
775 case FSDIR_COLUMN_MTIME:
776 sqlite3_result_int64(ctx, pCur->sStat.st_mtime);
777 break;
778
779 case FSDIR_COLUMN_DATA: {
780 mode_t m = pCur->sStat.st_mode;
781 if( S_ISDIR(m) ){
782 sqlite3_result_null(ctx);
783 #if !defined(_WIN32) && !defined(WIN32)
784 }else if( S_ISLNK(m) ){
785 char aStatic[64];
786 char *aBuf = aStatic;
787 sqlite3_int64 nBuf = 64;
788 int n;
789
790 while( 1 ){
791 n = readlink(pCur->zPath, aBuf, nBuf);
792 if( n<nBuf ) break;
793 if( aBuf!=aStatic ) sqlite3_free(aBuf);
794 nBuf = nBuf*2;
795 aBuf = sqlite3_malloc64(nBuf);
796 if( aBuf==0 ){
797 sqlite3_result_error_nomem(ctx);
798 return SQLITE_NOMEM;
799 }
800 }
801
802 sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT);
803 if( aBuf!=aStatic ) sqlite3_free(aBuf);
804 #endif
805 }else{
806 readFileContents(ctx, pCur->zPath);
807 }
808 break;
809 }
810 case FSDIR_COLUMN_LEVEL:
811 sqlite3_result_int(ctx, pCur->iLvl+2);
812 break;
813 case FSDIR_COLUMN_PATH:
814 default: {
815 /* The FSDIR_COLUMN_PATH and FSDIR_COLUMN_DIR are input parameters.
816 ** always return their values as NULL */
817 break;
818 }
819 }
820 return SQLITE_OK;
821 }
822
823 /*
824 ** Return the rowid for the current row. In this implementation, the
825 ** first row returned is assigned rowid value 1, and each subsequent
826 ** row a value 1 more than that of the previous.
827 */
828 static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
829 fsdir_cursor *pCur = (fsdir_cursor*)cur;
830 *pRowid = pCur->iRowid;
831 return SQLITE_OK;
832 }
833
834 /*
835 ** Return TRUE if the cursor has been moved off of the last
836 ** row of output.
837 */
838 static int fsdirEof(sqlite3_vtab_cursor *cur){
839 fsdir_cursor *pCur = (fsdir_cursor*)cur;
840 return (pCur->zPath==0);
841 }
842
843 /*
844 ** xFilter callback.
845 **
846 ** idxNum bit Meaning
847 ** 0x01 PATH=N
848 ** 0x02 DIR=N
849 ** 0x04 LEVEL<N
850 ** 0x08 LEVEL<=N
851 */
852 static int fsdirFilter(
853 sqlite3_vtab_cursor *cur,
854 int idxNum, const char *idxStr,
855 int argc, sqlite3_value **argv
856 ){
857 const char *zDir = 0;
858 fsdir_cursor *pCur = (fsdir_cursor*)cur;
859 int i;
860 (void)idxStr;
861 fsdirResetCursor(pCur);
862
863 if( idxNum==0 ){
864 fsdirSetErrmsg(pCur, "table function fsdir requires an argument");
865 return SQLITE_ERROR;
866 }
867
868 assert( (idxNum & 0x01)!=0 && argc>0 );
869 zDir = (const char*)sqlite3_value_text(argv[0]);
870 if( zDir==0 ){
871 fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument");
872 return SQLITE_ERROR;
873 }
874 i = 1;
875 if( (idxNum & 0x02)!=0 ){
876 assert( argc>i );
877 pCur->zBase = (const char*)sqlite3_value_text(argv[i++]);
878 }
879 if( (idxNum & 0x0c)!=0 ){
880 assert( argc>i );
881 pCur->mxLvl = sqlite3_value_int(argv[i++]);
882 if( idxNum & 0x08 ) pCur->mxLvl++;
883 if( pCur->mxLvl<=0 ) pCur->mxLvl = 1000000000;
884 }else{
885 pCur->mxLvl = 1000000000;
886 }
887 if( pCur->zBase ){
888 pCur->nBase = (int)strlen(pCur->zBase)+1;
889 pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir);
890 }else{
891 pCur->zPath = sqlite3_mprintf("%s", zDir);
892 }
893
894 if( pCur->zPath==0 ){
895 return SQLITE_NOMEM;
896 }
897 if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
898 fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
899 return SQLITE_ERROR;
900 }
901
902 return SQLITE_OK;
903 }
904
905 /*
906 ** SQLite will invoke this method one or more times while planning a query
907 ** that uses the generate_series virtual table. This routine needs to create
908 ** a query plan for each invocation and compute an estimated cost for that
909 ** plan.
910 **
911 ** In this implementation idxNum is used to represent the
912 ** query plan. idxStr is unused.
913 **
914 ** The query plan is represented by bits in idxNum:
915 **
916 ** 0x01 The path value is supplied by argv[0]
917 ** 0x02 dir is in argv[1]
918 ** 0x04 maxdepth is in argv[1] or [2]
919 */
920 static int fsdirBestIndex(
921 sqlite3_vtab *tab,
922 sqlite3_index_info *pIdxInfo
923 ){
924 int i; /* Loop over constraints */
925 int idxPath = -1; /* Index in pIdxInfo->aConstraint of PATH= */
926 int idxDir = -1; /* Index in pIdxInfo->aConstraint of DIR= */
927 int idxLevel = -1; /* Index in pIdxInfo->aConstraint of LEVEL< or <= */
928 int idxLevelEQ = 0; /* 0x08 for LEVEL<= or LEVEL=. 0x04 for LEVEL< */
929 int omitLevel = 0; /* omit the LEVEL constraint */
930 int seenPath = 0; /* True if an unusable PATH= constraint is seen */
931 int seenDir = 0; /* True if an unusable DIR= constraint is seen */
932 const struct sqlite3_index_constraint *pConstraint;
933
934 (void)tab;
935 pConstraint = pIdxInfo->aConstraint;
936 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
937 if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
938 switch( pConstraint->iColumn ){
939 case FSDIR_COLUMN_PATH: {
940 if( pConstraint->usable ){
941 idxPath = i;
942 seenPath = 0;
943 }else if( idxPath<0 ){
944 seenPath = 1;
945 }
946 break;
947 }
948 case FSDIR_COLUMN_DIR: {
949 if( pConstraint->usable ){
950 idxDir = i;
951 seenDir = 0;
952 }else if( idxDir<0 ){
953 seenDir = 1;
954 }
955 break;
956 }
957 case FSDIR_COLUMN_LEVEL: {
958 if( pConstraint->usable && idxLevel<0 ){
959 idxLevel = i;
960 idxLevelEQ = 0x08;
961 omitLevel = 0;
962 }
963 break;
964 }
965 }
966 }else
967 if( pConstraint->iColumn==FSDIR_COLUMN_LEVEL
968 && pConstraint->usable
969 && idxLevel<0
970 ){
971 if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_LE ){
972 idxLevel = i;
973 idxLevelEQ = 0x08;
974 omitLevel = 1;
975 }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_LT ){
976 idxLevel = i;
977 idxLevelEQ = 0x04;
978 omitLevel = 1;
979 }
980 }
981 }
982 if( seenPath || seenDir ){
983 /* If input parameters are unusable, disallow this plan */
984 return SQLITE_CONSTRAINT;
985 }
986
987 if( idxPath<0 ){
988 pIdxInfo->idxNum = 0;
989 /* The pIdxInfo->estimatedCost should have been initialized to a huge
990 ** number. Leave it unchanged. */
991 pIdxInfo->estimatedRows = 0x7fffffff;
992 }else{
993 pIdxInfo->aConstraintUsage[idxPath].omit = 1;
994 pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1;
995 pIdxInfo->idxNum = 0x01;
996 pIdxInfo->estimatedCost = 1.0e9;
997 i = 2;
998 if( idxDir>=0 ){
999 pIdxInfo->aConstraintUsage[idxDir].omit = 1;
1000 pIdxInfo->aConstraintUsage[idxDir].argvIndex = i++;
1001 pIdxInfo->idxNum |= 0x02;
1002 pIdxInfo->estimatedCost /= 1.0e4;
1003 }
1004 if( idxLevel>=0 ){
1005 pIdxInfo->aConstraintUsage[idxLevel].omit = omitLevel;
1006 pIdxInfo->aConstraintUsage[idxLevel].argvIndex = i++;
1007 pIdxInfo->idxNum |= idxLevelEQ;
1008 pIdxInfo->estimatedCost /= 1.0e4;
1009 }
1010 }
1011
1012 return SQLITE_OK;
1013 }
1014
1015 /*
1016 ** Register the "fsdir" virtual table.
1017 */
1018 static int fsdirRegister(sqlite3 *db){
1019 static sqlite3_module fsdirModule = {
1020 0, /* iVersion */
1021 0, /* xCreate */
1022 fsdirConnect, /* xConnect */
1023 fsdirBestIndex, /* xBestIndex */
1024 fsdirDisconnect, /* xDisconnect */
1025 0, /* xDestroy */
1026 fsdirOpen, /* xOpen - open a cursor */
1027 fsdirClose, /* xClose - close a cursor */
1028 fsdirFilter, /* xFilter - configure scan constraints */
1029 fsdirNext, /* xNext - advance a cursor */
1030 fsdirEof, /* xEof - check for end of scan */
1031 fsdirColumn, /* xColumn - read data */
1032 fsdirRowid, /* xRowid - read data */
1033 0, /* xUpdate */
1034 0, /* xBegin */
1035 0, /* xSync */
1036 0, /* xCommit */
1037 0, /* xRollback */
1038 0, /* xFindMethod */
1039 0, /* xRename */
1040 0, /* xSavepoint */
1041 0, /* xRelease */
1042 0, /* xRollbackTo */
1043 0, /* xShadowName */
1044 0 /* xIntegrity */
1045 };
1046
1047 int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0);
1048 return rc;
1049 }
1050 #else /* SQLITE_OMIT_VIRTUALTABLE */
1051 # define fsdirRegister(x) SQLITE_OK
1052 #endif
1053
1054 /*
1055 ** This version of realpath() works on any system. The string
1056 ** returned is held in memory allocated using sqlite3_malloc64().
1057 ** The caller is responsible for calling sqlite3_free().
1058 */
1059 static char *portable_realpath(const char *zPath){
1060 #if !defined(_WIN32) /* BEGIN unix */
1061
1062 char *zOut = 0; /* Result */
1063 char *z; /* Temporary buffer */
1064 #if defined(PATH_MAX)
1065 char zBuf[PATH_MAX+1]; /* Space for the temporary buffer */
1066 #endif
1067
1068 if( zPath==0 ) return 0;
1069 #if defined(PATH_MAX)
1070 z = realpath(zPath, zBuf);
1071 if( z ){
1072 zOut = sqlite3_mprintf("%s", zBuf);
1073 }
1074 #endif /* defined(PATH_MAX) */
1075 if( zOut==0 ){
1076 /* Try POSIX.1-2008 malloc behavior */
1077 z = realpath(zPath, NULL);
1078 if( z ){
1079 zOut = sqlite3_mprintf("%s", z);
1080 free(z);
1081 }
1082 }
1083 return zOut;
1084
1085 #else /* End UNIX, Begin WINDOWS */
1086
1087 wchar_t *zPath16; /* UTF16 translation of zPath */
1088 char *zOut = 0; /* Result */
1089 wchar_t *z = 0; /* Temporary buffer */
1090
1091 if( zPath==0 ) return 0;
1092
1093 zPath16 = sqlite3_win32_utf8_to_unicode(zPath);
1094 if( zPath16==0 ) return 0;
1095 z = _wfullpath(NULL, zPath16, 0);
1096 sqlite3_free(zPath16);
1097 if( z ){
1098 zOut = sqlite3_win32_unicode_to_utf8(z);
1099 free(z);
1100 }
1101 return zOut;
1102
1103 #endif /* End WINDOWS, Begin common code */
1104 }
1105
1106 /*
1107 ** SQL function: realpath(X)
1108 **
1109 ** Try to convert file or pathname X into its real, absolute pathname.
1110 ** Return NULL if unable.
1111 **
1112 ** The file or directory X is not required to exist. The answer is formed
1113 ** by calling system realpath() on the prefix of X that does exist and
1114 ** appending the tail of X that does not (yet) exist.
1115 */
1116 static void realpathFunc(
1117 sqlite3_context *context,
1118 int argc,
1119 sqlite3_value **argv
1120 ){
1121 const char *zPath; /* Original input path */
1122 char *zCopy; /* An editable copy of zPath */
1123 char *zOut; /* The result */
1124 char cSep = 0; /* Separator turned into \000 */
1125 size_t len; /* Prefix length before cSep */
1126 #ifdef _WIN32
1127 const int isWin = 1;
1128 #else
1129 const int isWin = 0;
1130 #endif
1131
1132 (void)argc;
1133 zPath = (const char*)sqlite3_value_text(argv[0]);
1134 if( zPath==0 ) return;
1135 if( zPath[0]==0 ) zPath = ".";
1136 zCopy = sqlite3_mprintf("%s",zPath);
1137 len = strlen(zCopy);
1138 while( len>1 && (zCopy[len-1]=='/' || (isWin && zCopy[len-1]=='\\')) ){
1139 len--;
1140 }
1141 zCopy[len] = 0;
1142 while( 1 /*exit-by-break*/ ){
1143 zOut = portable_realpath(zCopy);
1144 zCopy[len] = cSep;
1145 if( zOut ){
1146 if( cSep ){
1147 zOut = sqlite3_mprintf("%z%s",zOut,&zCopy[len]);
1148 }
1149 break;
1150 }else{
1151 size_t i = len-1;
1152 while( i>0 ){
1153 if( zCopy[i]=='/' || (isWin && zCopy[i]=='\\') ) break;
1154 i--;
1155 }
1156 if( i<=0 ){
1157 if( zCopy[0]=='/' ){
1158 zOut = zCopy;
1159 zCopy = 0;
1160 }else if( (zOut = portable_realpath("."))!=0 ){
1161 zOut = sqlite3_mprintf("%z/%s", zOut, zCopy);
1162 }
1163 break;
1164 }
1165 cSep = zCopy[i];
1166 zCopy[i] = 0;
1167 len = i;
1168 }
1169 }
1170 sqlite3_free(zCopy);
1171 if( zOut ){
1172 /* Simplify any "/./" or "/../" that might have snuck into the
1173 ** pathname due to appending of zCopy. We only have to consider
1174 ** unix "/" separators, because the _wfilepath() system call on
1175 ** Windows will have already done this simplification for us. */
1176 size_t i, j, n;
1177 n = strlen(zOut);
1178 for(i=j=0; i<n; i++){
1179 if( zOut[i]=='/' ){
1180 if( zOut[i+1]=='/' ) continue;
1181 if( zOut[i+1]=='.' && i+2<n && zOut[i+2]=='/' ){
1182 i += 1;
1183 continue;
1184 }
1185 if( zOut[i+1]=='.' && i+3<n && zOut[i+2]=='.' && zOut[i+3]=='/' ){
1186 while( j>0 && zOut[j-1]!='/' ){ j--; }
1187 if( j>0 ){ j--; }
1188 i += 2;
1189 continue;
1190 }
1191 }
1192 zOut[j++] = zOut[i];
1193 }
1194 zOut[j] = 0;
1195
1196 /* Return the result */
1197 sqlite3_result_text(context, zOut, -1, sqlite3_free);
1198 }
1199 }
1200
1201
1202 #ifdef _WIN32
1203 __declspec(dllexport)
1204 #endif
1205 int sqlite3_fileio_init(
1206 sqlite3 *db,
1207 char **pzErrMsg,
1208 const sqlite3_api_routines *pApi
1209 ){
1210 int rc = SQLITE_OK;
1211 SQLITE_EXTENSION_INIT2(pApi);
1212 (void)pzErrMsg; /* Unused parameter */
1213 rc = sqlite3_create_function(db, "readfile", 1,
1214 SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
1215 readfileFunc, 0, 0);
1216 if( rc==SQLITE_OK ){
1217 rc = sqlite3_create_function(db, "writefile", -1,
1218 SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
1219 writefileFunc, 0, 0);
1220 }
1221 if( rc==SQLITE_OK ){
1222 rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0,
1223 lsModeFunc, 0, 0);
1224 }
1225 if( rc==SQLITE_OK ){
1226 rc = fsdirRegister(db);
1227 }
1228 if( rc==SQLITE_OK ){
1229 rc = sqlite3_create_function(db, "realpath", 1,
1230 SQLITE_UTF8, 0,
1231 realpathFunc, 0, 0);
1232 }
1233 return rc;
1234 }