** This function is called to recover data from the database. A script ** to construct a new database containing all recovered data is output ** on stream pState->out. */
| 22687 | ** on stream pState->out. |
| 22688 | */ |
| 22689 | static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){ |
| 22690 | int rc = SQLITE_OK; |
| 22691 | const char *zRecoveryDb = ""; /* Name of "recovery" database. Debug only */ |
| 22692 | const char *zLAF = "lost_and_found"; |
| 22693 | int bFreelist = 1; /* 0 if --ignore-freelist is specified */ |
| 22694 | int bRowids = 1; /* 0 if --no-rowids */ |
| 22695 | sqlite3_recover *p = 0; |
| 22696 | int i = 0; |
| 22697 | |
| 22698 | for(i=1; i<nArg; i++){ |
| 22699 | char *z = azArg[i]; |
| 22700 | int n; |
| 22701 | if( z[0]=='-' && z[1]=='-' ) z++; |
| 22702 | n = strlen30(z); |
| 22703 | if( n<=17 && memcmp("-ignore-freelist", z, n)==0 ){ |
| 22704 | bFreelist = 0; |
| 22705 | }else |
| 22706 | if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){ |
| 22707 | /* This option determines the name of the ATTACH-ed database used |
| 22708 | ** internally by the recovery extension. The default is "" which |
| 22709 | ** means to use a temporary database that is automatically deleted |
| 22710 | ** when closed. This option is undocumented and might disappear at |
| 22711 | ** any moment. */ |
| 22712 | i++; |
| 22713 | zRecoveryDb = azArg[i]; |
| 22714 | }else |
| 22715 | if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){ |
| 22716 | i++; |
| 22717 | zLAF = azArg[i]; |
| 22718 | }else |
| 22719 | if( n<=10 && memcmp("-no-rowids", z, n)==0 ){ |
| 22720 | bRowids = 0; |
| 22721 | } |
| 22722 | else{ |
| 22723 | utf8_printf(stderr, "unexpected option: %s\n", azArg[i]); |
| 22724 | showHelp(pState->out, azArg[0]); |
| 22725 | return 1; |
| 22726 | } |
| 22727 | } |
| 22728 | |
| 22729 | p = sqlite3_recover_init_sql( |
| 22730 | pState->db, "main", recoverSqlCb, (void*)pState |
| 22731 | ); |
| 22732 | |
| 22733 | sqlite3_recover_config(p, 789, (void*)zRecoveryDb); /* Debug use only */ |
| 22734 | sqlite3_recover_config(p, SQLITE_RECOVER_LOST_AND_FOUND, (void*)zLAF); |
| 22735 | sqlite3_recover_config(p, SQLITE_RECOVER_ROWIDS, (void*)&bRowids); |
| 22736 | sqlite3_recover_config(p, SQLITE_RECOVER_FREELIST_CORRUPT,(void*)&bFreelist); |
| 22737 | |
| 22738 | sqlite3_recover_run(p); |
| 22739 | if( sqlite3_recover_errcode(p)!=SQLITE_OK ){ |
| 22740 | const char *zErr = sqlite3_recover_errmsg(p); |
| 22741 | int errCode = sqlite3_recover_errcode(p); |
| 22742 | raw_printf(stderr, "sql error: %s (%d)\n", zErr, errCode); |
| 22743 | } |
| 22744 | rc = sqlite3_recover_finish(p); |
| 22745 | return rc; |
| 22746 | } |
no test coverage detected