** 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
| 22450 | ** "update" only overwrites if the size or mtime or mode has changed. |
| 22451 | */ |
| 22452 | static int arCreateOrUpdateCommand( |
| 22453 | ArCommand *pAr, /* Command arguments and options */ |
| 22454 | int bUpdate, /* true for a --create. */ |
| 22455 | int bOnlyIfChanged /* Only update if file has changed */ |
| 22456 | ){ |
| 22457 | const char *zCreate = |
| 22458 | "CREATE TABLE IF NOT EXISTS sqlar(\n" |
| 22459 | " name TEXT PRIMARY KEY, -- name of the file\n" |
| 22460 | " mode INT, -- access permissions\n" |
| 22461 | " mtime INT, -- last modification time\n" |
| 22462 | " sz INT, -- original file size\n" |
| 22463 | " data BLOB -- compressed content\n" |
| 22464 | ")"; |
| 22465 | const char *zDrop = "DROP TABLE IF EXISTS sqlar"; |
| 22466 | const char *zInsertFmt[2] = { |
| 22467 | "REPLACE INTO %s(name,mode,mtime,sz,data)\n" |
| 22468 | " SELECT\n" |
| 22469 | " %s,\n" |
| 22470 | " mode,\n" |
| 22471 | " mtime,\n" |
| 22472 | " CASE substr(lsmode(mode),1,1)\n" |
| 22473 | " WHEN '-' THEN length(data)\n" |
| 22474 | " WHEN 'd' THEN 0\n" |
| 22475 | " ELSE -1 END,\n" |
| 22476 | " sqlar_compress(data)\n" |
| 22477 | " FROM fsdir(%Q,%Q) AS disk\n" |
| 22478 | " WHERE lsmode(mode) NOT LIKE '?%%'%s;" |
| 22479 | , |
| 22480 | "REPLACE INTO %s(name,mode,mtime,data)\n" |
| 22481 | " SELECT\n" |
| 22482 | " %s,\n" |
| 22483 | " mode,\n" |
| 22484 | " mtime,\n" |
| 22485 | " data\n" |
| 22486 | " FROM fsdir(%Q,%Q) AS disk\n" |
| 22487 | " WHERE lsmode(mode) NOT LIKE '?%%'%s;" |
| 22488 | }; |
| 22489 | int i; /* For iterating through azFile[] */ |
| 22490 | int rc; /* Return code */ |
| 22491 | const char *zTab = 0; /* SQL table into which to insert */ |
| 22492 | char *zSql; |
| 22493 | char zTemp[50]; |
| 22494 | char *zExists = 0; |
| 22495 | |
| 22496 | arExecSql(pAr, "PRAGMA page_size=512"); |
| 22497 | rc = arExecSql(pAr, "SAVEPOINT ar;"); |
| 22498 | if( rc!=SQLITE_OK ) return rc; |
| 22499 | zTemp[0] = 0; |
| 22500 | if( pAr->bZip ){ |
| 22501 | /* Initialize the zipfile virtual table, if necessary */ |
| 22502 | if( pAr->zFile ){ |
| 22503 | sqlite3_uint64 r; |
| 22504 | sqlite3_randomness(sizeof(r),&r); |
| 22505 | sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r); |
| 22506 | zTab = zTemp; |
| 22507 | zSql = sqlite3_mprintf( |
| 22508 | "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)", |
| 22509 | zTab, pAr->zFile |
no test coverage detected