Branch data Line data Source code
1 : : #include "precizer.h"
2 : :
3 : : /**
4 : : * @brief Validates the existence of the primary database file
5 : : * @details Checks if the primary database file exists and is accessible. Updates the
6 : : * global config->db_primary_file_exists flag based on the check result.
7 : : * When the file exists, its metadata is stored in config->db_file_stat in a
8 : : * single stat() call to avoid a TOCTOU race between existence check and metadata
9 : : * retrieval. The function attempts to access the file using the path stored
10 : : * in the global configuration config->db_primary_file_path
11 : : *
12 : : * @return Return status code indicating the operation result:
13 : : * - SUCCESS: Check completed successfully
14 : : * - FAILURE: File check operation failed
15 : : *
16 : : * @note This function only verifies file existence and basic accessibility.
17 : : * It does not validate file format or content integrity.
18 : : *
19 : : * @see config->db_primary_file_exists
20 : : * @see config->db_primary_file_path
21 : : * @see config->db_file_stat
22 : : */
23 : 403 : Return db_primary_file_validate_existence(void)
24 : : {
25 : : /* Status returned by this function through provide()
26 : : Default value assumes successful completion */
27 : 403 : Return status = SUCCESS;
28 : :
29 : : /* Interrupt the function smoothly */
30 : : /* Interrupt when Ctrl+C */
31 [ - + ]: 403 : if(global_interrupt_flag == true)
32 : : {
33 : 0 : provide(status);
34 : : }
35 : :
36 : : // Primary DB file exists or not
37 : 403 : config->db_primary_file_exists = false;
38 : 403 : const char *db_primary_file_path = confstr(db_primary_file_path);
39 : :
40 : : // Path is initialized in db_determine_name() and must contain at least one
41 : : // real character beyond the trailing '\0' terminator
42 [ - + ]: 403 : if(conf(db_primary_file_path)->length <= 1)
43 : : {
44 : 0 : status = FAILURE;
45 : : }
46 : :
47 [ + - ]: 403 : if(SUCCESS == status)
48 : : {
49 : 403 : char *db_file_full_path = strdup(db_primary_file_path);
50 : :
51 [ - + ]: 403 : if(db_file_full_path == NULL)
52 : : {
53 : 0 : report("Memory allocation failed for database path copy");
54 : 0 : provide(FAILURE);
55 : : }
56 : :
57 : 403 : char *db_file_dir = dirname(db_file_full_path);
58 : :
59 [ + + ]: 403 : if(NOT_FOUND == file_availability(db_file_dir,NULL,SHOULD_BE_A_DIRECTORY))
60 : : {
61 : 2 : slog(ERROR,"Unable to create database file. Directory %s not found\n",db_file_dir);
62 : 2 : status = FAILURE;
63 : : }
64 : :
65 : 403 : free(db_file_full_path);
66 : :
67 [ + + ]: 403 : if(SUCCESS == status)
68 : : {
69 [ + + ]: 401 : if(EXISTS == file_availability(db_primary_file_path,&config->db_file_stat,SHOULD_BE_A_FILE))
70 : : {
71 : 120 : config->db_primary_file_exists = true;
72 : : } else {
73 : 281 : config->db_primary_file_exists = false;
74 : : }
75 : : }
76 : : }
77 : :
78 : 403 : provide(status);
79 : : }
|