2 Copyright (C) 2000-2003 Kern Sibbald and John Walker
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.
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.
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,
19 This file is based on GNU TAR source code. Except for a few key
20 ideas, it has been rewritten for Bacula.
24 Thanks to the TAR programmers.
31 extern int32_t name_max; /* filename max length */
32 extern int32_t path_max; /* path name max length */
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.
43 dev_t dev; /* device */
44 ino_t ino; /* inode with device is unique */
46 uint32_t FileIndex; /* Bacula FileIndex of this file */
47 char name[1]; /* The name */
50 static void free_dir_ff_pkt(FF_PKT *dir_ff_pkt)
52 free(dir_ff_pkt->fname);
53 free(dir_ff_pkt->link);
54 free_pool_memory(dir_ff_pkt->sys_fname);
60 * handle_file is the callback for handling the file.
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.
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)
70 struct utimbuf restore_times;
73 ff_pkt->fname = ff_pkt->link = fname;
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);
82 Dmsg1(300, "File ----: %s\n", fname);
84 /* Save current times of this directory in case we need to
85 * reset them because the user doesn't want them changed.
87 restore_times.actime = ff_pkt->statp.st_atime;
88 restore_times.modtime = ff_pkt->statp.st_mtime;
92 * If this is an Incremental backup, see if file was modified
93 * since our last "save_time", presumably the last Full save
96 if (ff_pkt->incremental && !S_ISDIR(ff_pkt->statp.st_mode)) {
97 Dmsg1(300, "Non-directory incremental: %s\n", ff_pkt->fname);
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);
108 /* ***FIXME*** implement this */
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);
118 * Handle hard linked files
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
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))) {
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 Jmsg0(jcr, M_WARNING, 0, _("Attempt to backup hard linked file %s twice ignored.\n"));
139 return 1; /* ignore */
141 ff_pkt->link = lp->name;
142 ff_pkt->type = FT_LNKSAVED; /* Handle link, file already saved */
143 ff_pkt->LinkFI = lp->FileIndex;
144 return handle_file(ff_pkt, pkt);
147 /* File not previously dumped. Chain it into our list. */
148 lp = (struct f_link *)bmalloc(sizeof(struct f_link) + strlen(fname) +1);
149 lp->ino = ff_pkt->statp.st_ino;
150 lp->dev = ff_pkt->statp.st_dev;
151 strcpy(lp->name, fname);
152 lp->next = ff_pkt->linklist;
153 ff_pkt->linklist = lp;
154 ff_pkt->linked = lp; /* mark saved link */
156 ff_pkt->linked = NULL;
159 /* This is not a link to a previously dumped file, so dump it. */
160 if (S_ISREG(ff_pkt->statp.st_mode)) {
163 sizeleft = ff_pkt->statp.st_size;
165 /* Don't bother opening empty, world readable files. Also do not open
166 files when archive is meant for /dev/null. */
167 if (ff_pkt->null_output_device || (sizeleft == 0
168 && MODE_RALL == (MODE_RALL & ff_pkt->statp.st_mode))) {
169 ff_pkt->type = FT_REGE;
171 ff_pkt->type = FT_REG;
173 rtn_stat = handle_file(ff_pkt, pkt);
174 if (ff_pkt->linked) {
175 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
180 } else if (S_ISLNK(ff_pkt->statp.st_mode)) { /* soft link */
182 char *buffer = (char *)alloca(path_max + name_max + 102);
184 size = readlink(fname, buffer, path_max + name_max + 101);
186 /* Could not follow link */
187 ff_pkt->type = FT_NOFOLLOW;
188 ff_pkt->ff_errno = errno;
189 rtn_stat = handle_file(ff_pkt, pkt);
190 if (ff_pkt->linked) {
191 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
196 ff_pkt->link = buffer; /* point to link */
197 ff_pkt->type = FT_LNK; /* got a real link */
198 rtn_stat = handle_file(ff_pkt, pkt);
199 if (ff_pkt->linked) {
200 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
204 } else if (S_ISDIR(ff_pkt->statp.st_mode)) {
206 struct dirent *entry, *result;
211 dev_t our_device = ff_pkt->statp.st_dev;
214 * If we are using Win32 (non-portable) backup API, don't check
215 * access as everything is more complicated, and
216 * in principle, we should be able to access everything.
218 if (!have_win32_api() || (ff_pkt->flags & FO_PORTABLE)) {
219 if (access(fname, R_OK) == -1 && geteuid() != 0) {
220 /* Could not access() directory */
221 ff_pkt->type = FT_NOACCESS;
222 ff_pkt->ff_errno = errno;
223 rtn_stat = handle_file(ff_pkt, pkt);
224 if (ff_pkt->linked) {
225 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
231 /* Build a canonical directory name with a trailing slash in link var */
233 link_len = len + 200;
234 link = (char *)bmalloc(link_len + 2);
235 bstrncpy(link, fname, link_len);
236 /* Strip all trailing slashes */
237 while (len >= 1 && link[len - 1] == '/')
239 link[len++] = '/'; /* add back one */
243 if (ff_pkt->incremental &&
244 (ff_pkt->statp.st_mtime < ff_pkt->save_time &&
245 ff_pkt->statp.st_ctime < ff_pkt->save_time)) {
246 /* Incremental option, directory entry not changed */
247 ff_pkt->type = FT_DIRNOCHG;
249 ff_pkt->type = FT_DIR;
253 * Create a temporary ff packet for this directory
254 * entry, and defer handling the directory until
255 * we have recursed into it. This saves the
256 * directory after all files have been processed, and
257 * during the restore, the directory permissions will
258 * be reset after all the files have been restored.
260 Dmsg1(300, "Create temp ff packet for dir: %s\n", ff_pkt->fname);
261 FF_PKT *dir_ff_pkt = (FF_PKT *)bmalloc(sizeof(FF_PKT));
262 memcpy(dir_ff_pkt, ff_pkt, sizeof(FF_PKT));
263 dir_ff_pkt->fname = bstrdup(ff_pkt->fname);
264 dir_ff_pkt->link = bstrdup(ff_pkt->link);
265 dir_ff_pkt->sys_fname = get_pool_memory(PM_FNAME);
266 dir_ff_pkt->included_files_list = NULL;
267 dir_ff_pkt->excluded_files_list = NULL;
268 dir_ff_pkt->excluded_paths_list = NULL;
269 dir_ff_pkt->linklist = NULL;
271 ff_pkt->link = ff_pkt->fname; /* reset "link" */
274 * Do not decend into subdirectories (recurse) if the
275 * user has turned it off for this directory.
277 if (ff_pkt->flags & FO_NO_RECURSION) {
278 /* No recursion into this directory */
279 ff_pkt->type = FT_NORECURSE;
280 rtn_stat = handle_file(ff_pkt, pkt);
281 if (ff_pkt->linked) {
282 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
285 free_dir_ff_pkt(dir_ff_pkt);
290 * See if we are crossing file systems, and
291 * avoid doing so if the user only wants to dump one file system.
293 if (!top_level && !(ff_pkt->flags & FO_MULTIFS) &&
294 parent_device != ff_pkt->statp.st_dev) {
295 /* returning here means we do not handle this directory */
296 ff_pkt->type = FT_NOFSCHG;
297 rtn_stat = handle_file(ff_pkt, pkt);
298 if (ff_pkt->linked) {
299 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
302 free_dir_ff_pkt(dir_ff_pkt);
306 * Decend into or "recurse" into the directory to read
307 * all the files in it.
310 if ((directory = opendir(fname)) == NULL) {
311 ff_pkt->type = FT_NOOPEN;
312 ff_pkt->ff_errno = errno;
313 rtn_stat = handle_file(ff_pkt, pkt);
314 if (ff_pkt->linked) {
315 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
318 free_dir_ff_pkt(dir_ff_pkt);
323 * Process all files in this directory entry (recursing).
324 * This would possibly run faster if we chdir to the directory
325 * before traversing it.
328 entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 100);
329 for ( ; !job_canceled(jcr); ) {
333 status = readdir_r(directory, entry, &result);
334 if (status != 0 || result == NULL) {
335 // Dmsg2(99, "readdir returned stat=%d result=0x%x\n",
336 // status, (long)result);
339 ASSERT(name_max+1 > (int)sizeof(struct dirent) + (int)NAMELEN(entry));
341 /* Skip `.', `..', and excluded file names. */
342 if (p[0] == '\0' || (p[0] == '.' && (p[1] == '\0' ||
343 (p[1] == '.' && p[2] == '\0')))) {
347 if ((int)NAMELEN(entry) + len >= link_len) {
348 link_len = len + NAMELEN(entry) + 1;
349 link = (char *)brealloc(link, link_len + 1);
352 for (i=0; i < (int)NAMELEN(entry); i++) {
356 if (!file_is_excluded(ff_pkt, link)) {
357 rtn_stat = find_one_file(jcr, ff_pkt, handle_file, pkt, link, our_device, 0);
358 if (ff_pkt->linked) {
359 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
368 * Now that we have recursed through all the files in the
369 * directory, we "save" the directory so that after all
370 * the files are restored, this entry will serve to reset
371 * the directory modes and dates. Temp directory values
372 * were used without this record.
374 handle_file(dir_ff_pkt, pkt); /* handle directory entry */
375 if (ff_pkt->linked) {
376 ff_pkt->linked->FileIndex = dir_ff_pkt->FileIndex;
378 free_dir_ff_pkt(dir_ff_pkt);
380 if (ff_pkt->flags & FO_KEEPATIME) {
381 utime(fname, &restore_times);
384 } /* end check for directory */
387 * If it is explicitly mentioned (i.e. top_level) and is
388 * a block device, we do a raw backup of it or if it is
389 * a fifo, we simply read it.
391 #ifdef HAVE_FREEBSD_OS
393 * On FreeBSD, all block devices are character devices, so
394 * to be able to read a raw disk, we need the check for
395 * a character device.
396 * crw-r----- 1 root operator - 116, 0x00040002 Jun 9 19:32 /dev/ad0s3
397 * crw-r----- 1 root operator - 116, 0x00040002 Jun 9 19:32 /dev/rad0s3
399 if (top_level && (S_ISBLK(ff_pkt->statp.st_mode) || S_ISCHR(ff_pkt->statp.st_mode))) {
401 if (top_level && S_ISBLK(ff_pkt->statp.st_mode)) {
403 ff_pkt->type = FT_RAW; /* raw partition */
404 } else if (top_level && S_ISFIFO(ff_pkt->statp.st_mode) &&
405 ff_pkt->flags & FO_READFIFO) {
406 ff_pkt->type = FT_FIFO;
408 /* The only remaining types are special (character, ...) files */
409 ff_pkt->type = FT_SPEC;
411 rtn_stat = handle_file(ff_pkt, pkt);
412 if (ff_pkt->linked) {
413 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
418 int term_find_one(FF_PKT *ff)
420 struct f_link *lp, *lc;
423 /* Free up list of hard linked files */
424 for (lp = ff->linklist; lp;) {