/ Check-in [33dc8fad]
Login

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

Overview
Comment:In the CLI with the -A command, if the file does not previously exist and its name looks like a ZIP archive name, then create it as a ZIP archive.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:33dc8fad7f2b467f259eb78eb7342a760f01d54d95da7fe4cace10e558788a58
User & Date: drh 2018-05-17 20:04:24
Context
2018-05-18
14:24
Improvements to integer/float comparisons on architectures that lack a "long double" type. check-in: 5139ea62 user: drh tags: trunk
13:39
Improvements to the sqlite3IntFloatCompare() routine for systems that lack the long double type. check-in: ea6a03a8 user: drh tags: int-float-compare
2018-05-17
20:04
In the CLI with the -A command, if the file does not previously exist and its name looks like a ZIP archive name, then create it as a ZIP archive. check-in: 33dc8fad user: drh tags: trunk
14:09
Improved error and help messages for the ".archive" command and "-A" option to the CLI. If a memory leak in --list processing. check-in: 02541ac6 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/shell.c.in.

  3543   3543   */
  3544   3544   int deduceDatabaseType(const char *zName, int dfltZip){
  3545   3545     FILE *f = fopen(zName, "rb");
  3546   3546     size_t n;
  3547   3547     int rc = SHELL_OPEN_UNSPEC;
  3548   3548     char zBuf[100];
  3549   3549     if( f==0 ){
  3550         -    if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ) return SHELL_OPEN_ZIPFILE;
  3551         -    return SHELL_OPEN_NORMAL;
         3550  +    if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
         3551  +       return SHELL_OPEN_ZIPFILE;
         3552  +    }else{
         3553  +       return SHELL_OPEN_NORMAL;
         3554  +    }
  3552   3555     }
  3553   3556     fseek(f, -25, SEEK_END);
  3554   3557     n = fread(zBuf, 25, 1, f);
  3555   3558     if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
  3556   3559       rc = SHELL_OPEN_APPENDVFS;
  3557   3560     }else{
  3558   3561       fseek(f, -22, SEEK_END);
................................................................................
  3564   3567         rc = SHELL_OPEN_ZIPFILE;
  3565   3568       }
  3566   3569     }
  3567   3570     fclose(f);
  3568   3571     return rc;  
  3569   3572   }
  3570   3573   
         3574  +/* Flags for open_db().
         3575  +**
         3576  +** The default behavior of open_db() is to exit(1) if the database fails to
         3577  +** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
         3578  +** but still returns without calling exit.
         3579  +**
         3580  +** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
         3581  +** ZIP archive if the file does not exist or is empty and its name matches
         3582  +** the *.zip pattern.
         3583  +*/
         3584  +#define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
         3585  +#define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
         3586  +
  3571   3587   /*
  3572   3588   ** Make sure the database is open.  If it is not, then open it.  If
  3573   3589   ** the database fails to open, print an error message and exit.
  3574   3590   */
  3575         -static void open_db(ShellState *p, int keepAlive){
         3591  +static void open_db(ShellState *p, int openFlags){
  3576   3592     if( p->db==0 ){
  3577   3593       if( p->openMode==SHELL_OPEN_UNSPEC ){
  3578   3594         if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){
  3579   3595           p->openMode = SHELL_OPEN_NORMAL;
  3580         -      }else if( access(p->zDbFilename,0)==0 ){
  3581         -        p->openMode = (u8)deduceDatabaseType(p->zDbFilename, 0);
         3596  +      }else{
         3597  +        p->openMode = (u8)deduceDatabaseType(p->zDbFilename, 
         3598  +                             (openFlags & OPEN_DB_ZIPFILE)!=0);
  3582   3599         }
  3583   3600       }
  3584   3601       switch( p->openMode ){
  3585   3602         case SHELL_OPEN_APPENDVFS: {
  3586   3603           sqlite3_open_v2(p->zDbFilename, &p->db, 
  3587   3604              SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, "apndvfs");
  3588   3605           break;
................................................................................
  3601   3618           break;
  3602   3619         }
  3603   3620       }
  3604   3621       globalDb = p->db;
  3605   3622       if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
  3606   3623         utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
  3607   3624             p->zDbFilename, sqlite3_errmsg(p->db));
  3608         -      if( keepAlive ) return;
         3625  +      if( openFlags & OPEN_DB_KEEPALIVE ) return;
  3609   3626         exit(1);
  3610   3627       }
  3611   3628   #ifndef SQLITE_OMIT_LOAD_EXTENSION
  3612   3629       sqlite3_enable_load_extension(p->db, 1);
  3613   3630   #endif
  3614   3631       sqlite3_fileio_init(p->db, 0, 0);
  3615   3632       sqlite3_shathree_init(p->db, 0, 0);
................................................................................
  6519   6536         }
  6520   6537       }
  6521   6538       /* If a filename is specified, try to open it first */
  6522   6539       zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
  6523   6540       if( zNewFilename ){
  6524   6541         if( newFlag ) shellDeleteFile(zNewFilename);
  6525   6542         p->zDbFilename = zNewFilename;
  6526         -      open_db(p, 1);
         6543  +      open_db(p, OPEN_DB_KEEPALIVE);
  6527   6544         if( p->db==0 ){
  6528   6545           utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
  6529   6546           sqlite3_free(zNewFilename);
  6530   6547         }else{
  6531   6548           p->zFreeOnClose = zNewFilename;
  6532   6549         }
  6533   6550       }
................................................................................
  8649   8666   #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
  8650   8667       }else if( strncmp(z, "-A", 2)==0 ){
  8651   8668         if( nCmd>0 ){
  8652   8669           utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
  8653   8670                               " with \"%s\"\n", z);
  8654   8671           return 1;
  8655   8672         }
  8656         -      open_db(&data, 0);
         8673  +      open_db(&data, OPEN_DB_ZIPFILE);
  8657   8674         if( z[2] ){
  8658   8675           argv[i] = &z[2];
  8659   8676           arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
  8660   8677         }else{
  8661   8678           arDotCommand(&data, 1, argv+i, argc-i);
  8662   8679         }
  8663   8680         readStdin = 0;