-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.c
221 lines (195 loc) · 7.41 KB
/
db.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* This file is part of cclip, clipboard manager for wayland
* Copyright (C) 2024 heather7283
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <sys/wait.h>
#include <stdio.h> /* snprintf */
#include <stdlib.h> /* getenv */
#include <unistd.h> /* getuid, access */
#include <sys/types.h> /* getpwuid */
#include <pwd.h> /* getpwuid */
#include <string.h> /* strlen */
#include <sqlite3.h> /* all the sqlite stuff */
#include <stdbool.h>
#include <libgen.h>
#include <errno.h>
#include "common.h"
#include "db.h"
#define MAX_DB_PATH_LENGTH 1024
struct sqlite3* db = NULL;
char* get_default_db_path(void) {
char* db_path = malloc(MAX_DB_PATH_LENGTH * sizeof(char));
if (db_path == NULL) {
die("failed to allocate memory for db path string\n");
}
char* xdg_data_home = getenv("XDG_DATA_HOME");
if (xdg_data_home != NULL) {
snprintf(db_path, MAX_DB_PATH_LENGTH, "%s/%s", xdg_data_home, "cclip/db.sqlite3");
} else {
char* home = getenv("HOME");
if (home == NULL) {
die("both HOME and XDG_DATA_HOME are unset, unable to determine db file path\n");
}
snprintf(db_path, MAX_DB_PATH_LENGTH, "%s/.local/share/%s", home, "cclip/db.sqlite3");
}
return db_path;
}
void db_init(const char* const db_path, bool create_if_not_exists) {
char* errmsg = NULL;
int ret_code = 0;
if (access(db_path, F_OK) == -1) {
if (!create_if_not_exists) {
die("database file %s does not exist\n", db_path);
} else {
info("database file %s does not exist, "
"attempting to create\n", db_path);
}
char* db_path_dup = strdup(db_path);
char* db_dir = dirname(db_path_dup);
char* mkdir_cmd[] = {"mkdir", "-p", db_dir, NULL};
pid_t child_pid = fork();
if (child_pid == -1) {
warn("forking child failed: %s\n", strerror(errno));
} else if (child_pid == 0) {
/* child */
execvp(mkdir_cmd[0], mkdir_cmd);
warn("execing into mkdir -p failed: %s\n", strerror(errno));
exit(1);
} else {
/* parent */
if (waitpid(child_pid, NULL, 0) == -1) {
warn("failed to wait for child: %s\n", strerror(errno));
};
}
free(db_path_dup);
FILE* db_file = fopen(db_path, "w+");
if (db_file == NULL) {
die("unable to create database file: %s\n", strerror(errno));
}
fclose(db_file);
}
ret_code = sqlite3_open(db_path, &db);
if (ret_code != SQLITE_OK) {
die("sqlite error: %s\n", sqlite3_errstr(ret_code));
}
/* enable WAL https://sqlite.org/wal.html */
ret_code = sqlite3_exec(db, "PRAGMA journal_mode=WAL", NULL, NULL, &errmsg);
if (ret_code != SQLITE_OK) {
die("sqlite error: %s\n", errmsg);
}
ret_code = sqlite3_exec(db, "PRAGMA synchronous=NORMAL", NULL, NULL, &errmsg);
if (ret_code != SQLITE_OK) {
die("sqlite error: %s\n", errmsg);
}
const char* db_create_expr =
"CREATE TABLE IF NOT EXISTS history ("
" data BLOB NOT NULL UNIQUE,"
" data_size INTEGER NOT NULL,"
" preview TEXT NOT NULL,"
" mime_type TEXT NOT NULL,"
" timestamp INTEGER NOT NULL"
")";
ret_code = sqlite3_exec(db, db_create_expr, NULL, NULL, &errmsg);
if (ret_code != SQLITE_OK) {
die("sqlite error: %s\n", errmsg);
}
const char* db_create_data_index_expr =
"CREATE UNIQUE INDEX IF NOT EXISTS idx_history_data ON history(data)";
ret_code = sqlite3_exec(db, db_create_data_index_expr, NULL, NULL, &errmsg);
if (ret_code != SQLITE_OK) {
die("sqlite error: %s\n", errmsg);
}
const char* db_create_timestamp_index_expr =
"CREATE UNIQUE INDEX IF NOT EXISTS idx_history_timestamp ON history(timestamp)";
ret_code = sqlite3_exec(db, db_create_timestamp_index_expr, NULL, NULL, &errmsg);
if (ret_code != SQLITE_OK) {
die("sqlite error: %s\n", errmsg);
}
}
int insert_db_entry(const struct db_entry* const entry, int max_entries_count) {
sqlite3_stmt* stmt = NULL;
int retcode = 0;
char* errmsg = NULL;
/* something something atomic */
retcode = sqlite3_exec(db, "BEGIN TRANSACTION", NULL, NULL, &errmsg);
if (retcode != SQLITE_OK) {
critical("sqlite error: %s\n", errmsg);
goto rollback;
}
if (max_entries_count > 0) {
int entries_count;
/* find out how many entries are currently in db */
const char* count_query =
"SELECT COUNT(*) FROM history";
retcode = sqlite3_prepare_v2(db, count_query, -1, &stmt, NULL);
if (retcode != SQLITE_OK) {
critical("sqlite error: %s\n", sqlite3_errmsg(db));
goto rollback;
}
retcode = sqlite3_step(stmt);
if (retcode == SQLITE_ROW) {
entries_count = sqlite3_column_int(stmt, 0);
} else {
critical("sqlite error: %s\n", sqlite3_errstr(retcode));
goto rollback;
}
sqlite3_finalize(stmt);
/* delete oldest */
if (entries_count > max_entries_count) {
debug("deleting oldest entry\n");
const char* delete_statement =
"DELETE FROM history WHERE timestamp=(SELECT MIN(timestamp) FROM history)";
retcode = sqlite3_exec(db, delete_statement, NULL, NULL, &errmsg);
if (retcode != SQLITE_OK) {
critical("sqlite error: %s\n", errmsg);
goto rollback;
}
}
}
/* prepare the SQL statement */
const char* insert_statement =
"INSERT OR REPLACE INTO history "
"(data, data_size, preview, mime_type, timestamp) "
"VALUES (?, ?, ?, ?, ?)";
retcode = sqlite3_prepare_v2(db, insert_statement, -1, &stmt, NULL);
if (retcode != SQLITE_OK) {
die("sqlite error: %s\n", sqlite3_errmsg(db));
}
/* bind parameters */
sqlite3_bind_blob(stmt, 1, entry->data, entry->data_size, SQLITE_STATIC);
sqlite3_bind_int64(stmt, 2, entry->data_size);
sqlite3_bind_text(stmt, 3, entry->preview, -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 4, entry->mime_type, -1, SQLITE_STATIC);
sqlite3_bind_int64(stmt, 5, entry->timestamp);
/* execute the statement */
retcode = sqlite3_step(stmt);
if (retcode != SQLITE_DONE) {
critical("sqlite error: %s\n", sqlite3_errmsg(db));
goto rollback;
} else {
debug("record inserted successfully\n");
}
sqlite3_finalize(stmt);
retcode = sqlite3_exec(db, "COMMIT", NULL, NULL, &errmsg);
if (retcode != SQLITE_OK) {
critical("sqlite error: %s\n", sqlite3_errmsg(db));
goto rollback;
}
return 0;
rollback:
retcode = sqlite3_exec(db, "ROLLBACK", NULL, NULL, &errmsg);
return 1;
}