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