2 * Create a file, and reset the modes
4 * Kern Sibbald, November MM
10 Copyright (C) 2000-2006 Kern Sibbald
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License
14 version 2 as amended with additional clauses defined in the
15 file LICENSE in the main source directory.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 the file LICENSE for additional details.
28 #define S_IRWXUGO (S_IRWXU | S_IRWXG | S_IRWXO)
36 static int separate_path_and_file(JCR *jcr, char *fname, char *ofile);
37 static int path_already_seen(JCR *jcr, char *path, int pnl);
41 * Create the file, or the directory
43 * fname is the original filename
44 * ofile is the output filename (may be in a different directory)
46 * Returns: CF_SKIP if file should be skipped
48 * CF_EXTRACT file created and data to restore
49 * CF_CREATED file created no data to restore
51 * Note, we create the file here, except for special files,
52 * we do not set the attributes because we want to first
53 * write the file, then when the writing is done, set the
55 * So, we return with the file descriptor open for normal
59 int create_file(JCR *jcr, ATTR *attr, BFILE *bfd, int replace)
61 int new_mode, parent_mode, mode;
68 if (is_win32_stream(attr->data_stream)) {
69 set_win32_backup(bfd);
71 set_portable_backup(bfd);
74 new_mode = attr->statp.st_mode;
75 Dmsg3(200, "type=%d newmode=%x file=%s\n", attr->type, new_mode, attr->ofname);
76 parent_mode = S_IWUSR | S_IXUSR | new_mode;
77 gid = attr->statp.st_gid;
78 uid = attr->statp.st_uid;
80 Dmsg2(400, "Replace=%c %d\n", (char)replace, replace);
81 if (lstat(attr->ofname, &mstatp) == 0) {
85 if (attr->statp.st_mtime <= mstatp.st_mtime) {
86 Qmsg(jcr, M_SKIPPED, 0, _("File skipped. Not newer: %s\n"), attr->ofname);
92 if (attr->statp.st_mtime >= mstatp.st_mtime) {
93 Qmsg(jcr, M_SKIPPED, 0, _("File skipped. Not older: %s\n"), attr->ofname);
99 Qmsg(jcr, M_SKIPPED, 0, _("File skipped. Already exists: %s\n"), attr->ofname);
106 switch (attr->type) {
107 case FT_RAW: /* raw device to be written */
108 case FT_FIFO: /* FIFO to be written to */
109 case FT_LNKSAVED: /* Hard linked, file already saved */
111 case FT_SPEC: /* fifo, ... to be backed up */
112 case FT_REGE: /* empty file */
113 case FT_REG: /* regular file */
115 * Note, we do not delete FT_RAW because these are device files
116 * or FIFOs that should already exist. If we blow it away,
117 * we may blow away a FIFO that is being used to read the
118 * restore data, or we may blow away a partition definition.
120 if (exists && attr->type != FT_RAW && attr->type != FT_FIFO) {
121 /* Get rid of old copy */
122 if (unlink(attr->ofname) == -1) {
124 Qmsg(jcr, M_ERROR, 0, _("File %s already exists and could not be replaced. ERR=%s.\n"),
125 attr->ofname, be.strerror());
126 /* Continue despite error */
130 * Here we do some preliminary work for all the above
131 * types to create the path to the file if it does
132 * not already exist. Below, we will split to
133 * do the file type specific work
135 pnl = separate_path_and_file(jcr, attr->fname, attr->ofname);
141 * If path length is <= 0 we are making a file in the root
142 * directory. Assume that the directory already exists.
146 savechr = attr->ofname[pnl];
147 attr->ofname[pnl] = 0; /* terminate path */
149 if (!path_already_seen(jcr, attr->ofname, pnl)) {
150 Dmsg1(100, "Make path %s\n", attr->ofname);
152 * If we need to make the directory, ensure that it is with
153 * execute bit set (i.e. parent_mode), and preserve what already
154 * exists. Normally, this should do nothing.
156 if (make_path(jcr, attr->ofname, parent_mode, parent_mode, uid, gid, 1, NULL) != 0) {
157 Dmsg1(10, "Could not make path. %s\n", attr->ofname);
158 attr->ofname[pnl] = savechr; /* restore full name */
162 attr->ofname[pnl] = savechr; /* restore full name */
165 /* Now we do the specific work for each file type */
169 Dmsg1(100, "Create file %s\n", attr->ofname);
170 mode = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY; /* O_NOFOLLOW; */
171 if (IS_CTG(attr->statp.st_mode)) {
172 mode |= O_CTG; /* set contiguous bit if needed */
174 Dmsg1(50, "Create file: %s\n", attr->ofname);
176 Qmsg1(jcr, M_ERROR, 0, _("bpkt already open fid=%d\n"), bfd->fid);
181 if ((bopen(bfd, attr->ofname, mode, S_IRUSR | S_IWUSR)) < 0) {
183 be.set_errno(bfd->berrno);
184 Qmsg2(jcr, M_ERROR, 0, _("Could not create %s: ERR=%s\n"),
185 attr->ofname, be.strerror());
189 /* this doesn't solve the Microsoft problem - Thorsten Engel
190 so I reused the code of V 1.40
192 * If the open fails, we attempt to cd into the directory
193 * and create the file with a relative path rather than
194 * the full absolute path. This is for Win32 where
195 * path names may be too long to create.
198 if ((bopen(bfd, attr->ofname, mode, S_IRUSR | S_IWUSR)) < 0) {
201 Dmsg2(000, "bopen failed errno=%d: ERR=%s\n", bfd->berrno,
202 be.strerror(bfd->berrno));
203 if (strlen(attr->ofname) > 250) { // Microsoft limitation
206 struct saved_cwd cwd;
207 savechr = attr->ofname[pnl];
208 attr->ofname[pnl] = 0; // terminate path
209 Dmsg1(000, "Do chdir %s\n", attr->ofname);
210 if (save_cwd(&cwd) != 0) {
211 Qmsg0(jcr, M_ERROR, 0, _("Could not save_dirn"));
212 attr->ofname[pnl] = savechr;
216 while ((e = strchr(p, '/'))) {
220 Qmsg2(jcr, M_ERROR, 0, _("Could not chdir to %s: ERR=%s\n"),
221 attr->ofname, be.strerror());
222 restore_cwd(&cwd, NULL, NULL);
224 attr->ofname[pnl] = savechr;
233 Qmsg2(jcr, M_ERROR, 0, _("Could not chdir to %s: ERR=%s\n"),
234 attr->ofname, be.strerror());
235 restore_cwd(&cwd, NULL, NULL);
237 attr->ofname[pnl] = savechr;
240 attr->ofname[pnl] = savechr;
241 Dmsg1(000, "Do open %s\n", &attr->ofname[pnl+1]);
242 if ((bopen(bfd, &attr->ofname[pnl+1], mode, S_IRUSR | S_IWUSR)) < 0) {
247 restore_cwd(&cwd, NULL, NULL);
249 if (stat == CF_EXTRACT) {
253 Qmsg2(jcr, M_ERROR, 0, _("Could not create %s: ERR=%s\n"),
254 attr->ofname, be.strerror(bfd->berrno));
258 #ifndef HAVE_WIN32 // none of these exists on MS Windows
259 case FT_RAW: /* Bacula raw device e.g. /dev/sda1 */
260 case FT_FIFO: /* Bacula fifo to save data */
262 if (S_ISFIFO(attr->statp.st_mode)) {
263 Dmsg1(200, "Restore fifo: %s\n", attr->ofname);
264 if (mkfifo(attr->ofname, attr->statp.st_mode) != 0 && errno != EEXIST) {
266 Qmsg2(jcr, M_ERROR, 0, _("Cannot make fifo %s: ERR=%s\n"),
267 attr->ofname, be.strerror());
270 } else if(S_ISSOCK(attr->statp.st_mode)) {
271 Dmsg1(200, "Skipping restore of socket: %s\n", attr->ofname);
273 Dmsg1(200, "Restore node: %s\n", attr->ofname);
274 if (mknod(attr->ofname, attr->statp.st_mode, attr->statp.st_rdev) != 0 && errno != EEXIST) {
276 Qmsg2(jcr, M_ERROR, 0, _("Cannot make node %s: ERR=%s\n"),
277 attr->ofname, be.strerror());
281 if (attr->type == FT_RAW || attr->type == FT_FIFO) {
283 Dmsg1(200, "FT_RAW|FT_FIFO %s\n", attr->ofname);
284 mode = O_WRONLY | O_BINARY;
285 /* Timeout open() in 60 seconds */
286 if (attr->type == FT_FIFO) {
287 Dmsg0(200, "Set FIFO timer\n");
288 tid = start_thread_timer(pthread_self(), 60);
293 Qmsg1(jcr, M_ERROR, 0, _("bpkt already open fid=%d\n"), bfd->fid);
295 Dmsg2(200, "open %s mode=0x%x\n", attr->ofname, mode);
296 if ((bopen(bfd, attr->ofname, mode, 0)) < 0) {
298 be.set_errno(bfd->berrno);
299 Qmsg2(jcr, M_ERROR, 0, _("Could not open %s: ERR=%s\n"),
300 attr->ofname, be.strerror());
301 stop_thread_timer(tid);
304 stop_thread_timer(tid);
307 Dmsg1(200, "FT_SPEC %s\n", attr->ofname);
311 Dmsg2(130, "FT_LNK should restore: %s -> %s\n", attr->ofname, attr->olname);
312 if (symlink(attr->olname, attr->ofname) != 0 && errno != EEXIST) {
314 Qmsg3(jcr, M_ERROR, 0, _("Could not symlink %s -> %s: ERR=%s\n"),
315 attr->ofname, attr->olname, be.strerror());
320 case FT_LNKSAVED: /* Hard linked, file already saved */
321 Dmsg2(130, "Hard link %s => %s\n", attr->ofname, attr->olname);
322 if (link(attr->olname, attr->ofname) != 0) {
324 Qmsg3(jcr, M_ERROR, 0, _("Could not hard link %s -> %s: ERR=%s\n"),
325 attr->ofname, attr->olname, be.strerror());
330 } /* End inner switch */
334 Dmsg2(200, "Make dir mode=%o dir=%s\n", new_mode, attr->ofname);
335 if (make_path(jcr, attr->ofname, new_mode, parent_mode, uid, gid, 0, NULL) != 0) {
339 * If we are using the Win32 Backup API, we open the
340 * directory so that the security info will be read
343 if (!is_portable_backup(bfd)) {
345 Qmsg1(jcr, M_ERROR, 0, _("bpkt already open fid=%d\n"), bfd->fid);
347 if ((bopen(bfd, attr->ofname, O_WRONLY|O_BINARY, 0)) < 0) {
349 be.set_errno(bfd->berrno);
351 /* Check for trying to create a drive, if so, skip */
352 if (attr->ofname[1] == ':' && attr->ofname[2] == '/' && attr->ofname[3] == 0) {
356 Qmsg2(jcr, M_ERROR, 0, _("Could not open %s: ERR=%s\n"),
357 attr->ofname, be.strerror());
365 /* The following should not occur */
375 Qmsg2(jcr, M_ERROR, 0, _("Original file %s not saved: type=%d\n"), attr->fname, attr->type);
378 Qmsg2(jcr, M_ERROR, 0, _("Unknown file type %d; not restored: %s\n"), attr->type, attr->fname);
385 * Returns: > 0 index into path where last path char is.
387 * -1 filename is zero length
389 static int separate_path_and_file(JCR *jcr, char *fname, char *ofile)
394 /* Separate pathname and filename */
395 for (p=f=ofile; *p; p++) {
397 f = p; /* possible filename */
406 /* The filename length must not be zero here because we
407 * are dealing with a file (i.e. FT_REGE or FT_REG).
409 Qmsg1(jcr, M_ERROR, 0, _("Zero length filename: %s\n"), fname);
417 * Primitive caching of path to prevent recreating a pathname
418 * each time as long as we remain in the same directory.
420 static int path_already_seen(JCR *jcr, char *path, int pnl)
422 if (!jcr->cached_path) {
423 jcr->cached_path = get_pool_memory(PM_FNAME);
425 if (jcr->cached_pnl == pnl && strcmp(path, jcr->cached_path) == 0) {
428 pm_strcpy(&jcr->cached_path, path);
429 jcr->cached_pnl = pnl;