2 * Create a file, and reset the modes
4 * Kern Sibbald, November MM
10 Copyright (C) 2000-2004 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(JCR *jcr, 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 *bfd, int replace)
66 int new_mode, parent_mode, mode;
73 if (is_win32_stream(attr->data_stream)) {
74 set_win32_backup(bfd);
76 set_portable_backup(bfd);
79 new_mode = attr->statp.st_mode;
80 Dmsg2(300, "newmode=%x file=%s\n", new_mode, attr->ofname);
81 parent_mode = S_IWUSR | S_IXUSR | new_mode;
82 gid = attr->statp.st_gid;
83 uid = attr->statp.st_uid;
85 Dmsg2(400, "Replace=%c %d\n", (char)replace, replace);
86 if (lstat(attr->ofname, &mstatp) == 0) {
90 if (attr->statp.st_mtime <= mstatp.st_mtime) {
91 Jmsg(jcr, M_SKIPPED, 0, _("File skipped. Not newer: %s\n"), attr->ofname);
97 if (attr->statp.st_mtime >= mstatp.st_mtime) {
98 Jmsg(jcr, M_SKIPPED, 0, _("File skipped. Not older: %s\n"), attr->ofname);
104 Jmsg(jcr, M_SKIPPED, 0, _("File skipped. Already exists: %s\n"), attr->ofname);
111 switch (attr->type) {
112 case FT_LNKSAVED: /* Hard linked, file already saved */
117 case FT_REGE: /* empty file */
118 case FT_REG: /* regular file */
120 /* Get rid of old copy */
123 #if USE_WIN32_UNICODE
124 WCHAR szBuf[MAX_PATH_UNICODE];
125 UTF8_2_wchar(szBuf, attr->ofname, MAX_PATH_UNICODE);
126 if (_wunlink(szBuf) == -1) {
128 if (unlink(attr->ofname) == -1) {
131 Jmsg(jcr, M_ERROR, 0, _("File %s already exists and could not be replaced. ERR=%s.\n"),
132 attr->ofname, be.strerror());
133 /* Continue despite error */
137 * Here we do some preliminary work for all the above
138 * types to create the path to the file if it does
139 * not already exist. Below, we will split to
140 * do the file type specific work
142 pnl = separate_path_and_file(jcr, attr->fname, attr->ofname);
148 * If path length is <= 0 we are making a file in the root
149 * directory. Assume that the directory already exists.
153 savechr = attr->ofname[pnl];
154 attr->ofname[pnl] = 0; /* terminate path */
156 if (!path_already_seen(jcr, attr->ofname, pnl)) {
157 Dmsg1(50, "Make path %s\n", attr->ofname);
159 * If we need to make the directory, ensure that it is with
160 * execute bit set (i.e. parent_mode), and preserve what already
161 * exists. Normally, this should do nothing.
163 if (make_path(jcr, attr->ofname, parent_mode, parent_mode, uid, gid, 1, NULL) != 0) {
164 Dmsg1(10, "Could not make path. %s\n", attr->ofname);
165 attr->ofname[pnl] = savechr; /* restore full name */
169 attr->ofname[pnl] = savechr; /* restore full name */
172 /* Now we do the specific work for each file type */
176 Dmsg1(100, "Create file %s\n", attr->ofname);
177 mode = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY; /* O_NOFOLLOW; */
178 if (IS_CTG(attr->statp.st_mode)) {
179 mode |= O_CTG; /* set contiguous bit if needed */
181 Dmsg1(50, "Create file: %s\n", attr->ofname);
183 Jmsg1(jcr, M_ERROR, 0, "bpkt already open fid=%d\n", bfd->fid);
186 if ((bopen(bfd, attr->ofname, mode, S_IRUSR | S_IWUSR)) < 0) {
188 be.set_errno(bfd->berrno);
189 Jmsg2(jcr, M_ERROR, 0, _("Could not create %s: ERR=%s\n"),
190 attr->ofname, be.strerror());
194 #ifndef HAVE_WIN32 // none of these exists on MS Windows
195 case FT_RAW: /* Bacula raw device e.g. /dev/sda1 */
196 case FT_FIFO: /* Bacula fifo to save data */
198 if (S_ISFIFO(attr->statp.st_mode)) {
199 Dmsg1(200, "Restore fifo: %s\n", attr->ofname);
200 if (mkfifo(attr->ofname, attr->statp.st_mode) != 0 && errno != EEXIST) {
202 Jmsg2(jcr, M_ERROR, 0, _("Cannot make fifo %s: ERR=%s\n"),
203 attr->ofname, be.strerror());
207 Dmsg1(200, "Restore node: %s\n", attr->ofname);
208 if (mknod(attr->ofname, attr->statp.st_mode, attr->statp.st_rdev) != 0 && errno != EEXIST) {
210 Jmsg2(jcr, M_ERROR, 0, _("Cannot make node %s: ERR=%s\n"),
211 attr->ofname, be.strerror());
215 if (attr->type == FT_RAW || attr->type == FT_FIFO) {
217 Dmsg1(200, "FT_RAW|FT_FIFO %s\n", attr->ofname);
218 mode = O_WRONLY | O_BINARY;
219 /* Timeout open() in 60 seconds */
220 if (attr->type == FT_FIFO) {
221 tid = start_thread_timer(pthread_self(), 60);
226 Jmsg1(jcr, M_ERROR, 0, "bpkt already open fid=%d\n", bfd->fid);
228 if ((bopen(bfd, attr->ofname, mode, 0)) < 0) {
230 be.set_errno(bfd->berrno);
231 Jmsg2(jcr, M_ERROR, 0, _("Could not open %s: ERR=%s\n"),
232 attr->ofname, be.strerror());
233 stop_thread_timer(tid);
236 stop_thread_timer(tid);
239 Dmsg1(200, "FT_SPEC %s\n", attr->ofname);
243 Dmsg2(130, "FT_LNK should restore: %s -> %s\n", attr->ofname, attr->olname);
244 if (symlink(attr->olname, attr->ofname) != 0 && errno != EEXIST) {
246 Jmsg3(jcr, M_ERROR, 0, _("Could not symlink %s -> %s: ERR=%s\n"),
247 attr->ofname, attr->olname, be.strerror());
252 case FT_LNKSAVED: /* Hard linked, file already saved */
253 Dmsg2(130, "Hard link %s => %s\n", attr->ofname, attr->olname);
254 if (link(attr->olname, attr->ofname) != 0) {
256 Jmsg3(jcr, M_ERROR, 0, _("Could not hard link %s -> %s: ERR=%s\n"),
257 attr->ofname, attr->olname, be.strerror());
262 } /* End inner switch */
266 Dmsg2(200, "Make dir mode=%o dir=%s\n", new_mode, attr->ofname);
267 if (make_path(jcr, attr->ofname, new_mode, parent_mode, uid, gid, 0, NULL) != 0) {
271 * If we are using the Win32 Backup API, we open the
272 * directory so that the security info will be read
275 if (!is_portable_backup(bfd)) {
277 Jmsg1(jcr, M_ERROR, 0, "bpkt already open fid=%d\n", bfd->fid);
279 if ((bopen(bfd, attr->ofname, O_WRONLY|O_BINARY, 0)) < 0) {
281 be.set_errno(bfd->berrno);
283 /* Check for trying to create a drive, if so, skip */
284 if (attr->ofname[1] == ':' && attr->ofname[2] == '/' && attr->ofname[3] == 0) {
288 Jmsg2(jcr, M_ERROR, 0, _("Could not open %s: ERR=%s\n"),
289 attr->ofname, be.strerror());
297 /* The following should not occur */
307 Jmsg2(jcr, M_ERROR, 0, _("Original file %s not saved: type=%d\n"), attr->fname, attr->type);
310 Jmsg2(jcr, M_ERROR, 0, _("Unknown file type %d; not restored: %s\n"), attr->type, attr->fname);
317 * Returns: > 0 index into path where last path char is.
319 * -1 filename is zero length
321 static int separate_path_and_file(JCR *jcr, char *fname, char *ofile)
326 /* Separate pathname and filename */
327 for (p=f=ofile; *p; p++) {
329 f = p; /* possible filename */
338 /* The filename length must not be zero here because we
339 * are dealing with a file (i.e. FT_REGE or FT_REG).
341 Jmsg1(jcr, M_ERROR, 0, _("Zero length filename: %s\n"), fname);
349 * Primitive caching of path to prevent recreating a pathname
350 * each time as long as we remain in the same directory.
352 static int path_already_seen(JCR *jcr, char *path, int pnl)
354 if (!jcr->cached_path) {
355 jcr->cached_path = get_pool_memory(PM_FNAME);
357 if (jcr->cached_pnl == pnl && strcmp(path, jcr->cached_path) == 0) {
360 pm_strcpy(&jcr->cached_path, path);
361 jcr->cached_pnl = pnl;