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