2 Copyright (C) 2000-2006 Kern Sibbald
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 version 2 as amended with additional clauses defined in the
7 file LICENSE in the main source directory.
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
12 the file LICENSE for additional details.
18 This file is based on GNU TAR source code. Except for a few key
19 ideas, it has been entirely rewritten for Bacula.
23 Thanks to the TAR programmers.
32 #include <sys/param.h>
33 #include <sys/mount.h>
37 extern int32_t name_max; /* filename max length */
38 extern int32_t path_max; /* path name max length */
41 * Structure for keeping track of hard linked files, we
42 * keep an entry for each hardlinked file that we save,
43 * which is the first one found. For all the other files that
44 * are linked to this one, we save only the directory
45 * entry so we can link it.
49 dev_t dev; /* device */
50 ino_t ino; /* inode with device is unique */
52 uint32_t FileIndex; /* Bacula FileIndex of this file */
53 char name[1]; /* The name */
56 typedef struct f_link link_t;
57 #define LINK_HASHTABLE_BITS 16
58 #define LINK_HASHTABLE_SIZE (1<<LINK_HASHTABLE_BITS)
59 #define LINK_HASHTABLE_MASK (LINK_HASHTABLE_SIZE-1)
61 static inline int LINKHASH(const struct stat &info)
63 int hash = info.st_dev;
64 unsigned long long i = info.st_ino;
72 return hash & LINK_HASHTABLE_MASK;
75 static void free_dir_ff_pkt(FF_PKT *dir_ff_pkt)
77 free(dir_ff_pkt->fname);
78 free(dir_ff_pkt->link);
79 free_pool_memory(dir_ff_pkt->sys_fname);
84 * Check to see if we allow the file system type of a file or directory.
85 * If we do not have a list of file system types, we accept anything.
87 static int accept_fstype(FF_PKT *ff, void *dummy) {
92 if (ff->fstypes.size()) {
94 if (!fstype(ff->fname, fs, sizeof(fs))) {
95 Dmsg1(50, "Cannot determine file system type for \"%s\"\n", ff->fname);
97 for (i = 0; i < ff->fstypes.size(); ++i) {
98 if (strcmp(fs, (char *)ff->fstypes.get(i)) == 0) {
99 Dmsg2(100, "Accepting fstype %s for \"%s\"\n", fs, ff->fname);
103 Dmsg3(200, "fstype %s for \"%s\" does not match %s\n", fs,
104 ff->fname, ff->fstypes.get(i));
112 * This function determines whether we can use getattrlist()
113 * It's odd, but we have to use the function to determine that...
114 * Also, the man pages talk about things as if they were implemented.
116 * On Mac OS X, this succesfully differentiates between HFS+ and UFS
117 * volumes, which makes me trust it is OK for others, too.
119 static bool volume_has_attrlist(const char *fname)
121 #ifdef HAVE_DARWIN_OS
123 struct volinfo_struct {
124 unsigned long length; /* Mandatory field */
125 vol_capabilities_attr_t info; /* Volume capabilities */
127 struct attrlist attrList;
129 memset(&attrList, 0, sizeof(attrList));
130 attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
131 attrList.volattr = ATTR_VOL_INFO | ATTR_VOL_CAPABILITIES;
132 if (statfs(fname, &st) == 0) {
133 /* We need to check on the mount point */
134 if (getattrlist(st.f_mntonname, &attrList, &vol, sizeof(vol), FSOPT_NOFOLLOW) == 0
135 && (vol.info.capabilities[VOL_CAPABILITIES_INTERFACES] & VOL_CAP_INT_ATTRLIST)
136 && (vol.info.valid[VOL_CAPABILITIES_INTERFACES] & VOL_CAP_INT_ATTRLIST)) {
145 * Find a single file.
146 * handle_file is the callback for handling the file.
148 * parent_device is the device we are currently on
149 * top_level is 1 when not recursing or 0 when
150 * descending into a directory.
153 find_one_file(JCR *jcr, FF_PKT *ff_pkt,
154 int handle_file(FF_PKT *ff, void *hpkt, bool top_level),
155 void *pkt, char *fname, dev_t parent_device, bool top_level)
157 struct utimbuf restore_times;
161 ff_pkt->fname = ff_pkt->link = fname;
163 if (lstat(fname, &ff_pkt->statp) != 0) {
164 /* Cannot stat file */
165 ff_pkt->type = FT_NOSTAT;
166 ff_pkt->ff_errno = errno;
167 return handle_file(ff_pkt, pkt, top_level);
170 Dmsg1(300, "File ----: %s\n", fname);
172 /* Save current times of this directory in case we need to
173 * reset them because the user doesn't want them changed.
175 restore_times.actime = ff_pkt->statp.st_atime;
176 restore_times.modtime = ff_pkt->statp.st_mtime;
179 * We check for allowed fstypes at top_level and fstype change (below).
182 if (!accept_fstype(ff_pkt, NULL)) {
183 ff_pkt->type = FT_INVALIDFS;
184 if (ff_pkt->flags & FO_KEEPATIME) {
185 utime(fname, &restore_times);
187 Jmsg1(jcr, M_ERROR, 0, _("Top level directory \"%s\" has an unlisted fstype\n"), fname);
188 return 1; /* Just ignore this error - or the whole backup is cancelled */
190 ff_pkt->volhas_attrlist = volume_has_attrlist(fname);
194 * If this is an Incremental backup, see if file was modified
195 * since our last "save_time", presumably the last Full save
198 if (ff_pkt->incremental && !S_ISDIR(ff_pkt->statp.st_mode)) {
199 Dmsg1(300, "Non-directory incremental: %s\n", ff_pkt->fname);
200 /* Not a directory */
201 if (ff_pkt->statp.st_mtime < ff_pkt->save_time
202 && ((ff_pkt->flags & FO_MTIMEONLY) ||
203 ff_pkt->statp.st_ctime < ff_pkt->save_time)) {
204 /* Incremental option, file not changed */
205 ff_pkt->type = FT_NOCHG;
206 return handle_file(ff_pkt, pkt, top_level);
210 #ifdef HAVE_DARWIN_OS
211 if (ff_pkt->flags & FO_HFSPLUS && ff_pkt->volhas_attrlist
212 && S_ISREG(ff_pkt->statp.st_mode)) {
213 /* TODO: initialise attrList once elsewhere? */
214 struct attrlist attrList;
215 memset(&attrList, 0, sizeof(attrList));
216 attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
217 attrList.commonattr = ATTR_CMN_FNDRINFO;
218 attrList.fileattr = ATTR_FILE_RSRCLENGTH;
219 if (getattrlist(fname, &attrList, &ff_pkt->hfsinfo,
220 sizeof(ff_pkt->hfsinfo), FSOPT_NOFOLLOW) != 0) {
221 ff_pkt->type = FT_NOSTAT;
222 ff_pkt->ff_errno = errno;
223 return handle_file(ff_pkt, pkt, top_level);
228 /* ***FIXME*** implement this */
230 /* See if we are trying to dump the archive. */
231 if (ar_dev && ff_pkt->statp.st_dev == ar_dev && ff_pkt->statp.st_ino == ar_ino) {
232 ff_pkt->type = FT_ISARCH;
233 return handle_file(ff_pkt, pkt, top_level);
238 * Handle hard linked files
240 * Maintain a list of hard linked files already backed up. This
241 * allows us to ensure that the data of each file gets backed
244 if (!(ff_pkt->flags & FO_NO_HARDLINK)
245 && ff_pkt->statp.st_nlink > 1
246 && (S_ISREG(ff_pkt->statp.st_mode)
247 || S_ISCHR(ff_pkt->statp.st_mode)
248 || S_ISBLK(ff_pkt->statp.st_mode)
249 || S_ISFIFO(ff_pkt->statp.st_mode)
250 || S_ISSOCK(ff_pkt->statp.st_mode))) {
253 if (ff_pkt->linkhash == NULL) {
254 ff_pkt->linkhash = (link_t **)bmalloc(LINK_HASHTABLE_SIZE * sizeof(link_t *));
255 memset(ff_pkt->linkhash, 0, LINK_HASHTABLE_SIZE * sizeof(link_t *));
257 const int linkhash = LINKHASH(ff_pkt->statp);
259 /* Search link list of hard linked files */
260 for (lp = ff_pkt->linkhash[linkhash]; lp; lp = lp->next)
261 if (lp->ino == (ino_t)ff_pkt->statp.st_ino &&
262 lp->dev == (dev_t)ff_pkt->statp.st_dev) {
263 /* If we have already backed up the hard linked file don't do it again */
264 if (strcmp(lp->name, fname) == 0) {
265 return 1; /* ignore */
267 ff_pkt->link = lp->name;
268 ff_pkt->type = FT_LNKSAVED; /* Handle link, file already saved */
269 ff_pkt->LinkFI = lp->FileIndex;
270 return handle_file(ff_pkt, pkt, top_level);
273 /* File not previously dumped. Chain it into our list. */
274 len = strlen(fname) + 1;
275 lp = (struct f_link *)bmalloc(sizeof(struct f_link) + len);
276 lp->ino = ff_pkt->statp.st_ino;
277 lp->dev = ff_pkt->statp.st_dev;
278 bstrncpy(lp->name, fname, len);
279 lp->next = ff_pkt->linkhash[linkhash];
280 ff_pkt->linkhash[linkhash] = lp;
281 ff_pkt->linked = lp; /* mark saved link */
283 ff_pkt->linked = NULL;
286 /* This is not a link to a previously dumped file, so dump it. */
287 if (S_ISREG(ff_pkt->statp.st_mode)) {
290 sizeleft = ff_pkt->statp.st_size;
292 /* Don't bother opening empty, world readable files. Also do not open
293 files when archive is meant for /dev/null. */
294 if (ff_pkt->null_output_device || (sizeleft == 0
295 && MODE_RALL == (MODE_RALL & ff_pkt->statp.st_mode))) {
296 ff_pkt->type = FT_REGE;
298 ff_pkt->type = FT_REG;
300 rtn_stat = handle_file(ff_pkt, pkt, top_level);
301 if (ff_pkt->linked) {
302 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
304 if (ff_pkt->flags & FO_KEEPATIME) {
305 utime(fname, &restore_times);
310 } else if (S_ISLNK(ff_pkt->statp.st_mode)) { /* soft link */
312 char *buffer = (char *)alloca(path_max + name_max + 102);
314 size = readlink(fname, buffer, path_max + name_max + 101);
316 /* Could not follow link */
317 ff_pkt->type = FT_NOFOLLOW;
318 ff_pkt->ff_errno = errno;
319 rtn_stat = handle_file(ff_pkt, pkt, top_level);
320 if (ff_pkt->linked) {
321 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
326 ff_pkt->link = buffer; /* point to link */
327 ff_pkt->type = FT_LNK; /* got a real link */
328 rtn_stat = handle_file(ff_pkt, pkt, top_level);
329 if (ff_pkt->linked) {
330 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
334 } else if (S_ISDIR(ff_pkt->statp.st_mode)) {
336 struct dirent *entry, *result;
341 dev_t our_device = ff_pkt->statp.st_dev;
343 bool volhas_attrlist = ff_pkt->volhas_attrlist; /* Remember this if we recurse */
346 * If we are using Win32 (non-portable) backup API, don't check
347 * access as everything is more complicated, and
348 * in principle, we should be able to access everything.
350 if (!have_win32_api() || (ff_pkt->flags & FO_PORTABLE)) {
351 if (access(fname, R_OK) == -1 && geteuid() != 0) {
352 /* Could not access() directory */
353 ff_pkt->type = FT_NOACCESS;
354 ff_pkt->ff_errno = errno;
355 rtn_stat = handle_file(ff_pkt, pkt, top_level);
356 if (ff_pkt->linked) {
357 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
363 /* Build a canonical directory name with a trailing slash in link var */
365 link_len = len + 200;
366 link = (char *)bmalloc(link_len + 2);
367 bstrncpy(link, fname, link_len);
368 /* Strip all trailing slashes */
369 while (len >= 1 && link[len - 1] == '/')
371 link[len++] = '/'; /* add back one */
375 if (ff_pkt->incremental &&
376 (ff_pkt->statp.st_mtime < ff_pkt->save_time &&
377 ((ff_pkt->flags & FO_MTIMEONLY) ||
378 ff_pkt->statp.st_ctime < ff_pkt->save_time))) {
379 /* Incremental option, directory entry not changed */
380 ff_pkt->type = FT_DIRNOCHG;
382 ff_pkt->type = FT_DIRBEGIN;
385 * Note, we return the directory to the calling program (handle_file)
386 * when we first see the directory (FT_DIRBEGIN.
387 * This allows the program to apply matches and make a
388 * choice whether or not to accept it. If it is accepted, we
389 * do not immediately save it, but do so only after everything
390 * in the directory is seen (i.e. the FT_DIREND).
392 rtn_stat = handle_file(ff_pkt, pkt, top_level);
393 if (rtn_stat < 1) { /* ignore or error status */
397 /* Done with DIRBEGIN, next call will be DIREND */
398 if (ff_pkt->type == FT_DIRBEGIN) {
399 ff_pkt->type = FT_DIREND;
403 * Create a temporary ff packet for this directory
404 * entry, and defer handling the directory until
405 * we have recursed into it. This saves the
406 * directory after all files have been processed, and
407 * during the restore, the directory permissions will
408 * be reset after all the files have been restored.
410 Dmsg1(300, "Create temp ff packet for dir: %s\n", ff_pkt->fname);
411 FF_PKT *dir_ff_pkt = (FF_PKT *)bmalloc(sizeof(FF_PKT));
412 memcpy(dir_ff_pkt, ff_pkt, sizeof(FF_PKT));
413 dir_ff_pkt->fname = bstrdup(ff_pkt->fname);
414 dir_ff_pkt->link = bstrdup(ff_pkt->link);
415 dir_ff_pkt->sys_fname = get_pool_memory(PM_FNAME);
416 dir_ff_pkt->included_files_list = NULL;
417 dir_ff_pkt->excluded_files_list = NULL;
418 dir_ff_pkt->excluded_paths_list = NULL;
419 dir_ff_pkt->linkhash = NULL;
422 * Do not descend into subdirectories (recurse) if the
423 * user has turned it off for this directory.
425 * If we are crossing file systems, we are either not allowed
426 * to cross, or we may be restricted by a list of permitted
429 if (!top_level && ff_pkt->flags & FO_NO_RECURSION) {
430 ff_pkt->type = FT_NORECURSE;
432 } else if (!top_level && parent_device != ff_pkt->statp.st_dev) {
433 if(!(ff_pkt->flags & FO_MULTIFS)) {
434 ff_pkt->type = FT_NOFSCHG;
436 } else if (!accept_fstype(ff_pkt, NULL)) {
437 ff_pkt->type = FT_INVALIDFS;
440 ff_pkt->volhas_attrlist = volume_has_attrlist(fname);
443 /* If not recursing, just backup dir and return */
445 rtn_stat = handle_file(ff_pkt, pkt, top_level);
446 if (ff_pkt->linked) {
447 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
450 free_dir_ff_pkt(dir_ff_pkt);
451 ff_pkt->link = ff_pkt->fname; /* reset "link" */
452 if (ff_pkt->flags & FO_KEEPATIME) {
453 utime(fname, &restore_times);
458 ff_pkt->link = ff_pkt->fname; /* reset "link" */
461 * Descend into or "recurse" into the directory to read
462 * all the files in it.
465 if ((directory = opendir(fname)) == NULL) {
466 ff_pkt->type = FT_NOOPEN;
467 ff_pkt->ff_errno = errno;
468 rtn_stat = handle_file(ff_pkt, pkt, top_level);
469 if (ff_pkt->linked) {
470 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
473 free_dir_ff_pkt(dir_ff_pkt);
478 * Process all files in this directory entry (recursing).
479 * This would possibly run faster if we chdir to the directory
480 * before traversing it.
483 entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 100);
484 for ( ; !job_canceled(jcr); ) {
488 status = readdir_r(directory, entry, &result);
489 if (status != 0 || result == NULL) {
490 // Dmsg2(99, "readdir returned stat=%d result=0x%x\n",
491 // status, (long)result);
494 ASSERT(name_max+1 > (int)sizeof(struct dirent) + (int)NAMELEN(entry));
496 /* Skip `.', `..', and excluded file names. */
497 if (p[0] == '\0' || (p[0] == '.' && (p[1] == '\0' ||
498 (p[1] == '.' && p[2] == '\0')))) {
502 if ((int)NAMELEN(entry) + len >= link_len) {
503 link_len = len + NAMELEN(entry) + 1;
504 link = (char *)brealloc(link, link_len + 1);
507 for (i=0; i < (int)NAMELEN(entry); i++) {
511 if (!file_is_excluded(ff_pkt, link)) {
512 rtn_stat = find_one_file(jcr, ff_pkt, handle_file, pkt, link, our_device, false);
513 if (ff_pkt->linked) {
514 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
523 * Now that we have recursed through all the files in the
524 * directory, we "save" the directory so that after all
525 * the files are restored, this entry will serve to reset
526 * the directory modes and dates. Temp directory values
527 * were used without this record.
529 handle_file(dir_ff_pkt, pkt, top_level); /* handle directory entry */
530 if (ff_pkt->linked) {
531 ff_pkt->linked->FileIndex = dir_ff_pkt->FileIndex;
533 free_dir_ff_pkt(dir_ff_pkt);
535 if (ff_pkt->flags & FO_KEEPATIME) {
536 utime(fname, &restore_times);
538 ff_pkt->volhas_attrlist = volhas_attrlist; /* Restore value in case it changed. */
540 } /* end check for directory */
543 * If it is explicitly mentioned (i.e. top_level) and is
544 * a block device, we do a raw backup of it or if it is
545 * a fifo, we simply read it.
547 #ifdef HAVE_FREEBSD_OS
549 * On FreeBSD, all block devices are character devices, so
550 * to be able to read a raw disk, we need the check for
551 * a character device.
552 * crw-r----- 1 root operator - 116, 0x00040002 Jun 9 19:32 /dev/ad0s3
553 * crw-r----- 1 root operator - 116, 0x00040002 Jun 9 19:32 /dev/rad0s3
555 if (top_level && (S_ISBLK(ff_pkt->statp.st_mode) || S_ISCHR(ff_pkt->statp.st_mode))) {
557 if (top_level && S_ISBLK(ff_pkt->statp.st_mode)) {
559 ff_pkt->type = FT_RAW; /* raw partition */
560 } else if (top_level && S_ISFIFO(ff_pkt->statp.st_mode) &&
561 ff_pkt->flags & FO_READFIFO) {
562 ff_pkt->type = FT_FIFO;
564 /* The only remaining types are special (character, ...) files */
565 ff_pkt->type = FT_SPEC;
567 rtn_stat = handle_file(ff_pkt, pkt, top_level);
568 if (ff_pkt->linked) {
569 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
574 int term_find_one(FF_PKT *ff)
576 struct f_link *lp, *lc;
581 if (ff->linkhash == NULL) return 0;
583 for (i =0 ; i < LINK_HASHTABLE_SIZE; i ++) {
584 /* Free up list of hard linked files */
585 lp = ff->linkhash[i];
594 ff->linkhash[i] = NULL;