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 122 : Return db_vacuum(const char *db_file_path)
10 : {
11 : /// The status that will be passed to return() before exiting.
12 : /// By default, the function worked without errors.
13 122 : Return status = SUCCESS;
14 :
15 122 : sqlite3 *db = NULL;
16 122 : char *err_msg = NULL;
17 122 : bool db_is_primary = false;
18 122 : bool db_file_modified = false;
19 :
20 : /* Validate input parameters */
21 122 : if(db_file_path == NULL)
22 : {
23 0 : slog(ERROR,"Invalid input parameters: db_file_path\n");
24 0 : provide(FAILURE);
25 : }
26 :
27 122 : 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 122 : if(strcmp(db_file_path,config->db_primary_file_path) == 0)
34 : {
35 116 : db_is_primary = true;
36 : }
37 :
38 : /* Open database in safe mode */
39 122 : int rc = sqlite3_open_v2(db_file_path,&db,SQLITE_OPEN_READWRITE,NULL);
40 :
41 122 : if(SQLITE_OK != rc)
42 : {
43 0 : log_sqlite_error(db,rc,NULL,"Failed to open database");
44 0 : status = FAILURE;
45 : }
46 :
47 122 : if(SUCCESS == status)
48 : {
49 : /* Create SQL statement */
50 122 : const char *sql =
51 : "PRAGMA analyze;"
52 : "PRAGMA optimize;"
53 : "VACUUM;"
54 : "PRAGMA analyze;"
55 : "PRAGMA optimize;";
56 :
57 122 : if(db_is_primary == true)
58 : {
59 116 : slog(EVERY,"Start vacuuming the primary database…\n");
60 : } else {
61 6 : slog(EVERY,"Start vacuuming…\n");
62 : }
63 :
64 : /* Execute SQL statement */
65 122 : rc = sqlite3_exec(db,sql,NULL,NULL,&err_msg);
66 :
67 122 : 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 122 : db_file_modified = true;
73 :
74 122 : if(db_is_primary == true)
75 : {
76 116 : slog(EVERY,"The primary database has been vacuumed\n");
77 : } else {
78 6 : slog(EVERY,"The database has been vacuumed\n");
79 : }
80 : }
81 : }
82 :
83 122 : 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 122 : if(db_is_primary == true)
92 : {
93 : /* Changes have been made to the database. Update
94 : this in the global variable value. */
95 116 : config->db_primary_file_modified = true;
96 : }
97 :
98 : }
99 :
100 : /* Cleanup */
101 122 : call(db_close(db,&db_file_modified));
102 :
103 122 : provide(status);
104 : }
|