Branch data Line data Source code
1 : : #include "precizer.h"
2 : :
3 : : /**
4 : : *
5 : : * The VACUUM command rebuilds the database file,
6 : : * repacking it into a minimal amount of disk space.
7 : : *
8 : : */
9 : 185 : Return db_vacuum(const char *db_file_path)
10 : : {
11 : : /* Status returned by this function through provide()
12 : : Default value assumes successful completion */
13 : 185 : Return status = SUCCESS;
14 : :
15 : 185 : sqlite3 *db = NULL;
16 : 185 : char *err_msg = NULL;
17 : 185 : bool db_is_primary = false;
18 : 185 : bool db_file_modified = false;
19 : :
20 : : /* Validate input parameters */
21 [ - + ]: 185 : if(db_file_path == NULL)
22 : : {
23 : 0 : slog(ERROR,"Invalid input parameters: db_file_path\n");
24 : 0 : provide(FAILURE);
25 : : }
26 : :
27 [ - + ]: 185 : if(config->dry_run == true)
28 : : {
29 : 0 : slog(TRACE,"Dry Run mode is enabled. The database doesn't require vacuuming\n");
30 : 0 : provide(status);
31 : : }
32 : :
33 [ + + ]: 185 : if(strcmp(db_file_path,confstr(db_primary_file_path)) == 0)
34 : : {
35 : 173 : db_is_primary = true;
36 : : }
37 : :
38 : : /* Open database in safe mode */
39 : 185 : int rc = sqlite3_open_v2(db_file_path,&db,SQLITE_OPEN_READWRITE,NULL);
40 : :
41 [ - + ]: 185 : if(SQLITE_OK != rc)
42 : : {
43 : 0 : log_sqlite_error(db,rc,NULL,"Failed to open database");
44 : 0 : status = FAILURE;
45 : : }
46 : :
47 [ + - ]: 185 : if(SUCCESS == status)
48 : : {
49 : : /* Create SQL statement */
50 : 185 : const char *sql =
51 : : "PRAGMA analyze;"
52 : : "PRAGMA optimize;"
53 : : "VACUUM;"
54 : : "PRAGMA analyze;"
55 : : "PRAGMA optimize;";
56 : :
57 [ + + ]: 185 : if(db_is_primary == true)
58 : : {
59 : 173 : slog(EVERY,"Start vacuuming the primary database…\n");
60 : : } else {
61 : 12 : slog(EVERY,"Start vacuuming…\n");
62 : : }
63 : :
64 : : /* Execute SQL statement */
65 : 185 : rc = sqlite3_exec(db,sql,NULL,NULL,&err_msg);
66 : :
67 [ - + ]: 185 : if(SQLITE_OK != rc)
68 : : {
69 : 0 : log_sqlite_error(db,rc,err_msg,"Can't execute vacuum");
70 : 0 : status = FAILURE;
71 : : } else {
72 : 185 : db_file_modified = true;
73 : :
74 [ + + ]: 185 : if(db_is_primary == true)
75 : : {
76 : 173 : slog(EVERY,"The primary database has been vacuumed\n");
77 : : } else {
78 : 12 : slog(EVERY,"The database has been vacuumed\n");
79 : : }
80 : : }
81 : : }
82 : :
83 [ + - ]: 185 : if(SUCCESS == status)
84 : : {
85 : : /**
86 : : *
87 : : * If the database being updated is the primary one, adjust the global
88 : : * flag indicating that the main database file has been
89 : : * modified (this will consequently update the file's ctime, mtime, and size)
90 : : */
91 [ + + ]: 185 : if(db_is_primary == true)
92 : : {
93 : : /* Changes have been made to the database. Update
94 : : this in the global variable value. */
95 : 173 : config->db_primary_file_modified = true;
96 : : }
97 : :
98 : : }
99 : :
100 : : /* Cleanup */
101 [ - + - + ]: 185 : call(db_close(db,&db_file_modified));
102 : :
103 : 185 : provide(status);
104 : : }
|