]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/create_file.c
c947cecf1c7d8e7da5b90d4b16e64f08e236d59c
[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-2003 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(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          if (unlink(attr->ofname) == -1) {
122             Jmsg(jcr, M_ERROR, 0, _("File %s already exists and could not be replaced. ERR=%s.\n"),
123                attr->ofname, strerror(errno));
124             /* Continue despite error */
125          }
126       }
127       /* 
128        * Here we do some preliminary work for all the above
129        *   types to create the path to the file if it does
130        *   not already exist.  Below, we will split to
131        *   do the file type specific work
132        */
133       pnl = separate_path_and_file(jcr, attr->fname, attr->ofname);
134       if (pnl < 0) {
135          return CF_ERROR;
136       }
137
138       /*
139        * If path length is <= 0 we are making a file in the root
140        *  directory. Assume that the directory already exists.
141        */
142       if (pnl > 0) {
143          char savechr;
144          savechr = attr->ofname[pnl];
145          attr->ofname[pnl] = 0;                 /* terminate path */
146
147          if (!path_already_seen(attr->ofname, pnl)) {
148             Dmsg1(50, "Make path %s\n", attr->ofname);
149             /*
150              * If we need to make the directory, ensure that it is with
151              * execute bit set (i.e. parent_mode), and preserve what already
152              * exists. Normally, this should do nothing.
153              */
154             if (make_path(jcr, attr->ofname, parent_mode, parent_mode, uid, gid, 1, NULL) != 0) {
155                Dmsg1(10, "Could not make path. %s\n", attr->ofname);
156                attr->ofname[pnl] = savechr;     /* restore full name */
157                return CF_ERROR;
158             }
159          }
160          attr->ofname[pnl] = savechr;           /* restore full name */
161       }
162
163       /* Now we do the specific work for each file type */
164       switch(attr->type) {
165       case FT_REGE:
166       case FT_REG:
167          Dmsg1(100, "Create file %s\n", attr->ofname);
168          mode =  O_WRONLY | O_CREAT | O_TRUNC | O_BINARY; /*  O_NOFOLLOW; */
169          if (IS_CTG(attr->statp.st_mode)) {
170             mode |= O_CTG;               /* set contiguous bit if needed */
171          }
172          Dmsg1(50, "Create file: %s\n", attr->ofname);
173          if ((bopen(bfd, attr->ofname, mode, S_IRUSR | S_IWUSR)) < 0) {
174             Jmsg2(jcr, M_ERROR, 0, _("Could not create %s: %d ERR=%s\n"), 
175                   attr->ofname, berror(bfd));
176
177             return CF_ERROR;
178          }
179          return CF_EXTRACT;
180
181       case FT_RAW:                    /* Bacula raw device e.g. /dev/sda1 */
182       case FT_FIFO:                   /* Bacula fifo to save data */
183       case FT_SPEC:                      
184          if (S_ISFIFO(attr->statp.st_mode)) {
185             Dmsg1(200, "Restore fifo: %s\n", attr->ofname);
186             if (mkfifo(attr->ofname, attr->statp.st_mode) != 0 && errno != EEXIST) {
187                Jmsg2(jcr, M_ERROR, 0, _("Cannot make fifo %s: ERR=%s\n"), 
188                      attr->ofname, strerror(errno));
189                return CF_ERROR;
190             }
191          } else {          
192             Dmsg1(200, "Restore node: %s\n", attr->ofname);
193             if (mknod(attr->ofname, attr->statp.st_mode, attr->statp.st_rdev) != 0 && errno != EEXIST) {
194                Jmsg2(jcr, M_ERROR, 0, _("Cannot make node %s: ERR=%s\n"), 
195                      attr->ofname, strerror(errno));
196                return CF_ERROR;
197             }
198          }       
199          if (attr->type == FT_RAW || attr->type == FT_FIFO) {
200             btimer_id tid;
201             Dmsg1(200, "FT_RAW|FT_FIFO %s\n", attr->ofname);
202             mode =  O_WRONLY | O_BINARY;
203             /* Timeout open() in 60 seconds */
204             if (attr->type == FT_FIFO) {
205                tid = start_thread_timer(pthread_self(), 60);
206             } else {
207                tid = NULL;
208             }
209             if ((bopen(bfd, attr->ofname, mode, 0)) < 0) {
210                Jmsg2(jcr, M_ERROR, 0, _("Could not open %s: ERR=%s\n"), 
211                      attr->ofname, berror(bfd));
212                stop_thread_timer(tid);
213                return CF_ERROR;
214             }
215             stop_thread_timer(tid);
216             return CF_EXTRACT;
217          }
218          Dmsg1(200, "FT_SPEC %s\n", attr->ofname);
219          return CF_CREATED;
220
221       case FT_LNK:
222          Dmsg2(130, "FT_LNK should restore: %s -> %s\n", attr->ofname, attr->olname);
223          if (symlink(attr->olname, attr->ofname) != 0 && errno != EEXIST) {
224             Jmsg3(jcr, M_ERROR, 0, _("Could not symlink %s -> %s: ERR=%s\n"),
225                   attr->ofname, attr->olname, strerror(errno));
226             return CF_ERROR;
227          }
228          return CF_CREATED;
229
230       case FT_LNKSAVED:                  /* Hard linked, file already saved */
231          Dmsg2(130, "Hard link %s => %s\n", attr->ofname, attr->olname);
232          if (link(attr->olname, attr->ofname) != 0) {
233             Jmsg3(jcr, M_ERROR, 0, _("Could not hard link %s -> %s: ERR=%s\n"),
234                   attr->ofname, attr->olname, strerror(errno));
235             return CF_ERROR;
236          }
237          return CF_CREATED;
238
239       } /* End inner switch */
240
241    case FT_DIR:
242       Dmsg2(200, "Make dir mode=%o dir=%s\n", new_mode, attr->ofname);
243       if (make_path(jcr, attr->ofname, new_mode, parent_mode, uid, gid, 0, NULL) != 0) {
244          return CF_ERROR;
245       }
246       /*
247        * If we are using the Win32 Backup API, we open the
248        *   directory so that the security info will be read
249        *   and saved.
250        */
251       if (!is_portable_backup(bfd)) {
252          if ((bopen(bfd, attr->ofname, O_WRONLY|O_BINARY, 0)) < 0) {
253             Jmsg2(jcr, M_ERROR, 0, _("Could not open %s: ERR=%s\n"), 
254                   attr->ofname, berror(bfd));
255             return CF_ERROR;
256          }
257          return CF_EXTRACT;
258       } else {
259          return CF_CREATED;
260       }
261
262    /* The following should not occur */
263    case FT_NOACCESS:
264    case FT_NOFOLLOW:
265    case FT_NOSTAT:
266    case FT_DIRNOCHG:
267    case FT_NOCHG:
268    case FT_ISARCH:
269    case FT_NORECURSE:
270    case FT_NOFSCHG:
271    case FT_NOOPEN:
272       Jmsg2(jcr, M_ERROR, 0, _("Original file %s not saved: type=%d\n"), attr->fname, attr->type);
273       break;
274    default:
275       Jmsg2(jcr, M_ERROR, 0, _("Unknown file type %d; not restored: %s\n"), attr->type, attr->fname);
276       break;
277    }
278    return CF_ERROR;
279 }
280
281 /*
282  *  Returns: > 0 index into path where last path char is.
283  *           0  no path
284  *           -1 filename is zero length
285  */ 
286 static int separate_path_and_file(JCR *jcr, char *fname, char *ofile)
287 {
288    char *f, *p;
289    int fnl, pnl;
290
291    /* Separate pathname and filename */
292    for (p=f=ofile; *p; p++) {
293       if (*p == '/') {
294          f = p;                    /* possible filename */
295       }
296    }
297    if (*f == '/') {
298       f++;
299    }
300
301    fnl = p - f;
302    if (fnl == 0) {
303       /* The filename length must not be zero here because we
304        *  are dealing with a file (i.e. FT_REGE or FT_REG).
305        */
306       Jmsg1(jcr, M_ERROR, 0, _("Zero length filename: %s\n"), fname);
307       return -1;
308    }
309    pnl = f - ofile - 1;    
310    return pnl;
311 }
312
313 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
314
315 /* 
316  * Primitive caching of path to prevent recreating a pathname 
317  *   each time as long as we remain in the same directory.
318  */
319 static int path_already_seen(char *path, int pnl)
320 {
321    static int cached_pnl = 0;
322    static char cached_path[1000];
323
324    P(mutex);
325    if (cached_pnl == pnl && strcmp(path, cached_path) == 0) {
326       V(mutex);
327       return 1;
328    }
329    if (pnl < (int)(sizeof(cached_path)-1)) {
330       strcpy(cached_path, path);
331       cached_pnl = pnl;
332    }
333    V(mutex);
334    return 0;
335 }