Line data Source code
1 : #include "precizer.h"
2 :
3 : /**
4 : *
5 : * This function remove information about a specific
6 : * file from the database by its unique db ID
7 : *
8 : */
9 890 : Return db_delete_the_record_by_id(
10 : sqlite_int64 *ID,
11 : bool *first_iteration,
12 : const bool *clean_ignored,
13 : const char *relative_path,
14 : const char *runtime_path_prefix)
15 : {
16 : /// The status that will be passed to return() before exiting.
17 : /// By default, the function worked without errors.
18 890 : Return status = SUCCESS;
19 :
20 : // Indicates removal due to missing or unreadable path
21 890 : bool inaccessible = false;
22 :
23 890 : bool file_not_found = false;
24 :
25 890 : char *absolute_path = NULL;
26 :
27 890 : if(clean_ignored != NULL && *clean_ignored == false
28 838 : && runtime_path_prefix != NULL && relative_path != NULL)
29 : {
30 838 : int length = asprintf(&absolute_path,"%s/%s",runtime_path_prefix,relative_path);
31 :
32 838 : if(length == -1)
33 : {
34 0 : free(absolute_path);
35 0 : return(FAILURE);
36 : }
37 :
38 838 : FileAccessStatus access_status = file_check_access(absolute_path,(size_t)length);
39 :
40 838 : free(absolute_path);
41 :
42 838 : if(access_status == FILE_ACCESS_ERROR)
43 : {
44 0 : return(FAILURE);
45 : }
46 :
47 838 : if(access_status == FILE_NOT_FOUND)
48 : {
49 26 : file_not_found = true;
50 :
51 812 : } else if(access_status == FILE_ACCESS_DENIED){
52 :
53 4 : inaccessible = true;
54 :
55 808 : } else if(access_status == FILE_ACCESS_ALLOWED){
56 :
57 : /* The file remains available.
58 : Keep file references in the database! */
59 808 : return(SUCCESS);
60 : }
61 : }
62 :
63 82 : sqlite3_stmt *delete_stmt = NULL;
64 82 : int rc = 0;
65 :
66 82 : const char *sql = "DELETE FROM files WHERE ID=?1;";
67 :
68 : // Don't do anything in case of --dry-run
69 82 : if(config->dry_run == true)
70 : {
71 16 : sql = "SELECT ID FROM files WHERE ID=?1;";
72 : }
73 :
74 82 : rc = sqlite3_prepare_v2(config->db,sql,-1,&delete_stmt,NULL);
75 :
76 82 : if(SQLITE_OK != rc)
77 : {
78 0 : log_sqlite_error(config->db,rc,NULL,"Can't prepare delete statement");
79 0 : status = FAILURE;
80 : }
81 :
82 82 : if(SUCCESS == status)
83 : {
84 82 : rc = sqlite3_bind_int64(delete_stmt,1,*ID);
85 :
86 82 : if(SQLITE_OK != rc)
87 : {
88 0 : log_sqlite_error(config->db,rc,NULL,"Error binding value in delete");
89 0 : status = FAILURE;
90 : }
91 : }
92 :
93 82 : int sql_return = SQLITE_DONE;
94 :
95 82 : if(SUCCESS == status)
96 : {
97 : // Select instead Delete in Dry Run mode
98 82 : if(config->dry_run == true)
99 : {
100 16 : sql_return = SQLITE_ROW;
101 : }
102 :
103 : /* Execute SQL statement */
104 82 : if(sqlite3_step(delete_stmt) == sql_return)
105 : {
106 82 : if(*first_iteration == true)
107 : {
108 32 : *first_iteration = false;
109 :
110 32 : if(config->ignore != NULL)
111 : {
112 16 : if(config->db_clean_ignored == false)
113 : {
114 4 : slog(EVERY,"If the information about ignored files should be removed from the database the " BOLD "--db-clean-ignored" RESET " option must be specified. This is special protection against accidental deletion of information from the database\n");
115 : } else {
116 12 : slog(TRACE,"The " BOLD "--db-clean-ignored" RESET " option has been used, so the information about ignored files will be removed against the database %s\n",config->db_file_name);
117 : }
118 : }
119 :
120 32 : if(config->the_update_warning_has_already_been_shown == false)
121 : {
122 4 : slog(EVERY,"The " BOLD "--update" RESET " option has been used, so the information about files will be deleted against the database %s\n",config->db_file_name);
123 : }
124 :
125 : /* Reflect changes in global */
126 32 : if(config->dry_run == false)
127 : {
128 24 : config->db_primary_file_modified = true;
129 : }
130 :
131 32 : slog(EVERY,BOLD "These files are no longer exist or ignored and will be deleted against the DB %s:" RESET "\n",config->db_file_name);
132 : }
133 :
134 82 : if(*clean_ignored == true)
135 : {
136 52 : slog(EVERY|UNDECOR,"clean ignored %s\n",relative_path);
137 :
138 30 : } else if(inaccessible == true){
139 :
140 4 : slog(EVERY|UNDECOR,"inaccessible %s\n",relative_path);
141 :
142 26 : } else if(file_not_found == true){
143 :
144 26 : slog(EVERY|UNDECOR,"no longer exists %s\n",relative_path);
145 :
146 : } else {
147 :
148 0 : slog(ERROR,"An unexpected error that should never occur for %s\n",relative_path);
149 : }
150 :
151 : } else {
152 :
153 0 : log_sqlite_error(config->db,rc,NULL,"Delete statement didn't return right code %d",sql_return);
154 0 : status = FAILURE;
155 : }
156 : }
157 :
158 82 : sqlite3_finalize(delete_stmt);
159 :
160 82 : provide(status);
161 : }
|