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 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.
33 #include <sys/param.h>
34 #include <sys/mount.h>
38 extern int32_t name_max; /* filename max length */
39 extern int32_t path_max; /* path name max length */
42 * Structure for keeping track of hard linked files, we
43 * keep an entry for each hardlinked file that we save,
44 * which is the first one found. For all the other files that
45 * are linked to this one, we save only the directory
46 * entry so we can link it.
50 dev_t dev; /* device */
51 ino_t ino; /* inode with device is unique */
53 uint32_t FileIndex; /* Bacula FileIndex of this file */
54 char name[1]; /* The name */
57 static void free_dir_ff_pkt(FF_PKT *dir_ff_pkt)
59 free(dir_ff_pkt->fname);
60 free(dir_ff_pkt->link);
61 free_pool_memory(dir_ff_pkt->sys_fname);
66 * Check to see if we allow the file system type of a file or directory.
67 * If we do not have a list of file system types, we accept anything.
69 static int accept_fstype(FF_PKT *ff, void *dummy) {
74 if (ff->fstypes.size()) {
76 if (!fstype(ff->fname, fs, sizeof(fs))) {
77 Dmsg1(50, "Cannot determine file system type for \"%s\"\n", ff->fname);
79 for (i = 0; i < ff->fstypes.size(); ++i) {
80 if (strcmp(fs, (char *)ff->fstypes.get(i)) == 0) {
81 Dmsg2(100, "Accepting fstype %s for \"%s\"\n", fs, ff->fname);
85 Dmsg3(200, "fstype %s for \"%s\" does not match %s\n", fs,
86 ff->fname, ff->fstypes.get(i));
94 * This function determines whether we can use getattrlist()
95 * It's odd, but we have to use the function to determine that...
96 * Also, the man pages talk about things as if they were implemented.
98 * On Mac OS X, this succesfully differentiates between HFS+ and UFS
99 * volumes, which makes me trust it is OK for others, too.
101 static bool volume_has_attrlist(const char *fname)
103 #ifdef HAVE_DARWIN_OS
105 struct volinfo_struct {
106 unsigned long length; /* Mandatory field */
107 vol_capabilities_attr_t info; /* Volume capabilities */
109 struct attrlist attrList;
111 memset(&attrList, 0, sizeof(attrList));
112 attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
113 attrList.volattr = ATTR_VOL_INFO | ATTR_VOL_CAPABILITIES;
114 if (statfs(fname, &st) == 0) {
115 /* We need to check on the mount point */
116 if (getattrlist(st.f_mntonname, &attrList, &vol, sizeof(vol), FSOPT_NOFOLLOW) == 0
117 && (vol.info.capabilities[VOL_CAPABILITIES_INTERFACES] & VOL_CAP_INT_ATTRLIST)
118 && (vol.info.valid[VOL_CAPABILITIES_INTERFACES] & VOL_CAP_INT_ATTRLIST)) {
127 * Find a single file.
128 * handle_file is the callback for handling the file.
130 * parent_device is the device we are currently on
131 * top_level is 1 when not recursing or 0 when
132 * descending into a directory.
135 find_one_file(JCR *jcr, FF_PKT *ff_pkt,
136 int handle_file(FF_PKT *ff, void *hpkt, bool top_level),
137 void *pkt, char *fname, dev_t parent_device, bool top_level)
139 struct utimbuf restore_times;
143 ff_pkt->fname = ff_pkt->link = fname;
145 if (lstat(fname, &ff_pkt->statp) != 0) {
146 /* Cannot stat file */
147 ff_pkt->type = FT_NOSTAT;
148 ff_pkt->ff_errno = errno;
149 return handle_file(ff_pkt, pkt, top_level);
152 Dmsg1(300, "File ----: %s\n", fname);
154 /* Save current times of this directory in case we need to
155 * reset them because the user doesn't want them changed.
157 restore_times.actime = ff_pkt->statp.st_atime;
158 restore_times.modtime = ff_pkt->statp.st_mtime;
161 * We check for allowed fstypes at top_level and fstype change (below).
164 if (!accept_fstype(ff_pkt, NULL)) {
165 ff_pkt->type = FT_INVALIDFS;
166 if (ff_pkt->flags & FO_KEEPATIME) {
167 utime(fname, &restore_times);
169 Jmsg1(jcr, M_ERROR, 0, _("Top level directory \"%s\" has an unlisted fstype\n"), fname);
170 return 1; /* Just ignore this error - or the whole backup is cancelled */
172 ff_pkt->volhas_attrlist = volume_has_attrlist(fname);
176 * If this is an Incremental backup, see if file was modified
177 * since our last "save_time", presumably the last Full save
180 if (ff_pkt->incremental && !S_ISDIR(ff_pkt->statp.st_mode)) {
181 Dmsg1(300, "Non-directory incremental: %s\n", ff_pkt->fname);
182 /* Not a directory */
183 if (ff_pkt->statp.st_mtime < ff_pkt->save_time
184 && ((ff_pkt->flags & FO_MTIMEONLY) ||
185 ff_pkt->statp.st_ctime < ff_pkt->save_time)) {
186 /* Incremental option, file not changed */
187 ff_pkt->type = FT_NOCHG;
188 return handle_file(ff_pkt, pkt, top_level);
192 #ifdef HAVE_DARWIN_OS
193 if (ff_pkt->flags & FO_HFSPLUS && ff_pkt->volhas_attrlist
194 && S_ISREG(ff_pkt->statp.st_mode)) {
195 /* TODO: initialise attrList once elsewhere? */
196 struct attrlist attrList;
197 memset(&attrList, 0, sizeof(attrList));
198 attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
199 attrList.commonattr = ATTR_CMN_FNDRINFO;
200 attrList.fileattr = ATTR_FILE_RSRCLENGTH;
201 if (getattrlist(fname, &attrList, &ff_pkt->hfsinfo,
202 sizeof(ff_pkt->hfsinfo), FSOPT_NOFOLLOW) != 0) {
203 ff_pkt->type = FT_NOSTAT;
204 ff_pkt->ff_errno = errno;
205 return handle_file(ff_pkt, pkt, top_level);
210 /* ***FIXME*** implement this */
212 /* See if we are trying to dump the archive. */
213 if (ar_dev && ff_pkt->statp.st_dev == ar_dev && ff_pkt->statp.st_ino == ar_ino) {
214 ff_pkt->type = FT_ISARCH;
215 return handle_file(ff_pkt, pkt, top_level);
220 * Handle hard linked files
222 * Maintain a list of hard linked files already backed up. This
223 * allows us to ensure that the data of each file gets backed
226 if (!(ff_pkt->flags & FO_NO_HARDLINK)
227 && ff_pkt->statp.st_nlink > 1
228 && (S_ISREG(ff_pkt->statp.st_mode)
229 || S_ISCHR(ff_pkt->statp.st_mode)
230 || S_ISBLK(ff_pkt->statp.st_mode)
231 || S_ISFIFO(ff_pkt->statp.st_mode)
232 || S_ISSOCK(ff_pkt->statp.st_mode))) {
236 /* Search link list of hard linked files */
237 for (lp = ff_pkt->linklist; lp; lp = lp->next)
238 if (lp->ino == (ino_t)ff_pkt->statp.st_ino &&
239 lp->dev == (dev_t)ff_pkt->statp.st_dev) {
240 /* If we have already backed up the hard linked file don't do it again */
241 if (strcmp(lp->name, fname) == 0) {
242 Jmsg1(jcr, M_WARNING, 0, _("Attempt to backup hard linked file %s twice ignored.\n"),
244 return 1; /* ignore */
246 ff_pkt->link = lp->name;
247 ff_pkt->type = FT_LNKSAVED; /* Handle link, file already saved */
248 ff_pkt->LinkFI = lp->FileIndex;
249 return handle_file(ff_pkt, pkt, top_level);
252 /* File not previously dumped. Chain it into our list. */
253 len = strlen(fname) + 1;
254 lp = (struct f_link *)bmalloc(sizeof(struct f_link) + len);
255 lp->ino = ff_pkt->statp.st_ino;
256 lp->dev = ff_pkt->statp.st_dev;
257 bstrncpy(lp->name, fname, len);
258 lp->next = ff_pkt->linklist;
259 ff_pkt->linklist = lp;
260 ff_pkt->linked = lp; /* mark saved link */
262 ff_pkt->linked = NULL;
265 /* This is not a link to a previously dumped file, so dump it. */
266 if (S_ISREG(ff_pkt->statp.st_mode)) {
269 sizeleft = ff_pkt->statp.st_size;
271 /* Don't bother opening empty, world readable files. Also do not open
272 files when archive is meant for /dev/null. */
273 if (ff_pkt->null_output_device || (sizeleft == 0
274 && MODE_RALL == (MODE_RALL & ff_pkt->statp.st_mode))) {
275 ff_pkt->type = FT_REGE;
277 ff_pkt->type = FT_REG;
279 rtn_stat = handle_file(ff_pkt, pkt, top_level);
280 if (ff_pkt->linked) {
281 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
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->statp.st_ctime < ff_pkt->save_time)) {
354 /* Incremental option, directory entry not changed */
355 ff_pkt->type = FT_DIRNOCHG;
357 ff_pkt->type = FT_DIRBEGIN;
360 * Note, we return the directory to the calling program (handle_file)
361 * when we first see the directory (FT_DIRBEGIN.
362 * This allows the program to apply matches and make a
363 * choice whether or not to accept it. If it is accepted, we
364 * do not immediately save it, but do so only after everything
365 * in the directory is seen (i.e. the FT_DIREND).
367 rtn_stat = handle_file(ff_pkt, pkt, top_level);
368 if (rtn_stat < 1) { /* ignore or error status */
372 /* Done with DIRBEGIN, next call will be DIREND */
373 if (ff_pkt->type == FT_DIRBEGIN) {
374 ff_pkt->type = FT_DIREND;
378 * Create a temporary ff packet for this directory
379 * entry, and defer handling the directory until
380 * we have recursed into it. This saves the
381 * directory after all files have been processed, and
382 * during the restore, the directory permissions will
383 * be reset after all the files have been restored.
385 Dmsg1(300, "Create temp ff packet for dir: %s\n", ff_pkt->fname);
386 FF_PKT *dir_ff_pkt = (FF_PKT *)bmalloc(sizeof(FF_PKT));
387 memcpy(dir_ff_pkt, ff_pkt, sizeof(FF_PKT));
388 dir_ff_pkt->fname = bstrdup(ff_pkt->fname);
389 dir_ff_pkt->link = bstrdup(ff_pkt->link);
390 dir_ff_pkt->sys_fname = get_pool_memory(PM_FNAME);
391 dir_ff_pkt->included_files_list = NULL;
392 dir_ff_pkt->excluded_files_list = NULL;
393 dir_ff_pkt->excluded_paths_list = NULL;
394 dir_ff_pkt->linklist = NULL;
397 * Do not descend into subdirectories (recurse) if the
398 * user has turned it off for this directory.
400 * If we are crossing file systems, we are either not allowed
401 * to cross, or we may be restricted by a list of permitted
404 if (ff_pkt->flags & FO_NO_RECURSION) {
405 ff_pkt->type = FT_NORECURSE;
407 } else if (!top_level && parent_device != ff_pkt->statp.st_dev) {
408 if(!(ff_pkt->flags & FO_MULTIFS)) {
409 ff_pkt->type = FT_NOFSCHG;
411 } else if (!accept_fstype(ff_pkt, NULL)) {
412 ff_pkt->type = FT_INVALIDFS;
415 ff_pkt->volhas_attrlist = volume_has_attrlist(fname);
418 /* If not recursing, just backup dir and return */
420 rtn_stat = handle_file(ff_pkt, pkt, top_level);
421 if (ff_pkt->linked) {
422 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
425 free_dir_ff_pkt(dir_ff_pkt);
426 ff_pkt->link = ff_pkt->fname; /* reset "link" */
427 if (ff_pkt->flags & FO_KEEPATIME) {
428 utime(fname, &restore_times);
433 ff_pkt->link = ff_pkt->fname; /* reset "link" */
436 * Descend into or "recurse" into the directory to read
437 * all the files in it.
440 if ((directory = opendir(fname)) == NULL) {
441 ff_pkt->type = FT_NOOPEN;
442 ff_pkt->ff_errno = errno;
443 rtn_stat = handle_file(ff_pkt, pkt, top_level);
444 if (ff_pkt->linked) {
445 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
448 free_dir_ff_pkt(dir_ff_pkt);
453 * Process all files in this directory entry (recursing).
454 * This would possibly run faster if we chdir to the directory
455 * before traversing it.
458 entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 100);
459 for ( ; !job_canceled(jcr); ) {
463 status = readdir_r(directory, entry, &result);
464 if (status != 0 || result == NULL) {
465 // Dmsg2(99, "readdir returned stat=%d result=0x%x\n",
466 // status, (long)result);
469 ASSERT(name_max+1 > (int)sizeof(struct dirent) + (int)NAMELEN(entry));
471 /* Skip `.', `..', and excluded file names. */
472 if (p[0] == '\0' || (p[0] == '.' && (p[1] == '\0' ||
473 (p[1] == '.' && p[2] == '\0')))) {
477 if ((int)NAMELEN(entry) + len >= link_len) {
478 link_len = len + NAMELEN(entry) + 1;
479 link = (char *)brealloc(link, link_len + 1);
482 for (i=0; i < (int)NAMELEN(entry); i++) {
486 if (!file_is_excluded(ff_pkt, link)) {
487 rtn_stat = find_one_file(jcr, ff_pkt, handle_file, pkt, link, our_device, false);
488 if (ff_pkt->linked) {
489 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
498 * Now that we have recursed through all the files in the
499 * directory, we "save" the directory so that after all
500 * the files are restored, this entry will serve to reset
501 * the directory modes and dates. Temp directory values
502 * were used without this record.
504 handle_file(dir_ff_pkt, pkt, top_level); /* handle directory entry */
505 if (ff_pkt->linked) {
506 ff_pkt->linked->FileIndex = dir_ff_pkt->FileIndex;
508 free_dir_ff_pkt(dir_ff_pkt);
510 if (ff_pkt->flags & FO_KEEPATIME) {
511 utime(fname, &restore_times);
513 ff_pkt->volhas_attrlist = volhas_attrlist; /* Restore value in case it changed. */
515 } /* end check for directory */
518 * If it is explicitly mentioned (i.e. top_level) and is
519 * a block device, we do a raw backup of it or if it is
520 * a fifo, we simply read it.
522 #ifdef HAVE_FREEBSD_OS
524 * On FreeBSD, all block devices are character devices, so
525 * to be able to read a raw disk, we need the check for
526 * a character device.
527 * crw-r----- 1 root operator - 116, 0x00040002 Jun 9 19:32 /dev/ad0s3
528 * crw-r----- 1 root operator - 116, 0x00040002 Jun 9 19:32 /dev/rad0s3
530 if (top_level && (S_ISBLK(ff_pkt->statp.st_mode) || S_ISCHR(ff_pkt->statp.st_mode))) {
532 if (top_level && S_ISBLK(ff_pkt->statp.st_mode)) {
534 ff_pkt->type = FT_RAW; /* raw partition */
535 } else if (top_level && S_ISFIFO(ff_pkt->statp.st_mode) &&
536 ff_pkt->flags & FO_READFIFO) {
537 ff_pkt->type = FT_FIFO;
539 /* The only remaining types are special (character, ...) files */
540 ff_pkt->type = FT_SPEC;
542 rtn_stat = handle_file(ff_pkt, pkt, top_level);
543 if (ff_pkt->linked) {
544 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
549 int term_find_one(FF_PKT *ff)
551 struct f_link *lp, *lc;
554 /* Free up list of hard linked files */
555 for (lp = ff->linklist; lp;) {