]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/find_one.c
- Remove xpg4 lib from FreeBSD build as it is no longer needed and
[bacula/bacula] / bacula / src / findlib / find_one.c
1 /*
2    Copyright (C) 2000-2006 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 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)
60
61 static inline int LINKHASH(const struct stat &info)
62 {
63     int hash = info.st_dev;
64     unsigned long long i = info.st_ino;
65     hash ^= i;
66     i >>= 16;
67     hash ^= i;
68     i >>= 16;
69     hash ^= i;
70     i >>= 16;
71     hash ^= i;
72     return hash & LINK_HASHTABLE_MASK;
73 }
74
75 static void free_dir_ff_pkt(FF_PKT *dir_ff_pkt)
76 {
77    free(dir_ff_pkt->fname);
78    free(dir_ff_pkt->link);
79    free_pool_memory(dir_ff_pkt->sys_fname);
80    free(dir_ff_pkt);
81 }
82
83 /*
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.
86  */
87 static int accept_fstype(FF_PKT *ff, void *dummy) {
88    int i;
89    char fs[1000];
90    bool accept = true;
91
92    if (ff->fstypes.size()) {
93       accept = false;
94       if (!fstype(ff->fname, fs, sizeof(fs))) {
95          Dmsg1(50, "Cannot determine file system type for \"%s\"\n", ff->fname);
96       } else {
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);
100                accept = true;
101                break;
102             }
103             Dmsg3(200, "fstype %s for \"%s\" does not match %s\n", fs,
104                   ff->fname, ff->fstypes.get(i));
105          }
106       }
107    }
108    return accept;
109 }
110
111 /*
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.
115  *
116  * On Mac OS X, this succesfully differentiates between HFS+ and UFS
117  * volumes, which makes me trust it is OK for others, too.
118  */
119 static bool volume_has_attrlist(const char *fname)
120 {
121 #ifdef HAVE_DARWIN_OS
122    struct statfs st;
123    struct volinfo_struct {
124       unsigned long length;               /* Mandatory field */
125       vol_capabilities_attr_t info;       /* Volume capabilities */
126    } vol;
127    struct attrlist attrList;
128
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)) {
137          return true;
138       }
139    }
140 #endif
141    return false;
142 }
143
144 /*
145  * Find a single file.
146  * handle_file is the callback for handling the file.
147  * p is the filename
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.
151  */
152 int
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)
156 {
157    struct utimbuf restore_times;
158    int rtn_stat;
159    int len;
160
161    ff_pkt->fname = ff_pkt->link = fname;
162
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);
168    }
169
170    Dmsg1(300, "File ----: %s\n", fname);
171
172    /* Save current times of this directory in case we need to
173     * reset them because the user doesn't want them changed.
174     */
175    restore_times.actime = ff_pkt->statp.st_atime;
176    restore_times.modtime = ff_pkt->statp.st_mtime;
177
178    /*
179     * We check for allowed fstypes at top_level and fstype change (below).
180     */
181    if (top_level) {
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);
186          }
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 */
189       }
190       ff_pkt->volhas_attrlist = volume_has_attrlist(fname);
191    }
192
193    /*
194     * If this is an Incremental backup, see if file was modified
195     * since our last "save_time", presumably the last Full save
196     * or Incremental.
197     */
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);
207       }
208    }
209
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);
224        }
225    }
226 #endif
227
228 /* ***FIXME*** implement this */
229 #if xxxxxxx
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);
234    }
235 #endif
236    ff_pkt->LinkFI = 0;
237    /*
238     * Handle hard linked files
239     *
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
242     *  up only once.
243     */
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))) {
251
252        struct f_link *lp;
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 *));
256        }
257        const int linkhash = LINKHASH(ff_pkt->statp);
258
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 */
266              }
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);
271          }
272
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 */
282    } else {
283       ff_pkt->linked = NULL;
284    }
285
286    /* This is not a link to a previously dumped file, so dump it.  */
287    if (S_ISREG(ff_pkt->statp.st_mode)) {
288       off_t sizeleft;
289
290       sizeleft = ff_pkt->statp.st_size;
291
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;
297       } else {
298          ff_pkt->type = FT_REG;
299       }
300       rtn_stat = handle_file(ff_pkt, pkt, top_level);
301       if (ff_pkt->linked) {
302          ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
303       }
304       if (ff_pkt->flags & FO_KEEPATIME) {
305          utime(fname, &restore_times);
306       }       
307       return rtn_stat;
308
309
310    } else if (S_ISLNK(ff_pkt->statp.st_mode)) {  /* soft link */
311       int size;
312       char *buffer = (char *)alloca(path_max + name_max + 102);
313
314       size = readlink(fname, buffer, path_max + name_max + 101);
315       if (size < 0) {
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;
322          }
323          return rtn_stat;
324       }
325       buffer[size] = 0;
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;
331       }
332       return rtn_stat;
333
334    } else if (S_ISDIR(ff_pkt->statp.st_mode)) {
335       DIR *directory;
336       struct dirent *entry, *result;
337       char *link;
338       int link_len;
339       int len;
340       int status;
341       dev_t our_device = ff_pkt->statp.st_dev;
342       bool recurse = true;
343       bool volhas_attrlist = ff_pkt->volhas_attrlist;    /* Remember this if we recurse */
344
345       /*
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.
349        */
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;
358             }
359             return rtn_stat;
360          }
361       }
362
363       /* Build a canonical directory name with a trailing slash in link var */
364       len = strlen(fname);
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] == '/')
370         len--;
371       link[len++] = '/';             /* add back one */
372       link[len] = 0;
373
374       ff_pkt->link = link;
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;
381       } else {
382          ff_pkt->type = FT_DIRBEGIN;
383       }
384       /*
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).
391        */
392       rtn_stat = handle_file(ff_pkt, pkt, top_level);
393       if (rtn_stat < 1) {             /* ignore or error status */
394          free(link);
395          return rtn_stat;
396       }
397       /* Done with DIRBEGIN, next call will be DIREND */
398       if (ff_pkt->type == FT_DIRBEGIN) {
399          ff_pkt->type = FT_DIREND;
400       }
401
402       /*
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.
409        */
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;
420
421       /*
422        * Do not descend into subdirectories (recurse) if the
423        * user has turned it off for this directory.
424        *
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
427        * file systems.
428        */
429       if (!top_level && ff_pkt->flags & FO_NO_RECURSION) {
430          ff_pkt->type = FT_NORECURSE;
431          recurse = false;
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;
435             recurse = false;
436          } else if (!accept_fstype(ff_pkt, NULL)) {
437             ff_pkt->type = FT_INVALIDFS;
438             recurse = false;
439          } else {
440             ff_pkt->volhas_attrlist = volume_has_attrlist(fname);
441          }
442       }
443       /* If not recursing, just backup dir and return */
444       if (!recurse) {
445          rtn_stat = handle_file(ff_pkt, pkt, top_level);
446          if (ff_pkt->linked) {
447             ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
448          }
449          free(link);
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);
454          }
455          return rtn_stat;
456       }
457
458       ff_pkt->link = ff_pkt->fname;     /* reset "link" */
459
460       /*
461        * Descend into or "recurse" into the directory to read
462        *   all the files in it.
463        */
464       errno = 0;
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;
471          }
472          free(link);
473          free_dir_ff_pkt(dir_ff_pkt);
474          return rtn_stat;
475       }
476
477       /*
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.
481        */
482       rtn_stat = 1;
483       entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 100);
484       for ( ; !job_canceled(jcr); ) {
485          char *p, *q;
486          int i;
487
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);
492             break;
493          }
494          ASSERT(name_max+1 > (int)sizeof(struct dirent) + (int)NAMELEN(entry));
495          p = entry->d_name;
496          /* Skip `.', `..', and excluded file names.  */
497          if (p[0] == '\0' || (p[0] == '.' && (p[1] == '\0' ||
498              (p[1] == '.' && p[2] == '\0')))) {
499             continue;
500          }
501
502          if ((int)NAMELEN(entry) + len >= link_len) {
503              link_len = len + NAMELEN(entry) + 1;
504              link = (char *)brealloc(link, link_len + 1);
505          }
506          q = link + len;
507          for (i=0; i < (int)NAMELEN(entry); i++) {
508             *q++ = *p++;
509          }
510          *q = 0;
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;
515             }
516          }
517       }
518       closedir(directory);
519       free(link);
520       free(entry);
521
522       /*
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.
528        */
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;
532       }
533       free_dir_ff_pkt(dir_ff_pkt);
534
535       if (ff_pkt->flags & FO_KEEPATIME) {
536          utime(fname, &restore_times);
537       }
538       ff_pkt->volhas_attrlist = volhas_attrlist;      /* Restore value in case it changed. */
539       return rtn_stat;
540    } /* end check for directory */
541
542    /*
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.
546     */
547 #ifdef HAVE_FREEBSD_OS
548    /*
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
554     */
555    if (top_level && (S_ISBLK(ff_pkt->statp.st_mode) || S_ISCHR(ff_pkt->statp.st_mode))) {
556 #else
557    if (top_level && S_ISBLK(ff_pkt->statp.st_mode)) {
558 #endif
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;
563    } else {
564       /* The only remaining types are special (character, ...) files */
565       ff_pkt->type = FT_SPEC;
566    }
567    rtn_stat = handle_file(ff_pkt, pkt, top_level);
568    if (ff_pkt->linked) {
569       ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
570    }
571    return rtn_stat;
572 }
573
574 int term_find_one(FF_PKT *ff)
575 {
576    struct f_link *lp, *lc;
577    int count = 0;
578    int i;
579
580    
581    if (ff->linkhash == NULL) return 0;
582
583    for (i =0 ; i < LINK_HASHTABLE_SIZE; i ++) {
584    /* Free up list of hard linked files */
585        lp = ff->linkhash[i];
586        while (lp) {
587       lc = lp;
588       lp = lp->next;
589       if (lc) {
590          free(lc);
591          count++;
592       }
593    }
594        ff->linkhash[i] = NULL;
595    }
596    free(ff->linkhash);
597    ff->linkhash = NULL;
598    return count;
599 }