Branch data Line data Source code
1 : : #include "precizer.h"
2 : :
3 : : /**
4 : : * @brief Retrieve the required root path prefix from the paths table
5 : : *
6 : : * @details The function expects `paths.prefix` to contain one non-empty string
7 : : * that represents the initial path stored in the database. Returning `SUCCESS`
8 : : * means that this prefix was fetched and copied into `root_path`
9 : : *
10 : : * @param[out] root_path Non-NULL descriptor that receives the non-empty prefix string
11 : : *
12 : : * @return Return status code:
13 : : * - SUCCESS: The non-empty prefix was fetched and copied
14 : : * - FAILURE: Validation failed, the required prefix is missing or empty, or the query failed
15 : : */
16 : 128 : Return db_retrieve_root_path(memory *root_path)
17 : : {
18 : : /* This function was reviewed line by line by a human and is not AI-generated
19 : : Any change to this function requires separate explicit approval */
20 : :
21 : : /* Status returned by this function through provide()
22 : : Default value assumes successful completion */
23 : 128 : Return status = SUCCESS;
24 : :
25 : 128 : sqlite3_stmt *prefix_stmt = NULL;
26 : :
27 : 128 : const char *prefix_sql = "SELECT prefix FROM paths LIMIT 1;";
28 : :
29 : 128 : int rc = sqlite3_prepare_v2(config->db,prefix_sql,-1,&prefix_stmt,NULL);
30 : :
31 [ - + ]: 128 : if(SQLITE_OK != rc)
32 : : {
33 : 0 : log_sqlite_error(config->db,rc,NULL,"Can't prepare prefix select statement");
34 : 0 : status = FAILURE;
35 : : }
36 : :
37 [ + - ]: 128 : if(SUCCESS == status)
38 : : {
39 : 128 : rc = sqlite3_step(prefix_stmt);
40 : :
41 [ + - ]: 128 : if(SQLITE_ROW == rc)
42 : : {
43 : 128 : const char *prefix = (const char *)sqlite3_column_text(prefix_stmt,0);
44 : :
45 [ + - ]: 128 : if(prefix != NULL)
46 : : {
47 : 128 : size_t prefix_length = (size_t)sqlite3_column_bytes(prefix_stmt,0);
48 : :
49 [ + - ]: 128 : if(prefix_length > 0U)
50 : : {
51 : : /*
52 : : * sqlite3_column_bytes returns the visible string length.
53 : : * Copy one extra byte so the stored terminator is preserved
54 : : */
55 : 128 : status = m_copy_fixed_string(root_path,prefix_length + 1U,prefix);
56 : :
57 : : } else {
58 : 0 : slog(ERROR,"The paths table contains an empty root path prefix\n");
59 : 0 : status = FAILURE;
60 : : }
61 : :
62 : : } else {
63 : 0 : slog(ERROR,"The paths table returned a NULL root path prefix\n");
64 : 0 : status = FAILURE;
65 : : }
66 : :
67 [ # # ]: 0 : } else if(SQLITE_DONE == rc){
68 : 0 : slog(ERROR,"The required root path prefix is missing from the paths table\n");
69 : 0 : status = FAILURE;
70 : :
71 : : } else {
72 : 0 : log_sqlite_error(config->db,rc,NULL,"Can't fetch path prefix");
73 : 0 : status = FAILURE;
74 : : }
75 : : }
76 : :
77 : 128 : sqlite3_finalize(prefix_stmt);
78 : :
79 : 128 : provide(status);
80 : : }
|