LCOV - code coverage report
Current view: top level - src - db_save_file_record.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 89.5 % 19 17
Test Date: 2026-07-12 01:01:34 Functions: 100.0 % 1 1
Branches: 70.8 % 24 17

             Branch data     Line data    Source code
       1                 :             : #include "precizer.h"
       2                 :             : 
       3                 :             : /**
       4                 :             :  * @brief Save the current per-file state into the database
       5                 :             :  *
       6                 :             :  * This function is the common save point for one file row. It writes the file's
       7                 :             :  * current metadata, SHA512 digest or partial SHA512 resume state, and row status
       8                 :             :  * to SQLite. It chooses whether to insert a new row or update an existing row by
       9                 :             :  * looking at @p path_known
      10                 :             :  *
      11                 :             :  * The helper is used by both final file saves and silent SHA512 checkpoints. A
      12                 :             :  * checkpoint may be the first time a new file is written to SQLite during the
      13                 :             :  * current traversal. In that case the helper inserts the row, stores the new row
      14                 :             :  * ID in @p file, and updates @p path_known so later saves for the same file
      15                 :             :  * update that row instead of inserting a duplicate. When @p path_known is already
      16                 :             :  * true, the helper updates the existing row directly
      17                 :             :  *
      18                 :             :  * @p mark_visible_change controls user-facing reporting. Final saves pass true
      19                 :             :  * so show_file() can say whether the file was inserted or updated. Silent
      20                 :             :  * checkpoints pass false because they are only recovery points for interrupted
      21                 :             :  * hashing and should not appear as ordinary traversal changes
      22                 :             :  *
      23                 :             :  * @param[in] relative_path File path relative to the traversal root
      24                 :             :  * @param[in,out] file Per-file state to persist and annotate
      25                 :             :  * @param[in,out] path_known True when the row currently exists in SQLite
      26                 :             :  * @param[in] mark_visible_change True for the final save reported to the user
      27                 :             :  * @return SUCCESS when the row was saved, otherwise FAILURE
      28                 :             :  */
      29                 :        2682 : Return db_save_file_record(
      30                 :             :         const memory *relative_path,
      31                 :             :         File         *file,
      32                 :             :         bool         *path_known,
      33                 :             :         const bool   mark_visible_change)
      34                 :             : {
      35                 :             :         /* Status returned by this function through provide()
      36                 :             :            Default value assumes successful completion */
      37                 :        2682 :         Return status = SUCCESS;
      38                 :             : 
      39   [ +  -  +  -  :        2682 :         if(relative_path == NULL || file == NULL || file->db == NULL || path_known == NULL)
             +  -  -  + ]
      40                 :             :         {
      41                 :           0 :                 provide(FAILURE);
      42                 :             :         }
      43                 :             : 
      44   [ +  +  -  + ]:        2682 :         if(config->dry_run == true && mark_visible_change == false)
      45                 :             :         {
      46                 :           0 :                 provide(status);
      47                 :             :         }
      48                 :             : 
      49                 :             :         /*
      50                 :             :          * Remember whether the row existed before this file started processing.
      51                 :             :          * A silent checkpoint can insert a new row before the final save, but the
      52                 :             :          * user-facing result should still say "inserted", not "updated"
      53                 :             :          */
      54                 :        2682 :         const bool row_existed_before_processing = file->db->relative_path_was_in_db_before_processing == true;
      55                 :             : 
      56                 :             :         /*
      57                 :             :          * Save into the row that is known for the current traversal state.
      58                 :             :          * Existing paths are updated by ID. New paths are inserted once, then their
      59                 :             :          * generated ID and path_known flag are kept so later checkpoints or the final
      60                 :             :          * save update the same row instead of inserting another one
      61                 :             :          */
      62         [ +  + ]:        2682 :         if(*path_known == true)
      63                 :             :         {
      64                 :         206 :                 status = db_update_the_record_by_id(file);
      65                 :             : 
      66                 :             :         } else {
      67                 :        2476 :                 status = db_insert_the_record(relative_path,file);
      68                 :             : 
      69         [ +  - ]:        2476 :                 if(TRIUMPH & status)
      70                 :             :                 {
      71         [ +  + ]:        2476 :                         if(config->dry_run == false)
      72                 :             :                         {
      73                 :        2396 :                                 file->db->ID = sqlite3_last_insert_rowid(config->db);
      74                 :        2396 :                                 *path_known = true;
      75                 :             :                         }
      76                 :             :                 }
      77                 :             :         }
      78                 :             : 
      79                 :             :         /*
      80                 :             :          * Mark only visible saves for the traversal report.
      81                 :             :          * Silent checkpoints may write the same row earlier, but they must not make
      82                 :             :          * the file look updated to the user. The original row state decides whether
      83                 :             :          * the final visible result is an insertion or an update
      84                 :             :          */
      85   [ +  -  +  + ]:        2682 :         if((TRIUMPH & status) && mark_visible_change == true)
      86                 :             :         {
      87         [ +  + ]:        2678 :                 if(row_existed_before_processing == true)
      88                 :             :                 {
      89                 :         204 :                         file->db_record_updated = true;
      90                 :             : 
      91                 :             :                 } else {
      92                 :        2474 :                         file->new_db_record_inserted = true;
      93                 :             :                 }
      94                 :             :         }
      95                 :             : 
      96                 :        2682 :         provide(status);
      97                 :             : }
        

Generated by: LCOV version 2.0-1