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