LCOV - code coverage report
Current view: top level - src - db_update_the_record_by_id.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 66.7 % 51 34
Test Date: 2026-01-12 05:34:38 Functions: 100.0 % 1 1

            Line data    Source code
       1              : #include "precizer.h"
       2              : 
       3              : /**
       4              :  * @brief Update the record in database
       5              :  * @details Update information about the file, its
       6              :  * metadata and checksum against the database
       7              :  *
       8              :  * @param[in] ID Database record identifier
       9              :  * @param[in] offset File offset value
      10              :  * @param[in] sha512 SHA512 checksum
      11              :  * @param[in] stat File metadata structure
      12              :  * @param[in] mdContext SHA512 context
      13              :  *
      14              :  * @return Return status code:
      15              :  *         - SUCCESS: Record updated successfully
      16              :  *         - FAILURE: Error occurred during update
      17              :  *
      18              :  * @note In dry run mode, the function returns SUCCESS without modifying the database
      19              :  */
      20          148 : Return db_update_the_record_by_id(
      21              :         const sqlite3_int64  *ID,
      22              :         const sqlite3_int64  *offset,
      23              :         const unsigned char  *sha512,
      24              :         const CmpctStat      *stat,
      25              :         const SHA512_Context *mdContext,
      26              :         const bool           *zero_size_file,
      27              :         const bool           *wrong_file_type)
      28              : {
      29              :         /// The status that will be passed to return() before exiting.
      30              :         /// By default, the function worked without errors.
      31          148 :         Return status = SUCCESS;
      32              : 
      33              :         /* Skip database operations in dry run mode --dry-run */
      34          148 :         if(config->dry_run == true)
      35              :         {
      36            4 :                 provide(status);
      37              :         }
      38              : 
      39          144 :         int rc = 0;
      40              : 
      41          144 :         sqlite3_stmt *update_stmt = NULL;
      42              : 
      43          144 :         const char *update_sql = "UPDATE files SET offset = ?1,sha512 = ?2,stat = ?3,mdContext = ?4 WHERE ID = ?5;";
      44              : 
      45              :         /* Create SQL statement. Prepare to write */
      46          144 :         rc = sqlite3_prepare_v2(config->db,update_sql,-1,&update_stmt,NULL);
      47              : 
      48          144 :         if(SQLITE_OK != rc)
      49              :         {
      50            0 :                 log_sqlite_error(config->db,rc,NULL,"Failed to prepare update statement %s",update_sql);
      51            0 :                 status = FAILURE;
      52              :         }
      53              : 
      54              :         /* Bind offset value */
      55          144 :         if(SUCCESS == status)
      56              :         {
      57          144 :                 if(*offset == 0)
      58              :                 {
      59          144 :                         rc = sqlite3_bind_null(update_stmt,1);
      60              :                 } else {
      61            0 :                         rc = sqlite3_bind_int64(update_stmt,1,*offset);
      62              :                 }
      63              : 
      64          144 :                 if(SQLITE_OK != rc)
      65              :                 {
      66            0 :                         log_sqlite_error(config->db,rc,NULL,"Failed to bind offset value in update");
      67            0 :                         status = FAILURE;
      68              :                 }
      69              :         }
      70              : 
      71          144 :         if(SUCCESS == status)
      72              :         {
      73          144 :                 rc = sqlite3_bind_int64(update_stmt,5,*ID);
      74              : 
      75          144 :                 if(SQLITE_OK != rc)
      76              :                 {
      77            0 :                         log_sqlite_error(config->db,rc,NULL,"Error binding value in update");
      78            0 :                         status = FAILURE;
      79              :                 }
      80              :         }
      81              : 
      82              :         /* Bind SHA512 checksum */
      83          144 :         if(SUCCESS == status)
      84              :         {
      85          144 :                 if(*offset == 0 && *zero_size_file == false && *wrong_file_type == false)
      86              :                 {
      87          144 :                         rc = sqlite3_bind_blob(update_stmt,2,sha512,SHA512_DIGEST_LENGTH,NULL);
      88              :                 } else {
      89            0 :                         rc = sqlite3_bind_null(update_stmt,2);
      90              :                 }
      91              : 
      92          144 :                 if(SQLITE_OK != rc)
      93              :                 {
      94            0 :                         log_sqlite_error(config->db,rc,NULL,"Failed to bind sha512 hash value in update");
      95            0 :                         status = FAILURE;
      96              :                 }
      97              :         }
      98              : 
      99              :         /* Copy and bind file metadata */
     100          144 :         if(SUCCESS == status)
     101              :         {
     102          144 :                 rc = sqlite3_bind_blob(update_stmt,3,stat,sizeof(CmpctStat),NULL);
     103              : 
     104          144 :                 if(SQLITE_OK != rc)
     105              :                 {
     106            0 :                         log_sqlite_error(config->db,rc,NULL,"Error binding value in update");
     107            0 :                         status = FAILURE;
     108              :                 }
     109              :         }
     110              : 
     111              :         /* Bind SHA512 context */
     112          144 :         if(SUCCESS == status)
     113              :         {
     114          144 :                 if(*offset == 0)
     115              :                 {
     116          144 :                         rc = sqlite3_bind_null(update_stmt,4);
     117              :                 } else {
     118            0 :                         rc = sqlite3_bind_blob(update_stmt,4,mdContext,sizeof(SHA512_Context),NULL);
     119              :                 }
     120              : 
     121          144 :                 if(SQLITE_OK != rc)
     122              :                 {
     123            0 :                         log_sqlite_error(config->db,rc,NULL,"Error binding value in update");
     124            0 :                         status = FAILURE;
     125              :                 }
     126              :         }
     127              : 
     128              :         /* Execute prepared statement */
     129          144 :         if(SUCCESS == status)
     130              :         {
     131          144 :                 rc = sqlite3_step(update_stmt);
     132              : 
     133          144 :                 if(SQLITE_DONE != rc)
     134              :                 {
     135            0 :                         log_sqlite_error(config->db,rc,NULL,"Update statement failed");
     136            0 :                         status = FAILURE;
     137              :                 }
     138              :         }
     139              : 
     140          144 :         if(SUCCESS == status)
     141              :         {
     142              :                 /* Reflect changes in global */
     143          144 :                 config->db_primary_file_modified = true;
     144              :         }
     145              : 
     146          144 :         sqlite3_finalize(update_stmt);
     147              : 
     148          144 :         provide(status);
     149              : }
        

Generated by: LCOV version 2.0-1