]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/find_one.c
- Get next volume from Scratch pool before creating a volume.
[bacula/bacula] / bacula / src / findlib / find_one.c
1 /*
2    Copyright (C) 2000-2005 Kern Sibbald
3
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.
8
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.
13
14  */
15
16 /*
17
18    This file is based on GNU TAR source code. Except for a few key
19    ideas, it has been entirely rewritten for Bacula.
20
21       Kern Sibbald, MM
22
23    Thanks to the TAR programmers.
24
25      Version $Id$
26
27  */
28
29 #include "bacula.h"
30 #include "find.h"
31 #ifdef HAVE_DARWIN_OS
32 #include <sys/param.h>
33 #include <sys/mount.h>
34 #include <sys/attr.h>
35 #endif
36
37 extern int32_t name_max;              /* filename max length */
38 extern int32_t path_max;              /* path name max length */
39
40 /*
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.
46  */
47 struct f_link {
48     struct f_link *next;
49     dev_t dev;                        /* device */
50     ino_t ino;                        /* inode with device is unique */
51     short linkcount;
52     uint32_t FileIndex;               /* Bacula FileIndex of this file */
53     char name[1];                     /* The name */
54 };
55
56 static void free_dir_ff_pkt(FF_PKT *dir_ff_pkt)
57 {
58    free(dir_ff_pkt->fname);
59    free(dir_ff_pkt->link);
60    free_pool_memory(dir_ff_pkt->sys_fname);
61    free(dir_ff_pkt);
62 }
63
64 /*
65  * Check to see if we allow the file system type of a file or directory.
66  * If we do not have a list of file system types, we accept anything.
67  */
68 static int accept_fstype(FF_PKT *ff, void *dummy) {
69    int i;
70    char fs[1000];
71    bool accept = true;
72
73    if (ff->fstypes.size()) {
74       accept = false;
75       if (!fstype(ff->fname, fs, sizeof(fs))) {
76          Dmsg1(50, "Cannot determine file system type for \"%s\"\n", ff->fname);
77       } else {
78          for (i = 0; i < ff->fstypes.size(); ++i) {
79             if (strcmp(fs, (char *)ff->fstypes.get(i)) == 0) {
80                Dmsg2(100, "Accepting fstype %s for \"%s\"\n", fs, ff->fname);
81                accept = true;
82                break;
83             }
84             Dmsg3(200, "fstype %s for \"%s\" does not match %s\n", fs,
85                   ff->fname, ff->fstypes.get(i));
86          }
87       }
88    }
89    return accept;
90 }
91
92 /*
93  * This function determines whether we can use getattrlist()
94  * It's odd, but we have to use the function to determine that...
95  * Also, the man pages talk about things as if they were implemented.
96  *
97  * On Mac OS X, this succesfully differentiates between HFS+ and UFS
98  * volumes, which makes me trust it is OK for others, too.
99  */
100 static bool volume_has_attrlist(const char *fname)
101 {
102 #ifdef HAVE_DARWIN_OS
103    struct statfs st;
104    struct volinfo_struct {
105       unsigned long length;               /* Mandatory field */
106       vol_capabilities_attr_t info;       /* Volume capabilities */
107    } vol;
108    struct attrlist attrList;
109
110    memset(&attrList, 0, sizeof(attrList));
111    attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
112    attrList.volattr = ATTR_VOL_INFO | ATTR_VOL_CAPABILITIES;
113    if (statfs(fname, &st) == 0) {
114       /* We need to check on the mount point */
115       if (getattrlist(st.f_mntonname, &attrList, &vol, sizeof(vol), FSOPT_NOFOLLOW) == 0
116             && (vol.info.capabilities[VOL_CAPABILITIES_INTERFACES] & VOL_CAP_INT_ATTRLIST)
117             && (vol.info.valid[VOL_CAPABILITIES_INTERFACES] & VOL_CAP_INT_ATTRLIST)) {
118          return true;
119       }
120    }
121 #endif
122    return false;
123 }
124
125 /*
126  * Find a single file.
127  * handle_file is the callback for handling the file.
128  * p is the filename
129  * parent_device is the device we are currently on
130  * top_level is 1 when not recursing or 0 when
131  *  descending into a directory.
132  */
133 int
134 find_one_file(JCR *jcr, FF_PKT *ff_pkt, 
135                int handle_file(FF_PKT *ff, void *hpkt, bool top_level),
136                void *pkt, char *fname, dev_t parent_device, bool top_level)
137 {
138    struct utimbuf restore_times;
139    int rtn_stat;
140    int len;
141
142    ff_pkt->fname = ff_pkt->link = fname;
143
144    if (lstat(fname, &ff_pkt->statp) != 0) {
145        /* Cannot stat file */
146        ff_pkt->type = FT_NOSTAT;
147        ff_pkt->ff_errno = errno;
148        return handle_file(ff_pkt, pkt, top_level);
149    }
150
151    Dmsg1(300, "File ----: %s\n", fname);
152
153    /* Save current times of this directory in case we need to
154     * reset them because the user doesn't want them changed.
155     */
156    restore_times.actime = ff_pkt->statp.st_atime;
157    restore_times.modtime = ff_pkt->statp.st_mtime;
158
159    /*
160     * We check for allowed fstypes at top_level and fstype change (below).
161     */
162    if (top_level) {
163       if (!accept_fstype(ff_pkt, NULL)) {
164          ff_pkt->type = FT_INVALIDFS;
165          if (ff_pkt->flags & FO_KEEPATIME) {
166             utime(fname, &restore_times);
167          }
168          Jmsg1(jcr, M_ERROR, 0, _("Top level directory \"%s\" has an unlisted fstype\n"), fname);
169          return 1;      /* Just ignore this error - or the whole backup is cancelled */
170       }
171       ff_pkt->volhas_attrlist = volume_has_attrlist(fname);
172    }
173
174    /*
175     * If this is an Incremental backup, see if file was modified
176     * since our last "save_time", presumably the last Full save
177     * or Incremental.
178     */
179    if (ff_pkt->incremental && !S_ISDIR(ff_pkt->statp.st_mode)) {
180       Dmsg1(300, "Non-directory incremental: %s\n", ff_pkt->fname);
181       /* Not a directory */
182       if (ff_pkt->statp.st_mtime < ff_pkt->save_time
183           && ((ff_pkt->flags & FO_MTIMEONLY) ||
184               ff_pkt->statp.st_ctime < ff_pkt->save_time)) {
185          /* Incremental option, file not changed */
186          ff_pkt->type = FT_NOCHG;
187          return handle_file(ff_pkt, pkt, top_level);
188       }
189    }
190
191 #ifdef HAVE_DARWIN_OS
192    if (ff_pkt->flags & FO_HFSPLUS && ff_pkt->volhas_attrlist
193          && S_ISREG(ff_pkt->statp.st_mode)) {
194        /* TODO: initialise attrList once elsewhere? */
195        struct attrlist attrList;
196        memset(&attrList, 0, sizeof(attrList));
197        attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
198        attrList.commonattr = ATTR_CMN_FNDRINFO;
199        attrList.fileattr = ATTR_FILE_RSRCLENGTH;
200        if (getattrlist(fname, &attrList, &ff_pkt->hfsinfo,
201                 sizeof(ff_pkt->hfsinfo), FSOPT_NOFOLLOW) != 0) {
202           ff_pkt->type = FT_NOSTAT;
203           ff_pkt->ff_errno = errno;
204           return handle_file(ff_pkt, pkt, top_level);
205        }
206    }
207 #endif
208
209 /* ***FIXME*** implement this */
210 #if xxxxxxx
211    /* See if we are trying to dump the archive.  */
212    if (ar_dev && ff_pkt->statp.st_dev == ar_dev && ff_pkt->statp.st_ino == ar_ino) {
213        ff_pkt->type = FT_ISARCH;
214        return handle_file(ff_pkt, pkt, top_level);
215    }
216 #endif
217    ff_pkt->LinkFI = 0;
218    /*
219     * Handle hard linked files
220     *
221     * Maintain a list of hard linked files already backed up. This
222     *  allows us to ensure that the data of each file gets backed
223     *  up only once.
224     */
225    if (!(ff_pkt->flags & FO_NO_HARDLINK)
226        && ff_pkt->statp.st_nlink > 1
227        && (S_ISREG(ff_pkt->statp.st_mode)
228            || S_ISCHR(ff_pkt->statp.st_mode)
229            || S_ISBLK(ff_pkt->statp.st_mode)
230            || S_ISFIFO(ff_pkt->statp.st_mode)
231            || S_ISSOCK(ff_pkt->statp.st_mode))) {
232
233        struct f_link *lp;
234
235       /* Search link list of hard linked files */
236       for (lp = ff_pkt->linklist; lp; lp = lp->next)
237          if (lp->ino == (ino_t)ff_pkt->statp.st_ino &&
238              lp->dev == (dev_t)ff_pkt->statp.st_dev) {
239              /* If we have already backed up the hard linked file don't do it again */
240              if (strcmp(lp->name, fname) == 0) {
241                 Jmsg1(jcr, M_WARNING, 0, _("Attempt to backup hard linked file %s twice ignored.\n"),
242                    fname);
243                 return 1;             /* ignore */
244              }
245              ff_pkt->link = lp->name;
246              ff_pkt->type = FT_LNKSAVED;       /* Handle link, file already saved */
247              ff_pkt->LinkFI = lp->FileIndex;
248              return handle_file(ff_pkt, pkt, top_level);
249          }
250
251       /* File not previously dumped. Chain it into our list. */
252       len = strlen(fname) + 1;
253       lp = (struct f_link *)bmalloc(sizeof(struct f_link) + len);
254       lp->ino = ff_pkt->statp.st_ino;
255       lp->dev = ff_pkt->statp.st_dev;
256       bstrncpy(lp->name, fname, len);
257       lp->next = ff_pkt->linklist;
258       ff_pkt->linklist = lp;
259       ff_pkt->linked = lp;            /* mark saved link */
260    } else {
261       ff_pkt->linked = NULL;
262    }
263
264    /* This is not a link to a previously dumped file, so dump it.  */
265    if (S_ISREG(ff_pkt->statp.st_mode)) {
266       off_t sizeleft;
267
268       sizeleft = ff_pkt->statp.st_size;
269
270       /* Don't bother opening empty, world readable files.  Also do not open
271          files when archive is meant for /dev/null.  */
272       if (ff_pkt->null_output_device || (sizeleft == 0
273               && MODE_RALL == (MODE_RALL & ff_pkt->statp.st_mode))) {
274          ff_pkt->type = FT_REGE;
275       } else {
276          ff_pkt->type = FT_REG;
277       }
278       rtn_stat = handle_file(ff_pkt, pkt, top_level);
279       if (ff_pkt->linked) {
280          ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
281       }
282       if (ff_pkt->flags & FO_KEEPATIME) {
283          utime(fname, &restore_times);
284       }       
285       return rtn_stat;
286
287
288    } else if (S_ISLNK(ff_pkt->statp.st_mode)) {  /* soft link */
289       int size;
290       char *buffer = (char *)alloca(path_max + name_max + 102);
291
292       size = readlink(fname, buffer, path_max + name_max + 101);
293       if (size < 0) {
294          /* Could not follow link */
295          ff_pkt->type = FT_NOFOLLOW;
296          ff_pkt->ff_errno = errno;
297          rtn_stat = handle_file(ff_pkt, pkt, top_level);
298          if (ff_pkt->linked) {
299             ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
300          }
301          return rtn_stat;
302       }
303       buffer[size] = 0;
304       ff_pkt->link = buffer;          /* point to link */
305       ff_pkt->type = FT_LNK;          /* got a real link */
306       rtn_stat = handle_file(ff_pkt, pkt, top_level);
307       if (ff_pkt->linked) {
308          ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
309       }
310       return rtn_stat;
311
312    } else if (S_ISDIR(ff_pkt->statp.st_mode)) {
313       DIR *directory;
314       struct dirent *entry, *result;
315       char *link;
316       int link_len;
317       int len;
318       int status;
319       dev_t our_device = ff_pkt->statp.st_dev;
320       bool recurse = true;
321       bool volhas_attrlist = ff_pkt->volhas_attrlist;    /* Remember this if we recurse */
322
323       /*
324        * If we are using Win32 (non-portable) backup API, don't check
325        *  access as everything is more complicated, and
326        *  in principle, we should be able to access everything.
327        */
328       if (!have_win32_api() || (ff_pkt->flags & FO_PORTABLE)) {
329          if (access(fname, R_OK) == -1 && geteuid() != 0) {
330             /* Could not access() directory */
331             ff_pkt->type = FT_NOACCESS;
332             ff_pkt->ff_errno = errno;
333             rtn_stat = handle_file(ff_pkt, pkt, top_level);
334             if (ff_pkt->linked) {
335                ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
336             }
337             return rtn_stat;
338          }
339       }
340
341       /* Build a canonical directory name with a trailing slash in link var */
342       len = strlen(fname);
343       link_len = len + 200;
344       link = (char *)bmalloc(link_len + 2);
345       bstrncpy(link, fname, link_len);
346       /* Strip all trailing slashes */
347       while (len >= 1 && link[len - 1] == '/')
348         len--;
349       link[len++] = '/';             /* add back one */
350       link[len] = 0;
351
352       ff_pkt->link = link;
353       if (ff_pkt->incremental &&
354           (ff_pkt->statp.st_mtime < ff_pkt->save_time &&
355              ((ff_pkt->flags & FO_MTIMEONLY) ||
356                ff_pkt->statp.st_ctime < ff_pkt->save_time))) {
357          /* Incremental option, directory entry not changed */
358          ff_pkt->type = FT_DIRNOCHG;
359       } else {
360          ff_pkt->type = FT_DIRBEGIN;
361       }
362       /*
363        * Note, we return the directory to the calling program (handle_file)
364        * when we first see the directory (FT_DIRBEGIN.
365        * This allows the program to apply matches and make a
366        * choice whether or not to accept it.  If it is accepted, we
367        * do not immediately save it, but do so only after everything
368        * in the directory is seen (i.e. the FT_DIREND).
369        */
370       rtn_stat = handle_file(ff_pkt, pkt, top_level);
371       if (rtn_stat < 1) {             /* ignore or error status */
372          free(link);
373          return rtn_stat;
374       }
375       /* Done with DIRBEGIN, next call will be DIREND */
376       if (ff_pkt->type == FT_DIRBEGIN) {
377          ff_pkt->type = FT_DIREND;
378       }
379
380       /*
381        * Create a temporary ff packet for this directory
382        *   entry, and defer handling the directory until
383        *   we have recursed into it.  This saves the
384        *   directory after all files have been processed, and
385        *   during the restore, the directory permissions will
386        *   be reset after all the files have been restored.
387        */
388       Dmsg1(300, "Create temp ff packet for dir: %s\n", ff_pkt->fname);
389       FF_PKT *dir_ff_pkt = (FF_PKT *)bmalloc(sizeof(FF_PKT));
390       memcpy(dir_ff_pkt, ff_pkt, sizeof(FF_PKT));
391       dir_ff_pkt->fname = bstrdup(ff_pkt->fname);
392       dir_ff_pkt->link = bstrdup(ff_pkt->link);
393       dir_ff_pkt->sys_fname = get_pool_memory(PM_FNAME);
394       dir_ff_pkt->included_files_list = NULL;
395       dir_ff_pkt->excluded_files_list = NULL;
396       dir_ff_pkt->excluded_paths_list = NULL;
397       dir_ff_pkt->linklist = NULL;
398
399       /*
400        * Do not descend into subdirectories (recurse) if the
401        * user has turned it off for this directory.
402        *
403        * If we are crossing file systems, we are either not allowed
404        * to cross, or we may be restricted by a list of permitted
405        * file systems.
406        */
407       if (!top_level && ff_pkt->flags & FO_NO_RECURSION) {
408          ff_pkt->type = FT_NORECURSE;
409          recurse = false;
410       } else if (!top_level && parent_device != ff_pkt->statp.st_dev) {
411          if(!(ff_pkt->flags & FO_MULTIFS)) {
412             ff_pkt->type = FT_NOFSCHG;
413             recurse = false;
414          } else if (!accept_fstype(ff_pkt, NULL)) {
415             ff_pkt->type = FT_INVALIDFS;
416             recurse = false;
417          } else {
418             ff_pkt->volhas_attrlist = volume_has_attrlist(fname);
419          }
420       }
421       /* If not recursing, just backup dir and return */
422       if (!recurse) {
423          rtn_stat = handle_file(ff_pkt, pkt, top_level);
424          if (ff_pkt->linked) {
425             ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
426          }
427          free(link);
428          free_dir_ff_pkt(dir_ff_pkt);
429          ff_pkt->link = ff_pkt->fname;     /* reset "link" */
430          if (ff_pkt->flags & FO_KEEPATIME) {
431             utime(fname, &restore_times);
432          }
433          return rtn_stat;
434       }
435
436       ff_pkt->link = ff_pkt->fname;     /* reset "link" */
437
438       /*
439        * Descend into or "recurse" into the directory to read
440        *   all the files in it.
441        */
442       errno = 0;
443       if ((directory = opendir(fname)) == NULL) {
444          ff_pkt->type = FT_NOOPEN;
445          ff_pkt->ff_errno = errno;
446          rtn_stat = handle_file(ff_pkt, pkt, top_level);
447          if (ff_pkt->linked) {
448             ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
449          }
450          free(link);
451          free_dir_ff_pkt(dir_ff_pkt);
452          return rtn_stat;
453       }
454
455       /*
456        * Process all files in this directory entry (recursing).
457        *    This would possibly run faster if we chdir to the directory
458        *    before traversing it.
459        */
460       rtn_stat = 1;
461       entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 100);
462       for ( ; !job_canceled(jcr); ) {
463          char *p, *q;
464          int i;
465
466          status  = readdir_r(directory, entry, &result);
467          if (status != 0 || result == NULL) {
468 //          Dmsg2(99, "readdir returned stat=%d result=0x%x\n",
469 //             status, (long)result);
470             break;
471          }
472          ASSERT(name_max+1 > (int)sizeof(struct dirent) + (int)NAMELEN(entry));
473          p = entry->d_name;
474          /* Skip `.', `..', and excluded file names.  */
475          if (p[0] == '\0' || (p[0] == '.' && (p[1] == '\0' ||
476              (p[1] == '.' && p[2] == '\0')))) {
477             continue;
478          }
479
480          if ((int)NAMELEN(entry) + len >= link_len) {
481              link_len = len + NAMELEN(entry) + 1;
482              link = (char *)brealloc(link, link_len + 1);
483          }
484          q = link + len;
485          for (i=0; i < (int)NAMELEN(entry); i++) {
486             *q++ = *p++;
487          }
488          *q = 0;
489          if (!file_is_excluded(ff_pkt, link)) {
490             rtn_stat = find_one_file(jcr, ff_pkt, handle_file, pkt, link, our_device, false);
491             if (ff_pkt->linked) {
492                ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
493             }
494          }
495       }
496       closedir(directory);
497       free(link);
498       free(entry);
499
500       /*
501        * Now that we have recursed through all the files in the
502        *  directory, we "save" the directory so that after all
503        *  the files are restored, this entry will serve to reset
504        *  the directory modes and dates.  Temp directory values
505        *  were used without this record.
506        */
507       handle_file(dir_ff_pkt, pkt, top_level);       /* handle directory entry */
508       if (ff_pkt->linked) {
509          ff_pkt->linked->FileIndex = dir_ff_pkt->FileIndex;
510       }
511       free_dir_ff_pkt(dir_ff_pkt);
512
513       if (ff_pkt->flags & FO_KEEPATIME) {
514          utime(fname, &restore_times);
515       }
516       ff_pkt->volhas_attrlist = volhas_attrlist;      /* Restore value in case it changed. */
517       return rtn_stat;
518    } /* end check for directory */
519
520    /*
521     * If it is explicitly mentioned (i.e. top_level) and is
522     *  a block device, we do a raw backup of it or if it is
523     *  a fifo, we simply read it.
524     */
525 #ifdef HAVE_FREEBSD_OS
526    /*
527     * On FreeBSD, all block devices are character devices, so
528     *   to be able to read a raw disk, we need the check for
529     *   a character device.
530     * crw-r-----  1 root  operator  - 116, 0x00040002 Jun  9 19:32 /dev/ad0s3
531     * crw-r-----  1 root  operator  - 116, 0x00040002 Jun  9 19:32 /dev/rad0s3
532     */
533    if (top_level && (S_ISBLK(ff_pkt->statp.st_mode) || S_ISCHR(ff_pkt->statp.st_mode))) {
534 #else
535    if (top_level && S_ISBLK(ff_pkt->statp.st_mode)) {
536 #endif
537       ff_pkt->type = FT_RAW;          /* raw partition */
538    } else if (top_level && S_ISFIFO(ff_pkt->statp.st_mode) &&
539               ff_pkt->flags & FO_READFIFO) {
540       ff_pkt->type = FT_FIFO;
541    } else {
542       /* The only remaining types are special (character, ...) files */
543       ff_pkt->type = FT_SPEC;
544    }
545    rtn_stat = handle_file(ff_pkt, pkt, top_level);
546    if (ff_pkt->linked) {
547       ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
548    }
549    return rtn_stat;
550 }
551
552 int term_find_one(FF_PKT *ff)
553 {
554    struct f_link *lp, *lc;
555    int count = 0;
556
557    /* Free up list of hard linked files */
558    for (lp = ff->linklist; lp;) {
559       lc = lp;
560       lp = lp->next;
561       if (lc) {
562          free(lc);
563          count++;
564       }
565    }
566    ff->linklist = NULL;
567    return count;
568 }