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