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.
32 extern size_t name_max; /* filename max length */
33 extern size_t path_max; /* path name max length */
35 #ifndef HAVE_READDIR_R
36 int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
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 */
69 * handle_file is the callback for handling the file.
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.
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)
79 struct utimbuf restore_times;
82 ff_pkt->fname = ff_pkt->link = fname;
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);
91 Dmsg1(60, "File ----: %s\n", fname);
93 if (S_ISLNK(ff_pkt->statp.st_mode))
94 Dmsg1(60, "Link-------------: %s \n", fname);
97 /* Save current times of this directory in case we need to
98 * reset them because the user doesn't want them changed.
100 restore_times.actime = ff_pkt->statp.st_atime;
101 restore_times.modtime = ff_pkt->statp.st_mtime;
105 * If this is an Incremental backup, see if file was modified
106 * since our last "save_time", presumably the last Full save
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);
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);
134 * Handle hard linked files
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
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))) {
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);
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 */
167 ff_pkt->linked = NULL;
170 /* This is not a link to a previously dumped file, so dump it. */
171 if (S_ISREG(ff_pkt->statp.st_mode)) {
174 sizeleft = ff_pkt->statp.st_size;
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;
182 ff_pkt->type = FT_REG;
184 rtn_stat = handle_file(ff_pkt, pkt);
185 if (ff_pkt->linked) {
186 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
191 } else if (S_ISLNK(ff_pkt->statp.st_mode)) {
193 char *buffer = (char *)alloca(path_max + name_max + 2);
195 size = readlink(fname, buffer, path_max + name_max + 1);
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;
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;
215 } else if (S_ISDIR(ff_pkt->statp.st_mode)) {
217 struct dirent *entry, *result;
222 dev_t our_device = ff_pkt->statp.st_dev;
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;
235 /* Build a canonical directory name with a trailing slash. */
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] == '/')
243 link[len++] = '/'; /* add back one */
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;
253 ff_pkt->type = FT_DIR;
255 handle_file(ff_pkt, pkt); /* handle directory entry */
256 if (ff_pkt->linked) {
257 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
260 ff_pkt->link = ff_pkt->fname; /* reset "link" */
263 * Do not decend into subdirectories (recurse) if the
264 * user has turned it off for this directory.
266 if (ff_pkt->flags & FO_NO_RECURSION) {
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;
278 * See if we are crossing file systems, and
279 * avoid doing so if the user only wants to dump one file system.
281 if (!top_level && !(ff_pkt->flags & FO_MULTIFS) &&
282 parent_device != ff_pkt->statp.st_dev) {
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;
293 * Now process the files in this directory.
296 if ((directory = opendir(fname)) == NULL) {
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;
308 * This would possibly run faster if we chdir to the directory
309 * before traversing it.
312 entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 100);
313 for ( ; !job_cancelled(jcr); ) {
317 status = readdir_r(directory, entry, &result);
318 Dmsg3(200, "readdir stat=%d result=%x name=%s\n", status, result,
320 if (status != 0 || result == NULL) {
323 ASSERT(name_max+1 > sizeof(struct dirent) + (int)NAMELEN(entry));
325 /* Skip `.', `..', and excluded file names. */
326 if (p[0] == '\0' || (p[0] == '.' && (p[1] == '\0' ||
327 (p[1] == '.' && p[2] == '\0')))) {
331 if ((int)NAMELEN(entry) + len >= link_len) {
332 link_len = len + NAMELEN(entry) + 1;
333 link = (char *)brealloc(link, link_len + 1);
336 for (i=0; i < (int)NAMELEN(entry); i++) {
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;
351 if (ff_pkt->atime_preserve) {
352 utime(fname, &restore_times);
355 } /* end check for directory */
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.
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;
368 /* The only remaining types are special (character, ...) files */
369 ff_pkt->type = FT_SPEC;
371 rtn_stat = handle_file(ff_pkt, pkt);
372 if (ff_pkt->linked) {
373 ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
378 int term_find_one(FF_PKT *ff)
380 struct f_link *lp, *lc;
383 /* Free up list of hard linked files */
384 for (lp = ff->linklist; lp;) {