SQLite

Check-in [38dbeb1e77]
Login

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

Overview
Comment:Add support for parsing options in non-traditional tar form to the ".ar" command. Have writefile() attempt to create any missing path components. And not to throw an exception if it is called to create a directory that already exists.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | sqlar-shell-support
Files: files | file ages | folders
SHA3-256: 38dbeb1e777aa7ec742aa27002ad4dcee28af520dc43de96e5c56c39f16574ff
User & Date: dan 2017-12-12 20:04:59.331
Context
2017-12-12
20:28
Add tests and fixes for the shell ".ar" command -f option. (check-in: 1a9867973c user: dan tags: sqlar-shell-support)
20:04
Add support for parsing options in non-traditional tar form to the ".ar" command. Have writefile() attempt to create any missing path components. And not to throw an exception if it is called to create a directory that already exists. (check-in: 38dbeb1e77 user: dan tags: sqlar-shell-support)
2017-12-11
20:22
Enhance virtual table "fsdir" in ext/misc/fileio.c. Add support for "-C" to the shell command's ".ar c" command. (check-in: 0394889afe user: dan tags: sqlar-shell-support)
Changes
Unified Diff Ignore Whitespace Patch
Changes to ext/misc/fileio.c.
36
37
38
39
40
41
42

43
44
45
46
47
48
49
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <time.h>
#include <utime.h>



#define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)"

static void readFileContents(sqlite3_context *ctx, const char *zName){
  FILE *in;
  long nIn;







>







36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <time.h>
#include <utime.h>
#include <errno.h>


#define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)"

static void readFileContents(sqlite3_context *ctx, const char *zName){
  FILE *in;
  long nIn;
85
86
87
88
89
90
91

































































































92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
  va_list ap;
  va_start(ap, zFmt);
  zMsg = sqlite3_vmprintf(zFmt, ap);
  sqlite3_result_error(ctx, zMsg, -1);
  sqlite3_free(zMsg);
  va_end(ap);
}


































































































/*
** Implementation of the "writefile(W,X[,Y]])" SQL function.  
**
** The argument X is written into file W.  The number of bytes written is
** returned. Or NULL is returned if something goes wrong, such as being unable
** to open file X for writing.
*/
static void writefileFunc(
  sqlite3_context *context,
  int argc,
  sqlite3_value **argv
){
  const char *zFile;
  mode_t mode = 0;


  if( argc<2 || argc>3 ){
    sqlite3_result_error(context, 
        "wrong number of arguments to function writefile()", -1
    );
    return;
  }

  zFile = (const char*)sqlite3_value_text(argv[0]);
  if( zFile==0 ) return;
  if( argc>=3 ){
    sqlite3_result_int(context, 0);
    mode = sqlite3_value_int(argv[2]);
  }

  if( S_ISLNK(mode) ){
    const char *zTo = (const char*)sqlite3_value_text(argv[1]);
    if( symlink(zTo, zFile)<0 ){
      ctxErrorMsg(context, "failed to create symlink: %s", zFile);
      return;
    }
  }else{
    if( S_ISDIR(mode) ){
      if( mkdir(zFile, mode) ){
        ctxErrorMsg(context, "failed to create directory: %s", zFile);
        return;
      }
    }else{
      sqlite3_int64 nWrite = 0;
      const char *z;
      int rc = 0;
      FILE *out = fopen(zFile, "wb");
      if( out==0 ){
        if( argc>2 ){
          ctxErrorMsg(context, "failed to open file for writing: %s", zFile);
        }
        return;
      }
      z = (const char*)sqlite3_value_blob(argv[1]);
      if( z ){
        sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out);
        nWrite = sqlite3_value_bytes(argv[1]);
        if( nWrite!=n ){
          ctxErrorMsg(context, "failed to write file: %s", zFile);
          rc = 1;
        }
      }
      fclose(out);
      if( rc ) return;
      sqlite3_result_int64(context, nWrite);
    }

    if( argc>2 && chmod(zFile, mode & 0777) ){
      ctxErrorMsg(context, "failed to chmod file: %s", zFile);
      return;
    }
  }
}

#ifndef SQLITE_OMIT_VIRTUALTABLE

/* 







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















>















<
<
|
<
<
<
<
|
|
|
<
|
<
<
<
<
<
<
<
<
|
<
|
<
|
<
<
|
|
<
<
<
<
|
|
|
<
<
|
<







86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220


221




222
223
224

225








226

227

228


229
230




231
232
233


234

235
236
237
238
239
240
241
  va_list ap;
  va_start(ap, zFmt);
  zMsg = sqlite3_vmprintf(zFmt, ap);
  sqlite3_result_error(ctx, zMsg, -1);
  sqlite3_free(zMsg);
  va_end(ap);
}

/*
** Argument zFile is the name of a file that will be created and/or written
** by SQL function writefile(). This function ensures that the directory
** zFile will be written to exists, creating it if required. The permissions
** for any path components created by this function are set to (mode&0777).
**
** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
** SQLITE_OK is returned if the directory is successfully created, or
** SQLITE_ERROR otherwise.
*/
static int makeDirectory(
  const char *zFile,
  mode_t mode
){
  char *zCopy = sqlite3_mprintf("%s", zFile);
  int rc = SQLITE_OK;

  if( zCopy==0 ){
    rc = SQLITE_NOMEM;
  }else{
    int nCopy = strlen(zCopy);
    int i = 1;

    while( rc==SQLITE_OK ){
      struct stat sStat;
      int rc;

      for(; zCopy[i]!='/' && i<nCopy; i++);
      if( i==nCopy ) break;
      zCopy[i] = '\0';

      rc = stat(zCopy, &sStat);
      if( rc!=0 ){
        if( mkdir(zCopy, mode & 0777) ) rc = SQLITE_ERROR;
      }else{
        if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
      }
      zCopy[i] = '/';
      i++;
    }

    sqlite3_free(zCopy);
  }

  return rc;
}

static int writeFile(
  sqlite3_context *pCtx, 
  const char *zFile, 
  mode_t mode, 
  sqlite3_value *pData
){
  if( S_ISLNK(mode) ){
    const char *zTo = (const char*)sqlite3_value_text(pData);
    if( symlink(zTo, zFile)<0 ) return 1;
  }else{
    if( S_ISDIR(mode) ){
      if( mkdir(zFile, mode) ){
        /* The mkdir() call to create the directory failed. This might not
        ** be an error though - if there is already a directory at the same
        ** path and either the permissions already match or can be changed
        ** to do so using chmod(), it is not an error.  */
        struct stat sStat;
        if( errno!=EEXIST
         || 0!=stat(zFile, &sStat)
         || !S_ISDIR(sStat.st_mode)
         || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777))
        ){
          return 1;
        }
      }
    }else{
      sqlite3_int64 nWrite = 0;
      const char *z;
      int rc = 0;
      FILE *out = fopen(zFile, "wb");
      if( out==0 ) return 1;
      z = (const char*)sqlite3_value_blob(pData);
      if( z ){
        sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
        nWrite = sqlite3_value_bytes(pData);
        if( nWrite!=n ){
          rc = 1;
        }
      }
      fclose(out);
      if( rc==0 && chmod(zFile, mode & 0777) ){
        rc = 1;
      }
      if( rc ) return 2;
      sqlite3_result_int64(pCtx, nWrite);
    }
  }
  return 0;
}

/*
** Implementation of the "writefile(W,X[,Y]])" SQL function.  
**
** The argument X is written into file W.  The number of bytes written is
** returned. Or NULL is returned if something goes wrong, such as being unable
** to open file X for writing.
*/
static void writefileFunc(
  sqlite3_context *context,
  int argc,
  sqlite3_value **argv
){
  const char *zFile;
  mode_t mode = 0;
  int res;

  if( argc<2 || argc>3 ){
    sqlite3_result_error(context, 
        "wrong number of arguments to function writefile()", -1
    );
    return;
  }

  zFile = (const char*)sqlite3_value_text(argv[0]);
  if( zFile==0 ) return;
  if( argc>=3 ){
    sqlite3_result_int(context, 0);
    mode = sqlite3_value_int(argv[2]);
  }



  res = writeFile(context, zFile, mode, argv[1]);




  if( res==1 && errno==ENOENT ){
    if( makeDirectory(zFile, mode)==SQLITE_OK ){
      res = writeFile(context, zFile, mode, argv[1]);

    }








  }



  if( res!=0 ){


    if( S_ISLNK(mode) ){
      ctxErrorMsg(context, "failed to create symlink: %s", zFile);




    }else if( S_ISDIR(mode) ){
      ctxErrorMsg(context, "failed to create directory: %s", zFile);
    }else{


      ctxErrorMsg(context, "failed to write file: %s", zFile);

    }
  }
}

#ifndef SQLITE_OMIT_VIRTUALTABLE

/* 
Changes to src/shell.c.in.
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
  raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
  raw_printf(stderr, "Where sub-commands are:\n");
  raw_printf(stderr, "    fkey-indexes\n");
  return SQLITE_ERROR;
}

static void shellPrepare(
  ShellState *p, 
  int *pRc, 
  const char *zSql, 
  sqlite3_stmt **ppStmt
){
  *ppStmt = 0;
  if( *pRc==SQLITE_OK ){
    int rc = sqlite3_prepare_v2(p->db, zSql, -1, ppStmt, 0);
    if( rc!=SQLITE_OK ){
      raw_printf(stderr, "sql error: %s (%d)\n", 
          sqlite3_errmsg(p->db), sqlite3_errcode(p->db)
      );
      *pRc = rc;
    }
  }
}

static void shellFinalize(







|






|


|







4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
  raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
  raw_printf(stderr, "Where sub-commands are:\n");
  raw_printf(stderr, "    fkey-indexes\n");
  return SQLITE_ERROR;
}

static void shellPrepare(
  sqlite3 *db, 
  int *pRc, 
  const char *zSql, 
  sqlite3_stmt **ppStmt
){
  *ppStmt = 0;
  if( *pRc==SQLITE_OK ){
    int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
    if( rc!=SQLITE_OK ){
      raw_printf(stderr, "sql error: %s (%d)\n", 
          sqlite3_errmsg(db), sqlite3_errcode(db)
      );
      *pRc = rc;
    }
  }
}

static void shellFinalize(
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
































4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159

















4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170

4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208





































































4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
  raw_printf(stderr, "error in .ar command line\n");
  return SQLITE_ERROR;
}

/*
** Values for ArCommand.eCmd.
*/
#define AR_CMD_CREATE  1
#define AR_CMD_EXTRACT 2
#define AR_CMD_LIST    3
#define AR_CMD_UPDATE  4

































/*
** Parse the command line for an ".ar" command. The results are written into
** structure (*pAr). SQLITE_OK is returned if the command line is parsed
** successfully, otherwise an error message is written to stderr and 
** SQLITE_ERROR returned.
*/
static int arParseCommand(
  char **azArg,                   /* Array of arguments passed to dot command */
  int nArg,                       /* Number of entries in azArg[] */
  ArCommand *pAr                  /* Populate this object */
){

















  if( nArg<=1 ){
    return arUsage();
  }else{
    char *z = azArg[1];
    memset(pAr, 0, sizeof(ArCommand));

    if( z[0]!='-' ){
      /* Traditional style [tar] invocation */
      int i;
      int iArg = 2;
      for(i=0; z[i]; i++){

        switch( z[i] ){
          case 'c':
            if( pAr->eCmd ) return arUsage();
            pAr->eCmd = AR_CMD_CREATE;
            break;
          case 'x':
            if( pAr->eCmd ) return arUsage();
            pAr->eCmd = AR_CMD_EXTRACT;
            break;
          case 't':
            if( pAr->eCmd ) return arUsage();
            pAr->eCmd = AR_CMD_LIST;
            break;
          case 'u':
            if( pAr->eCmd ) return arUsage();
            pAr->eCmd = AR_CMD_UPDATE;
            break;

          case 'v':
            pAr->bVerbose = 1;
            break;
          case 'f':
            if( iArg>=nArg ) return arUsage();
            pAr->zFile = azArg[iArg++];
            break;
          case 'C':
            if( iArg>=nArg ) return arUsage();
            pAr->zDir = azArg[iArg++];
            break;

          default:
            return arUsage();
        }
      }

      pAr->nArg = nArg-iArg;
      if( pAr->nArg>0 ){
        pAr->azArg = &azArg[iArg];





































































      }
    }
  }

  return SQLITE_OK;
}

/*
** Implementation of .ar "Update" command. 
*/
static int arUpdateCmd(ShellState *p, ArCommand *pAr){
  raw_printf(stderr, "todo...\n");
  return SQLITE_OK;
}

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


/*
** Implementation of .ar "eXtract" command. 
*/
static int arExtractCommand(ShellState *p, 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"; 








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












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











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



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










|







|








|







4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221

4222

4223












4224




4225
4226


4227
4228

4229

4230
4231


4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
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
  raw_printf(stderr, "error in .ar command line\n");
  return SQLITE_ERROR;
}

/*
** Values for ArCommand.eCmd.
*/
#define AR_CMD_CREATE       1
#define AR_CMD_EXTRACT      2
#define AR_CMD_LIST         3
#define AR_CMD_UPDATE       4

/*
** Other (non-command) switches.
*/
#define AR_SWITCH_VERBOSE   5
#define AR_SWITCH_FILE      6
#define AR_SWITCH_DIRECTORY 7

static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
  switch( eSwitch ){
    case AR_CMD_CREATE:
    case AR_CMD_EXTRACT:
    case AR_CMD_LIST:
    case AR_CMD_UPDATE:
      if( pAr->eCmd ) return arUsage();
      pAr->eCmd = eSwitch;
      break;

    case AR_SWITCH_VERBOSE:
      pAr->bVerbose = 1;
      break;

    case AR_SWITCH_FILE:
      pAr->zFile = zArg;
      break;
    case AR_SWITCH_DIRECTORY:
      pAr->zDir = zArg;
      break;
  }

  return SQLITE_OK;
}

/*
** Parse the command line for an ".ar" command. The results are written into
** structure (*pAr). SQLITE_OK is returned if the command line is parsed
** successfully, otherwise an error message is written to stderr and 
** SQLITE_ERROR returned.
*/
static int arParseCommand(
  char **azArg,                   /* Array of arguments passed to dot command */
  int nArg,                       /* Number of entries in azArg[] */
  ArCommand *pAr                  /* Populate this object */
){
  struct ArSwitch {
    char cShort;
    const char *zLong;
    int eSwitch;
    int bArg;
  } aSwitch[] = {
    { 'c', "create",    AR_CMD_CREATE, 0 },
    { 'x', "extract",   AR_CMD_EXTRACT, 0 },
    { 't', "list",      AR_CMD_LIST, 0 },
    { 'u', "update",    AR_CMD_UPDATE, 0 },
    { 'v', "verbose",   AR_SWITCH_VERBOSE, 0 },
    { 'f', "file",      AR_SWITCH_FILE, 1 },
    { 'C', "directory", AR_SWITCH_DIRECTORY, 1 }
  };
  int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
  struct ArSwitch *pEnd = &aSwitch[nSwitch];

  if( nArg<=1 ){
    return arUsage();
  }else{
    char *z = azArg[1];
    memset(pAr, 0, sizeof(ArCommand));

    if( z[0]!='-' ){
      /* Traditional style [tar] invocation */
      int i;
      int iArg = 2;
      for(i=0; z[i]; i++){
        const char *zArg = 0;
        struct ArSwitch *pOpt;

        for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){

          if( z[i]==pOpt->cShort ) break;












        }




        if( pOpt==pEnd ) return arUsage();
        if( pOpt->bArg ){


          if( iArg>=nArg ) return arUsage();
          zArg = azArg[iArg++];

        }

        if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
      }


      pAr->nArg = nArg-iArg;
      if( pAr->nArg>0 ){
        pAr->azArg = &azArg[iArg];
      }
    }else{
      /* Non-traditional invocation */
      int iArg;
      for(iArg=1; iArg<nArg; iArg++){
        int n;
        z = azArg[iArg];
        if( z[0]!='-' ){
          /* All remaining command line words are command arguments. */
          pAr->azArg = &azArg[iArg];
          pAr->nArg = nArg-iArg;
          break;
        }
        n = strlen(z);

        if( z[1]!='-' ){
          int i;
          /* One or more short options */
          for(i=1; i<n; i++){
            const char *zArg = 0;
            struct ArSwitch *pOpt;
            for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
              if( z[i]==pOpt->cShort ) break;
            }
            if( pOpt==pEnd ) return arUsage();
            if( pOpt->bArg ){
              if( i<(n-1) ){
                zArg = &z[i+1];
                i = n;
              }else{
                if( iArg>=(nArg-1) ) return arUsage();
                zArg = azArg[++iArg];
              }
            }
            if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
          }
        }else if( z[2]=='\0' ){
          /* A -- option, indicating that all remaining command line words
          ** are command arguments.  */
          pAr->azArg = &azArg[iArg+1];
          pAr->nArg = nArg-iArg-1;
          break;
        }else{
          /* A long option */
          const char *zArg = 0;             /* Argument for option, if any */
          struct ArSwitch *pMatch = 0;      /* Matching option */
          struct ArSwitch *pOpt;            /* Iterator */
          for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
            const char *zLong = pOpt->zLong;
            if( (n-2)<=strlen(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
              if( pMatch ){
                /* ambiguous option */
                return arUsage();
              }else{
                pMatch = pOpt;
              }
            }
          }

          if( pMatch==0 ){
            /* no such option. */
            return arUsage();
          }
          if( pMatch->bArg ){
            if( iArg>=(nArg-1) ) return arUsage();
            zArg = azArg[++iArg];
          }
          if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
        }
      }
    }
  }

  return SQLITE_OK;
}

/*
** 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"; 

4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
  }else{
    zDir = sqlite3_mprintf("");
  }

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

  shellPrepare(p, &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(p, &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) ){







|










|







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
  }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) ){
4292
4293
4294
4295
4296
4297
4298

4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
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
** Create the "sqlar" table in the database if it does not already exist.
** Then add each file in the azFile[] array to the archive. Directories
** are added recursively. If argument bVerbose is non-zero, a message is
** printed on stdout for each file archived.
*/
static int arCreateCommand(
  ShellState *p,                  /* Shell state pointer */

  ArCommand *pAr                  /* Command arguments and options */
){
  const char *zSql = 
    "SELECT name, mode, mtime, data FROM fsdir(?, ?)";

  const char *zSqlar = 
      "CREATE TABLE IF NOT EXISTS sqlar("
      "name TEXT PRIMARY KEY,  -- name of the file\n"
      "mode INT,               -- access permissions\n"
      "mtime INT,              -- last modification time\n"
      "sz INT,                 -- original file size\n"
      "data BLOB               -- compressed content\n"
      ")";

  const char *zInsert = "REPLACE INTO sqlar VALUES(?, ?, ?, ?, ?)";

  sqlite3_stmt *pStmt = 0;        /* Directory traverser */
  sqlite3_stmt *pInsert = 0;      /* Compilation of zInsert */
  int i;                          /* For iterating through azFile[] */
  int rc;                         /* Return code */

  Bytef *aCompress = 0;           /* Compression buffer */
  int nCompress = 0;              /* Size of compression buffer */

  rc = sqlite3_exec(p->db, "SAVEPOINT ar;", 0, 0, 0);
  if( rc!=SQLITE_OK ) return rc;

  rc = sqlite3_exec(p->db, zSqlar, 0, 0, 0);



  shellPrepare(p, &rc, zInsert, &pInsert);
  shellPrepare(p, &rc, zSql, &pStmt);
  sqlite3_bind_text(pStmt, 2, pAr->zDir, -1, SQLITE_STATIC);

  for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
    sqlite3_bind_text(pStmt, 1, pAr->azArg[i], -1, SQLITE_STATIC);
    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
      int sz;
      const char *zName = (const char*)sqlite3_column_text(pStmt, 0);







>


<
|
<
|






|
|










|


|
>
>
>
|
|







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
** Create the "sqlar" table in the database if it does not already exist.
** Then add each file in the azFile[] array to the archive. Directories
** are added recursively. If argument bVerbose is non-zero, a message is
** printed on stdout for each file archived.
*/
static int arCreateCommand(
  ShellState *p,                  /* Shell state pointer */
  sqlite3 *db,
  ArCommand *pAr                  /* Command arguments and options */
){

  const char *zSql = "SELECT name, mode, mtime, data FROM fsdir(?, ?)";

  const char *zCreate = 
      "CREATE TABLE IF NOT EXISTS sqlar("
      "name TEXT PRIMARY KEY,  -- name of the file\n"
      "mode INT,               -- access permissions\n"
      "mtime INT,              -- last modification time\n"
      "sz INT,                 -- original file size\n"
      "data BLOB               -- compressed content\n"
  ")";
  const char *zDrop = "DROP TABLE IF EXISTS sqlar";
  const char *zInsert = "REPLACE INTO sqlar VALUES(?, ?, ?, ?, ?)";

  sqlite3_stmt *pStmt = 0;        /* Directory traverser */
  sqlite3_stmt *pInsert = 0;      /* Compilation of zInsert */
  int i;                          /* For iterating through azFile[] */
  int rc;                         /* Return code */

  Bytef *aCompress = 0;           /* Compression buffer */
  int nCompress = 0;              /* Size of compression buffer */

  rc = sqlite3_exec(db, "SAVEPOINT ar;", 0, 0, 0);
  if( rc!=SQLITE_OK ) return rc;

  rc = sqlite3_exec(db, zDrop, 0, 0, 0);
  if( rc!=SQLITE_OK ) return rc;

  rc = sqlite3_exec(db, zCreate, 0, 0, 0);
  shellPrepare(db, &rc, zInsert, &pInsert);
  shellPrepare(db, &rc, zSql, &pStmt);
  sqlite3_bind_text(pStmt, 2, pAr->zDir, -1, SQLITE_STATIC);

  for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
    sqlite3_bind_text(pStmt, 1, pAr->azArg[i], -1, SQLITE_STATIC);
    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
      int sz;
      const char *zName = (const char*)sqlite3_column_text(pStmt, 0);
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
        rc = sqlite3_reset(pInsert);
      }
    }
    shellReset(&rc, pStmt);
  }

  if( rc!=SQLITE_OK ){
    sqlite3_exec(p->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
  }else{
    rc = sqlite3_exec(p->db, "RELEASE ar;", 0, 0, 0);
  }
  shellFinalize(&rc, pStmt);
  shellFinalize(&rc, pInsert);
  sqlite3_free(aCompress);
  return rc;
}

/*
** Implementation of ".ar" dot command.
*/
static int arDotCommand(
  ShellState *pState,             /* Current shell tool state */
  char **azArg,                   /* Array of arguments passed to dot command */
  int nArg                        /* Number of entries in azArg[] */
){
  ArCommand cmd;
  int rc;
  rc = arParseCommand(azArg, nArg, &cmd);
  if( rc==SQLITE_OK ){





















    switch( cmd.eCmd ){
      case AR_CMD_CREATE:
        rc = arCreateCommand(pState, &cmd);
        break;

      case AR_CMD_EXTRACT:
        rc = arExtractCommand(pState, &cmd);
        break;

      case AR_CMD_LIST:
        rc = arListCommand(pState, &cmd);
        break;

      default:
        assert( cmd.eCmd==AR_CMD_UPDATE );
        rc = arUpdateCmd(pState, &cmd);
        break;
    }




  }

  return rc;
}


/*







|

|



















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


|



|



|




|


>
>
>
>







4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
        rc = sqlite3_reset(pInsert);
      }
    }
    shellReset(&rc, pStmt);
  }

  if( rc!=SQLITE_OK ){
    sqlite3_exec(db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
  }else{
    rc = sqlite3_exec(db, "RELEASE ar;", 0, 0, 0);
  }
  shellFinalize(&rc, pStmt);
  shellFinalize(&rc, pInsert);
  sqlite3_free(aCompress);
  return rc;
}

/*
** Implementation of ".ar" dot command.
*/
static int arDotCommand(
  ShellState *pState,             /* Current shell tool state */
  char **azArg,                   /* Array of arguments passed to dot command */
  int nArg                        /* Number of entries in azArg[] */
){
  ArCommand cmd;
  int rc;
  rc = arParseCommand(azArg, nArg, &cmd);
  if( rc==SQLITE_OK ){
    sqlite3 *db = 0;              /* Database handle to use as archive */

    if( cmd.zFile ){
      int flags;
      if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_UPDATE ){
        flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
      }else{
        flags = SQLITE_OPEN_READONLY;
      }
      rc = sqlite3_open_v2(cmd.zFile, &db, flags, 0);
      if( rc!=SQLITE_OK ){
        raw_printf(stderr, "cannot open file: %s (%s)\n", 
            cmd.zFile, sqlite3_errmsg(db)
        );
        sqlite3_close(db);
        return rc;
      }
    }else{
      db = pState->db;
    }

    switch( cmd.eCmd ){
      case AR_CMD_CREATE:
        rc = arCreateCommand(pState, db, &cmd);
        break;

      case AR_CMD_EXTRACT:
        rc = arExtractCommand(pState, db, &cmd);
        break;

      case AR_CMD_LIST:
        rc = arListCommand(pState, db, &cmd);
        break;

      default:
        assert( cmd.eCmd==AR_CMD_UPDATE );
        rc = arUpdateCmd(pState, db, &cmd);
        break;
    }

    if( cmd.zFile ){
      sqlite3_close(db);
    }
  }

  return rc;
}


/*
Changes to test/shell8.test.
58
59
60
61
62
63
64





































65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102


proc dir_compare {d1 d2} {
  set l1 [dir_to_list $d1]
  set l2 [dir_to_list $d1]
  string compare $l1 $l2
}






































populate_dir ar1 {
  file1 "abcd" 
  file2 "efgh"
  dir1/file3 "ijkl"
}

set expected [dir_to_list ar1]
# puts "# $expected"
do_test 1.1 {
  forcedelete test_ar.db

  catchcmd test_ar.db ".ar c ar1"
  file delete -force ar1
  catchcmd test_ar.db ".ar x"

  dir_to_list ar1
} $expected

do_test 1.2 {
  file delete -force ar3
  file mkdir ar3
  catchcmd test_ar.db ".ar xvC ar3"
  dir_to_list ar3/ar1
} $expected

do_test 1.3 {
  file delete -force ar3
  file mkdir ar3

  forcedelete test_ar.db
  catchcmd test_ar.db ".ar cC ar1 file1 file2 dir1"
  catchcmd test_ar.db ".ar xC ar3"
  dir_to_list ar3
} $expected



finish_test








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

|
|
|
|
|
|
|
<
<
<

|
<
<
<
<




>
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

107
108
109


110
111
112

113
114
115
116
117
118
119
120
121
122



123
124




125
126
127
128
129

proc dir_compare {d1 d2} {
  set l1 [dir_to_list $d1]
  set l2 [dir_to_list $d1]
  string compare $l1 $l2
}

foreach {tn tcl} {
  1 {
    set c1 ".ar c ar1"
    set x1 ".ar x"

    set c2 ".ar cC ar1 ."
    set x2 ".ar Cx ar3"
  }

  2 {
    set c1 ".ar -c ar1"
    set x1 ".ar -x"

    set c2 ".ar -cC ar1 ."
    set x2 ".ar -xC ar3"
  }

  3 {
    set c1 ".ar --create ar1"
    set x1 ".ar --extract"

    set c2 ".ar --directory ar1 --create ."
    set x2 ".ar --extract --dir ar3"
  }

  4 {
    set c1 ".ar --cr ar1"
    set x1 ".ar --e"

    set c2 ".ar -C ar1 -c ."
    set x2 ".ar -x -C ar3"
  }
} {
  eval $tcl

  # Populate directory "ar1" with some files.
  #
  populate_dir ar1 {
    file1 "abcd" 
    file2 "efgh"
    dir1/file3 "ijkl"
  }

  set expected [dir_to_list ar1]

  do_test 1.$tn.1 {


    catchcmd test_ar.db $c1
    file delete -force ar1
    catchcmd test_ar.db $x1

    dir_to_list ar1
  } $expected

  do_test 1.$tn.2 {
    file delete -force ar3
    catchcmd test_ar.db $c2
    catchcmd test_ar.db $x2
    dir_to_list ar3
  } $expected
}




finish_test







finish_test