You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

499 lines
12 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
la_mojo: fix memory leaks and heap corruption Continuous leaks: - protocolParser: on package-pool overflow the oldest package was removed but never freed while the newest (still pooled) one was freed. Every packet a handler does not consume (e.g. other sensors on a shared bus) leaked once the pool was full. Free the removed package, free pooled packages in the destructor and getPackage(), use memmove for the overlapping buffer shifts. - SQLite_Connector::Query: loop only while sqlite3_step() returns SQLITE_ROW. Looping until SQLITE_DONE spun forever allocating rows on SQLITE_BUSY/ERROR. Set a busy timeout, close handles after a failed open, fix escapeBinary() writing into uninitialised buffers. - basicThread/basicDualThread: threads that ended by themselves were never joined, leaking their stacks on every stream drop/restart. Always join once (detach when called from the thread itself); check pthread_create result correctly. - curlRequest: free the header list; initialise m_pPostData and the content type; curlRequestsManager deletes requests it cannot queue. - kernelModuleModemManager: free the phonebook ID list on every reload. - deviceNodeFactory: do not append the same pending node every cycle. - streamHandler: free frames left in the encoding queue on destruction. Heap corruption: - kernelModuleFactory: module that failed to start was deleted twice. - mojoRemoteClient: basicRing::push() returns an ID (may be 0), not a bool; compare with -1. Fix packetExtractor and destructor leaks. - utilizeBasicEvent: payloads are malloc'ed, release with free(). - videoAVLIBSource: avoid sws context double free, NULL check order. - trim(): memmove for overlapping copy. Smaller leaks: dropSession duplicate query, LRF upload data, report backupper / fly hunter settings, Kafka consumer handle, BVMS uniq ID. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 day ago
​
​
la_mojo: fix memory leaks and heap corruption Continuous leaks: - protocolParser: on package-pool overflow the oldest package was removed but never freed while the newest (still pooled) one was freed. Every packet a handler does not consume (e.g. other sensors on a shared bus) leaked once the pool was full. Free the removed package, free pooled packages in the destructor and getPackage(), use memmove for the overlapping buffer shifts. - SQLite_Connector::Query: loop only while sqlite3_step() returns SQLITE_ROW. Looping until SQLITE_DONE spun forever allocating rows on SQLITE_BUSY/ERROR. Set a busy timeout, close handles after a failed open, fix escapeBinary() writing into uninitialised buffers. - basicThread/basicDualThread: threads that ended by themselves were never joined, leaking their stacks on every stream drop/restart. Always join once (detach when called from the thread itself); check pthread_create result correctly. - curlRequest: free the header list; initialise m_pPostData and the content type; curlRequestsManager deletes requests it cannot queue. - kernelModuleModemManager: free the phonebook ID list on every reload. - deviceNodeFactory: do not append the same pending node every cycle. - streamHandler: free frames left in the encoding queue on destruction. Heap corruption: - kernelModuleFactory: module that failed to start was deleted twice. - mojoRemoteClient: basicRing::push() returns an ID (may be 0), not a bool; compare with -1. Fix packetExtractor and destructor leaks. - utilizeBasicEvent: payloads are malloc'ed, release with free(). - videoAVLIBSource: avoid sws context double free, NULL check order. - trim(): memmove for overlapping copy. Smaller leaks: dropSession duplicate query, LRF upload data, report backupper / fly hunter settings, Kafka consumer handle, BVMS uniq ID. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 day ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
la_mojo: fix memory leaks and heap corruption Continuous leaks: - protocolParser: on package-pool overflow the oldest package was removed but never freed while the newest (still pooled) one was freed. Every packet a handler does not consume (e.g. other sensors on a shared bus) leaked once the pool was full. Free the removed package, free pooled packages in the destructor and getPackage(), use memmove for the overlapping buffer shifts. - SQLite_Connector::Query: loop only while sqlite3_step() returns SQLITE_ROW. Looping until SQLITE_DONE spun forever allocating rows on SQLITE_BUSY/ERROR. Set a busy timeout, close handles after a failed open, fix escapeBinary() writing into uninitialised buffers. - basicThread/basicDualThread: threads that ended by themselves were never joined, leaking their stacks on every stream drop/restart. Always join once (detach when called from the thread itself); check pthread_create result correctly. - curlRequest: free the header list; initialise m_pPostData and the content type; curlRequestsManager deletes requests it cannot queue. - kernelModuleModemManager: free the phonebook ID list on every reload. - deviceNodeFactory: do not append the same pending node every cycle. - streamHandler: free frames left in the encoding queue on destruction. Heap corruption: - kernelModuleFactory: module that failed to start was deleted twice. - mojoRemoteClient: basicRing::push() returns an ID (may be 0), not a bool; compare with -1. Fix packetExtractor and destructor leaks. - utilizeBasicEvent: payloads are malloc'ed, release with free(). - videoAVLIBSource: avoid sws context double free, NULL check order. - trim(): memmove for overlapping copy. Smaller leaks: dropSession duplicate query, LRF upload data, report backupper / fly hunter settings, Kafka consumer handle, BVMS uniq ID. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 day ago
​
​
la_mojo: fix memory leaks and heap corruption Continuous leaks: - protocolParser: on package-pool overflow the oldest package was removed but never freed while the newest (still pooled) one was freed. Every packet a handler does not consume (e.g. other sensors on a shared bus) leaked once the pool was full. Free the removed package, free pooled packages in the destructor and getPackage(), use memmove for the overlapping buffer shifts. - SQLite_Connector::Query: loop only while sqlite3_step() returns SQLITE_ROW. Looping until SQLITE_DONE spun forever allocating rows on SQLITE_BUSY/ERROR. Set a busy timeout, close handles after a failed open, fix escapeBinary() writing into uninitialised buffers. - basicThread/basicDualThread: threads that ended by themselves were never joined, leaking their stacks on every stream drop/restart. Always join once (detach when called from the thread itself); check pthread_create result correctly. - curlRequest: free the header list; initialise m_pPostData and the content type; curlRequestsManager deletes requests it cannot queue. - kernelModuleModemManager: free the phonebook ID list on every reload. - deviceNodeFactory: do not append the same pending node every cycle. - streamHandler: free frames left in the encoding queue on destruction. Heap corruption: - kernelModuleFactory: module that failed to start was deleted twice. - mojoRemoteClient: basicRing::push() returns an ID (may be 0), not a bool; compare with -1. Fix packetExtractor and destructor leaks. - utilizeBasicEvent: payloads are malloc'ed, release with free(). - videoAVLIBSource: avoid sws context double free, NULL check order. - trim(): memmove for overlapping copy. Smaller leaks: dropSession duplicate query, LRF upload data, report backupper / fly hunter settings, Kafka consumer handle, BVMS uniq ID. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 day ago
​
​
la_mojo: fix memory leaks and heap corruption Continuous leaks: - protocolParser: on package-pool overflow the oldest package was removed but never freed while the newest (still pooled) one was freed. Every packet a handler does not consume (e.g. other sensors on a shared bus) leaked once the pool was full. Free the removed package, free pooled packages in the destructor and getPackage(), use memmove for the overlapping buffer shifts. - SQLite_Connector::Query: loop only while sqlite3_step() returns SQLITE_ROW. Looping until SQLITE_DONE spun forever allocating rows on SQLITE_BUSY/ERROR. Set a busy timeout, close handles after a failed open, fix escapeBinary() writing into uninitialised buffers. - basicThread/basicDualThread: threads that ended by themselves were never joined, leaking their stacks on every stream drop/restart. Always join once (detach when called from the thread itself); check pthread_create result correctly. - curlRequest: free the header list; initialise m_pPostData and the content type; curlRequestsManager deletes requests it cannot queue. - kernelModuleModemManager: free the phonebook ID list on every reload. - deviceNodeFactory: do not append the same pending node every cycle. - streamHandler: free frames left in the encoding queue on destruction. Heap corruption: - kernelModuleFactory: module that failed to start was deleted twice. - mojoRemoteClient: basicRing::push() returns an ID (may be 0), not a bool; compare with -1. Fix packetExtractor and destructor leaks. - utilizeBasicEvent: payloads are malloc'ed, release with free(). - videoAVLIBSource: avoid sws context double free, NULL check order. - trim(): memmove for overlapping copy. Smaller leaks: dropSession duplicate query, LRF upload data, report backupper / fly hunter settings, Kafka consumer handle, BVMS uniq ID. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 day ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
la_mojo: fix memory leaks and heap corruption Continuous leaks: - protocolParser: on package-pool overflow the oldest package was removed but never freed while the newest (still pooled) one was freed. Every packet a handler does not consume (e.g. other sensors on a shared bus) leaked once the pool was full. Free the removed package, free pooled packages in the destructor and getPackage(), use memmove for the overlapping buffer shifts. - SQLite_Connector::Query: loop only while sqlite3_step() returns SQLITE_ROW. Looping until SQLITE_DONE spun forever allocating rows on SQLITE_BUSY/ERROR. Set a busy timeout, close handles after a failed open, fix escapeBinary() writing into uninitialised buffers. - basicThread/basicDualThread: threads that ended by themselves were never joined, leaking their stacks on every stream drop/restart. Always join once (detach when called from the thread itself); check pthread_create result correctly. - curlRequest: free the header list; initialise m_pPostData and the content type; curlRequestsManager deletes requests it cannot queue. - kernelModuleModemManager: free the phonebook ID list on every reload. - deviceNodeFactory: do not append the same pending node every cycle. - streamHandler: free frames left in the encoding queue on destruction. Heap corruption: - kernelModuleFactory: module that failed to start was deleted twice. - mojoRemoteClient: basicRing::push() returns an ID (may be 0), not a bool; compare with -1. Fix packetExtractor and destructor leaks. - utilizeBasicEvent: payloads are malloc'ed, release with free(). - videoAVLIBSource: avoid sws context double free, NULL check order. - trim(): memmove for overlapping copy. Smaller leaks: dropSession duplicate query, LRF upload data, report backupper / fly hunter settings, Kafka consumer handle, BVMS uniq ID. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 day ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
la_mojo: fix memory leaks and heap corruption Continuous leaks: - protocolParser: on package-pool overflow the oldest package was removed but never freed while the newest (still pooled) one was freed. Every packet a handler does not consume (e.g. other sensors on a shared bus) leaked once the pool was full. Free the removed package, free pooled packages in the destructor and getPackage(), use memmove for the overlapping buffer shifts. - SQLite_Connector::Query: loop only while sqlite3_step() returns SQLITE_ROW. Looping until SQLITE_DONE spun forever allocating rows on SQLITE_BUSY/ERROR. Set a busy timeout, close handles after a failed open, fix escapeBinary() writing into uninitialised buffers. - basicThread/basicDualThread: threads that ended by themselves were never joined, leaking their stacks on every stream drop/restart. Always join once (detach when called from the thread itself); check pthread_create result correctly. - curlRequest: free the header list; initialise m_pPostData and the content type; curlRequestsManager deletes requests it cannot queue. - kernelModuleModemManager: free the phonebook ID list on every reload. - deviceNodeFactory: do not append the same pending node every cycle. - streamHandler: free frames left in the encoding queue on destruction. Heap corruption: - kernelModuleFactory: module that failed to start was deleted twice. - mojoRemoteClient: basicRing::push() returns an ID (may be 0), not a bool; compare with -1. Fix packetExtractor and destructor leaks. - utilizeBasicEvent: payloads are malloc'ed, release with free(). - videoAVLIBSource: avoid sws context double free, NULL check order. - trim(): memmove for overlapping copy. Smaller leaks: dropSession duplicate query, LRF upload data, report backupper / fly hunter settings, Kafka consumer handle, BVMS uniq ID. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 day ago
​
​
​
​
​
​
la_mojo: fix memory leaks and heap corruption Continuous leaks: - protocolParser: on package-pool overflow the oldest package was removed but never freed while the newest (still pooled) one was freed. Every packet a handler does not consume (e.g. other sensors on a shared bus) leaked once the pool was full. Free the removed package, free pooled packages in the destructor and getPackage(), use memmove for the overlapping buffer shifts. - SQLite_Connector::Query: loop only while sqlite3_step() returns SQLITE_ROW. Looping until SQLITE_DONE spun forever allocating rows on SQLITE_BUSY/ERROR. Set a busy timeout, close handles after a failed open, fix escapeBinary() writing into uninitialised buffers. - basicThread/basicDualThread: threads that ended by themselves were never joined, leaking their stacks on every stream drop/restart. Always join once (detach when called from the thread itself); check pthread_create result correctly. - curlRequest: free the header list; initialise m_pPostData and the content type; curlRequestsManager deletes requests it cannot queue. - kernelModuleModemManager: free the phonebook ID list on every reload. - deviceNodeFactory: do not append the same pending node every cycle. - streamHandler: free frames left in the encoding queue on destruction. Heap corruption: - kernelModuleFactory: module that failed to start was deleted twice. - mojoRemoteClient: basicRing::push() returns an ID (may be 0), not a bool; compare with -1. Fix packetExtractor and destructor leaks. - utilizeBasicEvent: payloads are malloc'ed, release with free(). - videoAVLIBSource: avoid sws context double free, NULL check order. - trim(): memmove for overlapping copy. Smaller leaks: dropSession duplicate query, LRF upload data, report backupper / fly hunter settings, Kafka consumer handle, BVMS uniq ID. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 day ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
la_mojo: fix memory leaks and heap corruption Continuous leaks: - protocolParser: on package-pool overflow the oldest package was removed but never freed while the newest (still pooled) one was freed. Every packet a handler does not consume (e.g. other sensors on a shared bus) leaked once the pool was full. Free the removed package, free pooled packages in the destructor and getPackage(), use memmove for the overlapping buffer shifts. - SQLite_Connector::Query: loop only while sqlite3_step() returns SQLITE_ROW. Looping until SQLITE_DONE spun forever allocating rows on SQLITE_BUSY/ERROR. Set a busy timeout, close handles after a failed open, fix escapeBinary() writing into uninitialised buffers. - basicThread/basicDualThread: threads that ended by themselves were never joined, leaking their stacks on every stream drop/restart. Always join once (detach when called from the thread itself); check pthread_create result correctly. - curlRequest: free the header list; initialise m_pPostData and the content type; curlRequestsManager deletes requests it cannot queue. - kernelModuleModemManager: free the phonebook ID list on every reload. - deviceNodeFactory: do not append the same pending node every cycle. - streamHandler: free frames left in the encoding queue on destruction. Heap corruption: - kernelModuleFactory: module that failed to start was deleted twice. - mojoRemoteClient: basicRing::push() returns an ID (may be 0), not a bool; compare with -1. Fix packetExtractor and destructor leaks. - utilizeBasicEvent: payloads are malloc'ed, release with free(). - videoAVLIBSource: avoid sws context double free, NULL check order. - trim(): memmove for overlapping copy. Smaller leaks: dropSession duplicate query, LRF upload data, report backupper / fly hunter settings, Kafka consumer handle, BVMS uniq ID. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 day ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
la_mojo: fix memory leaks and heap corruption Continuous leaks: - protocolParser: on package-pool overflow the oldest package was removed but never freed while the newest (still pooled) one was freed. Every packet a handler does not consume (e.g. other sensors on a shared bus) leaked once the pool was full. Free the removed package, free pooled packages in the destructor and getPackage(), use memmove for the overlapping buffer shifts. - SQLite_Connector::Query: loop only while sqlite3_step() returns SQLITE_ROW. Looping until SQLITE_DONE spun forever allocating rows on SQLITE_BUSY/ERROR. Set a busy timeout, close handles after a failed open, fix escapeBinary() writing into uninitialised buffers. - basicThread/basicDualThread: threads that ended by themselves were never joined, leaking their stacks on every stream drop/restart. Always join once (detach when called from the thread itself); check pthread_create result correctly. - curlRequest: free the header list; initialise m_pPostData and the content type; curlRequestsManager deletes requests it cannot queue. - kernelModuleModemManager: free the phonebook ID list on every reload. - deviceNodeFactory: do not append the same pending node every cycle. - streamHandler: free frames left in the encoding queue on destruction. Heap corruption: - kernelModuleFactory: module that failed to start was deleted twice. - mojoRemoteClient: basicRing::push() returns an ID (may be 0), not a bool; compare with -1. Fix packetExtractor and destructor leaks. - utilizeBasicEvent: payloads are malloc'ed, release with free(). - videoAVLIBSource: avoid sws context double free, NULL check order. - trim(): memmove for overlapping copy. Smaller leaks: dropSession duplicate query, LRF upload data, report backupper / fly hunter settings, Kafka consumer handle, BVMS uniq ID. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 day ago
​
​
​
​
​
la_mojo: fix memory leaks and heap corruption Continuous leaks: - protocolParser: on package-pool overflow the oldest package was removed but never freed while the newest (still pooled) one was freed. Every packet a handler does not consume (e.g. other sensors on a shared bus) leaked once the pool was full. Free the removed package, free pooled packages in the destructor and getPackage(), use memmove for the overlapping buffer shifts. - SQLite_Connector::Query: loop only while sqlite3_step() returns SQLITE_ROW. Looping until SQLITE_DONE spun forever allocating rows on SQLITE_BUSY/ERROR. Set a busy timeout, close handles after a failed open, fix escapeBinary() writing into uninitialised buffers. - basicThread/basicDualThread: threads that ended by themselves were never joined, leaking their stacks on every stream drop/restart. Always join once (detach when called from the thread itself); check pthread_create result correctly. - curlRequest: free the header list; initialise m_pPostData and the content type; curlRequestsManager deletes requests it cannot queue. - kernelModuleModemManager: free the phonebook ID list on every reload. - deviceNodeFactory: do not append the same pending node every cycle. - streamHandler: free frames left in the encoding queue on destruction. Heap corruption: - kernelModuleFactory: module that failed to start was deleted twice. - mojoRemoteClient: basicRing::push() returns an ID (may be 0), not a bool; compare with -1. Fix packetExtractor and destructor leaks. - utilizeBasicEvent: payloads are malloc'ed, release with free(). - videoAVLIBSource: avoid sws context double free, NULL check order. - trim(): memmove for overlapping copy. Smaller leaks: dropSession duplicate query, LRF upload data, report backupper / fly hunter settings, Kafka consumer handle, BVMS uniq ID. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 day ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "sqlite_class.h"
  2. #define LOG(X) m_pLog->Log("SQDB", this, X)
  3. #define LOG_ERROR(X) m_pLog->Log("SQDB", this, _LOG_ERROR, X)
  4. static bool sqlite_global_config_is_set = false;
  5. static uint8_t sqlite_global_inctances_count = 0;
  6. // https://www.sqlite.org/recovery.html
  7. /*
  8. // *
  9. // ** This function is called to recover data from the database.
  10. // * /
  11. static int recoverDatabase(sqlite3 *db){
  12. int rc; // Return code from this routine
  13. const char *zLAF = "lost_and_found"; // Name of "lost_and_found" table
  14. int bFreelist = 1; // True to scan the freelist
  15. int bRowids = 1; // True to restore ROWID values
  16. sqlite3_recover *p; // The recovery object
  17. p = sqlite3_recover_init_sql(db, "main", recoverSqlCb, 0);
  18. sqlite3_recover_config(p, SQLITE_RECOVER_LOST_AND_FOUND, (void*)zLAF);
  19. sqlite3_recover_config(p, SQLITE_RECOVER_ROWIDS, (void*)&bRowids);
  20. sqlite3_recover_config(p, SQLITE_RECOVER_FREELIST_CORRUPT,(void*)&bFreelist);
  21. sqlite3_recover_run(p);
  22. if( sqlite3_recover_errcode(p)!=SQLITE_OK ){
  23. const char *zErr = sqlite3_recover_errmsg(p);
  24. int errCode = sqlite3_recover_errcode(p);
  25. if( eVerbosity>0 ){
  26. printf("recovery error: %s (%d)\n", zErr, errCode);
  27. }
  28. }
  29. rc = sqlite3_recover_finish(p);
  30. if( eVerbosity>0 && rc ){
  31. printf("recovery returns error code %d\n", rc);
  32. }
  33. return rc;
  34. }
  35. // *
  36. // ** This function is called to recover data from the database. A script
  37. // ** to construct a new database containing all recovered data is output
  38. // ** on stream pState->out.
  39. // * /
  40. static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
  41. int rc = SQLITE_OK;
  42. const char *zRecoveryDb = ""; // Name of "recovery" database. Debug only
  43. const char *zLAF = "lost_and_found";
  44. int bFreelist = 1; // 0 if --ignore-freelist is specified
  45. int bRowids = 1; // 0 if --no-rowids
  46. sqlite3_recover *p = 0;
  47. int i = 0;
  48. for(i=1; i<nArg; i++){
  49. char *z = azArg[i];
  50. int n;
  51. if( z[0]=='-' && z[1]=='-' ) z++;
  52. n = strlen30(z);
  53. if( n<=17 && memcmp("-ignore-freelist", z, n)==0 ){
  54. bFreelist = 0;
  55. }else
  56. if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
  57. // This option determines the name of the ATTACH-ed database used
  58. // internally by the recovery extension. The default is "" which
  59. // means to use a temporary database that is automatically deleted
  60. // when closed. This option is undocumented and might disappear at
  61. // any moment.
  62. i++;
  63. zRecoveryDb = azArg[i];
  64. }else
  65. if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
  66. i++;
  67. zLAF = azArg[i];
  68. }else
  69. if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
  70. bRowids = 0;
  71. }
  72. else{
  73. utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
  74. showHelp(pState->out, azArg[0]);
  75. return 1;
  76. }
  77. }
  78. p = sqlite3_recover_init_sql(
  79. pState->db, "main", recoverSqlCb, (void*)pState
  80. );
  81. sqlite3_recover_config(p, 789, (void*)zRecoveryDb); // Debug use only
  82. sqlite3_recover_config(p, SQLITE_RECOVER_LOST_AND_FOUND, (void*)zLAF);
  83. sqlite3_recover_config(p, SQLITE_RECOVER_ROWIDS, (void*)&bRowids);
  84. sqlite3_recover_config(p, SQLITE_RECOVER_FREELIST_CORRUPT,(void*)&bFreelist);
  85. sqlite3_recover_run(p);
  86. if( sqlite3_recover_errcode(p)!=SQLITE_OK ){
  87. const char *zErr = sqlite3_recover_errmsg(p);
  88. int errCode = sqlite3_recover_errcode(p);
  89. raw_printf(stderr, "sql error: %s (%d)\n", zErr, errCode);
  90. }
  91. rc = sqlite3_recover_finish(p);
  92. return rc;
  93. }
  94. */
  95. static void errorLogCallback(void * pArg, int iErrCode, const char * zMsg)
  96. {
  97. char tB[1024];
  98. sprintf(tB, "(%d) %s", iErrCode, zMsg);
  99. basicLogCollection::getInstance()->Log("SQDB_G", pArg, tB);
  100. }
  101. SQLite_Connector::SQLite_Connector()
  102. {
  103. m_pLog = basicLogCollection::getInstance();
  104. m_bIsConnected = false;
  105. m_bPersistentConnection = false;
  106. m_pDBLink = NULL;
  107. if (! sqlite_global_config_is_set)
  108. {
  109. // if( SQLITE_OK != sqlite3_config(int, ...) ){ SQLITE_MISUSE
  110. sqlite3_config(SQLITE_CONFIG_LOG, errorLogCallback, this);
  111. sqlite3_initialize();
  112. sqlite_global_config_is_set = true;
  113. };
  114. sqlite_global_inctances_count++;
  115. }
  116. SQLite_Connector::~SQLite_Connector()
  117. {
  118. m_bIsConnected = ! Disconnect();
  119. if (sqlite_global_inctances_count == 1)
  120. {
  121. sqlite3_shutdown();
  122. };
  123. sqlite_global_inctances_count--;
  124. }
  125. bool SQLite_Connector::Connect(CSTR FileName, int AUX_FLAGS, bool Persistent)
  126. {
  127. m_mAccessMutex.lock();
  128. m_bPersistentConnection = Persistent;
  129. if (m_bIsConnected)
  130. {
  131. #ifdef DEBUG_ALL
  132. LOG("Connected -> Disconnect!");
  133. #endif
  134. m_bIsConnected = false;
  135. if (m_pDBLink != NULL)
  136. {
  137. if (SQLITE_OK != sqlite3_close(m_pDBLink))
  138. {
  139. m_mAccessMutex.unlock();
  140. return false;
  141. };
  142. };
  143. };
  144. sprintf(m_pDBName, "%s", FileName);
  145. m_nFLAGS = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | AUX_FLAGS;
  146. if (m_bPersistentConnection)
  147. {
  148. #ifdef DEBUG_ALL
  149. LOG("Making Connection....");
  150. #endif
  151. if (SQLITE_OK == sqlite3_open_v2(m_pDBName, &m_pDBLink, m_nFLAGS, NULL))
  152. {
  153. // Wait for locks held by other connections instead of failing immediately
  154. sqlite3_busy_timeout(m_pDBLink, SQLITE_BUSY_TIMEOUT_MS);
  155. m_bIsConnected = true;
  156. }
  157. else if (m_pDBLink != NULL)
  158. {
  159. // sqlite3_open_v2 allocates a handle even on failure
  160. sqlite3_close(m_pDBLink);
  161. m_pDBLink = NULL;
  162. }
  163. #ifdef DEBUG_ALL
  164. LOG("Done!");
  165. #endif
  166. }
  167. else
  168. {
  169. m_bIsConnected = true;
  170. };
  171. m_mAccessMutex.unlock();
  172. return m_bIsConnected;
  173. }
  174. bool SQLite_Connector::ReConnect(void)
  175. {
  176. if (m_pDBLink != NULL)
  177. {
  178. if (SQLITE_OK != sqlite3_close(m_pDBLink))
  179. {
  180. return false;
  181. }
  182. m_pDBLink = NULL;
  183. }
  184. if (SQLITE_OK == sqlite3_open_v2(m_pDBName, &m_pDBLink, m_nFLAGS, NULL))
  185. {
  186. // Wait for locks held by other connections instead of failing immediately
  187. sqlite3_busy_timeout(m_pDBLink, SQLITE_BUSY_TIMEOUT_MS);
  188. return true;
  189. }
  190. if (m_pDBLink != NULL)
  191. {
  192. // sqlite3_open_v2 allocates a handle even on failure
  193. sqlite3_close(m_pDBLink);
  194. m_pDBLink = NULL;
  195. }
  196. return false;
  197. }
  198. bool SQLite_Connector::Disconnect(void)
  199. {
  200. if (m_pDBLink != NULL)
  201. {
  202. if (SQLITE_OK != sqlite3_close(m_pDBLink))
  203. {
  204. return false;
  205. }
  206. }
  207. m_pDBLink = NULL;
  208. return true;
  209. }
  210. bool SQLite_Connector::SilentQuery(CSTR QS)
  211. {
  212. if (m_bIsConnected)
  213. {
  214. int QSlen = strlen(QS);
  215. m_mAccessMutex.lock();
  216. if (! m_bPersistentConnection)
  217. {
  218. if (! ReConnect())
  219. {
  220. m_mAccessMutex.unlock();
  221. return false;
  222. };
  223. };
  224. sqlite3_stmt * query = NULL;
  225. // if( SQLITE_OK != sqlite3_prepare_v2( m_pDBLink, QS, QSlen, NULL, NULL ) ){
  226. if (SQLITE_OK != sqlite3_prepare_v2(m_pDBLink, QS, QSlen, &query, NULL))
  227. {
  228. m_mAccessMutex.unlock();
  229. return false;
  230. };
  231. #ifdef DEBUG_ALL
  232. LOG(sqlite3_expanded_sql(query));
  233. #endif
  234. if (SQLITE_DONE != sqlite3_step(query))
  235. {
  236. sqlite3_finalize(query);
  237. m_mAccessMutex.unlock();
  238. return false;
  239. };
  240. sqlite3_finalize(query);
  241. if (! m_bPersistentConnection)
  242. {
  243. if (! Disconnect())
  244. {
  245. m_mAccessMutex.unlock();
  246. return false;
  247. };
  248. };
  249. m_mAccessMutex.unlock();
  250. return true;
  251. }
  252. else
  253. {
  254. LOG_ERROR("No Connection to DB!");
  255. };
  256. return false;
  257. }
  258. AAssoc * SQLite_Connector::Query(CSTR QS, bool useCache)
  259. {
  260. AAssoc * RetVal = NULL;
  261. Assoc * tElement;
  262. //unsigned int num_Fields;
  263. sqlite3_stmt * query = NULL;
  264. //char temp[256];
  265. if (useCache)useCache=false;
  266. int QSlen = strlen(QS);
  267. m_mAccessMutex.lock();
  268. if (! m_bPersistentConnection)
  269. {
  270. if (! ReConnect())
  271. {
  272. m_mAccessMutex.unlock();
  273. return RetVal;
  274. };
  275. };
  276. if (SQLITE_OK != sqlite3_prepare_v2(m_pDBLink, QS, QSlen, &query, NULL))
  277. {
  278. m_mAccessMutex.unlock();
  279. return RetVal;
  280. };
  281. #ifdef DEBUG_ALL
  282. LOG(sqlite3_expanded_sql(query));
  283. #endif
  284. RetVal = new AAssoc();
  285. int stepRC;
  286. // Only SQLITE_ROW carries data. Looping until SQLITE_DONE spins forever (allocating a new
  287. // row every iteration) when step returns SQLITE_BUSY / SQLITE_ERROR / SQLITE_MISUSE.
  288. while ((stepRC = sqlite3_step(query)) == SQLITE_ROW)
  289. {
  290. string Key;
  291. string Val;
  292. uint32_t num_cols = sqlite3_column_count(query);
  293. if (num_cols)
  294. {
  295. tElement = new Assoc();
  296. if (tElement)
  297. {
  298. for (uint32_t i = 0; i < num_cols; i++)
  299. {
  300. Key = string(sqlite3_column_name(query, i));
  301. switch (sqlite3_column_type(query, i))
  302. {
  303. case SQLITE_INTEGER: Val = std::string((char *) sqlite3_column_text(query, i)); break;
  304. case SQLITE_FLOAT : Val = std::string((char *) sqlite3_column_text(query, i)); break;
  305. case SQLITE3_TEXT : Val = std::string((char *) sqlite3_column_text(query, i)); break;
  306. default : Val.clear(); break; // NULL / BLOB: don't reuse previous column's value
  307. };
  308. (*tElement)[Key] = Val;
  309. };
  310. RetVal->push_back(tElement);
  311. };
  312. };
  313. };
  314. if (stepRC != SQLITE_DONE)
  315. {
  316. char tB[256];
  317. snprintf(tB, sizeof(tB), "Query step failed (%d): %s", stepRC, sqlite3_errmsg(m_pDBLink));
  318. LOG_ERROR(tB);
  319. };
  320. sqlite3_finalize(query);
  321. if (! m_bPersistentConnection)
  322. {
  323. if (! Disconnect())
  324. {
  325. m_mAccessMutex.unlock();
  326. return RetVal;
  327. };
  328. };
  329. m_mAccessMutex.unlock();
  330. return RetVal;
  331. }
  332. #include "../../components/base/basicFunctionality.h"
  333. CSTR SQLite_Connector::escapeString(CSTR value)
  334. {
  335. // CSTR buffer = NULL;
  336. int v_len = strlen(value);
  337. CSTR result = (CSTR) malloc(3 * v_len);
  338. if (result)
  339. {
  340. /*
  341. int idx = 0;
  342. for( int i = 0; i < v_len; i++ ){
  343. //If character is printable
  344. if(isprint( value[i] ) != 0){
  345. strncat( result+idx, value+i, 1 );
  346. idx++;
  347. }else{
  348. char tB[4];
  349. snprintf( tB, 4, "#%02X", value[i]);
  350. strncat( result+idx, tB, 3 );
  351. idx+=3;
  352. };
  353. };
  354. buffer = ( CSTR ) malloc( idx+1 );
  355. if( buffer ) {
  356. strncat( buffer, result, idx );
  357. buffer[idx] = (char)0;
  358. };
  359. free(result);
  360. */
  361. escape_string(result, 3 * v_len, value, v_len);
  362. };
  363. return result;
  364. }
  365. CSTR SQLite_Connector::escapeBinary(uint8_t * value, uint32_t Size)
  366. {
  367. CSTR buffer = NULL;
  368. if (m_bIsConnected)
  369. {
  370. int v_len = Size;
  371. CSTR result = (CSTR) malloc(3 * v_len + 1);
  372. if (result)
  373. {
  374. int idx = 0;
  375. for (int i = 0; i < v_len; i++)
  376. {
  377. // If character is printable
  378. if (isprint(value[i]) != 0)
  379. {
  380. memcpy(result + idx, value + i, 1);
  381. idx++;
  382. }
  383. else
  384. {
  385. char tB[4];
  386. snprintf(tB, 4, "#%02X", value[i]);
  387. memcpy(result + idx, tB, 3); // result is not zero-terminated: strncat was undefined
  388. idx += 3;
  389. };
  390. };
  391. buffer = (CSTR) malloc(idx + 1);
  392. if (buffer)
  393. {
  394. memcpy(buffer, result, idx);
  395. buffer[idx] = (char) 0;
  396. };
  397. free(result);
  398. };
  399. };
  400. return buffer;
  401. }
  402. uint64_t SQLite_Connector::QueryID(CSTR QS)
  403. {
  404. uint64_t RetVal = 0;
  405. uint64_t tVal = 0;
  406. if (m_bIsConnected)
  407. {
  408. //Assoc * tElement;
  409. //unsigned int num_Fields;
  410. sqlite3_stmt * query = NULL;
  411. //char temp[256];
  412. int QSlen = strlen(QS);
  413. m_mAccessMutex.lock();
  414. if (! m_bPersistentConnection)
  415. {
  416. if (! ReConnect())
  417. {
  418. m_mAccessMutex.unlock();
  419. return RetVal;
  420. };
  421. };
  422. if (SQLITE_OK != sqlite3_prepare_v2(m_pDBLink, QS, QSlen, &query, NULL))
  423. {
  424. m_mAccessMutex.unlock();
  425. return RetVal;
  426. };
  427. #ifdef DEBUG_ALL
  428. LOG(sqlite3_expanded_sql(query));
  429. #endif
  430. tVal = sqlite3_last_insert_rowid(m_pDBLink);
  431. if (SQLITE_DONE != sqlite3_step(query))
  432. {
  433. sqlite3_finalize(query);
  434. m_mAccessMutex.unlock();
  435. return RetVal;
  436. };
  437. RetVal = sqlite3_last_insert_rowid(m_pDBLink);
  438. sqlite3_finalize(query);
  439. if (! m_bPersistentConnection)
  440. {
  441. if (! Disconnect())
  442. {
  443. m_mAccessMutex.unlock();
  444. return RetVal;
  445. };
  446. };
  447. m_mAccessMutex.unlock();
  448. if (RetVal == tVal)
  449. {
  450. RetVal = 0;
  451. }
  452. };
  453. return RetVal;
  454. }
  455. void SQLite_Connector::deleteResult(AAssoc *& R)
  456. {
  457. if (R)
  458. {
  459. while (R->size())
  460. {
  461. delete((Assoc *) R->at(0));
  462. R->erase(R->begin());
  463. };
  464. R->clear();
  465. delete(R);
  466. R = NULL;
  467. };
  468. }