]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/find_one.c
- Implemented 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  */
27
28 #include "bacula.h"
29 #include "find.h"
30
31 extern int32_t name_max;              /* filename max length */
32 extern int32_t path_max;              /* path name max length */
33
34 /*
35  * Structure for keeping track of hard linked files, we   
36  *   keep an entry for each hardlinked file that we save,
37  *   which is the first one found. For all the other files that
38  *   are linked to this one, we save only the directory
39  *   entry so we can link it.
40  */
41 struct f_link {
42     struct f_link *next;
43     dev_t dev;                        /* device */
44     ino_t ino;                        /* inode with device is unique */
45     short linkcount;
46     uint32_t FileIndex;               /* Bacula FileIndex of this file */
47     char name[1];                     /* The name */
48 };
49
50 static void free_dir_ff_pkt(FF_PKT *dir_ff_pkt)
51 {
52    free(dir_ff_pkt->fname);
53    free(dir_ff_pkt->link);
54    free_pool_memory(dir_ff_pkt->sys_fname);
55    free(dir_ff_pkt);
56 }
57
58 /*
59  * Find a single file.                        
60  * handle_file is the callback for handling the file.
61  * p is the filename
62  * parent_device is the device we are currently on 
63  * top_level is 1 when not recursing or 0 when 
64  *  descending into a directory.
65  */
66 int
67 find_one_file(JCR *jcr, FF_PKT *ff_pkt, int handle_file(FF_PKT *ff, void *hpkt), 
68                void *pkt, char *fname, dev_t parent_device, int top_level)
69 {
70    struct utimbuf restore_times;
71    int rtn_stat;
72
73    ff_pkt->fname = ff_pkt->link = fname;
74
75    if (lstat(fname, &ff_pkt->statp) != 0) {
76        /* Cannot stat file */
77        ff_pkt->type = FT_NOSTAT;
78        ff_pkt->ff_errno = errno;
79        return handle_file(ff_pkt, pkt);
80    }
81
82    Dmsg1(300, "File ----: %s\n", fname);
83
84    /* Save current times of this directory in case we need to
85     * reset them because the user doesn't want them changed.
86     */
87    restore_times.actime = ff_pkt->statp.st_atime;
88    restore_times.modtime = ff_pkt->statp.st_mtime;
89
90
91    /* 
92     * If this is an Incremental backup, see if file was modified
93     * since our last "save_time", presumably the last Full save
94     * or Incremental.
95     */
96    if (ff_pkt->incremental && !S_ISDIR(ff_pkt->statp.st_mode)) {
97       Dmsg1(300, "Non-directory incremental: %s\n", ff_pkt->fname);
98       /* Not a directory */
99       if (ff_pkt->statp.st_mtime < ff_pkt->save_time
100           && ((ff_pkt->flags & FO_MTIMEONLY) || 
101               ff_pkt->statp.st_ctime < ff_pkt->save_time)) {
102          /* Incremental option, file not changed */
103          ff_pkt->type = FT_NOCHG;
104          return handle_file(ff_pkt, pkt);
105       }
106    }
107
108 /* ***FIXME*** implement this */
109 #if xxxxxxx
110    /* See if we are trying to dump the archive.  */
111    if (ar_dev && ff_pkt->statp.st_dev == ar_dev && ff_pkt->statp.st_ino == ar_ino) {
112        ff_pkt->type = FT_ISARCH;
113        return handle_file(ff_pkt, pkt);
114    }
115 #endif
116    ff_pkt->LinkFI = 0;
117    /* 
118     * Handle hard linked files
119     *
120     * Maintain a list of hard linked files already backed up. This
121     *  allows us to ensure that the data of each file gets backed 
122     *  up only once.
123     */
124    if (!(ff_pkt->flags & FO_NO_HARDLINK)
125        && ff_pkt->statp.st_nlink > 1
126        && (S_ISREG(ff_pkt->statp.st_mode)
127            || S_ISCHR(ff_pkt->statp.st_mode)
128            || S_ISBLK(ff_pkt->statp.st_mode)
129            || S_ISFIFO(ff_pkt->statp.st_mode)
130            || S_ISSOCK(ff_pkt->statp.st_mode))) {
131
132        struct f_link *lp;
133
134       /* Search link list of hard linked files */
135       for (lp = ff_pkt->linklist; lp; lp = lp->next)
136          if (lp->ino == (ino_t)ff_pkt->statp.st_ino && 
137              lp->dev == (dev_t)ff_pkt->statp.st_dev) {
138              /* If we have already backed up the hard linked file don't do it again */
139              if (strcmp(lp->name, fname) == 0) {
140                 Jmsg1(jcr, M_WARNING, 0, _("Attempt to backup hard linked file %s twice ignored.\n"),
141                    fname);
142                 return 1;             /* ignore */
143              }
144              ff_pkt->link = lp->name;
145              ff_pkt->type = FT_LNKSAVED;       /* Handle link, file already saved */
146              ff_pkt->LinkFI = lp->FileIndex;
147              return handle_file(ff_pkt, pkt);
148          }
149
150       /* File not previously dumped. Chain it into our list. */
151       lp = (struct f_link *)bmalloc(sizeof(struct f_link) + strlen(fname) +1);
152       lp->ino = ff_pkt->statp.st_ino;
153       lp->dev = ff_pkt->statp.st_dev;
154       strcpy(lp->name, fname);
155       lp->next = ff_pkt->linklist;
156       ff_pkt->linklist = lp;
157       ff_pkt->linked = lp;            /* mark saved link */
158    } else {
159       ff_pkt->linked = NULL;
160    }
161
162    /* This is not a link to a previously dumped file, so dump it.  */
163    if (S_ISREG(ff_pkt->statp.st_mode)) {
164       off_t sizeleft;
165
166       sizeleft = ff_pkt->statp.st_size;
167
168       /* Don't bother opening empty, world readable files.  Also do not open
169          files when archive is meant for /dev/null.  */
170       if (ff_pkt->null_output_device || (sizeleft == 0
171               && MODE_RALL == (MODE_RALL & ff_pkt->statp.st_mode))) {
172          ff_pkt->type = FT_REGE;
173       } else {
174          ff_pkt->type = FT_REG;
175       }
176       rtn_stat = handle_file(ff_pkt, pkt);
177       if (ff_pkt->linked) {
178          ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
179       }
180       return rtn_stat;
181
182
183    } else if (S_ISLNK(ff_pkt->statp.st_mode)) {  /* soft link */
184       int size;
185       char *buffer = (char *)alloca(path_max + name_max + 102);
186
187       size = readlink(fname, buffer, path_max + name_max + 101);
188       if (size < 0) {
189          /* Could not follow link */                             
190          ff_pkt->type = FT_NOFOLLOW;
191          ff_pkt->ff_errno = errno;
192          rtn_stat = handle_file(ff_pkt, pkt);
193          if (ff_pkt->linked) {
194             ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
195          }
196          return rtn_stat;
197       }
198       buffer[size] = 0;
199       ff_pkt->link = buffer;          /* point to link */
200       ff_pkt->type = FT_LNK;          /* got a real link */
201       rtn_stat = handle_file(ff_pkt, pkt);
202       if (ff_pkt->linked) {
203          ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
204       }
205       return rtn_stat;
206
207    } else if (S_ISDIR(ff_pkt->statp.st_mode)) {
208       DIR *directory;
209       struct dirent *entry, *result;
210       char *link;
211       int link_len;
212       int len;   
213       int status;
214       dev_t our_device = ff_pkt->statp.st_dev;
215       bool recurse = true;
216
217       /*  
218        * If we are using Win32 (non-portable) backup API, don't check
219        *  access as everything is more complicated, and
220        *  in principle, we should be able to access everything.
221        */
222       if (!have_win32_api() || (ff_pkt->flags & FO_PORTABLE)) {
223          if (access(fname, R_OK) == -1 && geteuid() != 0) {
224             /* Could not access() directory */
225             ff_pkt->type = FT_NOACCESS;
226             ff_pkt->ff_errno = errno;
227             rtn_stat = handle_file(ff_pkt, pkt);
228             if (ff_pkt->linked) {
229                ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
230             }
231             return rtn_stat;
232          }
233       }
234
235       /* Build a canonical directory name with a trailing slash in link var */
236       len = strlen(fname);
237       link_len = len + 200;
238       link = (char *)bmalloc(link_len + 2);
239       bstrncpy(link, fname, link_len);
240       /* Strip all trailing slashes */
241       while (len >= 1 && link[len - 1] == '/')
242         len--;
243       link[len++] = '/';             /* add back one */
244       link[len] = 0;
245
246       ff_pkt->link = link;
247       if (ff_pkt->incremental &&
248           (ff_pkt->statp.st_mtime < ff_pkt->save_time &&
249            ff_pkt->statp.st_ctime < ff_pkt->save_time)) {
250          /* Incremental option, directory entry not changed */
251          ff_pkt->type = FT_DIRNOCHG;
252       } else {
253          ff_pkt->type = FT_DIRBEGIN;
254       }
255       /* 
256        * Note, we return the directory to the calling program (handle_file)
257        * when we first see the directory (FT_DIRBEGIN. 
258        * This allows the program to apply matches and make a
259        * choice whether or not to accept it.  If it is accepted, we
260        * do not immediately save it, but do so only after everything
261        * in the directory is seen (i.e. the FT_DIREND).
262        */
263       rtn_stat = handle_file(ff_pkt, pkt);
264       if (rtn_stat < 1) {             /* ignore or error status */
265          free(link);
266          return rtn_stat;
267       }
268       /* Done with DIRBEGIN, next call will be DIREND */
269       if (ff_pkt->type == FT_DIRBEGIN) {
270          ff_pkt->type = FT_DIREND;
271       }
272
273       /*
274        * Create a temporary ff packet for this directory
275        *   entry, and defer handling the directory until
276        *   we have recursed into it.  This saves the
277        *   directory after all files have been processed, and
278        *   during the restore, the directory permissions will
279        *   be reset after all the files have been restored.
280        */
281       Dmsg1(300, "Create temp ff packet for dir: %s\n", ff_pkt->fname);
282       FF_PKT *dir_ff_pkt = (FF_PKT *)bmalloc(sizeof(FF_PKT));
283       memcpy(dir_ff_pkt, ff_pkt, sizeof(FF_PKT));
284       dir_ff_pkt->fname = bstrdup(ff_pkt->fname);
285       dir_ff_pkt->link = bstrdup(ff_pkt->link);
286       dir_ff_pkt->sys_fname = get_pool_memory(PM_FNAME);
287       dir_ff_pkt->included_files_list = NULL;
288       dir_ff_pkt->excluded_files_list = NULL;
289       dir_ff_pkt->excluded_paths_list = NULL;
290       dir_ff_pkt->linklist = NULL;
291
292       /* 
293        * Do not descend into subdirectories (recurse) if the
294        * user has turned it off for this directory.
295        * Or if we are crossing file systems, 
296        * avoid doing so if the user only wants to dump one file system.
297        */
298       if (ff_pkt->flags & FO_NO_RECURSION) {
299          ff_pkt->type = FT_NORECURSE;
300          recurse = false;
301       } else if (!top_level && !(ff_pkt->flags & FO_MULTIFS) &&
302            parent_device != ff_pkt->statp.st_dev) {
303          ff_pkt->type = FT_NOFSCHG;
304          recurse = false;
305       }
306       if (!recurse) {
307          rtn_stat = handle_file(ff_pkt, pkt);
308          if (ff_pkt->linked) {
309             ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
310          }
311          free(link);
312          free_dir_ff_pkt(dir_ff_pkt);
313          ff_pkt->link = ff_pkt->fname;     /* reset "link" */
314          return rtn_stat;
315       }
316
317       ff_pkt->link = ff_pkt->fname;     /* reset "link" */
318
319       /* 
320        * Descend into or "recurse" into the directory to read
321        *   all the files in it.
322        */
323       errno = 0;
324       if ((directory = opendir(fname)) == NULL) {
325          ff_pkt->type = FT_NOOPEN;
326          ff_pkt->ff_errno = errno;
327          rtn_stat = handle_file(ff_pkt, pkt);
328          if (ff_pkt->linked) {
329             ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
330          }
331          free(link);
332          free_dir_ff_pkt(dir_ff_pkt);
333          return rtn_stat;
334       }
335
336       /*
337        * Process all files in this directory entry (recursing).
338        *    This would possibly run faster if we chdir to the directory
339        *    before traversing it.
340        */
341       rtn_stat = 1;
342       entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 100);
343       for ( ; !job_canceled(jcr); ) {
344          char *p, *q;
345          int i;
346
347          status  = readdir_r(directory, entry, &result);
348          if (status != 0 || result == NULL) {
349 //          Dmsg2(99, "readdir returned stat=%d result=0x%x\n",
350 //             status, (long)result);
351             break;
352          }
353          ASSERT(name_max+1 > (int)sizeof(struct dirent) + (int)NAMELEN(entry));
354          p = entry->d_name;
355          /* Skip `.', `..', and excluded file names.  */
356          if (p[0] == '\0' || (p[0] == '.' && (p[1] == '\0' ||
357              (p[1] == '.' && p[2] == '\0')))) {
358             continue;
359          }
360
361          if ((int)NAMELEN(entry) + len >= link_len) {
362              link_len = len + NAMELEN(entry) + 1;
363              link = (char *)brealloc(link, link_len + 1);
364          }
365          q = link + len;
366          for (i=0; i < (int)NAMELEN(entry); i++) {
367             *q++ = *p++;
368          }
369          *q = 0;
370          if (!file_is_excluded(ff_pkt, link)) {
371             rtn_stat = find_one_file(jcr, ff_pkt, handle_file, pkt, link, our_device, 0);
372             if (ff_pkt->linked) {
373                ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
374             }
375          }
376       }
377       closedir(directory);
378       free(link);
379       free(entry);
380
381       /*
382        * Now that we have recursed through all the files in the
383        *  directory, we "save" the directory so that after all
384        *  the files are restored, this entry will serve to reset
385        *  the directory modes and dates.  Temp directory values
386        *  were used without this record.
387        */
388       handle_file(dir_ff_pkt, pkt);       /* handle directory entry */
389       if (ff_pkt->linked) {
390          ff_pkt->linked->FileIndex = dir_ff_pkt->FileIndex;
391       }
392       free_dir_ff_pkt(dir_ff_pkt);
393
394       if (ff_pkt->flags & FO_KEEPATIME) {
395          utime(fname, &restore_times);
396       }
397       return rtn_stat;
398    } /* end check for directory */
399
400    /*
401     * If it is explicitly mentioned (i.e. top_level) and is
402     *  a block device, we do a raw backup of it or if it is
403     *  a fifo, we simply read it.
404     */
405 #ifdef HAVE_FREEBSD_OS
406    /*
407     * On FreeBSD, all block devices are character devices, so
408     *   to be able to read a raw disk, we need the check for
409     *   a character device.
410     * crw-r-----  1 root  operator  - 116, 0x00040002 Jun  9 19:32 /dev/ad0s3
411     * crw-r-----  1 root  operator  - 116, 0x00040002 Jun  9 19:32 /dev/rad0s3
412     */
413    if (top_level && (S_ISBLK(ff_pkt->statp.st_mode) || S_ISCHR(ff_pkt->statp.st_mode))) {
414 #else
415    if (top_level && S_ISBLK(ff_pkt->statp.st_mode)) {
416 #endif
417       ff_pkt->type = FT_RAW;          /* raw partition */
418    } else if (top_level && S_ISFIFO(ff_pkt->statp.st_mode) &&
419               ff_pkt->flags & FO_READFIFO) {
420       ff_pkt->type = FT_FIFO;
421    } else {
422       /* The only remaining types are special (character, ...) files */
423       ff_pkt->type = FT_SPEC;
424    }
425    rtn_stat = handle_file(ff_pkt, pkt);
426    if (ff_pkt->linked) {
427       ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
428    }
429    return rtn_stat;
430 }
431
432 int term_find_one(FF_PKT *ff)
433 {
434    struct f_link *lp, *lc;
435    int count = 0;
436   
437    /* Free up list of hard linked files */
438    for (lp = ff->linklist; lp;) {
439       lc = lp;
440       lp = lp->next;
441       if (lc) {
442          free(lc);
443          count++;
444       }
445    }
446    return count;
447 }