Branch data Line data Source code
1 : : #include "precizer.h"
2 : :
3 : : /**
4 : : * @brief Determine the primary database path and display name
5 : : *
6 : : * In compare mode the primary connection is a disposable in-memory database
7 : : * used to attach the two compared database files. In normal scanning mode, an
8 : : * explicit `--database` path is kept, or the default path is built from the
9 : : * local host name plus the `.db` suffix when no database argument was supplied
10 : : *
11 : : * @return SUCCESS when the database name state was prepared, otherwise FAILURE
12 : : */
13 : 498 : Return db_determine_name(void)
14 : : {
15 : : /* Status returned by this function through provide()
16 : : Default value assumes successful completion */
17 : 498 : Return status = SUCCESS;
18 : :
19 : : /* Interrupt the function smoothly */
20 : : /* Interrupt when Ctrl+C */
21 [ - + ]: 498 : if(global_interrupt_flag == true)
22 : : {
23 : 0 : provide(status);
24 : : }
25 : :
26 [ + + ]: 498 : if(config->compare == true)
27 : : {
28 [ + - ]: 118 : if(config->db_primary_file_path.string_length == 0)
29 : : {
30 : : // In-memory database
31 : 118 : config->db_primary_path_is_memory = true;
32 : :
33 : : // Set primary DB path to SQLite in-memory marker
34 : 118 : run(m_copy_literal(conf(db_primary_file_path),":memory:"));
35 : :
36 : : // Set display-friendly DB name for logs
37 : 118 : run(m_copy_literal(conf(db_file_name),"DisposableDB"));
38 : :
39 [ - + ]: 118 : if(CRITICAL & status)
40 : : {
41 : 0 : slog(ERROR,"Failed to initialize in-memory database names\n");
42 : 0 : call(m_del(conf(db_primary_file_path)));
43 : : }
44 : : } else {
45 : 0 : slog(ERROR,"General failure. config->db_primary_file_path must be blank for this case\n");
46 : 0 : status = FAILURE;
47 : : }
48 : :
49 : : } else {
50 : :
51 [ + + ]: 380 : if(config->db_primary_file_path.string_length == 0)
52 : : {
53 : 18 : call(m_del(conf(db_file_name)));
54 : 18 : config->db_primary_path_is_memory = false;
55 : :
56 : 18 : struct utsname utsname = {0};
57 : :
58 : : // Determine local host name
59 [ - + ]: 18 : if(uname(&utsname) != 0)
60 : : {
61 : 0 : slog(ERROR,"Failed to get hostname\n");
62 : 0 : status = FAILURE;
63 : : } else {
64 : : // Build default DB path from hostname plus ".db" suffix
65 : 18 : run(m_copy_string(conf(db_primary_file_path),utsname.nodename));
66 : 18 : run(m_concat_literal(conf(db_primary_file_path),".db"));
67 : : // Copy the same path to db_file_name
68 : 18 : run(m_copy(conf(db_file_name),conf(db_primary_file_path)));
69 : :
70 [ - + ]: 18 : if(CRITICAL & status)
71 : : {
72 : 0 : slog(ERROR,"Failed to build default database name from hostname\n");
73 : 0 : call(m_del(conf(db_primary_file_path)));
74 : : }
75 : : }
76 : : }
77 : : }
78 : :
79 : : // Log message when database file is specified and confirmed as persistent storage (non-memory database)
80 [ + - ]: 498 : if(TRIUMPH & status)
81 : : {
82 [ + + + + ]: 498 : if(config->db_primary_path_is_memory == false || ((rational_logger_mode & REGULAR) == 0))
83 : : {
84 : 492 : slog(EVERY,"Primary database file name: %s\n",confstr(db_file_name));
85 : : }
86 : :
87 : 498 : slog(TRACE,"Primary database file path: %s\n",confstr(db_primary_file_path));
88 : : }
89 : :
90 : 498 : slog(TRACE,"Finished determining database name\n");
91 : :
92 : 498 : provide(status);
93 : : }
|