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 : * The function attempts to access the file using the path stored
8 : * in the global configuration config->db_primary_file_path
9 : *
10 : * @return Return status code indicating the operation result:
11 : * - SUCCESS: Check completed successfully
12 : * - FAILURE: File check operation failed
13 : *
14 : * @note This function only verifies file existence and basic accessibility.
15 : * It does not validate file format or content integrity.
16 : *
17 : * @see config->db_primary_file_exists
18 : * @see config->db_primary_file_path
19 : */
20 202 : Return db_primary_file_validate_existence(void)
21 : {
22 : /** @var Return status
23 : * @brief The status that will be passed to return() before exiting
24 : * @details By default, the function worked without errors
25 : */
26 202 : Return status = SUCCESS;
27 :
28 : /* Interrupt the function smoothly */
29 : /* Interrupt when Ctrl+C */
30 202 : if(global_interrupt_flag == true)
31 : {
32 0 : provide(status);
33 : }
34 :
35 : // Primary DB file exists or not
36 202 : config->db_primary_file_exists = false;
37 :
38 : // The variable is defined in db_determine_name()
39 : // and must not be empty
40 202 : if(config->db_primary_file_path == NULL)
41 : {
42 0 : status = FAILURE;
43 : }
44 :
45 202 : if(SUCCESS == status)
46 : {
47 202 : char *db_file_full_path = strdup(config->db_primary_file_path);
48 202 : char *db_file_dir = dirname(db_file_full_path);
49 :
50 202 : if(NOT_FOUND == file_availability(db_file_dir,SHOULD_BE_A_DIRECTORY))
51 : {
52 2 : slog(ERROR,"Unable to create database file. Directory %s not found\n",db_file_dir);
53 2 : status = FAILURE;
54 : }
55 :
56 202 : free(db_file_full_path);
57 :
58 202 : if(SUCCESS == status)
59 : {
60 200 : if(EXISTS == file_availability(config->db_primary_file_path,SHOULD_BE_A_FILE))
61 : {
62 82 : config->db_primary_file_exists = true;
63 :
64 82 : int rc = stat(config->db_primary_file_path,&config->db_file_stat);
65 :
66 82 : if(rc < 0)
67 : {
68 0 : report("Stat of %s failed with error code: %d",config->db_primary_file_path,rc);
69 0 : status = FAILURE;
70 : }
71 :
72 : } else {
73 118 : config->db_primary_file_exists = false;
74 : }
75 : }
76 : }
77 :
78 202 : provide(status);
79 : }
|