Branch data 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] file Per-file state object. Uses db->ID when selecting the row
9 : : * and uses checksum_offset, sha512, stat, mdContext,
10 : : * zero_size_file, and wrong_file_type when binding the updated
11 : : * row values
12 : : *
13 : : * @return Return status code:
14 : : * - SUCCESS: Record updated successfully
15 : : * - FAILURE: Error occurred during update
16 : : *
17 : : * @note In dry run mode, the function returns SUCCESS without modifying the database
18 : : */
19 : 200 : Return db_update_the_record_by_id(const File *file)
20 : : {
21 : : /* Status returned by this function through provide()
22 : : Default value assumes successful completion */
23 : 200 : Return status = SUCCESS;
24 : :
25 : : /* Skip database operations in dry run mode --dry-run */
26 [ + + ]: 200 : if(config->dry_run == true)
27 : : {
28 : 4 : provide(status);
29 : : }
30 : :
31 : 196 : int rc = 0;
32 : :
33 : 196 : sqlite3_stmt *update_stmt = NULL;
34 : :
35 : 196 : const char *update_sql = "UPDATE files SET offset = ?1,sha512 = ?2,stat = ?3,mdContext = ?4 WHERE ID = ?5;";
36 : :
37 : : /* Create SQL statement. Prepare to write */
38 : 196 : rc = sqlite3_prepare_v2(config->db,update_sql,-1,&update_stmt,NULL);
39 : :
40 [ - + ]: 196 : if(SQLITE_OK != rc)
41 : : {
42 : 0 : log_sqlite_error(config->db,rc,NULL,"Failed to prepare update statement %s",update_sql);
43 : 0 : status = FAILURE;
44 : : }
45 : :
46 : : /* Bind offset value */
47 [ + - ]: 196 : if(SUCCESS == status)
48 : : {
49 [ + - ]: 196 : if(file->checksum_offset == 0)
50 : : {
51 : 196 : rc = sqlite3_bind_null(update_stmt,1);
52 : : } else {
53 : 0 : rc = sqlite3_bind_int64(update_stmt,1,file->checksum_offset);
54 : : }
55 : :
56 [ - + ]: 196 : if(SQLITE_OK != rc)
57 : : {
58 : 0 : log_sqlite_error(config->db,rc,NULL,"Failed to bind offset value in update");
59 : 0 : status = FAILURE;
60 : : }
61 : : }
62 : :
63 [ + - ]: 196 : if(SUCCESS == status)
64 : : {
65 : 196 : rc = sqlite3_bind_int64(update_stmt,5,file->db->ID);
66 : :
67 [ - + ]: 196 : if(SQLITE_OK != rc)
68 : : {
69 : 0 : log_sqlite_error(config->db,rc,NULL,"Error binding value in update");
70 : 0 : status = FAILURE;
71 : : }
72 : : }
73 : :
74 : : /* Bind SHA512 checksum */
75 [ + - ]: 196 : if(SUCCESS == status)
76 : : {
77 [ + - + + : 196 : if(file->checksum_offset == 0 && file->zero_size_file == false && file->wrong_file_type == false)
+ - ]
78 : : {
79 : 194 : rc = sqlite3_bind_blob(update_stmt,2,file->sha512,SHA512_DIGEST_LENGTH,NULL);
80 : : } else {
81 : 2 : rc = sqlite3_bind_null(update_stmt,2);
82 : : }
83 : :
84 [ - + ]: 196 : if(SQLITE_OK != rc)
85 : : {
86 : 0 : log_sqlite_error(config->db,rc,NULL,"Failed to bind sha512 hash value in update");
87 : 0 : status = FAILURE;
88 : : }
89 : : }
90 : :
91 : : /* Copy and bind file metadata */
92 [ + - ]: 196 : if(SUCCESS == status)
93 : : {
94 : 196 : rc = sqlite3_bind_blob(update_stmt,3,&file->stat,sizeof(CmpctStat),NULL);
95 : :
96 [ - + ]: 196 : if(SQLITE_OK != rc)
97 : : {
98 : 0 : log_sqlite_error(config->db,rc,NULL,"Error binding value in update");
99 : 0 : status = FAILURE;
100 : : }
101 : : }
102 : :
103 : : /* Bind SHA512 context */
104 [ + - ]: 196 : if(SUCCESS == status)
105 : : {
106 [ + - ]: 196 : if(file->checksum_offset == 0)
107 : : {
108 : 196 : rc = sqlite3_bind_null(update_stmt,4);
109 : : } else {
110 : 0 : rc = sqlite3_bind_blob(update_stmt,4,&file->mdContext,sizeof(SHA512_Context),NULL);
111 : : }
112 : :
113 [ - + ]: 196 : if(SQLITE_OK != rc)
114 : : {
115 : 0 : log_sqlite_error(config->db,rc,NULL,"Error binding value in update");
116 : 0 : status = FAILURE;
117 : : }
118 : : }
119 : :
120 : : /* Execute prepared statement */
121 [ + - ]: 196 : if(SUCCESS == status)
122 : : {
123 : 196 : rc = sqlite3_step(update_stmt);
124 : :
125 [ - + ]: 196 : if(SQLITE_DONE != rc)
126 : : {
127 : 0 : log_sqlite_error(config->db,rc,NULL,"Update statement failed");
128 : 0 : status = FAILURE;
129 : : }
130 : : }
131 : :
132 [ + - ]: 196 : if(SUCCESS == status)
133 : : {
134 : : /* Reflect changes in global */
135 : 196 : config->db_primary_file_modified = true;
136 : : }
137 : :
138 : 196 : sqlite3_finalize(update_stmt);
139 : :
140 : 196 : provide(status);
141 : : }
|