** Implementation of .ar "create", "insert", and "update" commands. ** ** create -> Create a new SQL archive ** insert -> Insert or reinsert all files listed ** update -> Insert files that have changed or that were not ** previously in the archive ** ** Create the "sqlar" table in the database if it does not already exist. ** Then add each file
| 16643 | ** "update" only overwrites if the size or mtime or mode has changed. |
| 16644 | */ |
| 16645 | static int arCreateOrUpdateCommand( |
| 16646 | ArCommand *pAr, /* Command arguments and options */ |
| 16647 | int bUpdate, /* true for a --create. */ |
| 16648 | int bOnlyIfChanged /* Only update if file has changed */ |
| 16649 | ){ |
| 16650 | const char *zCreate = |
| 16651 | "CREATE TABLE IF NOT EXISTS sqlar(\n" |
| 16652 | " name TEXT PRIMARY KEY, -- name of the file\n" |
| 16653 | " mode INT, -- access permissions\n" |
| 16654 | " mtime INT, -- last modification time\n" |
| 16655 | " sz INT, -- original file size\n" |
| 16656 | " data BLOB -- compressed content\n" |
| 16657 | ")"; |
| 16658 | const char *zDrop = "DROP TABLE IF EXISTS sqlar"; |
| 16659 | const char *zInsertFmt[2] = { |
| 16660 | "REPLACE INTO %s(name,mode,mtime,sz,data)\n" |
| 16661 | " SELECT\n" |
| 16662 | " %s,\n" |
| 16663 | " mode,\n" |
| 16664 | " mtime,\n" |
| 16665 | " CASE substr(lsmode(mode),1,1)\n" |
| 16666 | " WHEN '-' THEN length(data)\n" |
| 16667 | " WHEN 'd' THEN 0\n" |
| 16668 | " ELSE -1 END,\n" |
| 16669 | " sqlar_compress(data)\n" |
| 16670 | " FROM fsdir(%Q,%Q) AS disk\n" |
| 16671 | " WHERE lsmode(mode) NOT LIKE '?%%'%s;" |
| 16672 | , |
| 16673 | "REPLACE INTO %s(name,mode,mtime,data)\n" |
| 16674 | " SELECT\n" |
| 16675 | " %s,\n" |
| 16676 | " mode,\n" |
| 16677 | " mtime,\n" |
| 16678 | " data\n" |
| 16679 | " FROM fsdir(%Q,%Q) AS disk\n" |
| 16680 | " WHERE lsmode(mode) NOT LIKE '?%%'%s;" |
| 16681 | }; |
| 16682 | int i; /* For iterating through azFile[] */ |
| 16683 | int rc; /* Return code */ |
| 16684 | const char *zTab = 0; /* SQL table into which to insert */ |
| 16685 | char *zSql; |
| 16686 | char zTemp[50]; |
| 16687 | char *zExists = 0; |
| 16688 | |
| 16689 | arExecSql(pAr, "PRAGMA page_size=512"); |
| 16690 | rc = arExecSql(pAr, "SAVEPOINT ar;"); |
| 16691 | if( rc!=SQLITE_OK ) return rc; |
| 16692 | zTemp[0] = 0; |
| 16693 | if( pAr->bZip ){ |
| 16694 | /* Initialize the zipfile virtual table, if necessary */ |
| 16695 | if( pAr->zFile ){ |
| 16696 | sqlite3_uint64 r; |
| 16697 | sqlite3_randomness(sizeof(r),&r); |
| 16698 | sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r); |
| 16699 | zTab = zTemp; |
| 16700 | zSql = sqlite3_mprintf( |
| 16701 | "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)", |
| 16702 | zTab, pAr->zFile |
no test coverage detected