2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2008 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version two of the GNU General Public
10 License as published by the Free Software Foundation and included
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of John Walker.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
29 * Create a file, and reset the modes
31 * Kern Sibbald, November MM
41 #define S_IRWXUGO (S_IRWXU | S_IRWXG | S_IRWXO)
49 static int separate_path_and_file(JCR *jcr, char *fname, char *ofile);
50 static int path_already_seen(JCR *jcr, char *path, int pnl);
54 * Create the file, or the directory
56 * fname is the original filename
57 * ofile is the output filename (may be in a different directory)
59 * Returns: CF_SKIP if file should be skipped
61 * CF_EXTRACT file created and data to restore
62 * CF_CREATED file created no data to restore
64 * Note, we create the file here, except for special files,
65 * we do not set the attributes because we want to first
66 * write the file, then when the writing is done, set the
68 * So, we return with the file descriptor open for normal
72 int create_file(JCR *jcr, ATTR *attr, BFILE *bfd, int replace)
74 int new_mode, parent_mode, mode;
81 bfd->reparse_point = false;
82 if (is_win32_stream(attr->data_stream)) {
83 set_win32_backup(bfd);
85 set_portable_backup(bfd);
88 new_mode = attr->statp.st_mode;
89 Dmsg3(200, "type=%d newmode=%x file=%s\n", attr->type, new_mode, attr->ofname);
90 parent_mode = S_IWUSR | S_IXUSR | new_mode;
91 gid = attr->statp.st_gid;
92 uid = attr->statp.st_uid;
95 if (!bfd->use_backup_api) {
96 // eliminate invalid windows filename characters from foreign filenames
97 char *ch = (char *)attr->ofname;
98 if (ch[0] != 0 && ch[1] != 0) {
117 Dmsg2(400, "Replace=%c %d\n", (char)replace, replace);
118 if (lstat(attr->ofname, &mstatp) == 0) {
121 case REPLACE_IFNEWER:
122 if (attr->statp.st_mtime <= mstatp.st_mtime) {
123 Qmsg(jcr, M_SKIPPED, 0, _("File skipped. Not newer: %s\n"), attr->ofname);
128 case REPLACE_IFOLDER:
129 if (attr->statp.st_mtime >= mstatp.st_mtime) {
130 Qmsg(jcr, M_SKIPPED, 0, _("File skipped. Not older: %s\n"), attr->ofname);
136 Qmsg(jcr, M_SKIPPED, 0, _("File skipped. Already exists: %s\n"), attr->ofname);
143 switch (attr->type) {
144 case FT_RAW: /* raw device to be written */
145 case FT_FIFO: /* FIFO to be written to */
146 case FT_LNKSAVED: /* Hard linked, file already saved */
148 case FT_SPEC: /* fifo, ... to be backed up */
149 case FT_REGE: /* empty file */
150 case FT_REG: /* regular file */
152 * Note, we do not delete FT_RAW because these are device files
153 * or FIFOs that should already exist. If we blow it away,
154 * we may blow away a FIFO that is being used to read the
155 * restore data, or we may blow away a partition definition.
157 if (exists && attr->type != FT_RAW && attr->type != FT_FIFO) {
158 /* Get rid of old copy */
159 Dmsg1(400, "unlink %s\n", attr->ofname);
160 if (unlink(attr->ofname) == -1) {
162 Qmsg(jcr, M_ERROR, 0, _("File %s already exists and could not be replaced. ERR=%s.\n"),
163 attr->ofname, be.bstrerror());
164 /* Continue despite error */
168 * Here we do some preliminary work for all the above
169 * types to create the path to the file if it does
170 * not already exist. Below, we will split to
171 * do the file type specific work
173 pnl = separate_path_and_file(jcr, attr->fname, attr->ofname);
179 * If path length is <= 0 we are making a file in the root
180 * directory. Assume that the directory already exists.
184 savechr = attr->ofname[pnl];
185 attr->ofname[pnl] = 0; /* terminate path */
187 if (!path_already_seen(jcr, attr->ofname, pnl)) {
188 Dmsg1(400, "Make path %s\n", attr->ofname);
190 * If we need to make the directory, ensure that it is with
191 * execute bit set (i.e. parent_mode), and preserve what already
192 * exists. Normally, this should do nothing.
194 if (make_path(jcr, attr->ofname, parent_mode, parent_mode, uid, gid, 1, NULL) != 0) {
195 Dmsg1(10, "Could not make path. %s\n", attr->ofname);
196 attr->ofname[pnl] = savechr; /* restore full name */
200 attr->ofname[pnl] = savechr; /* restore full name */
203 /* Now we do the specific work for each file type */
207 Dmsg1(100, "Create file %s\n", attr->ofname);
208 mode = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY; /* O_NOFOLLOW; */
209 if (IS_CTG(attr->statp.st_mode)) {
210 mode |= O_CTG; /* set contiguous bit if needed */
212 Dmsg1(50, "Create file: %s\n", attr->ofname);
214 Qmsg1(jcr, M_ERROR, 0, _("bpkt already open fid=%d\n"), bfd->fid);
219 if ((bopen(bfd, attr->ofname, mode, S_IRUSR | S_IWUSR)) < 0) {
221 be.set_errno(bfd->berrno);
222 Qmsg2(jcr, M_ERROR, 0, _("Could not create %s: ERR=%s\n"),
223 attr->ofname, be.bstrerror());
228 #ifndef HAVE_WIN32 // none of these exists on MS Windows
229 case FT_RAW: /* Bacula raw device e.g. /dev/sda1 */
230 case FT_FIFO: /* Bacula fifo to save data */
232 if (S_ISFIFO(attr->statp.st_mode)) {
233 Dmsg1(400, "Restore fifo: %s\n", attr->ofname);
234 if (mkfifo(attr->ofname, attr->statp.st_mode) != 0 && errno != EEXIST) {
236 Qmsg2(jcr, M_ERROR, 0, _("Cannot make fifo %s: ERR=%s\n"),
237 attr->ofname, be.bstrerror());
240 } else if (S_ISSOCK(attr->statp.st_mode)) {
241 Dmsg1(200, "Skipping restore of socket: %s\n", attr->ofname);
242 #ifdef S_IFDOOR // Solaris high speed RPC mechanism
243 } else if (S_ISDOOR(attr->statp.st_mode)) {
244 Dmsg1(200, "Skipping restore of door file: %s\n", attr->ofname);
246 #ifdef S_IFPORT // Solaris event port for handling AIO
247 } else if (S_ISPORT(attr->statp.st_mode)) {
248 Dmsg1(200, "Skipping restore of event port file: %s\n", attr->ofname);
251 Dmsg1(400, "Restore node: %s\n", attr->ofname);
252 if (mknod(attr->ofname, attr->statp.st_mode, attr->statp.st_rdev) != 0 && errno != EEXIST) {
254 Qmsg2(jcr, M_ERROR, 0, _("Cannot make node %s: ERR=%s\n"),
255 attr->ofname, be.bstrerror());
260 * Here we are going to attempt to restore to a FIFO, which
261 * means that the FIFO must already exist, AND there must
262 * be some process already attempting to read from the
263 * FIFO, so we open it write-only.
265 if (attr->type == FT_RAW || attr->type == FT_FIFO) {
267 Dmsg1(400, "FT_RAW|FT_FIFO %s\n", attr->ofname);
268 mode = O_WRONLY | O_BINARY;
269 /* Timeout open() in 60 seconds */
270 if (attr->type == FT_FIFO) {
271 Dmsg0(400, "Set FIFO timer\n");
272 tid = start_thread_timer(pthread_self(), 60);
277 Qmsg1(jcr, M_ERROR, 0, _("bpkt already open fid=%d\n"), bfd->fid);
279 Dmsg2(400, "open %s mode=0x%x\n", attr->ofname, mode);
280 if ((bopen(bfd, attr->ofname, mode, 0)) < 0) {
282 be.set_errno(bfd->berrno);
283 Qmsg2(jcr, M_ERROR, 0, _("Could not open %s: ERR=%s\n"),
284 attr->ofname, be.bstrerror());
285 Dmsg2(400, "Could not open %s: ERR=%s\n", attr->ofname, be.bstrerror());
286 stop_thread_timer(tid);
289 stop_thread_timer(tid);
292 Dmsg1(400, "FT_SPEC %s\n", attr->ofname);
296 Dmsg2(130, "FT_LNK should restore: %s -> %s\n", attr->ofname, attr->olname);
297 if (symlink(attr->olname, attr->ofname) != 0 && errno != EEXIST) {
299 Qmsg3(jcr, M_ERROR, 0, _("Could not symlink %s -> %s: ERR=%s\n"),
300 attr->ofname, attr->olname, be.bstrerror());
305 case FT_LNKSAVED: /* Hard linked, file already saved */
306 Dmsg2(130, "Hard link %s => %s\n", attr->ofname, attr->olname);
307 if (link(attr->olname, attr->ofname) != 0) {
313 * If using BSD user flags, maybe has a file flag
314 * preventing this. So attempt to disable, retry link,
316 * Note that BSD securelevel may prevent disabling flag.
319 if (stat(attr->olname, &s) == 0 && s.st_flags != 0) {
320 if (chflags(attr->olname, 0) == 0) {
321 if (link(attr->olname, attr->ofname) != 0) {
322 /* restore original file flags even when linking failed */
323 if (chflags(attr->olname, s.st_flags) < 0) {
324 Qmsg2(jcr, M_ERROR, 0, _("Could not restore file flags for file %s: ERR=%s\n"),
325 attr->olname, be.bstrerror());
327 #endif /* HAVE_CHFLAGS */
328 Qmsg3(jcr, M_ERROR, 0, _("Could not hard link %s -> %s: ERR=%s\n"),
329 attr->ofname, attr->olname, be.bstrerror());
333 /* finally restore original file flags */
334 if (chflags(attr->olname, s.st_flags) < 0) {
335 Qmsg2(jcr, M_ERROR, 0, _("Could not restore file flags for file %s: ERR=%s\n"),
336 attr->olname, be.bstrerror());
339 Qmsg2(jcr, M_ERROR, 0, _("Could not reset file flags for file %s: ERR=%s\n"),
340 attr->olname, be.bstrerror());
343 Qmsg3(jcr, M_ERROR, 0, _("Could not hard link %s -> %s: ERR=%s\n"),
344 attr->ofname, attr->olname, be.bstrerror());
347 #endif /* HAVE_CHFLAGS */
352 } /* End inner switch */
355 bfd->reparse_point = true;
356 /* Fall through wanted */
359 Dmsg2(200, "Make dir mode=%o dir=%s\n", new_mode, attr->ofname);
360 if (make_path(jcr, attr->ofname, new_mode, parent_mode, uid, gid, 0, NULL) != 0) {
364 * If we are using the Win32 Backup API, we open the
365 * directory so that the security info will be read
368 if (!is_portable_backup(bfd)) {
370 Qmsg1(jcr, M_ERROR, 0, _("bpkt already open fid=%d\n"), bfd->fid);
372 if ((bopen(bfd, attr->ofname, O_WRONLY|O_BINARY, 0)) < 0) {
374 be.set_errno(bfd->berrno);
376 /* Check for trying to create a drive, if so, skip */
377 if (attr->ofname[1] == ':' &&
378 IsPathSeparator(attr->ofname[2]) &&
379 attr->ofname[3] == '\0') {
383 Qmsg2(jcr, M_ERROR, 0, _("Could not open %s: ERR=%s\n"),
384 attr->ofname, be.bstrerror());
392 /* The following should not occur */
402 Qmsg2(jcr, M_ERROR, 0, _("Original file %s not saved: type=%d\n"), attr->fname, attr->type);
405 Qmsg2(jcr, M_ERROR, 0, _("Unknown file type %d; not restored: %s\n"), attr->type, attr->fname);
412 * Returns: > 0 index into path where last path char is.
414 * -1 filename is zero length
416 static int separate_path_and_file(JCR *jcr, char *fname, char *ofile)
421 /* Separate pathname and filename */
422 for (q=p=f=ofile; *p; p++) {
424 if (IsPathSeparator(*p)) {
426 if (IsPathSeparator(p[1])) {
430 *q++ = *p; /* copy data */
432 if (IsPathSeparator(*p)) {
433 f = q; /* possible filename */
439 if (IsPathSeparator(*f)) {
442 *q = 0; /* terminate string */
446 /* The filename length must not be zero here because we
447 * are dealing with a file (i.e. FT_REGE or FT_REG).
449 Jmsg1(jcr, M_ERROR, 0, _("Zero length filename: %s\n"), fname);
457 * Primitive caching of path to prevent recreating a pathname
458 * each time as long as we remain in the same directory.
460 static int path_already_seen(JCR *jcr, char *path, int pnl)
462 if (!jcr->cached_path) {
463 jcr->cached_path = get_pool_memory(PM_FNAME);
465 if (jcr->cached_pnl == pnl && strcmp(path, jcr->cached_path) == 0) {
468 pm_strcpy(jcr->cached_path, path);
469 jcr->cached_pnl = pnl;