2 Copyright (C) 2000-2005 Kern Sibbald
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 version 2 as amended with additional clauses defined in the
7 file LICENSE in the main source directory.
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
12 the file LICENSE for additional details.
18 This file is based on GNU TAR source code. Except for a few key
19 ideas, it has been entirely rewritten for Bacula.
23 Thanks to the TAR programmers.
32 #include <sys/param.h>
33 #include <sys/mount.h>
37 extern int32_t name_max; /* filename max length */
38 extern int32_t path_max; /* path name max length */
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.
49 dev_t dev; /* device */
50 ino_t ino; /* inode with device is unique */
52 uint32_t FileIndex; /* Bacula FileIndex of this file */
53 char name[1]; /* The name */
56 static void free_dir_ff_pkt(FF_PKT *dir_ff_pkt)
58 free(dir_ff_pkt->fname);
59 free(dir_ff_pkt->link);
60 free_pool_memory(dir_ff_pkt->sys_fname);
65 * Check to see if we allow the file system type of a file or directory.
66 * If we do not have a list of file system types, we accept anything.
68 static int accept_fstype(FF_PKT *ff, void *dummy) {
73 if (ff->fstypes.size()) {
75 if (!fstype(ff->fname, fs, sizeof(fs))) {
76 Dmsg1(50, "Cannot determine file system type for \"%s\"\n", ff->fname);
78 for (i = 0; i < ff->fstypes.size(); ++i) {
79 if (strcmp(fs, (char *)ff->fstypes.get(i)) == 0) {
80 Dmsg2(100, "Accepting fstype %s for \"%s\"\n", fs, ff->fname);
84 Dmsg3(200, "fstype %s for \"%s\" does not match %s\n", fs,
85 ff->fname, ff->fstypes.get(i));
93 * This function determines whether we can use getattrlist()
94 * It's odd, but we have to use the function to determine that...
95 * Also, the man pages talk about things as if they were implemented.
97 * On Mac OS X, this succesfully differentiates between HFS+ and UFS
98 * volumes, which makes me trust it is OK for others, too.
100 static bool volume_has_attrlist(const char *fname)
102 #ifdef HAVE_DARWIN_OS
104 struct volinfo_struct {
105 unsigned long length; /* Mandatory field */
106 vol_capabilities_attr_t info; /* Volume capabilities */
108 struct attrlist attrList;
110 memset(&attrList, 0, sizeof(attrList));
111 attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
112 attrList.volattr = ATTR_VOL_INFO | ATTR_VOL_CAPABILITIES;
113 if (statfs(fname, &st) == 0) {
114 /* We need to check on the mount point */
115 if (getattrlist(st.f_mntonname, &attrList, &vol, sizeof(vol), FSOPT_NOFOLLOW) == 0
116 && (vol.info.capabilities[VOL_CAPABILITIES_INTERFACES] & VOL_CAP_INT_ATTRLIST)
117 && (vol.info.valid[VOL_CAPABILITIES_INTERFACES] & VOL_CAP_INT_ATTRLIST)) {
126 * Find a single file.
127 * handle_file is the callback for handling the file.
129 * parent_device is the device we are currently on
130 * top_level is 1 when not recursing or 0 when
131 * descending into a directory.
134 find_one_file(JCR *jcr, FF_PKT *ff_pkt,
135 int handle_file(FF_PKT *ff, void *hpkt, bool top_level),
136 void *pkt, char *fname, dev_t parent_device, bool top_level)
138 struct utimbuf restore_times;
142 ff_pkt->fname = ff_pkt->link = fname;
144 if (lstat(fname, &ff_pkt->statp) != 0) {
145 /* Cannot stat file */
146 ff_pkt->type = FT_NOSTAT;
147 ff_pkt->ff_errno = errno;
148 return handle_file(ff_pkt, pkt, top_level);
151 Dmsg1(300, "File ----: %s\n", fname);
153 /* Save current times of this directory in case we need to
154 * reset them because the user doesn't want them changed.
156 restore_times.actime = ff_pkt->statp.st_atime;
157 restore_times.modtime = ff_pkt->statp.st_mtime;
160 * We check for allowed fstypes at top_level and fstype change (below).
163 if (!accept_fstype(ff_pkt, NULL)) {
164 ff_pkt->type = FT_INVALIDFS;
165 if (ff_pkt->flags & FO_KEEPATIME) {
166 utime(fname, &restore_times);
168 Jmsg1(jcr, M_ERROR, 0, _("Top level directory \"%s\" has an unlisted fstype\n"), fname);
169 return 1; /* Just ignore this error - or the whole backup is cancelled */
171 ff_pkt->volhas_attrlist = volume_has_attrlist(fname);
175 * If this is an Incremental backup, see if file was modified
176 * since our last "save_time", presumably the last Full save
179 if (ff_pkt->incremental && !S_ISDIR(ff_pkt->statp.st_mode)) {
180 Dmsg1(300, "Non-directory incremental: %s\n", ff_pkt->fname);
181 /* Not a directory */
182 if (ff_pkt->statp.st_mtime < ff_pkt->save_time
183 && ((ff_pkt->flags & FO_MTIMEONLY) ||
184 ff_pkt->statp.st_ctime < ff_pkt->save_time)) {
185 /* Incremental option, file not changed */
186 ff_pkt->type = FT_NOCHG;
187 return handle_file(ff_pkt, pkt, top_level);
191 #ifdef HAVE_DARWIN_OS
192 if (ff_pkt->flags & FO_HFSPLUS && ff_pkt->volhas_attrlist
193 && S_ISREG(ff_pkt->statp.st_mode)) {
194 /* TODO: initialise attrList once elsewhere? */
195 struct attrlist attrList;
196 memset(&attrList, 0, sizeof(attrList));
197 attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
198 attrList.commonattr = ATTR_CMN_FNDRINFO;
199 attrList.fileattr = ATTR_FILE_RSRCLENGTH;
200 if (getattrlist(fname, &attrList, &ff_pkt->hfsinfo,
201 sizeof(ff_pkt->hfsinfo), FSOPT_NOFOLLOW) != 0) {
202 ff_pkt->type = FT_NOSTAT;
203 ff_pkt->ff_errno = errno;
204 return handle_file(ff_pkt, pkt, top_level);
209 /* ***FIXME*** implement this */
211 /* See if we are trying to dump the archive. */
212 if (ar_dev && ff_pkt->statp.st_dev == ar_dev && ff_pkt->statp.st_ino == ar_ino) {
213 ff_pkt->type = FT_ISARCH;
214 return handle_file(ff_pkt, pkt, top_level);
219 * Handle hard linked files
221 * Maintain a list of hard linked files already backed up. This
222 * allows us to ensure that the data of each file gets backed
225 if (!(ff_pkt->flags & FO_NO_HARDLINK)
226 && ff_pkt->statp.st_nlink > 1
227 && (S_ISREG(ff_pkt->statp.st_mode)
228 || S_ISCHR(ff_pkt->statp.st_mode)
229 || S_ISBLK(ff_pkt->statp.st_mode)
230 || S_ISFIFO(ff_pkt->statp.st_mode)
231 || S_ISSOCK(ff_pkt->statp.st_mode))) {
235 /* Search link list of hard linked files */
236 for (lp = ff_pkt->linklist; lp; lp = lp->next)
237 if (lp->ino == (ino_t)ff_pkt->statp.st_ino &&
238 lp->dev == (dev_t)ff_pkt->statp.st_dev) {
239 /* If we have already backed up the hard linked file don't do it again */
240 if (strcmp(lp->name, fname) == 0) {
241 return 1; /* ignore */
243 ff_pkt->link = lp->name;
244 ff_pkt->type = FT_LNKSAVED; /* Handle link, file already saved */
245 ff_pkt->LinkFI = lp->FileIndex;
246 return handle_file(ff_pkt, pkt, top_level);
249 /* File not previously dumped. Chain it into our list. */
250 len = strlen(fname) + 1;
251 lp = (struct f_link *)bmalloc(sizeof(struct f_link) + len);
252 lp->ino = ff_pkt->statp.st_ino;
253 lp->dev = ff_pkt->statp.st_dev;
254 bstrncpy(lp->name, fname, len);
255 lp->next = ff_pkt->linklist;
256 ff_pkt->linklist = lp;
257 ff_pkt->linked = lp; /* mark saved link */
259 ff_pkt->linked = NULL;
262 /* This is not a link to a previously dumped file, so dump it. */
263 if (S_ISREG(ff_pkt->statp.st_mode)) {
266 sizeleft = ff_pkt->statp.st_size;
268 /* Don't bother opening empty, world readable files. Also do not open
269 files when archive is meant for /dev/null. */
270 if (ff_pkt->null_output_device || (sizeleft == 0
271 && MODE_RALL == (MODE_RALL & ff_pkt->statp.st_mode))) {
272 ff_pkt->type = FT_REGE;
274 ff_pkt->type = FT_REG;
276 rtn_stat = handle_file(ff_pkt, pkt, top_level);
277 if (ff_pkt->linked) {
278 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
280 if (ff_pkt->flags & FO_KEEPATIME) {
281 utime(fname, &restore_times);
286 } else if (S_ISLNK(ff_pkt->statp.st_mode)) { /* soft link */
288 char *buffer = (char *)alloca(path_max + name_max + 102);
290 size = readlink(fname, buffer, path_max + name_max + 101);
292 /* Could not follow link */
293 ff_pkt->type = FT_NOFOLLOW;
294 ff_pkt->ff_errno = errno;
295 rtn_stat = handle_file(ff_pkt, pkt, top_level);
296 if (ff_pkt->linked) {
297 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
302 ff_pkt->link = buffer; /* point to link */
303 ff_pkt->type = FT_LNK; /* got a real link */
304 rtn_stat = handle_file(ff_pkt, pkt, top_level);
305 if (ff_pkt->linked) {
306 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
310 } else if (S_ISDIR(ff_pkt->statp.st_mode)) {
312 struct dirent *entry, *result;
317 dev_t our_device = ff_pkt->statp.st_dev;
319 bool volhas_attrlist = ff_pkt->volhas_attrlist; /* Remember this if we recurse */
322 * If we are using Win32 (non-portable) backup API, don't check
323 * access as everything is more complicated, and
324 * in principle, we should be able to access everything.
326 if (!have_win32_api() || (ff_pkt->flags & FO_PORTABLE)) {
327 if (access(fname, R_OK) == -1 && geteuid() != 0) {
328 /* Could not access() directory */
329 ff_pkt->type = FT_NOACCESS;
330 ff_pkt->ff_errno = errno;
331 rtn_stat = handle_file(ff_pkt, pkt, top_level);
332 if (ff_pkt->linked) {
333 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
339 /* Build a canonical directory name with a trailing slash in link var */
341 link_len = len + 200;
342 link = (char *)bmalloc(link_len + 2);
343 bstrncpy(link, fname, link_len);
344 /* Strip all trailing slashes */
345 while (len >= 1 && link[len - 1] == '/')
347 link[len++] = '/'; /* add back one */
351 if (ff_pkt->incremental &&
352 (ff_pkt->statp.st_mtime < ff_pkt->save_time &&
353 ((ff_pkt->flags & FO_MTIMEONLY) ||
354 ff_pkt->statp.st_ctime < ff_pkt->save_time))) {
355 /* Incremental option, directory entry not changed */
356 ff_pkt->type = FT_DIRNOCHG;
358 ff_pkt->type = FT_DIRBEGIN;
361 * Note, we return the directory to the calling program (handle_file)
362 * when we first see the directory (FT_DIRBEGIN.
363 * This allows the program to apply matches and make a
364 * choice whether or not to accept it. If it is accepted, we
365 * do not immediately save it, but do so only after everything
366 * in the directory is seen (i.e. the FT_DIREND).
368 rtn_stat = handle_file(ff_pkt, pkt, top_level);
369 if (rtn_stat < 1) { /* ignore or error status */
373 /* Done with DIRBEGIN, next call will be DIREND */
374 if (ff_pkt->type == FT_DIRBEGIN) {
375 ff_pkt->type = FT_DIREND;
379 * Create a temporary ff packet for this directory
380 * entry, and defer handling the directory until
381 * we have recursed into it. This saves the
382 * directory after all files have been processed, and
383 * during the restore, the directory permissions will
384 * be reset after all the files have been restored.
386 Dmsg1(300, "Create temp ff packet for dir: %s\n", ff_pkt->fname);
387 FF_PKT *dir_ff_pkt = (FF_PKT *)bmalloc(sizeof(FF_PKT));
388 memcpy(dir_ff_pkt, ff_pkt, sizeof(FF_PKT));
389 dir_ff_pkt->fname = bstrdup(ff_pkt->fname);
390 dir_ff_pkt->link = bstrdup(ff_pkt->link);
391 dir_ff_pkt->sys_fname = get_pool_memory(PM_FNAME);
392 dir_ff_pkt->included_files_list = NULL;
393 dir_ff_pkt->excluded_files_list = NULL;
394 dir_ff_pkt->excluded_paths_list = NULL;
395 dir_ff_pkt->linklist = NULL;
398 * Do not descend into subdirectories (recurse) if the
399 * user has turned it off for this directory.
401 * If we are crossing file systems, we are either not allowed
402 * to cross, or we may be restricted by a list of permitted
405 if (!top_level && ff_pkt->flags & FO_NO_RECURSION) {
406 ff_pkt->type = FT_NORECURSE;
408 } else if (!top_level && parent_device != ff_pkt->statp.st_dev) {
409 if(!(ff_pkt->flags & FO_MULTIFS)) {
410 ff_pkt->type = FT_NOFSCHG;
412 } else if (!accept_fstype(ff_pkt, NULL)) {
413 ff_pkt->type = FT_INVALIDFS;
416 ff_pkt->volhas_attrlist = volume_has_attrlist(fname);
419 /* If not recursing, just backup dir and return */
421 rtn_stat = handle_file(ff_pkt, pkt, top_level);
422 if (ff_pkt->linked) {
423 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
426 free_dir_ff_pkt(dir_ff_pkt);
427 ff_pkt->link = ff_pkt->fname; /* reset "link" */
428 if (ff_pkt->flags & FO_KEEPATIME) {
429 utime(fname, &restore_times);
434 ff_pkt->link = ff_pkt->fname; /* reset "link" */
437 * Descend into or "recurse" into the directory to read
438 * all the files in it.
441 if ((directory = opendir(fname)) == NULL) {
442 ff_pkt->type = FT_NOOPEN;
443 ff_pkt->ff_errno = errno;
444 rtn_stat = handle_file(ff_pkt, pkt, top_level);
445 if (ff_pkt->linked) {
446 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
449 free_dir_ff_pkt(dir_ff_pkt);
454 * Process all files in this directory entry (recursing).
455 * This would possibly run faster if we chdir to the directory
456 * before traversing it.
459 entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 100);
460 for ( ; !job_canceled(jcr); ) {
464 status = readdir_r(directory, entry, &result);
465 if (status != 0 || result == NULL) {
466 // Dmsg2(99, "readdir returned stat=%d result=0x%x\n",
467 // status, (long)result);
470 ASSERT(name_max+1 > (int)sizeof(struct dirent) + (int)NAMELEN(entry));
472 /* Skip `.', `..', and excluded file names. */
473 if (p[0] == '\0' || (p[0] == '.' && (p[1] == '\0' ||
474 (p[1] == '.' && p[2] == '\0')))) {
478 if ((int)NAMELEN(entry) + len >= link_len) {
479 link_len = len + NAMELEN(entry) + 1;
480 link = (char *)brealloc(link, link_len + 1);
483 for (i=0; i < (int)NAMELEN(entry); i++) {
487 if (!file_is_excluded(ff_pkt, link)) {
488 rtn_stat = find_one_file(jcr, ff_pkt, handle_file, pkt, link, our_device, false);
489 if (ff_pkt->linked) {
490 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
499 * Now that we have recursed through all the files in the
500 * directory, we "save" the directory so that after all
501 * the files are restored, this entry will serve to reset
502 * the directory modes and dates. Temp directory values
503 * were used without this record.
505 handle_file(dir_ff_pkt, pkt, top_level); /* handle directory entry */
506 if (ff_pkt->linked) {
507 ff_pkt->linked->FileIndex = dir_ff_pkt->FileIndex;
509 free_dir_ff_pkt(dir_ff_pkt);
511 if (ff_pkt->flags & FO_KEEPATIME) {
512 utime(fname, &restore_times);
514 ff_pkt->volhas_attrlist = volhas_attrlist; /* Restore value in case it changed. */
516 } /* end check for directory */
519 * If it is explicitly mentioned (i.e. top_level) and is
520 * a block device, we do a raw backup of it or if it is
521 * a fifo, we simply read it.
523 #ifdef HAVE_FREEBSD_OS
525 * On FreeBSD, all block devices are character devices, so
526 * to be able to read a raw disk, we need the check for
527 * a character device.
528 * crw-r----- 1 root operator - 116, 0x00040002 Jun 9 19:32 /dev/ad0s3
529 * crw-r----- 1 root operator - 116, 0x00040002 Jun 9 19:32 /dev/rad0s3
531 if (top_level && (S_ISBLK(ff_pkt->statp.st_mode) || S_ISCHR(ff_pkt->statp.st_mode))) {
533 if (top_level && S_ISBLK(ff_pkt->statp.st_mode)) {
535 ff_pkt->type = FT_RAW; /* raw partition */
536 } else if (top_level && S_ISFIFO(ff_pkt->statp.st_mode) &&
537 ff_pkt->flags & FO_READFIFO) {
538 ff_pkt->type = FT_FIFO;
540 /* The only remaining types are special (character, ...) files */
541 ff_pkt->type = FT_SPEC;
543 rtn_stat = handle_file(ff_pkt, pkt, top_level);
544 if (ff_pkt->linked) {
545 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
550 int term_find_one(FF_PKT *ff)
552 struct f_link *lp, *lc;
555 /* Free up list of hard linked files */
556 for (lp = ff->linklist; lp;) {