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