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, char *fname, char *ofile, char *lname,
65 int type, int stream, struct stat *statp,
66 char *attribsEx, BFILE *ofd, int replace)
68 int new_mode, parent_mode, mode;
74 new_mode = statp->st_mode;
75 Dmsg2(300, "newmode=%x file=%s\n", new_mode, ofile);
76 parent_mode = S_IWUSR | S_IXUSR | new_mode;
80 Dmsg2(400, "Replace=%c %d\n", (char)replace, replace);
81 /* If not always replacing, do a stat and decide */
82 if (replace != REPLACE_ALWAYS) {
84 if (lstat(ofile, &mstatp) == 0) {
87 if (statp->st_mtime <= mstatp.st_mtime) {
88 Jmsg(jcr, M_SKIPPED, 0, _("File skipped. Not newer: %s\n"), ofile);
93 if (statp->st_mtime >= mstatp.st_mtime) {
94 Jmsg(jcr, M_SKIPPED, 0, _("File skipped. Not older: %s\n"), ofile);
99 Jmsg(jcr, M_SKIPPED, 0, _("File skipped. Already exists: %s\n"), ofile);
105 case FT_LNKSAVED: /* Hard linked, file already saved */
110 case FT_REGE: /* empty file */
111 case FT_REG: /* regular file */
113 * Here we do some preliminary work for all the above
114 * types to create the path to the file if it does
115 * not already exist. Below, we will split to
116 * do the file type specific work
118 pnl = separate_path_and_file(jcr, fname, ofile);
124 * If path length is <= 0 we are making a file in the root
125 * directory. Assume that the directory already exists.
129 savechr = ofile[pnl];
130 ofile[pnl] = 0; /* terminate path */
132 if (!path_already_seen(ofile, pnl)) {
133 Dmsg1(50, "Make path %s\n", ofile);
135 * If we need to make the directory, ensure that it is with
136 * execute bit set (i.e. parent_mode), and preserve what already
137 * exists. Normally, this should do nothing.
139 if (make_path(jcr, ofile, parent_mode, parent_mode, uid, gid, 1, NULL) != 0) {
140 Dmsg1(0, "Could not make path. %s\n", ofile);
144 ofile[pnl] = savechr; /* restore full name */
147 /* Now we do the specific work for each file type */
151 Dmsg1(100, "Create file %s\n", ofile);
152 mode = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY; /* O_NOFOLLOW; */
153 if (IS_CTG(statp->st_mode)) {
154 mode |= O_CTG; /* set contiguous bit if needed */
156 Dmsg1(50, "Create file: %s\n", ofile);
157 if ((bopen(ofd, ofile, mode, S_IRUSR | S_IWUSR)) < 0) {
158 Jmsg2(jcr, M_ERROR, 0, _("Could not create %s: ERR=%s\n"),
164 case FT_RAW: /* Bacula raw device e.g. /dev/sda1 */
165 case FT_FIFO: /* Bacula fifo to save data */
167 if (S_ISFIFO(statp->st_mode)) {
168 Dmsg1(200, "Restore fifo: %s\n", ofile);
169 if (mkfifo(ofile, statp->st_mode) != 0 && errno != EEXIST) {
170 Jmsg2(jcr, M_ERROR, 0, _("Cannot make fifo %s: ERR=%s\n"),
171 ofile, strerror(errno));
175 Dmsg1(200, "Restore node: %s\n", ofile);
176 if (mknod(ofile, statp->st_mode, statp->st_rdev) != 0 && errno != EEXIST) {
177 Jmsg2(jcr, M_ERROR, 0, _("Cannot make node %s: ERR=%s\n"),
178 ofile, strerror(errno));
182 if (type == FT_RAW || type == FT_FIFO) {
184 Dmsg1(200, "FT_RAW|FT_FIFO %s\n", ofile);
185 mode = O_WRONLY | O_BINARY;
186 /* Timeout open() in 60 seconds */
187 if (type == FT_FIFO) {
188 tid = start_thread_timer(pthread_self(), 60);
192 if ((bopen(ofd, ofile, mode, 0)) < 0) {
193 Jmsg2(jcr, M_ERROR, 0, _("Could not open %s: ERR=%s\n"),
195 stop_thread_timer(tid);
198 stop_thread_timer(tid);
201 Dmsg1(200, "FT_SPEC %s\n", ofile);
205 Dmsg2(130, "FT_LNK should restore: %s -> %s\n", ofile, lname);
206 if (symlink(lname, ofile) != 0 && errno != EEXIST) {
207 Jmsg3(jcr, M_ERROR, 0, _("Could not symlink %s -> %s: ERR=%s\n"),
208 ofile, lname, strerror(errno));
213 case FT_LNKSAVED: /* Hard linked, file already saved */
214 Dmsg2(130, "Hard link %s => %s\n", ofile, lname);
215 if (link(lname, ofile) != 0) {
216 Jmsg3(jcr, M_ERROR, 0, _("Could not hard link %s -> %s: ERR=%s\n"),
217 ofile, lname, strerror(errno));
222 } /* End inner switch */
225 Dmsg2(300, "Make dir mode=%o dir=%s\n", new_mode, ofile);
226 if (make_path(jcr, ofile, new_mode, parent_mode, uid, gid, 0, NULL) != 0) {
230 * If we are using the Win32 Backup API, we open the
231 * directory so that the security info will be read
234 if (is_win32_backup()) {
235 if ((bopen(ofd, ofile, O_WRONLY|O_BINARY, 0)) < 0) {
236 Jmsg2(jcr, M_ERROR, 0, _("Could not open %s: ERR=%s\n"),
245 /* The following should not occur */
255 Jmsg2(jcr, M_ERROR, 0, _("Original file %s not saved: type=%d\n"), fname, type);
257 Jmsg2(jcr, M_ERROR, 0, _("Unknown file type %d; not restored: %s\n"), type, fname);
263 * Returns: > 0 index into path where last path char is.
265 * -1 filename is zero length
267 static int separate_path_and_file(JCR *jcr, char *fname, char *ofile)
272 /* Separate pathname and filename */
273 for (p=f=ofile; *p; p++) {
275 f = p; /* possible filename */
284 /* The filename length must not be zero here because we
285 * are dealing with a file (i.e. FT_REGE or FT_REG).
287 Jmsg1(jcr, M_ERROR, 0, _("Zero length filename: %s\n"), fname);
294 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
297 * Primitive caching of path to prevent recreating a pathname
298 * each time as long as we remain in the same directory.
300 static int path_already_seen(char *path, int pnl)
302 static int cached_pnl = 0;
303 static char cached_path[1000];
306 if (cached_pnl == pnl && strcmp(path, cached_path) == 0) {
310 if (pnl < (int)(sizeof(cached_path)-1)) {
311 strcpy(cached_path, path);