forked from freebsd/pkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.c
177 lines (145 loc) · 4.49 KB
/
backup.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
/*-
* Copyright (c) 2011-2020 Baptiste Daroussin <[email protected]>
* Copyright (c) 2012 Matthew Seaman <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer
* in this position and unchanged.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <assert.h>
#include <fcntl.h>
#include <libgen.h>
#include <string.h>
#include <errno.h>
#include "pkg.h"
#include "private/event.h"
#include "private/pkg.h"
#include "private/pkgdb.h"
#include <bsd_compat.h>
/* Number of pages to copy per call to sqlite3_backup_step()
Default page size is 1024 bytes on Unix */
#define NPAGES 4
static int
ps_cb(void *ps, int ncols, char **coltext, __unused char **colnames)
{
/* We should have exactly one row and one column of output */
if (ncols != 1)
return (-1); /* ABORT! */
*(off_t *)ps = strtoll(coltext[0], NULL, 10);
return (0);
}
static int
copy_database(sqlite3 *src, sqlite3 *dst)
{
sqlite3_backup *b;
char *errmsg;
off_t total;
off_t done;
off_t page_size;
int ret;
assert(src != NULL);
assert(dst != NULL);
ret = sqlite3_exec(dst, "PRAGMA main.locking_mode=EXCLUSIVE;"
"BEGIN IMMEDIATE;COMMIT;", NULL, NULL, &errmsg);
if (ret != SQLITE_OK) {
pkg_emit_error("sqlite error -- %s", errmsg);
sqlite3_free(errmsg);
return (EPKG_FATAL);
}
ret = sqlite3_exec(dst, "PRAGMA page_size", ps_cb, &page_size, &errmsg);
if (ret != SQLITE_OK) {
pkg_emit_error("sqlite error -- %s", errmsg);
sqlite3_free(errmsg);
return (EPKG_FATAL);
}
b = sqlite3_backup_init(dst, "main", src, "main");
pkg_emit_progress_start(NULL);
do {
ret = sqlite3_backup_step(b, NPAGES);
total = sqlite3_backup_pagecount(b);
done = total - sqlite3_backup_remaining(b);
pkg_emit_progress_tick(done, total);
if (ret != SQLITE_OK && ret != SQLITE_DONE ) {
if (ret == SQLITE_BUSY) {
sqlite3_sleep(250);
} else {
ERROR_SQLITE(dst, "backup step");
break;
}
}
} while(done < total);
ret = sqlite3_backup_finish(b);
pkg_emit_progress_tick(total, total);
sqlite3_exec(dst, "PRAGMA main.locking_mode=NORMAL;"
"BEGIN IMMEDIATE;COMMIT;", NULL, NULL, &errmsg);
if (ret != SQLITE_OK) {
pkg_emit_error("sqlite error -- %s", errmsg);
sqlite3_free(errmsg);
return (EPKG_FATAL);
}
return ret;
}
int
pkgdb_dump(struct pkgdb *db, const char *dest)
{
sqlite3 *backup;
int ret;
int destdbfd;
int savedfd;
destdbfd = open(bsd_dirname(dest), O_DIRECTORY|O_CLOEXEC);
if (destdbfd == -1) {
pkg_fatal_errno("Unable to access '%s'",
bsd_dirname(dest));
}
savedfd = pkg_get_dbdirfd();
ctx.pkg_dbdirfd = destdbfd;
ret = sqlite3_open(dest, &backup);
if (ret != SQLITE_OK) {
ERROR_SQLITE(backup, "sqlite3_open");
sqlite3_close(backup);
return (EPKG_FATAL);
}
pkg_emit_backup();
ret = copy_database(db->sqlite, backup);
sqlite3_close(backup);
ctx.pkg_dbdirfd = savedfd;
close(savedfd);
return (ret == SQLITE_OK? EPKG_OK : EPKG_FATAL);
}
int
pkgdb_load(struct pkgdb *db, const char *src)
{
sqlite3 *restore;
int ret;
if (eaccess(src, R_OK)) {
pkg_fatal_errno("Unable to access '%s'", src);
}
ret = sqlite3_open(src, &restore);
if (ret != SQLITE_OK) {
ERROR_SQLITE(restore, "sqlite3_open");
sqlite3_close(restore);
return (EPKG_FATAL);
}
pkg_emit_restore();
ret = copy_database(restore, db->sqlite);
sqlite3_close(restore);
return (ret == SQLITE_OK? EPKG_OK : EPKG_FATAL);
}