2 Copyright (C) 2000-2003 Kern Sibbald and John Walker
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of
7 the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public
15 License along with this program; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 This file is based on GNU TAR source code. Except for a few key
20 ideas, it has been rewritten for Bacula.
24 Thanks to the TAR programmers.
31 extern size_t name_max; /* filename max length */
32 extern size_t path_max; /* path name max length */
35 * Structure for keeping track of hard linked files, we
36 * keep an entry for each hardlinked file that we save,
37 * which is the first one found. For all the other files that
38 * are linked to this one, we save only the directory
39 * entry so we can link it.
43 dev_t dev; /* device */
44 ino_t ino; /* inode with device is unique */
46 uint32_t FileIndex; /* Bacula FileIndex of this file */
47 char name[1]; /* The name */
52 * handle_file is the callback for handling the file.
54 * parent_device is the device we are currently on
55 * top_level is 1 when not recursing or 0 when
56 * decending into a directory.
59 find_one_file(JCR *jcr, FF_PKT *ff_pkt, int handle_file(FF_PKT *ff, void *hpkt),
60 void *pkt, char *fname, dev_t parent_device, int top_level)
62 struct utimbuf restore_times;
65 ff_pkt->fname = ff_pkt->link = fname;
67 if (lstat(fname, &ff_pkt->statp) != 0) {
68 /* Cannot stat file */
69 ff_pkt->type = FT_NOSTAT;
70 ff_pkt->ff_errno = errno;
71 return handle_file(ff_pkt, pkt);
74 Dmsg1(60, "File ----: %s\n", fname);
76 /* Save current times of this directory in case we need to
77 * reset them because the user doesn't want them changed.
79 restore_times.actime = ff_pkt->statp.st_atime;
80 restore_times.modtime = ff_pkt->statp.st_mtime;
84 * If this is an Incremental backup, see if file was modified
85 * since our last "save_time", presumably the last Full save
88 if (ff_pkt->incremental && !S_ISDIR(ff_pkt->statp.st_mode)) {
89 Dmsg1(100, "Non-directory incremental: %s\n", ff_pkt->fname);
91 if (ff_pkt->statp.st_mtime < ff_pkt->save_time
92 && (ff_pkt->mtime_only ||
93 ff_pkt->statp.st_ctime < ff_pkt->save_time)) {
94 /* Incremental option, file not changed */
95 ff_pkt->type = FT_NOCHG;
96 return handle_file(ff_pkt, pkt);
100 /* ***FIXME*** implement this */
102 /* See if we are trying to dump the archive. */
103 if (ar_dev && ff_pkt->statp.st_dev == ar_dev && ff_pkt->statp.st_ino == ar_ino) {
104 ff_pkt->type = FT_ISARCH;
105 return handle_file(ff_pkt, pkt);
110 * Handle hard linked files
112 * Maintain a list of hard linked files already backed up. This
113 * allows us to ensure that the data of each file gets backed
116 if (ff_pkt->statp.st_nlink > 1
117 && (S_ISREG(ff_pkt->statp.st_mode)
118 || S_ISCHR(ff_pkt->statp.st_mode)
119 || S_ISBLK(ff_pkt->statp.st_mode)
120 || S_ISFIFO(ff_pkt->statp.st_mode)
121 || S_ISSOCK(ff_pkt->statp.st_mode))) {
125 /* Search link list of hard linked files */
126 for (lp = ff_pkt->linklist; lp; lp = lp->next)
127 if (lp->ino == ff_pkt->statp.st_ino && lp->dev == ff_pkt->statp.st_dev) {
128 ff_pkt->link = lp->name;
129 ff_pkt->type = FT_LNKSAVED; /* Handle link, file already saved */
130 ff_pkt->LinkFI = lp->FileIndex;
131 return handle_file(ff_pkt, pkt);
134 /* File not previously dumped. Chain it into our list. */
135 lp = (struct f_link *)bmalloc(sizeof(struct f_link) + strlen(fname) +1);
136 lp->ino = ff_pkt->statp.st_ino;
137 lp->dev = ff_pkt->statp.st_dev;
138 strcpy(lp->name, fname);
139 lp->next = ff_pkt->linklist;
140 ff_pkt->linklist = lp;
141 ff_pkt->linked = lp; /* mark saved link */
143 ff_pkt->linked = NULL;
146 /* This is not a link to a previously dumped file, so dump it. */
147 if (S_ISREG(ff_pkt->statp.st_mode)) {
150 sizeleft = ff_pkt->statp.st_size;
152 /* Don't bother opening empty, world readable files. Also do not open
153 files when archive is meant for /dev/null. */
154 if (ff_pkt->null_output_device || (sizeleft == 0
155 && MODE_RALL == (MODE_RALL & ff_pkt->statp.st_mode))) {
156 ff_pkt->type = FT_REGE;
158 ff_pkt->type = FT_REG;
160 rtn_stat = handle_file(ff_pkt, pkt);
161 if (ff_pkt->linked) {
162 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
167 } else if (S_ISLNK(ff_pkt->statp.st_mode)) { /* soft link */
169 char *buffer = (char *)alloca(path_max + name_max + 102);
171 size = readlink(fname, buffer, path_max + name_max + 101);
173 /* Could not follow link */
174 ff_pkt->type = FT_NOFOLLOW;
175 ff_pkt->ff_errno = errno;
176 rtn_stat = handle_file(ff_pkt, pkt);
177 if (ff_pkt->linked) {
178 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
183 ff_pkt->link = buffer; /* point to link */
184 ff_pkt->type = FT_LNK; /* got a real link */
185 rtn_stat = handle_file(ff_pkt, pkt);
186 if (ff_pkt->linked) {
187 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
191 } else if (S_ISDIR(ff_pkt->statp.st_mode)) {
193 struct dirent *entry, *result;
198 dev_t our_device = ff_pkt->statp.st_dev;
200 if (access(fname, R_OK) == -1 && geteuid() != 0) {
201 /* Could not access() directory */
202 ff_pkt->type = FT_NOACCESS;
203 ff_pkt->ff_errno = errno;
204 rtn_stat = handle_file(ff_pkt, pkt);
205 if (ff_pkt->linked) {
206 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
211 /* Build a canonical directory name with a trailing slash in link var */
213 link_len = len + 200;
214 link = (char *)bmalloc(link_len + 2);
215 bstrncpy(link, fname, link_len);
216 /* Strip all trailing slashes */
217 while (len >= 1 && link[len - 1] == '/')
219 link[len++] = '/'; /* add back one */
223 if (ff_pkt->incremental &&
224 (ff_pkt->statp.st_mtime < ff_pkt->save_time &&
225 ff_pkt->statp.st_ctime < ff_pkt->save_time)) {
226 /* Incremental option, directory entry not changed */
227 ff_pkt->type = FT_DIRNOCHG;
229 ff_pkt->type = FT_DIR;
232 dir_ff_pkt = (FF_PKT *)bmalloc(sizeof(FF_PKT));
233 memcpy(dir_ff_pkt, ff_pkt, sizeof(FF_PKT));
234 dir_ff_pkt->fname = bstrdup(ff_pkt->fname);
235 dir_ff_pkt->link = bstrdup(ff_pkt->link);
237 ff_pkt->link = ff_pkt->fname; /* reset "link" */
240 * Do not decend into subdirectories (recurse) if the
241 * user has turned it off for this directory.
243 if (ff_pkt->flags & FO_NO_RECURSION) {
245 /* No recursion into this directory */
246 ff_pkt->type = FT_NORECURSE;
247 rtn_stat = handle_file(ff_pkt, pkt);
248 if (ff_pkt->linked) {
249 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
251 free(dir_ff_pkt->fname);
252 free(dir_ff_pkt->link);
258 * See if we are crossing file systems, and
259 * avoid doing so if the user only wants to dump one file system.
261 if (!top_level && !(ff_pkt->flags & FO_MULTIFS) &&
262 parent_device != ff_pkt->statp.st_dev) {
264 /* returning here means we do not handle this directory */
265 ff_pkt->type = FT_NOFSCHG;
266 rtn_stat = handle_file(ff_pkt, pkt);
267 if (ff_pkt->linked) {
268 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
270 free(dir_ff_pkt->fname);
271 free(dir_ff_pkt->link);
276 * Open directory for reading files within
279 if ((directory = opendir(fname)) == NULL) {
281 ff_pkt->type = FT_NOOPEN;
282 ff_pkt->ff_errno = errno;
283 rtn_stat = handle_file(ff_pkt, pkt);
284 if (ff_pkt->linked) {
285 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
287 free(dir_ff_pkt->fname);
288 free(dir_ff_pkt->link);
294 * Process all files in this directory entry (recursing).
295 * This would possibly run faster if we chdir to the directory
296 * before traversing it.
299 entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 100);
300 for ( ; !job_canceled(jcr); ) {
304 status = readdir_r(directory, entry, &result);
305 if (status != 0 || result == NULL) {
308 ASSERT(name_max+1 > sizeof(struct dirent) + (int)NAMELEN(entry));
310 /* Skip `.', `..', and excluded file names. */
311 if (p[0] == '\0' || (p[0] == '.' && (p[1] == '\0' ||
312 (p[1] == '.' && p[2] == '\0')))) {
316 if ((int)NAMELEN(entry) + len >= link_len) {
317 link_len = len + NAMELEN(entry) + 1;
318 link = (char *)brealloc(link, link_len + 1);
321 for (i=0; i < (int)NAMELEN(entry); i++) {
325 if (!file_is_excluded(ff_pkt, link)) {
326 rtn_stat = find_one_file(jcr, ff_pkt, handle_file, pkt, link, our_device, 0);
327 if (ff_pkt->linked) {
328 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
337 * Now that we have recursed through all the files in the
338 * directory, we "save" the directory so that after all
339 * the files are restored, this entry will serve to reset
340 * the directory modes and dates. Temp directory values
341 * were used without this record.
343 handle_file(dir_ff_pkt, pkt); /* handle directory entry */
344 if (ff_pkt->linked) {
345 ff_pkt->linked->FileIndex = dir_ff_pkt->FileIndex;
347 free(dir_ff_pkt->fname);
348 free(dir_ff_pkt->link);
351 if (ff_pkt->atime_preserve) {
352 utime(fname, &restore_times);
355 } /* end check for directory */
358 * If it is explicitly mentioned (i.e. top_level) and is
359 * a block device, we do a raw backup of it or if it is
360 * a fifo, we simply read it.
362 if (top_level && S_ISBLK(ff_pkt->statp.st_mode)) {
363 ff_pkt->type = FT_RAW; /* raw partition */
364 } else if (top_level && S_ISFIFO(ff_pkt->statp.st_mode) &&
365 ff_pkt->flags & FO_READFIFO) {
366 ff_pkt->type = FT_FIFO;
368 /* The only remaining types are special (character, ...) files */
369 ff_pkt->type = FT_SPEC;
371 rtn_stat = handle_file(ff_pkt, pkt);
372 if (ff_pkt->linked) {
373 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
378 int term_find_one(FF_PKT *ff)
380 struct f_link *lp, *lc;
383 /* Free up list of hard linked files */
384 for (lp = ff->linklist; lp;) {