-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfbBackup.cpp
More file actions
316 lines (271 loc) · 10.6 KB
/
fbBackup.cpp
File metadata and controls
316 lines (271 loc) · 10.6 KB
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
/*-
* Copyright (C) 2008 Lucas Holt. 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.
* 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 AUTHOR AND CONTRIBUTORS ``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 AUTHOR OR CONTRIBUTORS 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.
*/
/*-
* Copyright (c) 2007 Chris Reinhardt
* 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.
* 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 AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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.
*
* Copyright (c) 2008 Chris Tubbs
* All rights reserved.
* Do whatever you want with this code.
*
* $MidnightBSD: src/lib/libmport/bundle.c,v 1.1 2008/01/05 22:18:20 ctriv Exp $
*/
#include "fbBackup.h"
/* Constructor */
fbBackup::fbBackup(fbData* _data, const string& src, const string& dest):fbThread(_data), data(_data), backuppath(src), tarfile(dest), a(NULL)
{
data->debug(NONE,"fbBackup.this %s -> %s", backuppath.c_str(), tarfile.c_str());
startDelete();
}
/* Destructor */
fbBackup::~fbBackup()
{
data->debug(NONE,"fbBackup.~this %s -> %s", backuppath.c_str(), tarfile.c_str());
}
/* main code of thread */
void fbBackup::run()
{
data->msg(NONE, "Backup begun: %s -> %s", backuppath.c_str(), tarfile.c_str());
/* don't back up if certain requirements are not met */
if (!backuppath.length() || !tarfile.length())
{
data->warn(NONE, "Invalid backup attempted (backup source or destination empty)");
return;
}
/* create new archive and set options */
a = archive_write_new();
data->debug(NONE, "a = archive_write_new()");
archive_write_set_format_ustar(a);
archive_write_add_filter_bzip2(a);
/* open a new archive */
if (archive_write_open_filename(a, tarfile.c_str()) != ARCHIVE_OK)
{
data->debug(NONE, "archive_write_open_filename(a, \"%s\") failed", tarfile.c_str());
data->warn(NONE, "Unable to create backup file: %s", archive_error_string(a));
}
else
{
/* open successful, traverse the directory tree to back up */
data->debug(NONE, "archive_write_open_filename(a, \"%s\") successful", tarfile.c_str());
traverseDir(backuppath);
}
/* clean up the archive neatly */
if ( archive_write_free(a) != ARCHIVE_OK)
data->warn(NONE, "Unable to create backup file: %s", archive_error_string(a));
data->debug(NONE, "archive_write_finish(a)");
data->msg(NONE, "Backup complete: %s -> %s", backuppath.c_str(), tarfile.c_str());
/* fallback code */
/*
string cmd = "tar -cf ";
cmd += to;
cmd += " ";
cmd += to;
system(cmd.c_str());
*/
}
/* traverse a directory tree, and back up regular files and symlinks */
void fbBackup::traverseDir(const string& pathname)
{
struct stat st;
data->debug(NONE, "traverseDir(\"%s\")", pathname.c_str());
/* check if path exists */
if (lstat(pathname.c_str(), &st) < 0)
{
data->warn(NONE, "Can't lstat pathname: %s", pathname.c_str());
return;
}
data->debug(NONE, "lstat(\"%s\", &st)", pathname.c_str());
/* if it's a regular file or symlink, add it to the archive */
if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
{
data->debug(NONE, "\t...is a regular file");
addFile(pathname, &st);
}
/* otherwise, if it's a directory, traverse it */
else if (S_ISDIR(st.st_mode))
{
DIR *dir;
struct dirent *item;
queue<string> morepaths;
string nextpath;
/* attempt to open the directory and read the contents */
if (!(dir = opendir(pathname.c_str())))
{
data->warn(NONE, "Can't descend into path %s", pathname.c_str());
return;
}
data->debug(NONE, "Descending into path %s", pathname.c_str());
/* get everything in this directory */
while ((item = readdir(dir)) != NULL)
{
nextpath = pathname;
if (!strcmp(item->d_name, ".") || !strcmp(item->d_name, ".."))
continue;
/* append a separator if one doesn't already exist */
if (nextpath[nextpath.length()-1] != PATH_NAME_SEPARATOR)
nextpath += PATH_NAME_SEPARATOR;
/* append name of directory entry */
nextpath += item->d_name;
/* add it to the queue to check later, if it doesn't exceed the maximum length */
if (nextpath.length() < PATH_MAX)
morepaths.push(nextpath);
}
/* make sure directory is closed before traversing, to keep open file handles to a minimum */
closedir(dir);
/* while there is more in the directory, traverse to it */
while (!morepaths.empty())
{
traverseDir(morepaths.front());
morepaths.pop();
}
}
}
/* add a single regular file or symlink to the archive */
void fbBackup::addFile(const string& pathname, struct stat *st)
{
data->debug(NONE, "addFile(\"%s\", struct stat *st)", pathname.c_str());
struct archive_entry *entry = NULL;
int fd = 0;
int buff[1024 * 64];
ssize_t len = 0;
int resp;
char *ent_path = strdup(pathname.c_str());
/* create a new archive entry struct for this file */
if ((entry = archive_entry_new()) == NULL)
{
data->warn(NONE, "Cannot allocate memory for archive_entry_new()");
free(ent_path);
return;
}
data->debug(NONE, "Allocated memory for archive_entry_new()");
/* set the initial pathname for the file in the archive */
archive_entry_set_pathname(entry, ent_path);
data->debug(NONE, "Completed archive_entry_set_pathname(entry, \"%s\")", ent_path);
/* fix the pathname to get rid of prefix */
fixPath(entry);
data->debug(NONE, "Fixed path to read: \"%s\")", archive_entry_pathname(entry));
/* handle symlinks */
if (S_ISLNK(st->st_mode)) {
/* we have us a symlink */
int linklen;
char linkdata[PATH_MAX];
linklen = readlink(pathname.c_str(), linkdata, PATH_MAX);
if (linklen < 0)
{
data->warn(NONE, "Failed to copy symlink data");
archive_entry_free(entry);
free(ent_path);
return;
}
linkdata[linklen] = 0;
archive_entry_set_symlink(entry, linkdata);
}
#ifdef BSD
if (st->st_flags != 0)
archive_entry_set_fflags(entry, st->st_flags, 0);
#endif
archive_entry_copy_stat(entry, st);
data->debug(NONE, "Completed archive_entry_copy_stat(entry, st)");
/* try to open the file if it is a regular file */
if (!S_ISREG(st->st_mode))
{
data->debug(NONE, "%s is NOT a regular file!", pathname.c_str());
archive_entry_set_size(entry, 0);
}
else if ((fd = open(pathname.c_str(), O_RDONLY)) < 0)
{
data->warn(NONE, "Can't open pathname for archiving: %d = open(%s)", fd, pathname.c_str());
archive_entry_set_size(entry, 0);
}
/* write the header to the archive */
resp = archive_write_header(a, entry);
if (resp != ARCHIVE_OK)
{
data->debug(NONE, "Problem writing header for %s", pathname.c_str());
archive_entry_free(entry);
free(ent_path);
return;
}
/* copy data to the archive for the file that was previously opened */
if (archive_entry_size(entry) > 0) {
len = read(fd, buff, sizeof(buff));
while (len > 0) {
if (archive_write_data(a, buff, len) < 0)
data->debug(NONE, "Problem writing data to archive for %s", archive_entry_pathname(entry));
len = read(fd, buff, sizeof(buff));
}
}
data->debug(NONE, "Archived file: %s", archive_entry_pathname(entry));
/* clean up */
if (fd >= 0)
close(fd);
archive_write_finish_entry(a);
archive_entry_free(entry);
free(ent_path);
}
/* modify the archive_entry_pathname() to remove the path prefix */
void fbBackup::fixPath(struct archive_entry *entry)
{
const char *name = archive_entry_pathname(entry);
const char *root = backuppath.c_str();
/* trim off parts of name that correspond to original directory
* tree. so, if we archive '/var/log', then '/var/log/messages'
* will be archived as just 'messages' */
while (name[0] == root[0])
{
++name;
++root;
}
/* strip leading slashes */
while (name[0] == PATH_NAME_SEPARATOR)
++name;
/* check for zero string length */
if (*name == '\0')
name = ".";
char *fixed = strdup(name);
archive_entry_copy_pathname(entry, fixed);
free(fixed);
}