]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/create_file.c
Update config + write own fgets() + ...
[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 *ofd, 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    binit(ofd);
74    /* Set desired writing mode (BackupWrite() or write()) */
75    switch (attr->data_stream) {
76    case 0:
77       break;                          /* use default defined by system */
78
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"));
85          return CF_ERROR;
86       }
87
88    /* All other stream use standard system I/O (portable) */
89    default:
90       set_win32_backup(ofd, 0);       /* Disable using BackupWrite() */
91       break;
92    }
93
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;
99
100    Dmsg2(400, "Replace=%c %d\n", (char)replace, replace);
101    if (lstat(attr->ofname, &mstatp) == 0) {
102       exists = true;
103       switch (replace) {
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);
107             return CF_SKIP;
108          }
109          break;
110
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);
114             return CF_SKIP;
115          }
116          break;
117
118       case REPLACE_NEVER:
119          Jmsg(jcr, M_SKIPPED, 0, _("File skipped. Already exists: %s\n"), attr->ofname);
120          return CF_SKIP;
121
122       case REPLACE_ALWAYS:
123          break;
124       }
125    }
126    switch (attr->type) {
127    case FT_LNKSAVED:                  /* Hard linked, file already saved */
128    case FT_LNK:
129    case FT_RAW:
130    case FT_FIFO:
131    case FT_SPEC:
132    case FT_REGE:                      /* empty file */
133    case FT_REG:                       /* regular file */
134       if (exists) {
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 */
140          }
141       }
142       /* 
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
147        */
148       pnl = separate_path_and_file(jcr, attr->fname, attr->ofname);
149       if (pnl < 0) {
150          return CF_ERROR;
151       }
152
153       /*
154        * If path length is <= 0 we are making a file in the root
155        *  directory. Assume that the directory already exists.
156        */
157       if (pnl > 0) {
158          char savechr;
159          savechr = attr->ofname[pnl];
160          attr->ofname[pnl] = 0;                 /* terminate path */
161
162          if (!path_already_seen(attr->ofname, pnl)) {
163             Dmsg1(50, "Make path %s\n", attr->ofname);
164             /*
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.
168              */
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);
171                return CF_ERROR;
172             }
173          }
174          attr->ofname[pnl] = savechr;           /* restore full name */
175       }
176
177       /* Now we do the specific work for each file type */
178       switch(attr->type) {
179       case FT_REGE:
180       case FT_REG:
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 */
185          }
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));
190             return CF_ERROR;
191          }
192          return CF_EXTRACT;
193
194       case FT_RAW:                    /* Bacula raw device e.g. /dev/sda1 */
195       case FT_FIFO:                   /* Bacula fifo to save data */
196       case FT_SPEC:                      
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));
202                return CF_ERROR;
203             }
204          } else {          
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));
209                return CF_ERROR;
210             }
211          }       
212          if (attr->type == FT_RAW || attr->type == FT_FIFO) {
213             btimer_id tid;
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);
219             } else {
220                tid = NULL;
221             }
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);
226                return CF_ERROR;
227             }
228             stop_thread_timer(tid);
229             return CF_EXTRACT;
230          }
231          Dmsg1(200, "FT_SPEC %s\n", attr->ofname);
232          return CF_CREATED;
233
234       case FT_LNK:
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));
239             return CF_ERROR;
240          }
241          return CF_CREATED;
242
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));
248             return CF_ERROR;
249          }
250          return CF_CREATED;
251
252       } /* End inner switch */
253
254    case FT_DIR:
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) {
257          return CF_ERROR;
258       }
259       /*
260        * If we are using the Win32 Backup API, we open the
261        *   directory so that the security info will be read
262        *   and saved.
263        */
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));
268             return CF_ERROR;
269          }
270          return CF_EXTRACT;
271       } else {
272          return CF_CREATED;
273       }
274
275    /* The following should not occur */
276    case FT_NOACCESS:
277    case FT_NOFOLLOW:
278    case FT_NOSTAT:
279    case FT_DIRNOCHG:
280    case FT_NOCHG:
281    case FT_ISARCH:
282    case FT_NORECURSE:
283    case FT_NOFSCHG:
284    case FT_NOOPEN:
285       Jmsg2(jcr, M_ERROR, 0, _("Original file %s not saved: type=%d\n"), attr->fname, attr->type);
286       break;
287    default:
288       Jmsg2(jcr, M_ERROR, 0, _("Unknown file type %d; not restored: %s\n"), attr->type, attr->fname);
289       break;
290    }
291    return CF_ERROR;
292 }
293
294 /*
295  *  Returns: > 0 index into path where last path char is.
296  *           0  no path
297  *           -1 filename is zero length
298  */ 
299 static int separate_path_and_file(JCR *jcr, char *fname, char *ofile)
300 {
301    char *f, *p;
302    int fnl, pnl;
303
304    /* Separate pathname and filename */
305    for (p=f=ofile; *p; p++) {
306       if (*p == '/') {
307          f = p;                    /* possible filename */
308       }
309    }
310    if (*f == '/') {
311       f++;
312    }
313
314    fnl = p - f;
315    if (fnl == 0) {
316       /* The filename length must not be zero here because we
317        *  are dealing with a file (i.e. FT_REGE or FT_REG).
318        */
319       Jmsg1(jcr, M_ERROR, 0, _("Zero length filename: %s\n"), fname);
320       return -1;
321    }
322    pnl = f - ofile - 1;    
323    return pnl;
324 }
325
326 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
327
328 /* 
329  * Primitive caching of path to prevent recreating a pathname 
330  *   each time as long as we remain in the same directory.
331  */
332 static int path_already_seen(char *path, int pnl)
333 {
334    static int cached_pnl = 0;
335    static char cached_path[1000];
336
337    P(mutex);
338    if (cached_pnl == pnl && strcmp(path, cached_path) == 0) {
339       V(mutex);
340       return 1;
341    }
342    if (pnl < (int)(sizeof(cached_path)-1)) {
343       strcpy(cached_path, path);
344       cached_pnl = pnl;
345    }
346    V(mutex);
347    return 0;
348 }