File: sqlite_indexer.c

package info (click to toggle)
dump 0.4b52-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,076 kB
  • sloc: ansic: 15,394; sh: 5,006; cpp: 3,268; makefile: 183
file content (488 lines) | stat: -rw-r--r-- 11,805 bytes parent folder | download
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#include <config.h>
#include <ctype.h>
#include <compaterr.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <mntent.h>
#include <stdarg.h>

#include <sys/param.h>
#include <sys/time.h>
#include <time.h>
#include <linux/types.h>
#ifdef HAVE_EXT2FS_EXT2_FS_H
#include <ext2fs/ext2_fs.h>
#else
#include <linux/ext2_fs.h>
#endif
#include <ext2fs/ext2fs.h>
#include <sys/stat.h>
#include <bsdcompat.h>
#include <linux/fs.h>	/* for definition of BLKFLSBUF */

#include <protocols/dumprestore.h>

//#include "dump.h"
#include "indexer.h"
#include "pathnames.h"
#include "bylabel.h"

extern int tapeno;
extern dump_ino_t volinfo[]; // TP_NINOS

extern void msg (const char *fmt,...);

extern ext2_filsys fs;

#ifdef HAVE_SQLITE3
#include <sqlite3.h>
#include <uuid/uuid.h>

static sqlite3 *db;

// convenience macro that handles a basic sql statement.
#define EXEC(db, buffer) \
	rc = sqlite3_exec(db, buffer, NULL, 0, &errMsg); \
	if (rc != SQLITE_OK) { \
		msg("(%s:%d) SQL error: %s\n", __FILE__, __LINE__, sqlite3_errmsg(db)); \
		msg(buffer); \
		sqlite3_free(errMsg); \
		sqlite3_close(db); \
		db = NULL; \
		return -1; \
	}

/* prepared statement that allows us to use placeholder values. */
/* it also acts as sentinel to indicate if this is read or write mode. */
sqlite3_stmt *stmt = NULL;

/*
 * Ppopulate the 'backup' table.
 */
static int
sqlite_populate_backup_table()
{
	int rc;
	char *errMsg = NULL;
	char buffer[2000];
	char vol_uuid[40];
	char ts[40];
	char nts[40];
	char fs_uuid[40];
	time_t now;
	const char *tail;
	uuid_t uuid;

	time(&now);

	// populate 'backup table'. There will only be one entry.
	uuid_generate_time(uuid);
	uuid_unparse_lower(uuid, vol_uuid);
	uuid_unparse_lower(fs->super->s_uuid, fs_uuid);
	strftime(ts, sizeof ts, "%FT%T", gmtime((const time_t *) &spcl.c_date));
	strftime(nts, sizeof nts, "%FT%T", gmtime((const time_t *) &now));
	snprintf(buffer, sizeof buffer,
		"insert into backup(backup_id, vol_uuid, dt, start_dt, fs_uuid, dev, label, host, level) values (1, '%s', '%s', '%s', '%s', ?, ?, ?, %d)",
		vol_uuid, ts, nts, fs_uuid, spcl.c_level);

//		spcl.c_dev, spcl.c_label, spcl.c_host,

	// prepare statement.
	rc = sqlite3_prepare_v2(db, buffer, strlen(buffer), &stmt, &tail);
	if (rc != SQLITE_OK) {
		msg("(%s:%d) SQL error: %s\n", __FILE__, __LINE__, sqlite3_errmsg(db));
		msg(buffer);
		sqlite3_free(errMsg);
		sqlite3_close(db);
		db = NULL;
		return -1;
	}

	sqlite3_bind_text(stmt, 1, spcl.c_dev, strlen(spcl.c_dev), SQLITE_TRANSIENT);
	sqlite3_bind_text(stmt, 2, spcl.c_label, strlen(spcl.c_label), SQLITE_TRANSIENT);
	sqlite3_bind_text(stmt, 3, spcl.c_host, strlen(spcl.c_host), SQLITE_TRANSIENT);

	while ((rc = sqlite3_step(stmt)) == SQLITE_BUSY) {
		continue;
	}

	if (rc != SQLITE_DONE) {
		msg("(%s:%d) SQL error: %s\n", __FILE__, __LINE__, sqlite3_errmsg(db));
		msg("(%s:%d) %s\n", __FILE__, __LINE__, sqlite3_sql(stmt));
		sqlite3_free(errMsg);
		sqlite3_close(db);
		db = NULL;
		return -1;
	}

	rc = sqlite3_finalize(stmt);
	if (rc != SQLITE_OK) {
		msg("(%s:%d) SQL error: %s\n", __FILE__, __LINE__, sqlite3_errmsg(db));
		msg("(%s:%d) %s\n", __FILE__, __LINE__, sqlite3_sql(stmt));
		sqlite3_free(errMsg);
		sqlite3_close(db);
		db = NULL;
		return -1;
	}

	return 0;
}

/*
 * Create database schema
 */
static int
sqlite_create_schema(void)
{
	int rc;
	char *errMsg = NULL;
	char buffer[2000];
	const char *tail;

	// turn on foreign keys
	snprintf(buffer, sizeof buffer, "pragma foreign_keys = ON");

	EXEC(db, buffer);

	// turn off synchronous access. we have bigger problems
	// if we lose power in the middle of a backup.
	snprintf(buffer, sizeof buffer, "pragma synchronous = OFF\n");

	EXEC(db, buffer);

	// create 'backup' table.
	snprintf(buffer, sizeof buffer,
		"create table backup(backup_id int primary key not null, vol_uuid char[36] not null, dt timestamp not null, start_dt timestamp not null, end_dt timestamp, fs_uuid char[36] not null, dev varchar[%1$d] not null, label varchar[%1$d] not null, host varchar[%1$d] not null, level int not null, blocks int, volumes int)\n", EXT2_NAME_LEN);

	EXEC(db, buffer);

	snprintf(buffer, sizeof buffer,
		"create unique index i1 on backup(backup_id)\n");
	EXEC(db, buffer);

	// create 'entry' table. This contains information about
	// each directory entry
	snprintf(buffer, sizeof buffer,
		"create table entry(backup_id int not null, ino int not null, parent_ino int not null, type int not null, depth int, name varchar[%d] not null, path varchar[%d], foreign key(backup_id) references backup(backup_id))\n", EXT2_NAME_LEN, 1024);

	//	"create table entry(backup_id int not null, ino int not null, parent_ino int not null, type int not null, depth int, name varchar[%d] not null, path varchar[%d], foreign key(backup_id) references backup(backup_id), foreign key(parent_ino) references entry(ino))\n", EXT2_NAME_LEN, 1024);

	EXEC(db, buffer);

	// snprintf(buffer, sizeof buffer,
	//	"create unique index i2 on entry(ino)\n");

	// EXEC(db, buffer);

	snprintf(buffer, sizeof buffer,
		"create trigger t1 after insert on entry begin update entry set path = coalesce((select '.' where name = '.'), (select A.path || '/' || new.name from entry A where A.ino = new.parent_ino)) where rowid = new.rowid; end;\n");

	EXEC(db, buffer);

	// create 'inode' table. This contains information about each inode.
	snprintf(buffer, sizeof buffer,
		"create table inode(backup_id int not null, ino int not null, is_deleted char[1] not null default 'N', mode int not null, nlink int not null, uid int not null, gid int not null, rdev int not null, size int not null, atime timestamp not null, mtime timestamp not null, ctime timestamp not null, has_xattr char[1] not null default 'N', has_acl char[1] not null default 'N', volume int not null, recno int not null, foreign key(backup_id) references backup(backup_id))\n");

	//	"create table inode(backup_id int not null, ino int not null, is_deleted char[1] not null default 'N', mode int not null, nlink int not null, uid int not null, gid int not null, rdev int not null, size int not null, atime timestamp not null, mtime timestamp not null, ctime timestamp not null, has_xattr char[1] not null default 'N', has_acl char[1] not null default 'N', volume int not null, recno int not null, foreign key(backup_id) references backup(backup_id), foreign key(ino) references entry(ino))\n");

	EXEC(db, buffer);

	sqlite_populate_backup_table();

	// prepare statement for entry inserts.
	snprintf(buffer, sizeof buffer,
		"insert into entry(backup_id, ino, parent_ino, type, name) values(1, ?, ?, ?, ?)\n");

	rc = sqlite3_prepare_v2(db, buffer, strlen(buffer), &stmt, &tail);
	if (rc != SQLITE_OK) {
		msg("(%s:%d) SQL error: %s\n", __FILE__, __LINE__, sqlite3_errmsg(db));
		msg(buffer);
		sqlite3_free(errMsg);
		sqlite3_close(db);
		db = NULL;
		return -1;
	}

	return 0;
}

/*
 * Open database.
 */
static int
sqlite_open(const char *filename, int mode)
{
	if (mode == 0) {
		if (sqlite3_open(filename, &db)) {
			msg("Unable to open database: %s\n", sqlite3_errmsg(db));
			db = NULL;
			return -1;
		}
		stmt = NULL;
	} else if (mode == 1) {
		unlink(filename);

		/* sqlite will always create DB if it doesn't already exist. */
		if (sqlite3_open(filename, &db)) {
			msg("Unable to open database: %s\n", sqlite3_errmsg(db));
			db = NULL;
			return -1;
		}

		sqlite_create_schema();
	}

	return 0;
}

/*
 * Close database.
 */
static int
sqlite_close()
{
	int rc;
	char *errMsg = NULL;
	char buffer[2000];
	char uuid[40];
	char ts[40];
	time_t now;

	if (db == NULL) {
		msg("database is already closed!");
		return -1;
	}

	time(&now);

	/* if stmt is not null we're creating database */
	if (stmt != NULL) {
		// close prepared statement.
		rc = sqlite3_finalize(stmt);
		if (rc != SQLITE_OK) {
			msg("(%s:%d) SQL error: %s\n", __FILE__, __LINE__, sqlite3_errmsg(db));
			msg("(%s:%d) %s\n", __FILE__, __LINE__, sqlite3_sql(stmt));
			sqlite3_free(errMsg);
			sqlite3_close(db);
			db = NULL;
			return -1;
		}

		// update 'tape header' with number of volumes and blocks.
		uuid_unparse_lower(fs->super->s_uuid, uuid);
		strftime(ts, sizeof ts, "%FT%T", gmtime((const time_t *) &now));
		snprintf(buffer, sizeof buffer,
				"update backup set end_dt = '%s', blocks=%ld, volumes=%d where fs_uuid='%s'",
				ts, u_spcl_c_tapea(), spcl.c_volume, uuid);

		EXEC(db, buffer);
	}

	// close database.
	rc = sqlite3_close(db);
	if (rc != SQLITE_OK) {
		msg("(%s:%d) SQL error: %s\n", __FILE__, __LINE__, sqlite3_errmsg(db));
		msg(buffer);
		sqlite3_free(errMsg);
		db = NULL;
		return -1;
	}
	db = NULL;

	return 0;
}

/*
 * Add directory entry.
 */
static int
sqlite_addDirEntry(struct direct *dp, dump_ino_t parent_ino)
{
	int rc;
	char *errMsg = NULL;
	char buffer[2000];
	const char *tail;

	if (db == NULL) {
		return -1;
	}

	// don't include backlink.
	if (!strcmp(dp->d_name, "..")) {
		return -1;
	}

	sqlite3_bind_int(stmt, 1, dp->d_ino);
	sqlite3_bind_int(stmt, 2, parent_ino);
	sqlite3_bind_int(stmt, 3, dp->d_type);
	sqlite3_bind_text(stmt, 4, dp->d_name, strlen(dp->d_name), SQLITE_TRANSIENT);

	while ((rc = sqlite3_step(stmt)) == SQLITE_BUSY) {
		continue;
	}

	if (rc != SQLITE_DONE) {
		int i;
		msg("(%s:%d) SQL error: %s\n", __FILE__, __LINE__, sqlite3_errmsg(db));
		msg("(%s:%d) %s\n", __FILE__, __LINE__, sqlite3_sql(stmt));
		sqlite3_free(errMsg);
		sqlite3_close(db);
		db = NULL;
		return -1;
	}

	rc = sqlite3_reset(stmt);
	if (rc != SQLITE_OK) {
		msg("(%s:%d) SQL error: %s\n", __FILE__, __LINE__, sqlite3_errmsg(db));
		msg(buffer);
		sqlite3_free(errMsg);
		sqlite3_close(db);
		db = NULL;
		return -1;
	}

	rc = sqlite3_clear_bindings(stmt);
	if (rc != SQLITE_OK) {
		msg("(%s:%d) SQL error: %s\n", __FILE__, __LINE__, sqlite3_errmsg(db));
		msg(buffer);
		sqlite3_free(errMsg);
		sqlite3_close(db);
		db = NULL;
		return -1;
	}

	return 0;
}

/*
 * Add inode entry.
 */
static int
sqlite_addInode(struct ext2_inode_large *dp, dump_ino_t ino, int metadata_only)
{
	int rc;
	char *errMsg = NULL;
	char buffer[2000];
	char ats[40];
	char mts[40];
	char cts[40];
	time_t t;

	if (db == NULL) {
		return -1;
	}

	ats[0] = 0;
	mts[0] = 0;
	cts[0] = 0;

	t = dp->di_atime;
	strftime(ats, sizeof ats, "%FT%T", gmtime(&t));
	t = dp->di_mtime;
	strftime(mts, sizeof ats, "%FT%T", gmtime(&t));
	t = dp->di_ctime;
	strftime(cts, sizeof ats, "%FT%T", gmtime(&t));

	// xattr: dp->di_extraisize != 0
	// acl: dp->di_file_acl (inode?)
	// se linux?...

	snprintf(buffer, sizeof buffer,
		"insert into inode(backup_id, ino, is_deleted, mode, nlink, uid, gid, rdev, size, atime, mtime, ctime, has_xattr, has_acl, volume, recno) values(1, %d, '%s', %d, %d, %d, %d, %d, %d, '%s', '%s', '%s', '%s', '%s', %d, %lld)\n",
		ino, "N", dp->di_mode, dp->di_nlink, dp->di_uid, dp->di_gid, dp->di_rdev, dp->di_size, ats, mts, cts, (dp->di_extraisize == 0) ? "N" : "Y", (dp->di_file_acl != 0) ? "Y" : "N", spcl.c_volume, u_spcl_c_tapea());

	EXEC(db, buffer);

	return 0;
}

/*
 *
 */
static int
sqlite_endtape()
{
	return 0;
}

/*
 *
 */
static int
sqlite_writerec(const void *dp, int isspcl)
{
	return 0;
}

/*
 *
 */
static int
sqlite_openQfa()
{
	return 0;
}

/*
 *
 */
static int
sqlite_closeQfa()
{
	return 0;
}

/*
 *
 */
static int
sqlite_openQfaState(QFA_State *state)
{
	return 0;
}

/*
 *
 */
static int
sqlite_updateQfaState(QFA_State *state)
{
	return 0;
}

/*
 *
 */
static int
sqlite_updateQfa(QFA_State *state)
{
	return 0;
}


/*
 *
 */
Indexer indexer_sqlite =
{
		NULL,
		&sqlite_open,
		&sqlite_close,
		&sqlite_endtape,
		&sqlite_writerec,
		&sqlite_addInode,
		&sqlite_addDirEntry,

		&sqlite_openQfa,
		&sqlite_closeQfa,
		&sqlite_openQfaState,
		&sqlite_updateQfa,
		&sqlite_updateQfaState
};

#endif