]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/find_one.c
Mult Vols + fix restore hard links + file permissions
[bacula/bacula] / bacula / src / findlib / find_one.c
1 /* 
2    Copyright (C) 2000-2003 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
32 extern size_t name_max;               /* filename max length */
33 extern size_t path_max;               /* path name max length */
34
35 #ifndef HAVE_READDIR_R
36 int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
37 #endif
38
39
40 /*
41  * Structure for keeping track of hard linked files, we   
42  *   keep an entry for each hardlinked file that we save,
43  *   which is the first one found. For all the other files that
44  *   are linked to this one, we save only the directory
45  *   entry so we can link it.
46  */
47 struct f_link {
48     struct f_link *next;
49     dev_t dev;                        /* device */
50     ino_t ino;                        /* inode with device is unique */
51     short linkcount;
52     uint32_t FileIndex;               /* Bacula FileIndex of this file */
53     char name[1];                     /* The name */
54 };
55
56
57 #if HAVE_UTIME_H
58 # include <utime.h>
59 #else
60 struct utimbuf {
61     long actime;
62     long modtime;
63 };
64 #endif
65
66
67 /*
68  * Find a single file.                        
69  * handle_file is the callback for handling the file.
70  * p is the filename
71  * parent_device is the device we are currently on 
72  * top_level is 1 when not recursing or 0 when 
73  *  decending into a directory.
74  */
75 int
76 find_one_file(JCR *jcr, FF_PKT *ff_pkt, int handle_file(FF_PKT *ff, void *hpkt), 
77                void *pkt, char *fname, dev_t parent_device, int top_level)
78 {
79    struct utimbuf restore_times;
80    int rtn_stat;
81
82    ff_pkt->fname = ff_pkt->link = fname;
83
84    if (lstat(fname, &ff_pkt->statp) != 0) {
85        /* Cannot stat file */
86        ff_pkt->type = FT_NOSTAT;
87        ff_pkt->ff_errno = errno;
88        return handle_file(ff_pkt, pkt);
89    }
90
91    Dmsg1(60, "File ----: %s\n", fname);
92 #ifdef DEBUG
93    if (S_ISLNK(ff_pkt->statp.st_mode))
94       Dmsg1(60, "Link-------------: %s \n", fname);
95 #endif
96
97    /* Save current times of this directory in case we need to
98     * reset them because the user doesn't want them changed.
99     */
100    restore_times.actime = ff_pkt->statp.st_atime;
101    restore_times.modtime = ff_pkt->statp.st_mtime;
102
103
104    /* 
105     * If this is an Incremental backup, see if file was modified
106     * since our last "save_time", presumably the last Full save
107     * or Incremental.
108     */
109    if (ff_pkt->incremental && !S_ISDIR(ff_pkt->statp.st_mode)) {
110       Dmsg1(100, "Non-directory incremental: %s\n", ff_pkt->fname);
111       /* Not a directory */
112       if (ff_pkt->statp.st_mtime < ff_pkt->save_time
113           && (ff_pkt->mtime_only || 
114               ff_pkt->statp.st_ctime < ff_pkt->save_time)) {
115          /* Incremental option, file not changed */
116          ff_pkt->type = FT_NOCHG;
117          Dmsg1(100, "File not changed: %s\n", ff_pkt->fname);
118          Dmsg4(200, "save_time=%d mtime=%d mtime_only=%d st_ctime=%d\n",
119             ff_pkt->save_time, ff_pkt->statp.st_mtime, 
120             ff_pkt->mtime_only, ff_pkt->statp.st_ctime);
121          return handle_file(ff_pkt, pkt);
122       }
123    }
124
125 #if xxxxxxx
126    /* See if we are trying to dump the archive.  */
127    if (ar_dev && ff_pkt->statp.st_dev == ar_dev && ff_pkt->statp.st_ino == ar_ino) {
128        ff_pkt->type = FT_ISARCH;
129        return handle_file(ff_pkt, pkt);
130    }
131 #endif
132    ff_pkt->LinkFI = 0;
133    /* 
134     * Handle hard linked files
135     *
136     * Maintain a list of hard linked files already backed up. This
137     *  allows us to ensure that the data of each file gets backed 
138     *  up only once.
139     */
140    if (ff_pkt->statp.st_nlink > 1
141        && (S_ISREG(ff_pkt->statp.st_mode)
142            || S_ISCHR(ff_pkt->statp.st_mode)
143            || S_ISBLK(ff_pkt->statp.st_mode)
144            || S_ISFIFO(ff_pkt->statp.st_mode)
145            || S_ISSOCK(ff_pkt->statp.st_mode))) {
146
147        struct f_link *lp;
148
149       /* Search link list of hard linked files */
150       for (lp = ff_pkt->linklist; lp; lp = lp->next)
151          if (lp->ino == ff_pkt->statp.st_ino && lp->dev == ff_pkt->statp.st_dev) {
152              ff_pkt->link = lp->name;
153              ff_pkt->type = FT_LNKSAVED;       /* Handle link, file already saved */
154              ff_pkt->LinkFI = lp->FileIndex;
155              return handle_file(ff_pkt, pkt);
156          }
157
158       /* File not previously dumped. Chain it into our list. */
159       lp = (struct f_link *)bmalloc(sizeof(struct f_link) + strlen(fname) +1);
160       lp->ino = ff_pkt->statp.st_ino;
161       lp->dev = ff_pkt->statp.st_dev;
162       strcpy(lp->name, fname);
163       lp->next = ff_pkt->linklist;
164       ff_pkt->linklist = lp;
165       ff_pkt->linked = lp;            /* mark saved link */
166    } else {
167       ff_pkt->linked = NULL;
168    }
169
170    /* This is not a link to a previously dumped file, so dump it.  */
171    if (S_ISREG(ff_pkt->statp.st_mode)) {
172       off_t sizeleft;
173
174       sizeleft = ff_pkt->statp.st_size;
175
176       /* Don't bother opening empty, world readable files.  Also do not open
177          files when archive is meant for /dev/null.  */
178       if (ff_pkt->null_output_device || (sizeleft == 0
179               && MODE_RALL == (MODE_RALL & ff_pkt->statp.st_mode))) {
180          ff_pkt->type = FT_REGE;
181       } else {
182          ff_pkt->type = FT_REG;
183       }
184       rtn_stat = handle_file(ff_pkt, pkt);
185       if (ff_pkt->linked) {
186          ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
187       }
188       return rtn_stat;
189
190
191    } else if (S_ISLNK(ff_pkt->statp.st_mode)) {
192       int size;
193       char *buffer = (char *)alloca(path_max + name_max + 2);
194
195       size = readlink(fname, buffer, path_max + name_max + 1);
196       if (size < 0) {
197          /* Could not follow link */                             
198          ff_pkt->type = FT_NOFOLLOW;
199          ff_pkt->ff_errno = errno;
200          rtn_stat = handle_file(ff_pkt, pkt);
201          if (ff_pkt->linked) {
202             ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
203          }
204          return rtn_stat;
205       }
206       buffer[size] = 0;
207       ff_pkt->link = buffer;
208       ff_pkt->type = FT_LNK;           /* got a real link */
209       rtn_stat = handle_file(ff_pkt, pkt);
210       if (ff_pkt->linked) {
211          ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
212       }
213       return rtn_stat;
214
215    } else if (S_ISDIR(ff_pkt->statp.st_mode)) {
216       DIR *directory;
217       struct dirent *entry, *result;
218       char *link;
219       int link_len;
220       int len;
221       int status;
222       dev_t our_device = ff_pkt->statp.st_dev;
223
224       if (access(fname, R_OK) == -1 && geteuid() != 0) {
225          /* Could not access() directory */
226          ff_pkt->type = FT_NOACCESS;
227          ff_pkt->ff_errno = errno;
228          rtn_stat = handle_file(ff_pkt, pkt);
229          if (ff_pkt->linked) {
230             ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
231          }
232          return rtn_stat;
233       }
234
235       /* Build a canonical directory name with a trailing slash. */
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_DIR;
254       }
255       handle_file(ff_pkt, pkt);       /* handle directory entry */
256       if (ff_pkt->linked) {
257          ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
258       }
259
260       ff_pkt->link = ff_pkt->fname;     /* reset "link" */
261
262       /* 
263        * Do not decend into subdirectories (recurse) if the
264        * user has turned it off for this directory.
265        */
266       if (ff_pkt->flags & FO_NO_RECURSION) {
267          free(link);
268          /* No recursion into this directory */
269          ff_pkt->type = FT_NORECURSE;
270          rtn_stat = handle_file(ff_pkt, pkt);
271          if (ff_pkt->linked) {
272             ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
273          }
274          return rtn_stat;
275       }
276
277       /* 
278        * See if we are crossing file systems, and
279        * avoid doing so if the user only wants to dump one file system.
280        */
281       if (!top_level && !(ff_pkt->flags & FO_MULTIFS) &&
282            parent_device != ff_pkt->statp.st_dev) {
283          free(link);
284          /* returning here means we do not handle this directory */
285          ff_pkt->type = FT_NOFSCHG;
286          rtn_stat = handle_file(ff_pkt, pkt);
287          if (ff_pkt->linked) {
288             ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
289          }
290          return rtn_stat;
291       }
292       /* 
293        * Now process the files in this directory.
294        */
295       errno = 0;
296       if ((directory = opendir(fname)) == NULL) {
297          free(link);
298          ff_pkt->type = FT_NOOPEN;
299          ff_pkt->ff_errno = errno;
300          rtn_stat = handle_file(ff_pkt, pkt);
301          if (ff_pkt->linked) {
302             ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
303          }
304          return rtn_stat;
305       }
306
307       /*
308        * This would possibly run faster if we chdir to the directory
309        * before traversing it.
310        */
311       rtn_stat = 1;
312       entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 100);
313       for ( ; !job_cancelled(jcr); ) {
314          char *p, *q;
315          int i;
316
317          status  = readdir_r(directory, entry, &result);
318          Dmsg3(200, "readdir stat=%d result=%x name=%s\n", status, result,
319             entry->d_name);
320          if (status != 0 || result == NULL) {
321             break;
322          }
323          ASSERT(name_max+1 > sizeof(struct dirent) + (int)NAMELEN(entry));
324          p = entry->d_name;
325          /* Skip `.', `..', and excluded file names.  */
326          if (p[0] == '\0' || (p[0] == '.' && (p[1] == '\0' ||
327              (p[1] == '.' && p[2] == '\0')))) {
328             continue;
329          }
330
331          if ((int)NAMELEN(entry) + len >= link_len) {
332              link_len = len + NAMELEN(entry) + 1;
333              link = (char *)brealloc(link, link_len + 1);
334          }
335          q = link + len;
336          for (i=0; i < (int)NAMELEN(entry); i++) {
337             *q++ = *p++;
338          }
339          *q = 0;
340          if (!file_is_excluded(ff_pkt, link)) {
341             rtn_stat = find_one_file(jcr, ff_pkt, handle_file, pkt, link, our_device, 0);
342             if (ff_pkt->linked) {
343                ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
344             }
345          }
346       }
347       closedir(directory);
348       free(link);
349       free(entry);
350
351       if (ff_pkt->atime_preserve) {
352          utime(fname, &restore_times);
353       }
354       return rtn_stat;
355    } /* end check for directory */
356
357    /*
358     * If it is explicitly mentioned (i.e. top_level) and is
359     *  a block device, we do a raw backup of it or if it is
360     *  a fifo, we simply read it.
361     */
362    if (top_level && S_ISBLK(ff_pkt->statp.st_mode)) {
363       ff_pkt->type = FT_RAW;          /* raw partition */
364    } else if (top_level && S_ISFIFO(ff_pkt->statp.st_mode) &&
365               ff_pkt->flags & FO_READFIFO) {
366       ff_pkt->type = FT_FIFO;
367    } else {
368       /* The only remaining types are special (character, ...) files */
369       ff_pkt->type = FT_SPEC;
370    }
371    rtn_stat = handle_file(ff_pkt, pkt);
372    if (ff_pkt->linked) {
373       ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
374    }
375    return rtn_stat;
376 }
377
378 int term_find_one(FF_PKT *ff)
379 {
380    struct f_link *lp, *lc;
381    int count = 0;
382   
383    /* Free up list of hard linked files */
384    for (lp = ff->linklist; lp;) {
385       lc = lp;
386       lp = lp->next;
387       if (lc) {
388          free(lc);
389          count++;
390       }
391    }
392    return count;
393 }