1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-11-12 13:01:09 +03:00

Improve the shell tool ".ar --list --verbose" command.

FossilOrigin-Name: b64681a644c419bb98d00980a6cb56ef5a0aff5ef5321955631f0b4c88aac283
This commit is contained in:
dan
2017-12-27 21:13:21 +00:00
parent 5a78b81b1b
commit b5090e4897
4 changed files with 65 additions and 16 deletions

View File

@@ -4577,12 +4577,43 @@ static void arWhereClause(
*pzWhere = zWhere;
}
/*
** Argument zMode must point to a buffer at least 11 bytes in size. This
** function populates this buffer with the string interpretation of
** the unix file mode passed as the second argument (e.g. "drwxr-xr-x").
*/
static void shellModeToString(char *zMode, int mode){
int i;
/* Magic numbers copied from [man 2 stat] */
if( mode & 0040000 ){
zMode[0] = 'd';
}else if( (mode & 0120000)==0120000 ){
zMode[0] = 'l';
}else{
zMode[0] = '-';
}
for(i=0; i<3; i++){
int m = (mode >> ((2-i)*3));
char *a = &zMode[1 + i*3];
a[0] = (m & 0x4) ? 'r' : '-';
a[1] = (m & 0x2) ? 'w' : '-';
a[2] = (m & 0x1) ? 'x' : '-';
}
zMode[10] = '\0';
}
/*
** Implementation of .ar "lisT" command.
*/
static int arListCommand(ShellState *p, sqlite3 *db, ArCommand *pAr){
const char *zSql = "SELECT name FROM %s WHERE %s";
const char *zSql = "SELECT %s FROM %s WHERE %s";
const char *zTbl = (pAr->bZip ? "zipfile(?)" : "sqlar");
const char *azCols[] = {
"name",
"mode, sz, datetime(mtime, 'unixepoch'), name"
};
char *zWhere = 0;
sqlite3_stmt *pSql = 0;
@@ -4591,12 +4622,23 @@ static int arListCommand(ShellState *p, sqlite3 *db, ArCommand *pAr){
rc = arCheckEntries(db, pAr);
arWhereClause(&rc, pAr, &zWhere);
shellPreparePrintf(db, &rc, &pSql, zSql, zTbl, zWhere);
shellPreparePrintf(db, &rc, &pSql, zSql, azCols[pAr->bVerbose], zTbl, zWhere);
if( rc==SQLITE_OK && pAr->bZip ){
sqlite3_bind_text(pSql, 1, pAr->zFile, -1, SQLITE_TRANSIENT);
}
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
raw_printf(p->out, "%s\n", sqlite3_column_text(pSql, 0));
if( pAr->bVerbose ){
char zMode[11];
shellModeToString(zMode, sqlite3_column_int(pSql, 0));
raw_printf(p->out, "%s % 10d %s %s\n", zMode,
sqlite3_column_int(pSql, 1),
sqlite3_column_text(pSql, 2),
sqlite3_column_text(pSql, 3)
);
}else{
raw_printf(p->out, "%s\n", sqlite3_column_text(pSql, 0));
}
}
shellFinalize(&rc, pSql);