2 * Create a file, and reset the modes
4 * Kern Sibbald, November MM
10 Copyright (C) 2000-2005 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 ammended 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 Dmsg2(300, "newmode=%x file=%s\n", 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 Jmsg(jcr, M_SKIPPED, 0, _("File skipped. Not newer: %s\n"), attr->ofname);
92 if (attr->statp.st_mtime >= mstatp.st_mtime) {
93 Jmsg(jcr, M_SKIPPED, 0, _("File skipped. Not older: %s\n"), attr->ofname);
99 Jmsg(jcr, M_SKIPPED, 0, _("File skipped. Already exists: %s\n"), attr->ofname);
106 switch (attr->type) {
107 case FT_LNKSAVED: /* Hard linked, file already saved */
112 case FT_REGE: /* empty file */
113 case FT_REG: /* regular file */
115 /* Get rid of old copy */
116 if (unlink(attr->ofname) == -1) {
118 Jmsg(jcr, M_ERROR, 0, _("File %s already exists and could not be replaced. ERR=%s.\n"),
119 attr->ofname, be.strerror());
120 /* Continue despite error */
124 * Here we do some preliminary work for all the above
125 * types to create the path to the file if it does
126 * not already exist. Below, we will split to
127 * do the file type specific work
129 pnl = separate_path_and_file(jcr, attr->fname, attr->ofname);
135 * If path length is <= 0 we are making a file in the root
136 * directory. Assume that the directory already exists.
140 savechr = attr->ofname[pnl];
141 attr->ofname[pnl] = 0; /* terminate path */
143 if (!path_already_seen(jcr, attr->ofname, pnl)) {
144 Dmsg1(100, "Make path %s\n", attr->ofname);
146 * If we need to make the directory, ensure that it is with
147 * execute bit set (i.e. parent_mode), and preserve what already
148 * exists. Normally, this should do nothing.
150 if (make_path(jcr, attr->ofname, parent_mode, parent_mode, uid, gid, 1, NULL) != 0) {
151 Dmsg1(10, "Could not make path. %s\n", attr->ofname);
152 attr->ofname[pnl] = savechr; /* restore full name */
156 attr->ofname[pnl] = savechr; /* restore full name */
159 /* Now we do the specific work for each file type */
163 Dmsg1(100, "Create file %s\n", attr->ofname);
164 mode = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY; /* O_NOFOLLOW; */
165 if (IS_CTG(attr->statp.st_mode)) {
166 mode |= O_CTG; /* set contiguous bit if needed */
168 Dmsg1(50, "Create file: %s\n", attr->ofname);
170 Jmsg1(jcr, M_ERROR, 0, "bpkt already open fid=%d\n", bfd->fid);
174 * If the open fails, we attempt to cd into the directory
175 * and create the file with a relative path rather than
176 * the full absolute path. This is for Win32 where
177 * path names may be too long to create.
179 if ((bopen(bfd, attr->ofname, mode, S_IRUSR | S_IWUSR)) < 0) {
182 be.set_errno(bfd->berrno);
183 Dmsg2(000, "bopen failed errno=%d: ERR=%s\n", bfd->berrno,
184 be.strerror(bfd->berrno));
187 struct saved_cwd cwd;
188 savechr = attr->ofname[pnl];
189 attr->ofname[pnl] = 0; /* terminate path */
190 Dmsg1(000, "Do chdir %s\n", attr->ofname);
191 if (save_cwd(&cwd) != 0) {
192 Jmsg0(jcr, M_ERROR, 0, _("Could not save_dirn"));
193 attr->ofname[pnl] = savechr;
196 if (chdir(attr->ofname) < 0) {
197 Jmsg2(jcr, M_ERROR, 0, _("Could not chdir to %s: ERR=%s\n"),
198 attr->ofname, be.strerror());
199 restore_cwd(&cwd, NULL, NULL);
201 attr->ofname[pnl] = savechr;
204 attr->ofname[pnl] = savechr;
205 Dmsg1(000, "Do open %s\n", &attr->ofname[pnl+1]);
206 if ((bopen(bfd, &attr->ofname[pnl+1], mode, S_IRUSR | S_IWUSR)) < 0) {
211 restore_cwd(&cwd, NULL, NULL);
213 if (stat == CF_EXTRACT) {
217 Jmsg2(jcr, M_ERROR, 0, _("Could not create %s: ERR=%s\n"),
218 attr->ofname, be.strerror());
222 #ifndef HAVE_WIN32 // none of these exists on MS Windows
223 case FT_RAW: /* Bacula raw device e.g. /dev/sda1 */
224 case FT_FIFO: /* Bacula fifo to save data */
226 if (S_ISFIFO(attr->statp.st_mode)) {
227 Dmsg1(200, "Restore fifo: %s\n", attr->ofname);
228 if (mkfifo(attr->ofname, attr->statp.st_mode) != 0 && errno != EEXIST) {
230 Jmsg2(jcr, M_ERROR, 0, _("Cannot make fifo %s: ERR=%s\n"),
231 attr->ofname, be.strerror());
235 Dmsg1(200, "Restore node: %s\n", attr->ofname);
236 if (mknod(attr->ofname, attr->statp.st_mode, attr->statp.st_rdev) != 0 && errno != EEXIST) {
238 Jmsg2(jcr, M_ERROR, 0, _("Cannot make node %s: ERR=%s\n"),
239 attr->ofname, be.strerror());
243 if (attr->type == FT_RAW || attr->type == FT_FIFO) {
245 Dmsg1(200, "FT_RAW|FT_FIFO %s\n", attr->ofname);
246 mode = O_WRONLY | O_BINARY;
247 /* Timeout open() in 60 seconds */
248 if (attr->type == FT_FIFO) {
249 tid = start_thread_timer(pthread_self(), 60);
254 Jmsg1(jcr, M_ERROR, 0, "bpkt already open fid=%d\n", bfd->fid);
256 if ((bopen(bfd, attr->ofname, mode, 0)) < 0) {
258 be.set_errno(bfd->berrno);
259 Jmsg2(jcr, M_ERROR, 0, _("Could not open %s: ERR=%s\n"),
260 attr->ofname, be.strerror());
261 stop_thread_timer(tid);
264 stop_thread_timer(tid);
267 Dmsg1(200, "FT_SPEC %s\n", attr->ofname);
271 Dmsg2(130, "FT_LNK should restore: %s -> %s\n", attr->ofname, attr->olname);
272 if (symlink(attr->olname, attr->ofname) != 0 && errno != EEXIST) {
274 Jmsg3(jcr, M_ERROR, 0, _("Could not symlink %s -> %s: ERR=%s\n"),
275 attr->ofname, attr->olname, be.strerror());
280 case FT_LNKSAVED: /* Hard linked, file already saved */
281 Dmsg2(130, "Hard link %s => %s\n", attr->ofname, attr->olname);
282 if (link(attr->olname, attr->ofname) != 0) {
284 Jmsg3(jcr, M_ERROR, 0, _("Could not hard link %s -> %s: ERR=%s\n"),
285 attr->ofname, attr->olname, be.strerror());
290 } /* End inner switch */
294 Dmsg2(200, "Make dir mode=%o dir=%s\n", new_mode, attr->ofname);
295 if (make_path(jcr, attr->ofname, new_mode, parent_mode, uid, gid, 0, NULL) != 0) {
299 * If we are using the Win32 Backup API, we open the
300 * directory so that the security info will be read
303 if (!is_portable_backup(bfd)) {
305 Jmsg1(jcr, M_ERROR, 0, "bpkt already open fid=%d\n", bfd->fid);
307 if ((bopen(bfd, attr->ofname, O_WRONLY|O_BINARY, 0)) < 0) {
309 be.set_errno(bfd->berrno);
311 /* Check for trying to create a drive, if so, skip */
312 if (attr->ofname[1] == ':' && attr->ofname[2] == '/' && attr->ofname[3] == 0) {
316 Jmsg2(jcr, M_ERROR, 0, _("Could not open %s: ERR=%s\n"),
317 attr->ofname, be.strerror());
325 /* The following should not occur */
335 Jmsg2(jcr, M_ERROR, 0, _("Original file %s not saved: type=%d\n"), attr->fname, attr->type);
338 Jmsg2(jcr, M_ERROR, 0, _("Unknown file type %d; not restored: %s\n"), attr->type, attr->fname);
345 * Returns: > 0 index into path where last path char is.
347 * -1 filename is zero length
349 static int separate_path_and_file(JCR *jcr, char *fname, char *ofile)
354 /* Separate pathname and filename */
355 for (p=f=ofile; *p; p++) {
357 f = p; /* possible filename */
366 /* The filename length must not be zero here because we
367 * are dealing with a file (i.e. FT_REGE or FT_REG).
369 Jmsg1(jcr, M_ERROR, 0, _("Zero length filename: %s\n"), fname);
377 * Primitive caching of path to prevent recreating a pathname
378 * each time as long as we remain in the same directory.
380 static int path_already_seen(JCR *jcr, char *path, int pnl)
382 if (!jcr->cached_path) {
383 jcr->cached_path = get_pool_memory(PM_FNAME);
385 if (jcr->cached_pnl == pnl && strcmp(path, jcr->cached_path) == 0) {
388 pm_strcpy(&jcr->cached_path, path);
389 jcr->cached_pnl = pnl;