Branch data Line data Source code
1 : : #include "precizer.h"
2 : :
3 : : /**
4 : : * @brief Read one file row from the database into file->db
5 : : *
6 : : * Loads the saved row for @p relative_path into the DBrow attached to @p file.
7 : : * If the path is absent from the database, the attached row stays zeroed and
8 : : * relative_path_was_in_db_before_processing remains false
9 : : *
10 : : * @param[in,out] file Per-file state whose attached DB row receives the loaded values
11 : : * @param[in] relative_path Relative path descriptor looked up in the files table
12 : : * @return SUCCESS on a completed lookup, otherwise FAILURE
13 : : */
14 : 4198 : Return db_read_file_data_from(
15 : : File *file,
16 : : #if 0 // Disabled multi-root path index implementation
17 : : const sqlite3_int64 *path_prefix_index,
18 : : #endif
19 : : const memory *relative_path)
20 : : {
21 : : /* Status returned by this function through provide()
22 : : Default value assumes successful completion */
23 : 4198 : Return status = SUCCESS;
24 : :
25 : : /* Read from the database */
26 : 4198 : sqlite3_stmt *select_stmt = NULL;
27 : : int rc;
28 : : // Convenience alias for the attached DB row that receives the loaded values
29 : 4198 : DBrow *dbrow = file->db;
30 : :
31 : : /* Create SQL statement */
32 : : #if 0 // Disabled multi-root path index implementation
33 : : const char *select_sql = "SELECT ID,offset,stat,mdContext,sha512 FROM files WHERE path_prefix_index = ?1 and relative_path = ?2;";
34 : : #else
35 : 4198 : const char *select_sql = "SELECT ID,offset,stat,mdContext,sha512 FROM files WHERE relative_path = ?1;";
36 : : #endif
37 : 4198 : rc = sqlite3_prepare_v2(config->db,select_sql,-1,&select_stmt,NULL);
38 : :
39 [ - + ]: 4198 : if(SQLITE_OK != rc)
40 : : {
41 : 0 : log_sqlite_error(config->db,rc,NULL,"Can't prepare select statement %s",select_sql);
42 : 0 : status = FAILURE;
43 : : }
44 : :
45 : : #if 0 // Disabled multi-root path index implementation
46 : : rc = sqlite3_bind_int64(select_stmt,1,*path_prefix_index);
47 : :
48 : : if(SQLITE_OK != rc)
49 : : {
50 : : log_sqlite_error(config->db,rc,NULL,"Error binding value in select");
51 : : status = FAILURE;
52 : : }
53 : : #endif
54 : : size_t relative_path_length;
55 : :
56 [ + - ]: 4198 : if(SUCCESS & status)
57 : : {
58 : 4198 : status = m_string_length(relative_path,&relative_path_length);
59 : : }
60 : :
61 [ + - ]: 4198 : if(SUCCESS & status)
62 : : {
63 : 4198 : rc = sqlite3_bind_text(select_stmt,1,m_text(relative_path),(int)relative_path_length,NULL);
64 : :
65 [ - + ]: 4198 : if(SQLITE_OK != rc)
66 : : {
67 : 0 : log_sqlite_error(config->db,rc,NULL,"Error binding value in select");
68 : 0 : status = FAILURE;
69 : : }
70 : : }
71 : :
72 [ + - ]: 4198 : if(SUCCESS & status)
73 : : {
74 [ + + ]: 5853 : while(SQLITE_ROW == (rc = sqlite3_step(select_stmt)))
75 : : {
76 : 1655 : dbrow->ID = sqlite3_column_int64(select_stmt,0);
77 : 1655 : dbrow->saved_offset = sqlite3_column_int64(select_stmt,1);
78 : 1655 : const CmpctStat *get_stat = sqlite3_column_blob(select_stmt,2);
79 : :
80 [ + - ]: 1655 : if(get_stat != NULL)
81 : : {
82 : 1655 : memcpy(&dbrow->saved_stat,get_stat,sizeof(CmpctStat));
83 : : }
84 : 1655 : const SHA512_Context *get_mdContext = sqlite3_column_blob(select_stmt,3);
85 : :
86 [ + + ]: 1655 : if(get_mdContext != NULL)
87 : : {
88 : 7 : memcpy(&dbrow->saved_mdContext,get_mdContext,sizeof(SHA512_Context));
89 : : }
90 : :
91 : 1655 : const unsigned char *get_sha512 = sqlite3_column_blob(select_stmt,4);
92 : :
93 [ + + ]: 1655 : if(get_sha512 != NULL)
94 : : {
95 : 1642 : memcpy(&dbrow->sha512,get_sha512,SHA512_DIGEST_LENGTH);
96 : : }
97 : :
98 : 1655 : dbrow->relative_path_was_in_db_before_processing = true;
99 : : }
100 : :
101 [ - + ]: 4198 : if(SQLITE_DONE != rc)
102 : : {
103 : 0 : log_sqlite_error(config->db,rc,NULL,"Select statement didn't finish with DONE");
104 : 0 : status = FAILURE;
105 : : }
106 : : }
107 : 4198 : rc = sqlite3_finalize(select_stmt);
108 : :
109 [ + - - + ]: 4198 : if(SUCCESS & status && SQLITE_OK != rc)
110 : : {
111 : 0 : log_sqlite_error(config->db,rc,NULL,"Failed to finalize select statement");
112 : 0 : status = FAILURE;
113 : : }
114 : :
115 : 4198 : provide(status);
116 : : }
|