]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/find_one.c
More FileSet changes
[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  *  decending 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->statp.st_nlink > 1
125        && (S_ISREG(ff_pkt->statp.st_mode)
126            || S_ISCHR(ff_pkt->statp.st_mode)
127            || S_ISBLK(ff_pkt->statp.st_mode)
128            || S_ISFIFO(ff_pkt->statp.st_mode)
129            || S_ISSOCK(ff_pkt->statp.st_mode))) {
130
131        struct f_link *lp;
132
133       /* Search link list of hard linked files */
134       for (lp = ff_pkt->linklist; lp; lp = lp->next)
135          if (lp->ino == ff_pkt->statp.st_ino && lp->dev == ff_pkt->statp.st_dev) {
136              /* If we have already backed up the hard linked file don't do it again */
137              if (strcmp(lp->name, fname) == 0) {
138                 Jmsg1(jcr, M_WARNING, 0, _("Attempt to backup hard linked file %s twice ignored.\n"),
139                    fname);
140                 return 1;             /* ignore */
141              }
142              ff_pkt->link = lp->name;
143              ff_pkt->type = FT_LNKSAVED;       /* Handle link, file already saved */
144              ff_pkt->LinkFI = lp->FileIndex;
145              return handle_file(ff_pkt, pkt);
146          }
147
148       /* File not previously dumped. Chain it into our list. */
149       lp = (struct f_link *)bmalloc(sizeof(struct f_link) + strlen(fname) +1);
150       lp->ino = ff_pkt->statp.st_ino;
151       lp->dev = ff_pkt->statp.st_dev;
152       strcpy(lp->name, fname);
153       lp->next = ff_pkt->linklist;
154       ff_pkt->linklist = lp;
155       ff_pkt->linked = lp;            /* mark saved link */
156    } else {
157       ff_pkt->linked = NULL;
158    }
159
160    /* This is not a link to a previously dumped file, so dump it.  */
161    if (S_ISREG(ff_pkt->statp.st_mode)) {
162       off_t sizeleft;
163
164       sizeleft = ff_pkt->statp.st_size;
165
166       /* Don't bother opening empty, world readable files.  Also do not open
167          files when archive is meant for /dev/null.  */
168       if (ff_pkt->null_output_device || (sizeleft == 0
169               && MODE_RALL == (MODE_RALL & ff_pkt->statp.st_mode))) {
170          ff_pkt->type = FT_REGE;
171       } else {
172          ff_pkt->type = FT_REG;
173       }
174       rtn_stat = handle_file(ff_pkt, pkt);
175       if (ff_pkt->linked) {
176          ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
177       }
178       return rtn_stat;
179
180
181    } else if (S_ISLNK(ff_pkt->statp.st_mode)) {  /* soft link */
182       int size;
183       char *buffer = (char *)alloca(path_max + name_max + 102);
184
185       size = readlink(fname, buffer, path_max + name_max + 101);
186       if (size < 0) {
187          /* Could not follow link */                             
188          ff_pkt->type = FT_NOFOLLOW;
189          ff_pkt->ff_errno = errno;
190          rtn_stat = handle_file(ff_pkt, pkt);
191          if (ff_pkt->linked) {
192             ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
193          }
194          return rtn_stat;
195       }
196       buffer[size] = 0;
197       ff_pkt->link = buffer;          /* point to link */
198       ff_pkt->type = FT_LNK;          /* got a real link */
199       rtn_stat = handle_file(ff_pkt, pkt);
200       if (ff_pkt->linked) {
201          ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
202       }
203       return rtn_stat;
204
205    } else if (S_ISDIR(ff_pkt->statp.st_mode)) {
206       DIR *directory;
207       struct dirent *entry, *result;
208       char *link;
209       int link_len;
210       int len;   
211       int status;
212       dev_t our_device = ff_pkt->statp.st_dev;
213
214       /*  
215        * If we are using Win32 (non-portable) backup API, don't check
216        *  access as everything is more complicated, and
217        *  in principle, we should be able to access everything.
218        */
219       if (!have_win32_api() || (ff_pkt->flags & FO_PORTABLE)) {
220          if (access(fname, R_OK) == -1 && geteuid() != 0) {
221             /* Could not access() directory */
222             ff_pkt->type = FT_NOACCESS;
223             ff_pkt->ff_errno = errno;
224             rtn_stat = handle_file(ff_pkt, pkt);
225             if (ff_pkt->linked) {
226                ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
227             }
228             return rtn_stat;
229          }
230       }
231
232       /* Build a canonical directory name with a trailing slash in link var */
233       len = strlen(fname);
234       link_len = len + 200;
235       link = (char *)bmalloc(link_len + 2);
236       bstrncpy(link, fname, link_len);
237       /* Strip all trailing slashes */
238       while (len >= 1 && link[len - 1] == '/')
239         len--;
240       link[len++] = '/';             /* add back one */
241       link[len] = 0;
242
243       ff_pkt->link = link;
244       if (ff_pkt->incremental &&
245           (ff_pkt->statp.st_mtime < ff_pkt->save_time &&
246            ff_pkt->statp.st_ctime < ff_pkt->save_time)) {
247          /* Incremental option, directory entry not changed */
248          ff_pkt->type = FT_DIRNOCHG;
249       } else {
250          ff_pkt->type = FT_DIRBEGIN;
251       }
252       /* Send off Directory packet now */
253       if (!handle_file(ff_pkt, pkt)) {
254          free(link);
255          return 0;                    /* Do not save this directory */
256       }
257       if (ff_pkt->type == FT_DIRBEGIN) {
258          ff_pkt->type = FT_DIREND;
259       }
260
261       /*
262        * Create a temporary ff packet for this directory
263        *   entry, and defer handling the directory until
264        *   we have recursed into it.  This saves the
265        *   directory after all files have been processed, and
266        *   during the restore, the directory permissions will
267        *   be reset after all the files have been restored.
268        */
269       Dmsg1(300, "Create temp ff packet for dir: %s\n", ff_pkt->fname);
270       FF_PKT *dir_ff_pkt = (FF_PKT *)bmalloc(sizeof(FF_PKT));
271       memcpy(dir_ff_pkt, ff_pkt, sizeof(FF_PKT));
272       dir_ff_pkt->fname = bstrdup(ff_pkt->fname);
273       dir_ff_pkt->link = bstrdup(ff_pkt->link);
274       dir_ff_pkt->sys_fname = get_pool_memory(PM_FNAME);
275       dir_ff_pkt->included_files_list = NULL;
276       dir_ff_pkt->excluded_files_list = NULL;
277       dir_ff_pkt->excluded_paths_list = NULL;
278       dir_ff_pkt->linklist = NULL;
279         
280       ff_pkt->link = ff_pkt->fname;     /* reset "link" */
281
282       /* 
283        * Do not decend into subdirectories (recurse) if the
284        * user has turned it off for this directory.
285        */
286       if (ff_pkt->flags & FO_NO_RECURSION) {
287          /* No recursion into this directory */
288          ff_pkt->type = FT_NORECURSE;
289          rtn_stat = handle_file(ff_pkt, pkt);
290          if (ff_pkt->linked) {
291             ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
292          }
293          free(link);
294          free_dir_ff_pkt(dir_ff_pkt);
295          return rtn_stat;
296       }
297
298       /* 
299        * See if we are crossing file systems, and
300        * avoid doing so if the user only wants to dump one file system.
301        */
302       if (!top_level && !(ff_pkt->flags & FO_MULTIFS) &&
303            parent_device != ff_pkt->statp.st_dev) {
304          /* returning here means we do not handle this directory */
305          ff_pkt->type = FT_NOFSCHG;
306          rtn_stat = handle_file(ff_pkt, pkt);
307          if (ff_pkt->linked) {
308             ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
309          }
310          free(link);
311          free_dir_ff_pkt(dir_ff_pkt);
312          return rtn_stat;
313       }
314       /* 
315        * Decend into or "recurse" into the directory to read
316        *   all the files in it.
317        */
318       errno = 0;
319       if ((directory = opendir(fname)) == NULL) {
320          ff_pkt->type = FT_NOOPEN;
321          ff_pkt->ff_errno = errno;
322          rtn_stat = handle_file(ff_pkt, pkt);
323          if (ff_pkt->linked) {
324             ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
325          }
326          free(link);
327          free_dir_ff_pkt(dir_ff_pkt);
328          return rtn_stat;
329       }
330
331       /*
332        * Process all files in this directory entry (recursing).
333        *    This would possibly run faster if we chdir to the directory
334        *    before traversing it.
335        */
336       rtn_stat = 1;
337       entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 100);
338       for ( ; !job_canceled(jcr); ) {
339          char *p, *q;
340          int i;
341
342          status  = readdir_r(directory, entry, &result);
343          if (status != 0 || result == NULL) {
344 //          Dmsg2(99, "readdir returned stat=%d result=0x%x\n",
345 //             status, (long)result);
346             break;
347          }
348          ASSERT(name_max+1 > (int)sizeof(struct dirent) + (int)NAMELEN(entry));
349          p = entry->d_name;
350          /* Skip `.', `..', and excluded file names.  */
351          if (p[0] == '\0' || (p[0] == '.' && (p[1] == '\0' ||
352              (p[1] == '.' && p[2] == '\0')))) {
353             continue;
354          }
355
356          if ((int)NAMELEN(entry) + len >= link_len) {
357              link_len = len + NAMELEN(entry) + 1;
358              link = (char *)brealloc(link, link_len + 1);
359          }
360          q = link + len;
361          for (i=0; i < (int)NAMELEN(entry); i++) {
362             *q++ = *p++;
363          }
364          *q = 0;
365          if (!file_is_excluded(ff_pkt, link)) {
366             rtn_stat = find_one_file(jcr, ff_pkt, handle_file, pkt, link, our_device, 0);
367             if (ff_pkt->linked) {
368                ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
369             }
370          }
371       }
372       closedir(directory);
373       free(link);
374       free(entry);
375
376       /*
377        * Now that we have recursed through all the files in the
378        *  directory, we "save" the directory so that after all
379        *  the files are restored, this entry will serve to reset
380        *  the directory modes and dates.  Temp directory values
381        *  were used without this record.
382        */
383       handle_file(dir_ff_pkt, pkt);       /* handle directory entry */
384       if (ff_pkt->linked) {
385          ff_pkt->linked->FileIndex = dir_ff_pkt->FileIndex;
386       }
387       free_dir_ff_pkt(dir_ff_pkt);
388
389       if (ff_pkt->flags & FO_KEEPATIME) {
390          utime(fname, &restore_times);
391       }
392       return rtn_stat;
393    } /* end check for directory */
394
395    /*
396     * If it is explicitly mentioned (i.e. top_level) and is
397     *  a block device, we do a raw backup of it or if it is
398     *  a fifo, we simply read it.
399     */
400 #ifdef HAVE_FREEBSD_OS
401    /*
402     * On FreeBSD, all block devices are character devices, so
403     *   to be able to read a raw disk, we need the check for
404     *   a character device.
405     * crw-r-----  1 root  operator  - 116, 0x00040002 Jun  9 19:32 /dev/ad0s3
406     * crw-r-----  1 root  operator  - 116, 0x00040002 Jun  9 19:32 /dev/rad0s3
407     */
408    if (top_level && (S_ISBLK(ff_pkt->statp.st_mode) || S_ISCHR(ff_pkt->statp.st_mode))) {
409 #else
410    if (top_level && S_ISBLK(ff_pkt->statp.st_mode)) {
411 #endif
412       ff_pkt->type = FT_RAW;          /* raw partition */
413    } else if (top_level && S_ISFIFO(ff_pkt->statp.st_mode) &&
414               ff_pkt->flags & FO_READFIFO) {
415       ff_pkt->type = FT_FIFO;
416    } else {
417       /* The only remaining types are special (character, ...) files */
418       ff_pkt->type = FT_SPEC;
419    }
420    rtn_stat = handle_file(ff_pkt, pkt);
421    if (ff_pkt->linked) {
422       ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
423    }
424    return rtn_stat;
425 }
426
427 int term_find_one(FF_PKT *ff)
428 {
429    struct f_link *lp, *lc;
430    int count = 0;
431   
432    /* Free up list of hard linked files */
433    for (lp = ff->linklist; lp;) {
434       lc = lp;
435       lp = lp->next;
436       if (lc) {
437          free(lc);
438          count++;
439       }
440    }
441    return count;
442 }