/ Check-in [32c4fa25]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add support for the "--list" command. And for arguments to the "--extract" command.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | sqlar-shell-support
Files: files | file ages | folders
SHA3-256: 32c4fa2552bb0fa7d7d143108457efae7a756d6cb14b1d59312e56efac3b2656
User & Date: dan 2017-12-13 20:04:53
Context
2017-12-13
20:17
Add the shell tool ".ar --update" command. check-in: 825e3c03 user: dan tags: sqlar-shell-support
20:04
Add support for the "--list" command. And for arguments to the "--extract" command. check-in: 32c4fa25 user: dan tags: sqlar-shell-support
2017-12-12
20:28
Add tests and fixes for the shell ".ar" command -f option. check-in: 1a986797 user: dan tags: sqlar-shell-support
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/shell.c.in.

4087
4088
4089
4090
4091
4092
4093




















4094
4095
4096
4097
4098
4099
4100
....
4311
4312
4313
4314
4315
4316
4317
4318
4319









































































4320
4321
4322
4323












4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334



4335
4336
4337
4338
4339
4340
4341
4342

4343







4344
4345
4346
4347


4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
....
4373
4374
4375
4376
4377
4378
4379

4380
4381
4382
4383
4384
4385
4386
      raw_printf(stderr, "sql error: %s (%d)\n", 
          sqlite3_errmsg(db), sqlite3_errcode(db)
      );
      *pRc = rc;
    }
  }
}





















static void shellFinalize(
  int *pRc, 
  sqlite3_stmt *pStmt
){
  if( pStmt ){
    sqlite3 *db = sqlite3_db_handle(pStmt);
................................................................................
/*
** Implementation of .ar "Update" command. 
*/
static int arUpdateCmd(ShellState *p, sqlite3 *db, ArCommand *pAr){
  raw_printf(stderr, "todo...\n");
  return SQLITE_OK;
}

/*









































































** Implementation of .ar "lisT" command. 
*/
static int arListCommand(ShellState *p, sqlite3 *db, ArCommand *pAr){
  raw_printf(stderr, "todo...\n");












  return SQLITE_OK;
}


/*
** Implementation of .ar "eXtract" command. 
*/
static int arExtractCommand(ShellState *p, sqlite3 *db, ArCommand *pAr){
  const char *zSql1 = 
    "SELECT :1 || name, writefile(:1 || name, "
    "CASE WHEN (data AND sz>=0 AND sz!=length(data)) THEN uncompress(data) "



    "   ELSE data END, "
    "mode) FROM sqlar";
  const char *zSql2 = "SELECT :1 || name, mtime FROM sqlar"; 

  struct timespec times[2];
  sqlite3_stmt *pSql = 0;
  int rc = SQLITE_OK;
  char *zDir = 0;









  if( pAr->zDir ){
    zDir = sqlite3_mprintf("%s/", pAr->zDir);
  }else{
    zDir = sqlite3_mprintf("");


  }

  memset(times, 0, sizeof(times));
  times[0].tv_sec = time(0);

  shellPrepare(db, &rc, zSql1, &pSql);
  if( rc==SQLITE_OK ){
    sqlite3_bind_text(pSql, 1, zDir, -1, SQLITE_STATIC);
  }
  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
    if( pAr->bVerbose ){
      raw_printf(stdout, "%s\n", sqlite3_column_text(pSql, 0));
    }
  }
  shellFinalize(&rc, pSql);

  shellPrepare(db, &rc, zSql2, &pSql);
  if( rc==SQLITE_OK ){
    sqlite3_bind_text(pSql, 1, zDir, -1, SQLITE_STATIC);
  }
  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
    const char *zPath = (const char*)sqlite3_column_text(pSql, 0);
    times[1].tv_sec = (time_t)sqlite3_column_int64(pSql, 1);
    if( utimensat(AT_FDCWD, zPath, times, AT_SYMLINK_NOFOLLOW) ){
................................................................................
      rc = SQLITE_ERROR;
      break;
    }
  }
  shellFinalize(&rc, pSql);

  sqlite3_free(zDir);

  return rc;
}


/*
** Implementation of .ar "Create" command. 
**







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 









>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



<
>
>
>
>
>
>
>
>
>
>
>
>
|









|
>
>
>
|







>

>
>
>
>
>
>
>
|
|
|
|
>
>





|










|







 







>







4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
....
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415

4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
....
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
      raw_printf(stderr, "sql error: %s (%d)\n", 
          sqlite3_errmsg(db), sqlite3_errcode(db)
      );
      *pRc = rc;
    }
  }
}

static void shellPrepare2(
  sqlite3 *db, 
  int *pRc, 
  const char *zSql, 
  const char *zTail, 
  sqlite3_stmt **ppStmt
){
  if( *pRc==SQLITE_OK && zTail ){
    char *z = sqlite3_mprintf("%s %s", zSql, zTail);
    if( z==0 ){
      *pRc = SQLITE_NOMEM;
    }else{
      shellPrepare(db, pRc, z, ppStmt);
      sqlite3_free(z);
    }
  }else{
    shellPrepare(db, pRc, zSql, ppStmt);
  }
}

static void shellFinalize(
  int *pRc, 
  sqlite3_stmt *pStmt
){
  if( pStmt ){
    sqlite3 *db = sqlite3_db_handle(pStmt);
................................................................................
/*
** Implementation of .ar "Update" command. 
*/
static int arUpdateCmd(ShellState *p, sqlite3 *db, ArCommand *pAr){
  raw_printf(stderr, "todo...\n");
  return SQLITE_OK;
}

/*
** This function assumes that all arguments within the ArCommand.azArg[]
** array refer to archive members, as for the --extract or --list commands. 
** It checks that each of them are present. If any specified file is not
** present in the archive, an error is printed to stderr and an error
** code returned. Otherwise, if all specified arguments are present in
** the archive, SQLITE_OK is returned.
**
** This function strips any trailing '/' characters from each argument.
** This is consistent with the way the [tar] command seems to work on
** Linux.
*/
static int arCheckEntries(sqlite3 *db, ArCommand *pAr){
  int rc = SQLITE_OK;
  if( pAr->nArg ){
    int i;
    sqlite3_stmt *pTest = 0;

    shellPrepare(db, &rc, "SELECT name FROM sqlar WHERE name=?", &pTest);
    for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
      char *z = pAr->azArg[i];
      int n = strlen(z);
      int bOk = 0;
      while( n>0 && z[n-1]=='/' ) n--;
      z[n] = '\0';
      sqlite3_bind_text(pTest, 1, z, -1, SQLITE_STATIC);
      if( SQLITE_ROW==sqlite3_step(pTest) ){
        bOk = 1;
      }
      shellReset(&rc, pTest);
      if( rc==SQLITE_OK && bOk==0 ){
        raw_printf(stderr, "not found in archive: %s\n", z);
        rc = SQLITE_ERROR;
      }
    }
    shellFinalize(&rc, pTest);
  }

  return rc;
}

/*
** Format a WHERE clause that can be used against the "sqlar" table to
** identify all archive members that match the command arguments held
** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
** The caller is responsible for eventually calling sqlite3_free() on
** any non-NULL (*pzWhere) value.
*/
static void arWhereClause(
  int *pRc, 
  ArCommand *pAr, 
  char **pzWhere                  /* OUT: New WHERE clause (or NULL) */
){
  char *zWhere = 0;
  if( *pRc==SQLITE_OK ){
    int i;
    const char *zSep = "WHERE ";
    for(i=0; i<pAr->nArg; i++){
      const char *z = pAr->azArg[i];
      zWhere = sqlite3_mprintf(
          "%z%s name = '%q' OR name BETWEEN '%q/' AND '%q0'", 
          zWhere, zSep, z, z, z
      );
      if( zWhere==0 ){
        *pRc = SQLITE_NOMEM;
        break;
      }
      zSep = " OR ";
    }
  }
  *pzWhere = zWhere;
}

/*
** Implementation of .ar "lisT" command. 
*/
static int arListCommand(ShellState *p, sqlite3 *db, ArCommand *pAr){

  const char *zSql = "SELECT name FROM sqlar"; 
  char *zWhere = 0;
  sqlite3_stmt *pSql = 0;
  int rc;

  rc = arCheckEntries(db, pAr);
  arWhereClause(&rc, pAr, &zWhere);

  shellPrepare2(db, &rc, zSql, zWhere, &pSql);
  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
    raw_printf(p->out, "%s\n", sqlite3_column_text(pSql, 0));
  }
  return rc;
}


/*
** Implementation of .ar "eXtract" command. 
*/
static int arExtractCommand(ShellState *p, sqlite3 *db, ArCommand *pAr){
  const char *zSql1 = 
    "SELECT :1 || name, writefile(:1 || name, "
    "CASE WHEN (data AND sz>=0 AND sz!=length(data)) THEN "
    "    uncompress(data) "
    "ELSE"
    "    data "
    "END, "
    "mode) FROM sqlar";
  const char *zSql2 = "SELECT :1 || name, mtime FROM sqlar"; 

  struct timespec times[2];
  sqlite3_stmt *pSql = 0;
  int rc = SQLITE_OK;
  char *zDir = 0;
  char *zWhere = 0;

  /* If arguments are specified, check that they actually exist within
  ** the archive before proceeding. And formulate a WHERE clause to
  ** match them.  */
  rc = arCheckEntries(db, pAr);
  arWhereClause(&rc, pAr, &zWhere);

  if( rc==SQLITE_OK ){
    if( pAr->zDir ){
      zDir = sqlite3_mprintf("%s/", pAr->zDir);
    }else{
      zDir = sqlite3_mprintf("");
    }
    if( zDir==0 ) rc = SQLITE_NOMEM;
  }

  memset(times, 0, sizeof(times));
  times[0].tv_sec = time(0);

  shellPrepare2(db, &rc, zSql1, zWhere, &pSql);
  if( rc==SQLITE_OK ){
    sqlite3_bind_text(pSql, 1, zDir, -1, SQLITE_STATIC);
  }
  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
    if( pAr->bVerbose ){
      raw_printf(stdout, "%s\n", sqlite3_column_text(pSql, 0));
    }
  }
  shellFinalize(&rc, pSql);

  shellPrepare2(db, &rc, zSql2, zWhere, &pSql);
  if( rc==SQLITE_OK ){
    sqlite3_bind_text(pSql, 1, zDir, -1, SQLITE_STATIC);
  }
  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
    const char *zPath = (const char*)sqlite3_column_text(pSql, 0);
    times[1].tv_sec = (time_t)sqlite3_column_int64(pSql, 1);
    if( utimensat(AT_FDCWD, zPath, times, AT_SYMLINK_NOFOLLOW) ){
................................................................................
      rc = SQLITE_ERROR;
      break;
    }
  }
  shellFinalize(&rc, pSql);

  sqlite3_free(zDir);
  sqlite3_free(zWhere);
  return rc;
}


/*
** Implementation of .ar "Create" command. 
**