Branch data Line data Source code
1 : : #include "sute.h"
2 : :
3 : : /**
4 : : * @brief Verify that DB relative_path set matches expected list exactly
5 : : *
6 : : * @param[in] db_filename DB file name relative to TMPDIR
7 : : * @param[in] expected_paths Sorted expected relative_path values
8 : : * @param[in] expected_count Number of expected paths
9 : : *
10 : : * @return Return status code:
11 : : * - SUCCESS: DB rows match expected paths exactly
12 : : * - FAILURE: Mismatch or DB access error
13 : : */
14 : 10 : Return db_paths_match(
15 : : const char *db_filename,
16 : : const char *const *expected_paths,
17 : : const int expected_count)
18 : : {
19 : : /* Status returned by this function through provide()
20 : : Default value assumes successful completion */
21 : 10 : Return status = SUCCESS;
22 : 10 : sqlite3 *db = NULL;
23 : 10 : sqlite3_stmt *stmt = NULL;
24 : 10 : const char *sql = "SELECT relative_path FROM files ORDER BY relative_path ASC;";
25 : 10 : create(char,db_path);
26 : :
27 [ + - + - : 10 : if(SUCCESS == status && (db_filename == NULL || expected_paths == NULL || expected_count < 0))
+ - - + ]
28 : : {
29 : 0 : status = FAILURE;
30 : : }
31 : :
32 [ + - ]: 10 : if(SUCCESS == status)
33 : : {
34 : 10 : status = construct_path(db_filename,db_path);
35 : : }
36 : :
37 [ + - - + ]: 10 : if(SUCCESS == status && SQLITE_OK != sqlite3_open_v2(getcstring(db_path),&db,SQLITE_OPEN_READONLY,NULL))
38 : : {
39 : 0 : status = FAILURE;
40 : : }
41 : :
42 [ + - - + ]: 10 : if(SUCCESS == status && SQLITE_OK != sqlite3_prepare_v2(db,sql,-1,&stmt,NULL))
43 : : {
44 : 0 : status = FAILURE;
45 : : }
46 : :
47 : 10 : int index = 0;
48 : :
49 [ + - ]: 10 : if(SUCCESS == status)
50 : : {
51 : 10 : int rc = sqlite3_step(stmt);
52 : :
53 [ + + ]: 68 : while(rc == SQLITE_ROW)
54 : : {
55 [ - + ]: 58 : if(index >= expected_count)
56 : : {
57 : 0 : status = FAILURE;
58 : 0 : break;
59 : : }
60 : :
61 : 58 : const unsigned char *db_path_text = sqlite3_column_text(stmt,0);
62 : :
63 [ + - - + ]: 58 : if(db_path_text == NULL || strcmp((const char *)db_path_text,expected_paths[index]) != 0)
64 : : {
65 : 0 : status = FAILURE;
66 : 0 : break;
67 : : }
68 : :
69 : 58 : index++;
70 : 58 : rc = sqlite3_step(stmt);
71 : : }
72 : :
73 [ + - - + ]: 10 : if(SUCCESS == status && rc != SQLITE_DONE)
74 : : {
75 : 0 : status = FAILURE;
76 : : }
77 : : }
78 : :
79 [ + - - + ]: 10 : if(SUCCESS == status && index != expected_count)
80 : : {
81 : 0 : status = FAILURE;
82 : : }
83 : :
84 [ + - ]: 10 : if(stmt != NULL)
85 : : {
86 : 10 : (void)sqlite3_finalize(stmt);
87 : : }
88 : :
89 [ + - ]: 10 : if(db != NULL)
90 : : {
91 : 10 : (void)sqlite3_close(db);
92 : : }
93 : :
94 : 10 : del(db_path);
95 : :
96 : 10 : return(status);
97 : : }
|