]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/create_file.c
0022bb8bd96ab74b043f8b534665a829bb55b4e4
[bacula/bacula] / bacula / src / findlib / create_file.c
1 /*
2  *  Create a file, and reset the modes
3  *
4  *    Kern Sibbald, November MM
5  *
6  *   Version $Id$
7  *
8  */
9 /*
10    Copyright (C) 2000-2004 Kern Sibbald and John Walker
11
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.
16
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.
21
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,
25    MA 02111-1307, USA.
26
27  */
28
29 #include "bacula.h"
30 #include "find.h"
31
32 #ifndef S_IRWXUGO
33 #define S_IRWXUGO (S_IRWXU | S_IRWXG | S_IRWXO)
34 #endif
35
36 #ifndef IS_CTG
37 #define IS_CTG(x) 0
38 #define O_CTG 0
39 #endif
40
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);
43
44
45 /*
46  * Create the file, or the directory
47  *
48  *  fname is the original filename
49  *  ofile is the output filename (may be in a different directory)
50  *
51  * Returns:  CF_SKIP     if file should be skipped
52  *           CF_ERROR    on error
53  *           CF_EXTRACT  file created and data to restore
54  *           CF_CREATED  file created no data to restore
55  *
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
59  *     attributes.
60  *   So, we return with the file descriptor open for normal
61  *     files.
62  *
63  */
64 int create_file(JCR *jcr, ATTR *attr, BFILE *bfd, int replace)
65 {
66    int new_mode, parent_mode, mode;
67    uid_t uid;
68    gid_t gid;
69    int pnl;
70    bool exists = false;
71    struct stat mstatp;
72
73    if (is_win32_stream(attr->data_stream)) {
74       set_win32_backup(bfd);
75    } else {
76       set_portable_backup(bfd);
77    }
78
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;
84
85    Dmsg2(400, "Replace=%c %d\n", (char)replace, replace);
86    if (lstat(attr->ofname, &mstatp) == 0) {
87       exists = true;
88       switch (replace) {
89       case REPLACE_IFNEWER:
90          if (attr->statp.st_mtime <= mstatp.st_mtime) {
91             Jmsg(jcr, M_SKIPPED, 0, _("File skipped. Not newer: %s\n"), attr->ofname);
92             return CF_SKIP;
93          }
94          break;
95
96       case REPLACE_IFOLDER:
97          if (attr->statp.st_mtime >= mstatp.st_mtime) {
98             Jmsg(jcr, M_SKIPPED, 0, _("File skipped. Not older: %s\n"), attr->ofname);
99             return CF_SKIP;
100          }
101          break;
102
103       case REPLACE_NEVER:
104          Jmsg(jcr, M_SKIPPED, 0, _("File skipped. Already exists: %s\n"), attr->ofname);
105          return CF_SKIP;
106
107       case REPLACE_ALWAYS:
108          break;
109       }
110    }
111    switch (attr->type) {
112    case FT_LNKSAVED:                  /* Hard linked, file already saved */
113    case FT_LNK:
114    case FT_RAW:
115    case FT_FIFO:
116    case FT_SPEC:
117    case FT_REGE:                      /* empty file */
118    case FT_REG:                       /* regular file */
119       if (exists) {
120          /* Get rid of old copy */
121    int nRetCode;
122
123    if (p_wunlink)
124    {
125      WCHAR szBuf[MAX_PATH_UNICODE];
126      UTF8_2_wchar(szBuf, attr->ofname, MAX_PATH_UNICODE);
127
128      nRetCode = _wunlink(szBuf);
129    }
130    else
131       nRetCode = unlink(attr->ofname);
132    
133    if (nRetCode == -1) {
134             berrno be;
135             Jmsg(jcr, M_ERROR, 0, _("File %s already exists and could not be replaced. ERR=%s.\n"),
136                attr->ofname, be.strerror());
137             /* Continue despite error */
138          }
139       }
140       /*
141        * Here we do some preliminary work for all the above
142        *   types to create the path to the file if it does
143        *   not already exist.  Below, we will split to
144        *   do the file type specific work
145        */
146       pnl = separate_path_and_file(jcr, attr->fname, attr->ofname);
147       if (pnl < 0) {
148          return CF_ERROR;
149       }
150
151       /*
152        * If path length is <= 0 we are making a file in the root
153        *  directory. Assume that the directory already exists.
154        */
155       if (pnl > 0) {
156          char savechr;
157          savechr = attr->ofname[pnl];
158          attr->ofname[pnl] = 0;                 /* terminate path */
159
160          if (!path_already_seen(jcr, attr->ofname, pnl)) {
161             Dmsg1(50, "Make path %s\n", attr->ofname);
162             /*
163              * If we need to make the directory, ensure that it is with
164              * execute bit set (i.e. parent_mode), and preserve what already
165              * exists. Normally, this should do nothing.
166              */
167             if (make_path(jcr, attr->ofname, parent_mode, parent_mode, uid, gid, 1, NULL) != 0) {
168                Dmsg1(10, "Could not make path. %s\n", attr->ofname);
169                attr->ofname[pnl] = savechr;     /* restore full name */
170                return CF_ERROR;
171             }
172          }
173          attr->ofname[pnl] = savechr;           /* restore full name */
174       }
175
176       /* Now we do the specific work for each file type */
177       switch(attr->type) {
178       case FT_REGE:
179       case FT_REG:
180          Dmsg1(100, "Create file %s\n", attr->ofname);
181          mode =  O_WRONLY | O_CREAT | O_TRUNC | O_BINARY; /*  O_NOFOLLOW; */
182          if (IS_CTG(attr->statp.st_mode)) {
183             mode |= O_CTG;               /* set contiguous bit if needed */
184          }
185          Dmsg1(50, "Create file: %s\n", attr->ofname);
186          if (is_bopen(bfd)) {
187             Jmsg1(jcr, M_ERROR, 0, "bpkt already open fid=%d\n", bfd->fid);
188             bclose(bfd);
189          }
190          if ((bopen(bfd, attr->ofname, mode, S_IRUSR | S_IWUSR)) < 0) {
191             berrno be;
192             be.set_errno(bfd->berrno);
193             Jmsg2(jcr, M_ERROR, 0, _("Could not create %s: ERR=%s\n"),
194                   attr->ofname, be.strerror());
195             return CF_ERROR;
196          }
197          return CF_EXTRACT;
198 #ifndef HAVE_WIN32  // none of these exists on MS Windows
199       case FT_RAW:                    /* Bacula raw device e.g. /dev/sda1 */
200       case FT_FIFO:                   /* Bacula fifo to save data */
201       case FT_SPEC:
202          if (S_ISFIFO(attr->statp.st_mode)) {
203             Dmsg1(200, "Restore fifo: %s\n", attr->ofname);
204             if (mkfifo(attr->ofname, attr->statp.st_mode) != 0 && errno != EEXIST) {
205                berrno be;
206                Jmsg2(jcr, M_ERROR, 0, _("Cannot make fifo %s: ERR=%s\n"),
207                      attr->ofname, be.strerror());
208                return CF_ERROR;
209             }
210          } else {
211             Dmsg1(200, "Restore node: %s\n", attr->ofname);
212             if (mknod(attr->ofname, attr->statp.st_mode, attr->statp.st_rdev) != 0 && errno != EEXIST) {
213                berrno be;
214                Jmsg2(jcr, M_ERROR, 0, _("Cannot make node %s: ERR=%s\n"),
215                      attr->ofname, be.strerror());
216                return CF_ERROR;
217             }
218          }
219          if (attr->type == FT_RAW || attr->type == FT_FIFO) {
220             btimer_t *tid;
221             Dmsg1(200, "FT_RAW|FT_FIFO %s\n", attr->ofname);
222             mode =  O_WRONLY | O_BINARY;
223             /* Timeout open() in 60 seconds */
224             if (attr->type == FT_FIFO) {
225                tid = start_thread_timer(pthread_self(), 60);
226             } else {
227                tid = NULL;
228             }
229             if (is_bopen(bfd)) {
230                Jmsg1(jcr, M_ERROR, 0, "bpkt already open fid=%d\n", bfd->fid);
231             }
232             if ((bopen(bfd, attr->ofname, mode, 0)) < 0) {
233                berrno be;
234                be.set_errno(bfd->berrno);
235                Jmsg2(jcr, M_ERROR, 0, _("Could not open %s: ERR=%s\n"),
236                      attr->ofname, be.strerror());
237                stop_thread_timer(tid);
238                return CF_ERROR;
239             }
240             stop_thread_timer(tid);
241             return CF_EXTRACT;
242          }
243          Dmsg1(200, "FT_SPEC %s\n", attr->ofname);
244          return CF_CREATED;
245
246       case FT_LNK:
247          Dmsg2(130, "FT_LNK should restore: %s -> %s\n", attr->ofname, attr->olname);
248          if (symlink(attr->olname, attr->ofname) != 0 && errno != EEXIST) {
249             berrno be;
250             Jmsg3(jcr, M_ERROR, 0, _("Could not symlink %s -> %s: ERR=%s\n"),
251                   attr->ofname, attr->olname, be.strerror());
252             return CF_ERROR;
253          }
254          return CF_CREATED;
255
256       case FT_LNKSAVED:                  /* Hard linked, file already saved */
257          Dmsg2(130, "Hard link %s => %s\n", attr->ofname, attr->olname);
258          if (link(attr->olname, attr->ofname) != 0) {
259             berrno be;
260             Jmsg3(jcr, M_ERROR, 0, _("Could not hard link %s -> %s: ERR=%s\n"),
261                   attr->ofname, attr->olname, be.strerror());
262             return CF_ERROR;
263          }
264          return CF_CREATED;
265 #endif
266       } /* End inner switch */
267
268    case FT_DIRBEGIN:
269    case FT_DIREND:
270       Dmsg2(200, "Make dir mode=%o dir=%s\n", new_mode, attr->ofname);
271       if (make_path(jcr, attr->ofname, new_mode, parent_mode, uid, gid, 0, NULL) != 0) {
272          return CF_ERROR;
273       }
274       /*
275        * If we are using the Win32 Backup API, we open the
276        *   directory so that the security info will be read
277        *   and saved.
278        */
279       if (!is_portable_backup(bfd)) {
280          if (is_bopen(bfd)) {
281             Jmsg1(jcr, M_ERROR, 0, "bpkt already open fid=%d\n", bfd->fid);
282          }
283          if ((bopen(bfd, attr->ofname, O_WRONLY|O_BINARY, 0)) < 0) {
284             berrno be;
285             be.set_errno(bfd->berrno);
286 #ifdef HAVE_WIN32
287             /* Check for trying to create a drive, if so, skip */
288             if (attr->ofname[1] == ':' && attr->ofname[2] == '/' && attr->ofname[3] == 0) {
289                return CF_SKIP;
290             }
291 #endif
292             Jmsg2(jcr, M_ERROR, 0, _("Could not open %s: ERR=%s\n"),
293                   attr->ofname, be.strerror());
294             return CF_ERROR;
295          }
296          return CF_EXTRACT;
297       } else {
298          return CF_CREATED;
299       }
300
301    /* The following should not occur */
302    case FT_NOACCESS:
303    case FT_NOFOLLOW:
304    case FT_NOSTAT:
305    case FT_DIRNOCHG:
306    case FT_NOCHG:
307    case FT_ISARCH:
308    case FT_NORECURSE:
309    case FT_NOFSCHG:
310    case FT_NOOPEN:
311       Jmsg2(jcr, M_ERROR, 0, _("Original file %s not saved: type=%d\n"), attr->fname, attr->type);
312       break;
313    default:
314       Jmsg2(jcr, M_ERROR, 0, _("Unknown file type %d; not restored: %s\n"), attr->type, attr->fname);
315       break;
316    }
317    return CF_ERROR;
318 }
319
320 /*
321  *  Returns: > 0 index into path where last path char is.
322  *           0  no path
323  *           -1 filename is zero length
324  */
325 static int separate_path_and_file(JCR *jcr, char *fname, char *ofile)
326 {
327    char *f, *p;
328    int fnl, pnl;
329
330    /* Separate pathname and filename */
331    for (p=f=ofile; *p; p++) {
332       if (*p == '/') {
333          f = p;                    /* possible filename */
334       }
335    }
336    if (*f == '/') {
337       f++;
338    }
339
340    fnl = p - f;
341    if (fnl == 0) {
342       /* The filename length must not be zero here because we
343        *  are dealing with a file (i.e. FT_REGE or FT_REG).
344        */
345       Jmsg1(jcr, M_ERROR, 0, _("Zero length filename: %s\n"), fname);
346       return -1;
347    }
348    pnl = f - ofile - 1;
349    return pnl;
350 }
351
352 /*
353  * Primitive caching of path to prevent recreating a pathname
354  *   each time as long as we remain in the same directory.
355  */
356 static int path_already_seen(JCR *jcr, char *path, int pnl)
357 {
358    if (!jcr->cached_path) {
359       jcr->cached_path = get_pool_memory(PM_FNAME);
360    }
361    if (jcr->cached_pnl == pnl && strcmp(path, jcr->cached_path) == 0) {
362       return 1;
363    }
364    pm_strcpy(&jcr->cached_path, path);
365    jcr->cached_pnl = pnl;
366    return 0;
367 }