sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
| 1 | /* |
| 2 | ** 2001 September 15 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** Code for testing the pager.c module in SQLite. This code |
| 13 | ** is not included in the SQLite library. It is used for automated |
| 14 | ** testing of the SQLite library. |
| 15 | */ |
| 16 | #include "sqliteInt.h" |
| 17 | #include "tclsqlite.h" |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include <ctype.h> |
| 21 | |
| 22 | extern const char *sqlite3ErrName(int); |
| 23 | |
| 24 | /* |
| 25 | ** Page size and reserved size used for testing. |
| 26 | */ |
| 27 | static int test_pagesize = 1024; |
| 28 | |
| 29 | /* |
| 30 | ** Dummy page reinitializer |
| 31 | */ |
| 32 | static void pager_test_reiniter(DbPage *pNotUsed){ |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | /* |
| 37 | ** Usage: pager_open FILENAME N-PAGE |
| 38 | ** |
| 39 | ** Open a new pager |
| 40 | */ |
| 41 | static int SQLITE_TCLAPI pager_open( |
| 42 | void *NotUsed, |
| 43 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 44 | int argc, /* Number of arguments */ |
| 45 | const char **argv /* Text of each argument */ |
| 46 | ){ |
| 47 | u32 pageSize; |
| 48 | Pager *pPager; |
| 49 | int nPage; |
| 50 | int rc; |
| 51 | if( argc!=3 ){ |
| 52 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 53 | " FILENAME N-PAGE\"", NULL); |
| 54 | return TCL_ERROR; |
| 55 | } |
| 56 | if( Tcl_GetInt(interp, argv[2], &nPage) ) return TCL_ERROR; |
| 57 | rc = sqlite3PagerOpen(sqlite3_vfs_find(0), &pPager, argv[1], 0, 0, |
| 58 | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MAIN_DB, |
| 59 | pager_test_reiniter); |
| 60 | if( rc!=SQLITE_OK ){ |
| 61 | Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); |
| 62 | return TCL_ERROR; |
| 63 | } |
| 64 | sqlite3PagerSetCachesize(pPager, nPage); |
| 65 | pageSize = test_pagesize; |
| 66 | sqlite3PagerSetPagesize(pPager, &pageSize, -1); |
| 67 | Tcl_AppendResult(interp, sqlite3TestPtrToText(pPager), NULL); |
| 68 | return TCL_OK; |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | ** Usage: pager_close ID |
| 73 | ** |
| 74 | ** Close the given pager. |
| 75 | */ |
| 76 | static int SQLITE_TCLAPI pager_close( |
| 77 | void *NotUsed, |
| 78 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 79 | int argc, /* Number of arguments */ |
| 80 | const char **argv /* Text of each argument */ |
| 81 | ){ |
| 82 | Pager *pPager; |
| 83 | int rc; |
| 84 | if( argc!=2 ){ |
| 85 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 86 | " ID\"", NULL); |
| 87 | return TCL_ERROR; |
| 88 | } |
| 89 | pPager = sqlite3TestTextToPtr(argv[1]); |
| 90 | rc = sqlite3PagerClose(pPager, 0); |
| 91 | if( rc!=SQLITE_OK ){ |
| 92 | Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); |
| 93 | return TCL_ERROR; |
| 94 | } |
| 95 | return TCL_OK; |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | ** Usage: pager_rollback ID |
| 100 | ** |
| 101 | ** Rollback changes |
| 102 | */ |
| 103 | static int SQLITE_TCLAPI pager_rollback( |
| 104 | void *NotUsed, |
| 105 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 106 | int argc, /* Number of arguments */ |
| 107 | const char **argv /* Text of each argument */ |
| 108 | ){ |
| 109 | Pager *pPager; |
| 110 | int rc; |
| 111 | if( argc!=2 ){ |
| 112 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 113 | " ID\"", NULL); |
| 114 | return TCL_ERROR; |
| 115 | } |
| 116 | pPager = sqlite3TestTextToPtr(argv[1]); |
| 117 | rc = sqlite3PagerRollback(pPager); |
| 118 | if( rc!=SQLITE_OK ){ |
| 119 | Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); |
| 120 | return TCL_ERROR; |
| 121 | } |
| 122 | return TCL_OK; |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | ** Usage: pager_commit ID |
| 127 | ** |
| 128 | ** Commit all changes |
| 129 | */ |
| 130 | static int SQLITE_TCLAPI pager_commit( |
| 131 | void *NotUsed, |
| 132 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 133 | int argc, /* Number of arguments */ |
| 134 | const char **argv /* Text of each argument */ |
| 135 | ){ |
| 136 | Pager *pPager; |
| 137 | int rc; |
| 138 | if( argc!=2 ){ |
| 139 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 140 | " ID\"", NULL); |
| 141 | return TCL_ERROR; |
| 142 | } |
| 143 | pPager = sqlite3TestTextToPtr(argv[1]); |
| 144 | rc = sqlite3PagerCommitPhaseOne(pPager, 0, 0); |
| 145 | if( rc!=SQLITE_OK ){ |
| 146 | Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); |
| 147 | return TCL_ERROR; |
| 148 | } |
| 149 | rc = sqlite3PagerCommitPhaseTwo(pPager); |
| 150 | if( rc!=SQLITE_OK ){ |
| 151 | Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); |
| 152 | return TCL_ERROR; |
| 153 | } |
| 154 | return TCL_OK; |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | ** Usage: pager_stmt_begin ID |
| 159 | ** |
| 160 | ** Start a new checkpoint. |
| 161 | */ |
| 162 | static int SQLITE_TCLAPI pager_stmt_begin( |
| 163 | void *NotUsed, |
| 164 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 165 | int argc, /* Number of arguments */ |
| 166 | const char **argv /* Text of each argument */ |
| 167 | ){ |
| 168 | Pager *pPager; |
| 169 | int rc; |
| 170 | if( argc!=2 ){ |
| 171 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 172 | " ID\"", NULL); |
| 173 | return TCL_ERROR; |
| 174 | } |
| 175 | pPager = sqlite3TestTextToPtr(argv[1]); |
| 176 | rc = sqlite3PagerOpenSavepoint(pPager, 1); |
| 177 | if( rc!=SQLITE_OK ){ |
| 178 | Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); |
| 179 | return TCL_ERROR; |
| 180 | } |
| 181 | return TCL_OK; |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | ** Usage: pager_stmt_rollback ID |
| 186 | ** |
| 187 | ** Rollback changes to a checkpoint |
| 188 | */ |
| 189 | static int SQLITE_TCLAPI pager_stmt_rollback( |
| 190 | void *NotUsed, |
| 191 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 192 | int argc, /* Number of arguments */ |
| 193 | const char **argv /* Text of each argument */ |
| 194 | ){ |
| 195 | Pager *pPager; |
| 196 | int rc; |
| 197 | if( argc!=2 ){ |
| 198 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 199 | " ID\"", NULL); |
| 200 | return TCL_ERROR; |
| 201 | } |
| 202 | pPager = sqlite3TestTextToPtr(argv[1]); |
| 203 | rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, 0); |
| 204 | sqlite3PagerSavepoint(pPager, SAVEPOINT_RELEASE, 0); |
| 205 | if( rc!=SQLITE_OK ){ |
| 206 | Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); |
| 207 | return TCL_ERROR; |
| 208 | } |
| 209 | return TCL_OK; |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | ** Usage: pager_stmt_commit ID |
| 214 | ** |
| 215 | ** Commit changes to a checkpoint |
| 216 | */ |
| 217 | static int SQLITE_TCLAPI pager_stmt_commit( |
| 218 | void *NotUsed, |
| 219 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 220 | int argc, /* Number of arguments */ |
| 221 | const char **argv /* Text of each argument */ |
| 222 | ){ |
| 223 | Pager *pPager; |
| 224 | int rc; |
| 225 | if( argc!=2 ){ |
| 226 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 227 | " ID\"", NULL); |
| 228 | return TCL_ERROR; |
| 229 | } |
| 230 | pPager = sqlite3TestTextToPtr(argv[1]); |
| 231 | rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_RELEASE, 0); |
| 232 | if( rc!=SQLITE_OK ){ |
| 233 | Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); |
| 234 | return TCL_ERROR; |
| 235 | } |
| 236 | return TCL_OK; |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | ** Usage: pager_stats ID |
| 241 | ** |
| 242 | ** Return pager statistics. |
| 243 | */ |
| 244 | static int SQLITE_TCLAPI pager_stats( |
| 245 | void *NotUsed, |
| 246 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 247 | int argc, /* Number of arguments */ |
| 248 | const char **argv /* Text of each argument */ |
| 249 | ){ |
| 250 | Pager *pPager; |
| 251 | int i, *a; |
| 252 | if( argc!=2 ){ |
| 253 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 254 | " ID\"", NULL); |
| 255 | return TCL_ERROR; |
| 256 | } |
| 257 | pPager = sqlite3TestTextToPtr(argv[1]); |
| 258 | a = sqlite3PagerStats(pPager); |
| 259 | for(i=0; i<9; i++){ |
| 260 | static char *zName[] = { |
| 261 | "ref", "page", "max", "size", "state", "err", |
| 262 | "hit", "miss", "ovfl", |
| 263 | }; |
| 264 | char zBuf[100]; |
| 265 | Tcl_AppendElement(interp, zName[i]); |
| 266 | sqlite3_snprintf(sizeof(zBuf),zBuf,"%d",a[i]); |
| 267 | Tcl_AppendElement(interp, zBuf); |
| 268 | } |
| 269 | return TCL_OK; |
| 270 | } |
| 271 | |
| 272 | /* |
| 273 | ** Usage: pager_pagecount ID |
| 274 | ** |
| 275 | ** Return the size of the database file. |
| 276 | */ |
| 277 | static int SQLITE_TCLAPI pager_pagecount( |
| 278 | void *NotUsed, |
| 279 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 280 | int argc, /* Number of arguments */ |
| 281 | const char **argv /* Text of each argument */ |
| 282 | ){ |
| 283 | Pager *pPager; |
| 284 | char zBuf[100]; |
| 285 | int nPage; |
| 286 | if( argc!=2 ){ |
| 287 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 288 | " ID\"", NULL); |
| 289 | return TCL_ERROR; |
| 290 | } |
| 291 | pPager = sqlite3TestTextToPtr(argv[1]); |
| 292 | sqlite3PagerPagecount(pPager, &nPage); |
| 293 | sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", nPage); |
| 294 | Tcl_AppendResult(interp, zBuf, NULL); |
| 295 | return TCL_OK; |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | ** Usage: page_get ID PGNO |
| 300 | ** |
| 301 | ** Return a pointer to a page from the database. |
| 302 | */ |
| 303 | static int SQLITE_TCLAPI page_get( |
| 304 | void *NotUsed, |
| 305 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 306 | int argc, /* Number of arguments */ |
| 307 | const char **argv /* Text of each argument */ |
| 308 | ){ |
| 309 | Pager *pPager; |
| 310 | DbPage *pPage = 0; |
| 311 | int pgno; |
| 312 | int rc; |
| 313 | if( argc!=3 ){ |
| 314 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 315 | " ID PGNO\"", NULL); |
| 316 | return TCL_ERROR; |
| 317 | } |
| 318 | pPager = sqlite3TestTextToPtr(argv[1]); |
| 319 | if( Tcl_GetInt(interp, argv[2], &pgno) ) return TCL_ERROR; |
| 320 | rc = sqlite3PagerSharedLock(pPager); |
| 321 | if( rc==SQLITE_OK ){ |
| 322 | rc = sqlite3PagerGet(pPager, pgno, &pPage, 0); |
| 323 | } |
| 324 | if( rc!=SQLITE_OK ){ |
| 325 | Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); |
| 326 | return TCL_ERROR; |
| 327 | } |
| 328 | Tcl_AppendResult(interp, sqlite3TestPtrToText(pPage), NULL); |
| 329 | return TCL_OK; |
| 330 | } |
| 331 | |
| 332 | /* |
| 333 | ** Usage: page_lookup ID PGNO |
| 334 | ** |
| 335 | ** Return a pointer to a page if the page is already in cache. |
| 336 | ** If not in cache, return an empty string. |
| 337 | */ |
| 338 | static int SQLITE_TCLAPI page_lookup( |
| 339 | void *NotUsed, |
| 340 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 341 | int argc, /* Number of arguments */ |
| 342 | const char **argv /* Text of each argument */ |
| 343 | ){ |
| 344 | Pager *pPager; |
| 345 | DbPage *pPage; |
| 346 | int pgno; |
| 347 | if( argc!=3 ){ |
| 348 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 349 | " ID PGNO\"", NULL); |
| 350 | return TCL_ERROR; |
| 351 | } |
| 352 | pPager = sqlite3TestTextToPtr(argv[1]); |
| 353 | if( Tcl_GetInt(interp, argv[2], &pgno) ) return TCL_ERROR; |
| 354 | pPage = sqlite3PagerLookup(pPager, pgno); |
| 355 | if( pPage ){ |
| 356 | Tcl_AppendResult(interp, sqlite3TestPtrToText(pPage), NULL); |
| 357 | } |
| 358 | return TCL_OK; |
| 359 | } |
| 360 | |
| 361 | /* |
| 362 | ** Usage: pager_truncate ID PGNO |
| 363 | */ |
| 364 | static int SQLITE_TCLAPI pager_truncate( |
| 365 | void *NotUsed, |
| 366 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 367 | int argc, /* Number of arguments */ |
| 368 | const char **argv /* Text of each argument */ |
| 369 | ){ |
| 370 | Pager *pPager; |
| 371 | int pgno; |
| 372 | if( argc!=3 ){ |
| 373 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 374 | " ID PGNO\"", NULL); |
| 375 | return TCL_ERROR; |
| 376 | } |
| 377 | pPager = sqlite3TestTextToPtr(argv[1]); |
| 378 | if( Tcl_GetInt(interp, argv[2], &pgno) ) return TCL_ERROR; |
| 379 | sqlite3PagerTruncateImage(pPager, pgno); |
| 380 | return TCL_OK; |
| 381 | } |
| 382 | |
| 383 | |
| 384 | /* |
| 385 | ** Usage: page_unref PAGE |
| 386 | ** |
| 387 | ** Drop a pointer to a page. |
| 388 | */ |
| 389 | static int SQLITE_TCLAPI page_unref( |
| 390 | void *NotUsed, |
| 391 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 392 | int argc, /* Number of arguments */ |
| 393 | const char **argv /* Text of each argument */ |
| 394 | ){ |
| 395 | DbPage *pPage; |
| 396 | if( argc!=2 ){ |
| 397 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 398 | " PAGE\"", NULL); |
| 399 | return TCL_ERROR; |
| 400 | } |
| 401 | pPage = (DbPage *)sqlite3TestTextToPtr(argv[1]); |
| 402 | sqlite3PagerUnref(pPage); |
| 403 | return TCL_OK; |
| 404 | } |
| 405 | |
| 406 | /* |
| 407 | ** Usage: page_read PAGE |
| 408 | ** |
| 409 | ** Return the content of a page |
| 410 | */ |
| 411 | static int SQLITE_TCLAPI page_read( |
| 412 | void *NotUsed, |
| 413 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 414 | int argc, /* Number of arguments */ |
| 415 | const char **argv /* Text of each argument */ |
| 416 | ){ |
| 417 | char zBuf[100]; |
| 418 | DbPage *pPage; |
| 419 | if( argc!=2 ){ |
| 420 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 421 | " PAGE\"", NULL); |
| 422 | return TCL_ERROR; |
| 423 | } |
| 424 | pPage = sqlite3TestTextToPtr(argv[1]); |
| 425 | memcpy(zBuf, sqlite3PagerGetData(pPage), sizeof(zBuf)); |
| 426 | Tcl_AppendResult(interp, zBuf, NULL); |
| 427 | return TCL_OK; |
| 428 | } |
| 429 | |
| 430 | /* |
| 431 | ** Usage: page_number PAGE |
| 432 | ** |
| 433 | ** Return the page number for a page. |
| 434 | */ |
| 435 | static int SQLITE_TCLAPI page_number( |
| 436 | void *NotUsed, |
| 437 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 438 | int argc, /* Number of arguments */ |
| 439 | const char **argv /* Text of each argument */ |
| 440 | ){ |
| 441 | char zBuf[100]; |
| 442 | DbPage *pPage; |
| 443 | if( argc!=2 ){ |
| 444 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 445 | " PAGE\"", NULL); |
| 446 | return TCL_ERROR; |
| 447 | } |
| 448 | pPage = (DbPage *)sqlite3TestTextToPtr(argv[1]); |
| 449 | sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", sqlite3PagerPagenumber(pPage)); |
| 450 | Tcl_AppendResult(interp, zBuf, NULL); |
| 451 | return TCL_OK; |
| 452 | } |
| 453 | |
| 454 | /* |
| 455 | ** Usage: page_write PAGE DATA |
| 456 | ** |
| 457 | ** Write something into a page. |
| 458 | */ |
| 459 | static int SQLITE_TCLAPI page_write( |
| 460 | void *NotUsed, |
| 461 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 462 | int argc, /* Number of arguments */ |
| 463 | const char **argv /* Text of each argument */ |
| 464 | ){ |
| 465 | DbPage *pPage; |
| 466 | char *pData; |
| 467 | int rc; |
| 468 | if( argc!=3 ){ |
| 469 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 470 | " PAGE DATA\"", NULL); |
| 471 | return TCL_ERROR; |
| 472 | } |
| 473 | pPage = (DbPage *)sqlite3TestTextToPtr(argv[1]); |
| 474 | rc = sqlite3PagerWrite(pPage); |
| 475 | if( rc!=SQLITE_OK ){ |
| 476 | Tcl_AppendResult(interp, sqlite3ErrName(rc), NULL); |
| 477 | return TCL_ERROR; |
| 478 | } |
| 479 | pData = sqlite3PagerGetData(pPage); |
| 480 | strncpy(pData, argv[2], test_pagesize-1); |
| 481 | pData[test_pagesize-1] = 0; |
| 482 | return TCL_OK; |
| 483 | } |
| 484 | |
| 485 | #ifndef SQLITE_OMIT_DISKIO |
| 486 | /* |
| 487 | ** Usage: fake_big_file N FILENAME |
| 488 | ** |
| 489 | ** Write a few bytes at the N megabyte point of FILENAME. This will |
| 490 | ** create a large file. If the file was a valid SQLite database, then |
| 491 | ** the next time the database is opened, SQLite will begin allocating |
| 492 | ** new pages after N. If N is 2096 or bigger, this will test the |
| 493 | ** ability of SQLite to write to large files. |
| 494 | */ |
| 495 | static int SQLITE_TCLAPI fake_big_file( |
| 496 | void *NotUsed, |
| 497 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 498 | int argc, /* Number of arguments */ |
| 499 | const char **argv /* Text of each argument */ |
| 500 | ){ |
| 501 | sqlite3_vfs *pVfs; |
| 502 | sqlite3_file *fd = 0; |
| 503 | int rc; |
| 504 | int n; |
| 505 | i64 offset; |
| 506 | char *zFile; |
| 507 | int nFile; |
| 508 | if( argc!=3 ){ |
| 509 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 510 | " N-MEGABYTES FILE\"", NULL); |
| 511 | return TCL_ERROR; |
| 512 | } |
| 513 | if( Tcl_GetInt(interp, argv[1], &n) ) return TCL_ERROR; |
| 514 | #if defined(_WIN32) |
| 515 | if( n>2 ){ |
| 516 | Tcl_AppendResult(interp, "cannot create ", argv[1], |
| 517 | "MB file because Windows " |
| 518 | "does not support sparse files", (void*)0); |
| 519 | return TCL_ERROR; |
| 520 | } |
| 521 | #endif |
| 522 | |
| 523 | pVfs = sqlite3_vfs_find(0); |
| 524 | nFile = (int)strlen(argv[2]); |
| 525 | zFile = sqlite3_malloc( nFile+2 ); |
| 526 | if( zFile==0 ) return TCL_ERROR; |
| 527 | memcpy(zFile, argv[2], nFile+1); |
| 528 | zFile[nFile+1] = 0; |
| 529 | rc = sqlite3OsOpenMalloc(pVfs, zFile, &fd, |
| 530 | (SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_DB), 0 |
| 531 | ); |
| 532 | if( rc ){ |
| 533 | Tcl_AppendResult(interp, "open failed: ", sqlite3ErrName(rc), NULL); |
| 534 | sqlite3_free(zFile); |
| 535 | return TCL_ERROR; |
| 536 | } |
| 537 | offset = n; |
| 538 | offset *= 1024*1024; |
| 539 | rc = sqlite3OsWrite(fd, "Hello, World!", 14, offset); |
| 540 | sqlite3OsCloseFree(fd); |
| 541 | sqlite3_free(zFile); |
| 542 | if( rc ){ |
| 543 | Tcl_AppendResult(interp, "write failed: ", sqlite3ErrName(rc), NULL); |
| 544 | return TCL_ERROR; |
| 545 | } |
| 546 | return TCL_OK; |
| 547 | } |
| 548 | #endif |
| 549 | |
| 550 | |
| 551 | /* |
| 552 | ** test_control_pending_byte PENDING_BYTE |
| 553 | ** |
| 554 | ** Set the PENDING_BYTE using the sqlite3_test_control() interface. |
| 555 | */ |
| 556 | static int SQLITE_TCLAPI testPendingByte( |
| 557 | void *NotUsed, |
| 558 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 559 | int argc, /* Number of arguments */ |
| 560 | const char **argv /* Text of each argument */ |
| 561 | ){ |
| 562 | int pbyte; |
| 563 | int rc; |
| 564 | if( argc!=2 ){ |
| 565 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 566 | " PENDING-BYTE\"", (void*)0); |
| 567 | return TCL_ERROR; |
| 568 | } |
| 569 | if( Tcl_GetInt(interp, argv[1], &pbyte) ) return TCL_ERROR; |
| 570 | rc = sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, pbyte); |
| 571 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
| 572 | return TCL_OK; |
| 573 | } |
| 574 | |
| 575 | /* |
| 576 | ** The sqlite3FaultSim() callback: |
| 577 | */ |
| 578 | static Tcl_Interp *faultSimInterp = 0; |
| 579 | static int faultSimScriptSize = 0; |
| 580 | static char *faultSimScript; |
| 581 | static int faultSimCallback(int x){ |
| 582 | char zInt[30]; |
| 583 | int i; |
| 584 | int isNeg; |
| 585 | int rc; |
| 586 | if( x==0 ){ |
| 587 | memcpy(faultSimScript+faultSimScriptSize, "0", 2); |
| 588 | }else{ |
| 589 | /* Convert x to text without using any sqlite3 routines */ |
| 590 | if( x<0 ){ |
| 591 | isNeg = 1; |
| 592 | x = -x; |
| 593 | }else{ |
| 594 | isNeg = 0; |
| 595 | } |
| 596 | zInt[sizeof(zInt)-1] = 0; |
| 597 | for(i=sizeof(zInt)-2; i>0 && x>0; i--, x /= 10){ |
| 598 | zInt[i] = (x%10) + '0'; |
| 599 | } |
| 600 | if( isNeg ) zInt[i--] = '-'; |
| 601 | memcpy(faultSimScript+faultSimScriptSize, zInt+i+1, sizeof(zInt)-i-1); |
| 602 | } |
| 603 | rc = Tcl_Eval(faultSimInterp, faultSimScript); |
| 604 | if( rc ){ |
| 605 | fprintf(stderr, "fault simulator script failed: [%s]", faultSimScript); |
| 606 | rc = SQLITE_ERROR; |
| 607 | }else{ |
| 608 | rc = atoi(Tcl_GetStringResult(faultSimInterp)); |
| 609 | } |
| 610 | Tcl_ResetResult(faultSimInterp); |
| 611 | return rc; |
| 612 | } |
| 613 | |
| 614 | /* |
| 615 | ** sqlite3_test_control_fault_install SCRIPT |
| 616 | ** |
| 617 | ** Arrange to invoke SCRIPT with the integer argument to sqlite3FaultSim() |
| 618 | ** appended, whenever sqlite3FaultSim() is called. Or, if SCRIPT is the |
| 619 | ** empty string, cancel the sqlite3FaultSim() callback. |
| 620 | */ |
| 621 | static int SQLITE_TCLAPI faultInstallCmd( |
| 622 | void *NotUsed, |
| 623 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 624 | int argc, /* Number of arguments */ |
| 625 | const char **argv /* Text of each argument */ |
| 626 | ){ |
| 627 | const char *zScript; |
| 628 | int nScript; |
| 629 | int rc; |
| 630 | if( argc!=1 && argc!=2 ){ |
| 631 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 632 | " SCRIPT\"", (void*)0); |
| 633 | return SQLITE_ERROR; |
| 634 | } |
| 635 | zScript = argc==2 ? argv[1] : ""; |
| 636 | nScript = (int)strlen(zScript); |
| 637 | if( faultSimScript ){ |
| 638 | free(faultSimScript); |
| 639 | faultSimScript = 0; |
| 640 | } |
| 641 | if( nScript==0 ){ |
| 642 | rc = sqlite3_test_control(SQLITE_TESTCTRL_FAULT_INSTALL, 0); |
| 643 | }else{ |
| 644 | faultSimScript = malloc( nScript+100 ); |
| 645 | if( faultSimScript==0 ){ |
| 646 | Tcl_AppendResult(interp, "out of memory", (void*)0); |
| 647 | return SQLITE_ERROR; |
| 648 | } |
| 649 | memcpy(faultSimScript, zScript, nScript); |
| 650 | faultSimScript[nScript] = ' '; |
| 651 | faultSimScriptSize = nScript+1; |
| 652 | faultSimInterp = interp; |
| 653 | rc = sqlite3_test_control(SQLITE_TESTCTRL_FAULT_INSTALL, faultSimCallback); |
| 654 | } |
| 655 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
| 656 | return SQLITE_OK; |
| 657 | } |
| 658 | |
| 659 | /* |
| 660 | ** sqlite3BitvecBuiltinTest SIZE PROGRAM |
| 661 | ** |
| 662 | ** Invoke the SQLITE_TESTCTRL_BITVEC_TEST operator on test_control. |
| 663 | ** See comments on sqlite3BitvecBuiltinTest() for additional information. |
| 664 | */ |
| 665 | static int SQLITE_TCLAPI testBitvecBuiltinTest( |
| 666 | void *NotUsed, |
| 667 | Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ |
| 668 | int argc, /* Number of arguments */ |
| 669 | const char **argv /* Text of each argument */ |
| 670 | ){ |
| 671 | int sz, rc; |
| 672 | int nProg = 0; |
| 673 | int aProg[100]; |
| 674 | const char *z; |
| 675 | if( argc!=3 ){ |
| 676 | Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], |
| 677 | " SIZE PROGRAM\"", (void*)0); |
| 678 | } |
| 679 | if( Tcl_GetInt(interp, argv[1], &sz) ) return TCL_ERROR; |
| 680 | z = argv[2]; |
| 681 | while( nProg<99 && *z ){ |
| 682 | while( *z && !sqlite3Isdigit(*z) ){ z++; } |
| 683 | if( *z==0 ) break; |
| 684 | aProg[nProg++] = atoi(z); |
| 685 | while( sqlite3Isdigit(*z) ){ z++; } |
| 686 | } |
| 687 | aProg[nProg] = 0; |
| 688 | rc = sqlite3_test_control(SQLITE_TESTCTRL_BITVEC_TEST, sz, aProg); |
| 689 | Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); |
| 690 | return TCL_OK; |
| 691 | } |
| 692 | |
| 693 | /* |
| 694 | ** Register commands with the TCL interpreter. |
| 695 | */ |
| 696 | int Sqlitetest2_Init(Tcl_Interp *interp){ |
| 697 | extern int sqlite3_io_error_persist; |
| 698 | extern int sqlite3_io_error_pending; |
| 699 | extern int sqlite3_io_error_hit; |
| 700 | extern int sqlite3_io_error_hardhit; |
| 701 | extern int sqlite3_diskfull_pending; |
| 702 | extern int sqlite3_diskfull; |
| 703 | static struct { |
| 704 | char *zName; |
| 705 | Tcl_CmdProc *xProc; |
| 706 | } aCmd[] = { |
| 707 | { "pager_open", (Tcl_CmdProc*)pager_open }, |
| 708 | { "pager_close", (Tcl_CmdProc*)pager_close }, |
| 709 | { "pager_commit", (Tcl_CmdProc*)pager_commit }, |
| 710 | { "pager_rollback", (Tcl_CmdProc*)pager_rollback }, |
| 711 | { "pager_stmt_begin", (Tcl_CmdProc*)pager_stmt_begin }, |
| 712 | { "pager_stmt_commit", (Tcl_CmdProc*)pager_stmt_commit }, |
| 713 | { "pager_stmt_rollback", (Tcl_CmdProc*)pager_stmt_rollback }, |
| 714 | { "pager_stats", (Tcl_CmdProc*)pager_stats }, |
| 715 | { "pager_pagecount", (Tcl_CmdProc*)pager_pagecount }, |
| 716 | { "page_get", (Tcl_CmdProc*)page_get }, |
| 717 | { "page_lookup", (Tcl_CmdProc*)page_lookup }, |
| 718 | { "page_unref", (Tcl_CmdProc*)page_unref }, |
| 719 | { "page_read", (Tcl_CmdProc*)page_read }, |
| 720 | { "page_write", (Tcl_CmdProc*)page_write }, |
| 721 | { "page_number", (Tcl_CmdProc*)page_number }, |
| 722 | { "pager_truncate", (Tcl_CmdProc*)pager_truncate }, |
| 723 | #ifndef SQLITE_OMIT_DISKIO |
| 724 | { "fake_big_file", (Tcl_CmdProc*)fake_big_file }, |
| 725 | #endif |
| 726 | { "sqlite3BitvecBuiltinTest",(Tcl_CmdProc*)testBitvecBuiltinTest }, |
| 727 | { "sqlite3_test_control_pending_byte", (Tcl_CmdProc*)testPendingByte }, |
| 728 | { "sqlite3_test_control_fault_install", (Tcl_CmdProc*)faultInstallCmd }, |
| 729 | }; |
| 730 | int i; |
| 731 | for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){ |
| 732 | Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0); |
| 733 | } |
| 734 | Tcl_LinkVar(interp, "sqlite_io_error_pending", |
| 735 | (char*)&sqlite3_io_error_pending, TCL_LINK_INT); |
| 736 | Tcl_LinkVar(interp, "sqlite_io_error_persist", |
| 737 | (char*)&sqlite3_io_error_persist, TCL_LINK_INT); |
| 738 | Tcl_LinkVar(interp, "sqlite_io_error_hit", |
| 739 | (char*)&sqlite3_io_error_hit, TCL_LINK_INT); |
| 740 | Tcl_LinkVar(interp, "sqlite_io_error_hardhit", |
| 741 | (char*)&sqlite3_io_error_hardhit, TCL_LINK_INT); |
| 742 | Tcl_LinkVar(interp, "sqlite_diskfull_pending", |
| 743 | (char*)&sqlite3_diskfull_pending, TCL_LINK_INT); |
| 744 | Tcl_LinkVar(interp, "sqlite_diskfull", |
| 745 | (char*)&sqlite3_diskfull, TCL_LINK_INT); |
| 746 | #ifndef SQLITE_OMIT_WSD |
| 747 | Tcl_LinkVar(interp, "sqlite_pending_byte", |
| 748 | (char*)&sqlite3PendingByte, TCL_LINK_INT | TCL_LINK_READ_ONLY); |
| 749 | #endif |
| 750 | return TCL_OK; |
| 751 | } |