SQLite

Check-in [9699e9bd9a]
Login

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

Overview
Comment:Update the [showdb] program so that the database file is read directly (bypassing the SQLite VFS) only if the --raw option is specified. Otherwise, it is read using the default VFS. An alternative VFS may be specified by specifying the database file to open using the URI syntax.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | showdb-uri-option
Files: files | file ages | folders
SHA1: 9699e9bd9a0e813d04c9d37422920670697c46ed
User & Date: dan 2015-08-04 15:23:49.470
Context
2015-08-04
15:29
Update the [showdb] program so that the database file is read directly (bypassing the SQLite VFS) only if the --raw option is specified. Otherwise, it is read using the default VFS. Also, the URI syntax may be used on the command line to specify the name of the database file to examine, so an alternative VFS may be requested using a URI parameter. (check-in: e3c6d4b6e7 user: dan tags: trunk)
15:23
Update the [showdb] program so that the database file is read directly (bypassing the SQLite VFS) only if the --raw option is specified. Otherwise, it is read using the default VFS. An alternative VFS may be specified by specifying the database file to open using the URI syntax. (Closed-Leaf check-in: 9699e9bd9a user: dan tags: showdb-uri-option)
2015-08-03
17:03
Add the "--uri" option to utility program "showdb". This option causes showdb to use sqlite with the SQLITE_OPEN_URI option to open the database file, instead of opening it directly using the OS open() function. (check-in: 90e34d5119 user: dan tags: showdb-uri-option)
Changes
Unified Diff Ignore Whitespace Patch
Changes to tool/showdb.c.
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35


static struct GlobalData {
  int pagesize;                   /* Size of a database page */
  int dbfd;                       /* File descriptor for reading the DB */
  int mxPage;                     /* Last page number */
  int perLine;                    /* HEX elements to print per line */

  int bUri;                       /* True for URI mode */
  sqlite3_file *pFd;              /* File descriptor for URI mode */
  sqlite3 *pDb;                   /* Database handle that owns pFd */
} g = {1024, -1, 0, 16,   0, 0, 0};


typedef long long int i64;      /* Datatype for 64-bit integers */








<
|







20
21
22
23
24
25
26

27
28
29
30
31
32
33
34


static struct GlobalData {
  int pagesize;                   /* Size of a database page */
  int dbfd;                       /* File descriptor for reading the DB */
  int mxPage;                     /* Last page number */
  int perLine;                    /* HEX elements to print per line */

  int bRaw;                       /* True to access db file via OS APIs */
  sqlite3_file *pFd;              /* File descriptor for URI mode */
  sqlite3 *pDb;                   /* Database handle that owns pFd */
} g = {1024, -1, 0, 16,   0, 0, 0};


typedef long long int i64;      /* Datatype for 64-bit integers */

93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
*/

/*
** Open the database file.
*/
static void fileOpen(const char *zPrg, const char *zName){
  assert( g.dbfd<0 );
  if( g.bUri ){
    int rc;
    void *pArg = (void *)(&g.pFd);
    g.pDb = openDatabase(zPrg, zName);
    rc = sqlite3_file_control(g.pDb, "main", SQLITE_FCNTL_FILE_POINTER, pArg);
    if( rc!=SQLITE_OK ){
      fprintf(stderr, 
          "%s: failed to obtain fd for %s (SQLite too old?)\n", zPrg, zName







|







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

/*
** Open the database file.
*/
static void fileOpen(const char *zPrg, const char *zName){
  assert( g.dbfd<0 );
  if( g.bRaw==0 ){
    int rc;
    void *pArg = (void *)(&g.pFd);
    g.pDb = openDatabase(zPrg, zName);
    rc = sqlite3_file_control(g.pDb, "main", SQLITE_FCNTL_FILE_POINTER, pArg);
    if( rc!=SQLITE_OK ){
      fprintf(stderr, 
          "%s: failed to obtain fd for %s (SQLite too old?)\n", zPrg, zName
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  }
}

/*
** Close the database file opened by fileOpen()
*/
static void fileClose(){
  if( g.bUri ){
    sqlite3_close(g.pDb);
    g.pDb = 0;
    g.pFd = 0;
  }else{
    close(g.dbfd);
    g.dbfd = -1;
  }







|







116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
  }
}

/*
** Close the database file opened by fileOpen()
*/
static void fileClose(){
  if( g.bRaw==0 ){
    sqlite3_close(g.pDb);
    g.pDb = 0;
    g.pFd = 0;
  }else{
    close(g.dbfd);
    g.dbfd = -1;
  }
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
*/
static unsigned char *fileRead(sqlite3_int64 ofst, int nByte){
  unsigned char *aData;
  int got;
  aData = sqlite3_malloc(nByte+32);
  if( aData==0 ) out_of_memory();
  memset(aData, 0, nByte+32);
  if( g.bUri ){
    int rc = g.pFd->pMethods->xRead(g.pFd, (void*)aData, nByte, ofst);
    if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
      fprintf(stderr, "error in xRead() - %d\n", rc);
      exit(1);
    }
  }else{
    lseek(g.dbfd, ofst, SEEK_SET);
    got = read(g.dbfd, aData, nByte);
    if( got>0 && got<nByte ) memset(aData+got, 0, nByte-got);
  }
  return aData;
}

/*
** Return the size of the file in byte.
*/
static sqlite3_int64 fileGetsize(void){
  sqlite3_int64 res = 0;
  if( g.bUri ){
    int rc = g.pFd->pMethods->xFileSize(g.pFd, &res);
    if( rc!=SQLITE_OK ){
      fprintf(stderr, "error in xFileSize() - %d\n", rc);
      exit(1);
    }
  }else{
    struct stat sbuf;







|


















|







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
*/
static unsigned char *fileRead(sqlite3_int64 ofst, int nByte){
  unsigned char *aData;
  int got;
  aData = sqlite3_malloc(nByte+32);
  if( aData==0 ) out_of_memory();
  memset(aData, 0, nByte+32);
  if( g.bRaw==0 ){
    int rc = g.pFd->pMethods->xRead(g.pFd, (void*)aData, nByte, ofst);
    if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
      fprintf(stderr, "error in xRead() - %d\n", rc);
      exit(1);
    }
  }else{
    lseek(g.dbfd, ofst, SEEK_SET);
    got = read(g.dbfd, aData, nByte);
    if( got>0 && got<nByte ) memset(aData+got, 0, nByte-got);
  }
  return aData;
}

/*
** Return the size of the file in byte.
*/
static sqlite3_int64 fileGetsize(void){
  sqlite3_int64 res = 0;
  if( g.bRaw==0 ){
    int rc = g.pFd->pMethods->xFileSize(g.pFd, &res);
    if( rc!=SQLITE_OK ){
      fprintf(stderr, "error in xFileSize() - %d\n", rc);
      exit(1);
    }
  }else{
    struct stat sbuf;
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
/*
** Print a usage comment
*/
static void usage(const char *argv0){
  fprintf(stderr, "Usage %s ?--uri? FILENAME ?args...?\n\n", argv0);
  fprintf(stderr,
    "switches:\n"
    "    --uri           FILENAME is a URI\n"
    "args:\n"
    "    dbheader        Show database header\n"
    "    pgidx           Index of how each page is used\n"
    "    ptrmap          Show all PTRMAP page content\n"
    "    NNN..MMM        Show hex of pages NNN through MMM\n"
    "    NNN..end        Show hex of pages NNN through end of file\n"
    "    NNNb            Decode btree page NNN\n"







|







1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
/*
** Print a usage comment
*/
static void usage(const char *argv0){
  fprintf(stderr, "Usage %s ?--uri? FILENAME ?args...?\n\n", argv0);
  fprintf(stderr,
    "switches:\n"
    "    --raw           Read db file directly, bypassing SQLite VFS\n"
    "args:\n"
    "    dbheader        Show database header\n"
    "    pgidx           Index of how each page is used\n"
    "    ptrmap          Show all PTRMAP page content\n"
    "    NNN..MMM        Show hex of pages NNN through MMM\n"
    "    NNN..end        Show hex of pages NNN through end of file\n"
    "    NNNb            Decode btree page NNN\n"
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
  unsigned char *zPgSz;
  const char *zPrg = argv[0];     /* Name of this executable */
  char **azArg = argv;
  int nArg = argc;

  /* Check for the "--uri" or "-uri" switch. */
  if( nArg>1 ){
    if( sqlite3_stricmp("-uri", azArg[1])==0 
     || sqlite3_stricmp("--uri", azArg[1])==0
    ){
      g.bUri = 1;
      azArg++;
      nArg--;
    }
  }

  if( nArg<2 ){
    usage(zPrg);
    exit(1);
  }

  fileOpen(azArg[0], azArg[1]);
  szFile = fileGetsize();

  zPgSz = fileRead(16, 2);
  g.pagesize = zPgSz[0]*256 + zPgSz[1]*65536;
  if( g.pagesize==0 ) g.pagesize = 1024;
  sqlite3_free(zPgSz);








|
|

|










|







1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
  unsigned char *zPgSz;
  const char *zPrg = argv[0];     /* Name of this executable */
  char **azArg = argv;
  int nArg = argc;

  /* Check for the "--uri" or "-uri" switch. */
  if( nArg>1 ){
    if( sqlite3_stricmp("-raw", azArg[1])==0 
     || sqlite3_stricmp("--raw", azArg[1])==0
    ){
      g.bRaw = 1;
      azArg++;
      nArg--;
    }
  }

  if( nArg<2 ){
    usage(zPrg);
    exit(1);
  }

  fileOpen(zPrg, azArg[1]);
  szFile = fileGetsize();

  zPgSz = fileRead(16, 2);
  g.pagesize = zPgSz[0]*256 + zPgSz[1]*65536;
  if( g.pagesize==0 ) g.pagesize = 1024;
  sqlite3_free(zPgSz);

1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
        continue;
      }
      if( strcmp(azArg[i], "ptrmap")==0 ){
        ptrmap_coverage_report(azArg[1]);
        continue;
      }
      if( strcmp(azArg[i], "help")==0 ){
        usage(azArg[0]);
        continue;
      }
      if( !isdigit(azArg[i][0]) ){
        fprintf(stderr, "%s: unknown option: [%s]\n", azArg[0], azArg[i]);
        continue;
      }
      iStart = strtol(azArg[i], &zLeft, 0);
      if( zLeft && strcmp(zLeft,"..end")==0 ){
        iEnd = g.mxPage;
      }else if( zLeft && zLeft[0]=='.' && zLeft[1]=='.' ){
        iEnd = strtol(&zLeft[2], 0, 0);







|



|







1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
        continue;
      }
      if( strcmp(azArg[i], "ptrmap")==0 ){
        ptrmap_coverage_report(azArg[1]);
        continue;
      }
      if( strcmp(azArg[i], "help")==0 ){
        usage(zPrg);
        continue;
      }
      if( !isdigit(azArg[i][0]) ){
        fprintf(stderr, "%s: unknown option: [%s]\n", zPrg, azArg[i]);
        continue;
      }
      iStart = strtol(azArg[i], &zLeft, 0);
      if( zLeft && strcmp(zLeft,"..end")==0 ){
        iEnd = g.mxPage;
      }else if( zLeft && zLeft[0]=='.' && zLeft[1]=='.' ){
        iEnd = strtol(&zLeft[2], 0, 0);