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
|
/*
* nanobbs, a tiny forums software.
* Copyright (C) 2025-2026 Xavier Del Campo Romero
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#define _POSIX_C_SOURCE 200809L
#include "db.h"
#include "defs.h"
#include <dynstr.h>
#include <sqlite3.h>
#include <sys/stat.h>
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int column(sqlite3 *const db, sqlite3_stmt *const stmt,
const char *const name)
{
const int n = sqlite3_data_count(stmt);
if (!n)
{
fprintf(stderr, "%s: sqlite3_data_count: %s\n", __func__,
sqlite3_errmsg(db));
return -1;
}
for (int i = 0; i < n; i++)
{
const char *const col = sqlite3_column_name(stmt, i);
if (!col)
{
fprintf(stderr, "%s: sqlite3_column_name: %s\n", __func__,
sqlite3_errmsg(db));
return -1;
}
else if (!strcmp(col, name))
return i;
}
fprintf(stderr, "%s: could not find column \"%s\"\n", __func__, name);
return -1;
}
int db_int(sqlite3 *const db, sqlite3_stmt *const stmt, const char *const name,
int *const out)
{
const int col = column(db, stmt, name);
if (col < 0)
return -1;
*out = sqlite3_column_int(stmt, col);
return 0;
}
int db_uint(sqlite3 *const db, sqlite3_stmt *const stmt, const char *const name,
unsigned *const out)
{
int v;
if (db_int(db, stmt, name, &v))
return -1;
else if (v < 0)
{
fprintf(stderr, "%s: unexpected negative value for %s: %d\n", __func__,
name, v);
return -1;
}
*out = v;
return 0;
}
int db_bigint(sqlite3 *const db, sqlite3_stmt *const stmt,
const char *const name, long long *const out)
{
const int col = column(db, stmt, name);
if (col < 0)
return -1;
*out = sqlite3_column_int64(stmt, col);
return 0;
}
int db_biguint(sqlite3 *const db, sqlite3_stmt *const stmt,
const char *const name, unsigned long long *const out)
{
const int col = column(db, stmt, name);
sqlite3_int64 v;
if (col < 0 || (v = sqlite3_column_int64(stmt, col)) < 0)
return -1;
*out = v;
return 0;
}
char *db_str(sqlite3 *const db, sqlite3_stmt *const stmt,
const char *const name)
{
char *ret = NULL;
const int col = column(db, stmt, name);
if (col < 0)
return NULL;
const unsigned char *const s = sqlite3_column_text(stmt, col);
if (!s)
{
fprintf(stderr, "%s: sqlite3_column_text: %s\n", __func__,
sqlite3_errmsg(db));
goto failure;
}
else if (!(ret = strndup((const char *)s, sqlite3_column_bytes(stmt, col))))
{
fprintf(stderr, "%s: strndup(3): %s\n", __func__, strerror(errno));
goto failure;
}
return ret;
failure:
free(ret);
return NULL;
}
int db_rollback(sqlite3 *const db)
{
char *msg = NULL;
const int e = sqlite3_exec(db, "ROLLBACK;", NULL, NULL, &msg);
if (e != SQLITE_OK)
fprintf(stderr, "%s: sqlite3_exec: %s, msg=%s\n", __func__,
sqlite3_errstr(e), msg);
sqlite3_free(msg);
return e != SQLITE_OK;
}
int db_open(const char *const dir, sqlite3 **const out)
{
int ret = SQLITE_ERROR;
sqlite3 *db;
struct dynstr d;
const mode_t m = umask(S_IWGRP | S_IWOTH | S_IROTH);
dynstr_init(&d);
if (dynstr_append(&d, "%s/" PROJECT_NAME ".db", dir))
{
fprintf(stderr, "%s: dynstr_append failed\n", __func__);
goto end;
}
else if ((ret = sqlite3_open(d.str, &db)) != SQLITE_OK)
{
fprintf(stderr, "%s: sqlite3_open %s: %s\n", __func__, d.str,
sqlite3_errstr(ret));
goto end;
}
*out = db;
ret = 0;
end:
umask(m);
dynstr_free(&d);
return ret;
}
|