2 * Create a file, and reset the modes
4 * Kern Sibbald, November MM
10 Copyright (C) 2000-2003 Kern Sibbald and John Walker
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of
15 the License, or (at your option) any later version.
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 GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public
23 License along with this program; if not, write to the Free
24 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
33 #define S_IRWXUGO (S_IRWXU | S_IRWXG | S_IRWXO)
41 static int separate_path_and_file(JCR *jcr, char *fname, char *ofile);
42 static int path_already_seen(char *path, int pnl);
46 * Create the file, or the directory
48 * fname is the original filename
49 * ofile is the output filename (may be in a different directory)
51 * Returns: CF_SKIP if file should be skipped
53 * CF_EXTRACT file created and data to restore
54 * CF_CREATED file created no data to restore
56 * Note, we create the file here, except for special files,
57 * we do not set the attributes because we want to first
58 * write the file, then when the writing is done, set the
60 * So, we return with the file descriptor open for normal
64 int create_file(JCR *jcr, ATTR *attr, BFILE *ofd, int replace)
66 int new_mode, parent_mode, mode;
74 /* Set desired writing mode (BackupWrite() or write()) */
75 switch (attr->data_stream) {
77 break; /* use default defined by system */
79 /* These streams require using BackupWrite() */
80 case STREAM_WIN32_ATTRIBUTES:
81 case STREAM_WIN32_DATA:
82 case STREAM_WIN32_GZIP_DATA:
83 if (!set_win32_backup(ofd, 1)) { /* use BackupWrite() */
84 Jmsg(jcr, M_ERROR, 0, _("Could not set Win32 output format.\n"));
88 /* All other stream use standard system I/O (portable) */
90 set_win32_backup(ofd, 0); /* Disable using BackupWrite() */
94 new_mode = attr->statp.st_mode;
95 Dmsg2(300, "newmode=%x file=%s\n", new_mode, attr->ofname);
96 parent_mode = S_IWUSR | S_IXUSR | new_mode;
97 gid = attr->statp.st_gid;
98 uid = attr->statp.st_uid;
100 Dmsg2(400, "Replace=%c %d\n", (char)replace, replace);
101 if (lstat(attr->ofname, &mstatp) == 0) {
104 case REPLACE_IFNEWER:
105 if (attr->statp.st_mtime <= mstatp.st_mtime) {
106 Jmsg(jcr, M_SKIPPED, 0, _("File skipped. Not newer: %s\n"), attr->ofname);
111 case REPLACE_IFOLDER:
112 if (attr->statp.st_mtime >= mstatp.st_mtime) {
113 Jmsg(jcr, M_SKIPPED, 0, _("File skipped. Not older: %s\n"), attr->ofname);
119 Jmsg(jcr, M_SKIPPED, 0, _("File skipped. Already exists: %s\n"), attr->ofname);
126 switch (attr->type) {
127 case FT_LNKSAVED: /* Hard linked, file already saved */
132 case FT_REGE: /* empty file */
133 case FT_REG: /* regular file */
135 /* Get rid of old copy */
136 if (unlink(attr->ofname) == -1) {
137 Jmsg(jcr, M_ERROR, 0, _("File %s already exists and could not be replaced. ERR=%s.\n"),
138 attr->ofname, strerror(errno));
139 /* Continue despite error */
143 * Here we do some preliminary work for all the above
144 * types to create the path to the file if it does
145 * not already exist. Below, we will split to
146 * do the file type specific work
148 pnl = separate_path_and_file(jcr, attr->fname, attr->ofname);
154 * If path length is <= 0 we are making a file in the root
155 * directory. Assume that the directory already exists.
159 savechr = attr->ofname[pnl];
160 attr->ofname[pnl] = 0; /* terminate path */
162 if (!path_already_seen(attr->ofname, pnl)) {
163 Dmsg1(50, "Make path %s\n", attr->ofname);
165 * If we need to make the directory, ensure that it is with
166 * execute bit set (i.e. parent_mode), and preserve what already
167 * exists. Normally, this should do nothing.
169 if (make_path(jcr, attr->ofname, parent_mode, parent_mode, uid, gid, 1, NULL) != 0) {
170 Dmsg1(0, "Could not make path. %s\n", attr->ofname);
174 attr->ofname[pnl] = savechr; /* restore full name */
177 /* Now we do the specific work for each file type */
181 Dmsg1(100, "Create file %s\n", attr->ofname);
182 mode = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY; /* O_NOFOLLOW; */
183 if (IS_CTG(attr->statp.st_mode)) {
184 mode |= O_CTG; /* set contiguous bit if needed */
186 Dmsg1(50, "Create file: %s\n", attr->ofname);
187 if ((bopen(ofd, attr->ofname, mode, S_IRUSR | S_IWUSR)) < 0) {
188 Jmsg2(jcr, M_ERROR, 0, _("Could not create %s: ERR=%s\n"),
189 attr->ofname, berror(ofd));
194 case FT_RAW: /* Bacula raw device e.g. /dev/sda1 */
195 case FT_FIFO: /* Bacula fifo to save data */
197 if (S_ISFIFO(attr->statp.st_mode)) {
198 Dmsg1(200, "Restore fifo: %s\n", attr->ofname);
199 if (mkfifo(attr->ofname, attr->statp.st_mode) != 0 && errno != EEXIST) {
200 Jmsg2(jcr, M_ERROR, 0, _("Cannot make fifo %s: ERR=%s\n"),
201 attr->ofname, strerror(errno));
205 Dmsg1(200, "Restore node: %s\n", attr->ofname);
206 if (mknod(attr->ofname, attr->statp.st_mode, attr->statp.st_rdev) != 0 && errno != EEXIST) {
207 Jmsg2(jcr, M_ERROR, 0, _("Cannot make node %s: ERR=%s\n"),
208 attr->ofname, strerror(errno));
212 if (attr->type == FT_RAW || attr->type == FT_FIFO) {
214 Dmsg1(200, "FT_RAW|FT_FIFO %s\n", attr->ofname);
215 mode = O_WRONLY | O_BINARY;
216 /* Timeout open() in 60 seconds */
217 if (attr->type == FT_FIFO) {
218 tid = start_thread_timer(pthread_self(), 60);
222 if ((bopen(ofd, attr->ofname, mode, 0)) < 0) {
223 Jmsg2(jcr, M_ERROR, 0, _("Could not open %s: ERR=%s\n"),
224 attr->ofname, berror(ofd));
225 stop_thread_timer(tid);
228 stop_thread_timer(tid);
231 Dmsg1(200, "FT_SPEC %s\n", attr->ofname);
235 Dmsg2(130, "FT_LNK should restore: %s -> %s\n", attr->ofname, attr->olname);
236 if (symlink(attr->olname, attr->ofname) != 0 && errno != EEXIST) {
237 Jmsg3(jcr, M_ERROR, 0, _("Could not symlink %s -> %s: ERR=%s\n"),
238 attr->ofname, attr->olname, strerror(errno));
243 case FT_LNKSAVED: /* Hard linked, file already saved */
244 Dmsg2(130, "Hard link %s => %s\n", attr->ofname, attr->olname);
245 if (link(attr->olname, attr->ofname) != 0) {
246 Jmsg3(jcr, M_ERROR, 0, _("Could not hard link %s -> %s: ERR=%s\n"),
247 attr->ofname, attr->olname, strerror(errno));
252 } /* End inner switch */
255 Dmsg2(200, "Make dir mode=%o dir=%s\n", new_mode, attr->ofname);
256 if (make_path(jcr, attr->ofname, new_mode, parent_mode, uid, gid, 0, NULL) != 0) {
260 * If we are using the Win32 Backup API, we open the
261 * directory so that the security info will be read
264 if (is_win32_backup()) {
265 if ((bopen(ofd, attr->ofname, O_WRONLY|O_BINARY, 0)) < 0) {
266 Jmsg2(jcr, M_ERROR, 0, _("Could not open %s: ERR=%s\n"),
267 attr->ofname, berror(ofd));
275 /* The following should not occur */
285 Jmsg2(jcr, M_ERROR, 0, _("Original file %s not saved: type=%d\n"), attr->fname, attr->type);
288 Jmsg2(jcr, M_ERROR, 0, _("Unknown file type %d; not restored: %s\n"), attr->type, attr->fname);
295 * Returns: > 0 index into path where last path char is.
297 * -1 filename is zero length
299 static int separate_path_and_file(JCR *jcr, char *fname, char *ofile)
304 /* Separate pathname and filename */
305 for (p=f=ofile; *p; p++) {
307 f = p; /* possible filename */
316 /* The filename length must not be zero here because we
317 * are dealing with a file (i.e. FT_REGE or FT_REG).
319 Jmsg1(jcr, M_ERROR, 0, _("Zero length filename: %s\n"), fname);
326 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
329 * Primitive caching of path to prevent recreating a pathname
330 * each time as long as we remain in the same directory.
332 static int path_already_seen(char *path, int pnl)
334 static int cached_pnl = 0;
335 static char cached_path[1000];
338 if (cached_pnl == pnl && strcmp(path, cached_path) == 0) {
342 if (pnl < (int)(sizeof(cached_path)-1)) {
343 strcpy(cached_path, path);