]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/create_file.c
New query.sql, JobMedia VolIndex, add Release command
[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, char *fname, char *ofile, char *lname,
65                 int type, int stream, struct stat *statp, 
66                 char *attribsEx, BFILE *ofd, int replace)
67 {
68    int new_mode, parent_mode, mode;
69    uid_t uid;
70    gid_t gid;
71    int pnl;
72
73    binit(ofd);
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;
77    gid = statp->st_gid;
78    uid = statp->st_uid;
79
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) {
83       struct stat mstatp;
84       if (lstat(ofile, &mstatp) == 0) {
85          switch (replace) {
86          case REPLACE_IFNEWER:
87             if (statp->st_mtime <= mstatp.st_mtime) {
88                Jmsg(jcr, M_SKIPPED, 0, _("File skipped. Not newer: %s\n"), ofile);
89                return CF_SKIP;
90             }
91             break;
92          case REPLACE_IFOLDER:
93             if (statp->st_mtime >= mstatp.st_mtime) {
94                Jmsg(jcr, M_SKIPPED, 0, _("File skipped. Not older: %s\n"), ofile);
95                return CF_SKIP;
96             }
97             break;
98          case REPLACE_NEVER:
99             Jmsg(jcr, M_SKIPPED, 0, _("File skipped. Already exists: %s\n"), ofile);
100             return CF_SKIP;
101          }
102       }
103    }
104    switch (type) {
105    case FT_LNKSAVED:                  /* Hard linked, file already saved */
106    case FT_LNK:
107    case FT_RAW:
108    case FT_FIFO:
109    case FT_SPEC:
110    case FT_REGE:                      /* empty file */
111    case FT_REG:                       /* regular file */
112       /* 
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
117        */
118       pnl = separate_path_and_file(jcr, fname, ofile);
119       if (pnl < 0) {
120          return CF_ERROR;
121       }
122
123       /*
124        * If path length is <= 0 we are making a file in the root
125        *  directory. Assume that the directory already exists.
126        */
127       if (pnl > 0) {
128          char savechr;
129          savechr = ofile[pnl];
130          ofile[pnl] = 0;                 /* terminate path */
131
132          if (!path_already_seen(ofile, pnl)) {
133             Dmsg1(50, "Make path %s\n", ofile);
134             /*
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.
138              */
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);
141                return CF_ERROR;
142             }
143          }
144          ofile[pnl] = savechr;           /* restore full name */
145       }
146
147       /* Now we do the specific work for each file type */
148       switch(type) {
149       case FT_REGE:
150       case FT_REG:
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 */
155          }
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"), 
159                   ofile, berror(ofd));
160             return CF_ERROR;
161          }
162          return CF_EXTRACT;
163
164       case FT_RAW:                    /* Bacula raw device e.g. /dev/sda1 */
165       case FT_FIFO:                   /* Bacula fifo to save data */
166       case FT_SPEC:                      
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));
172                return CF_ERROR;
173             }
174          } else {          
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));
179                return CF_ERROR;
180             }
181          }       
182          if (type == FT_RAW || type == FT_FIFO) {
183             btimer_id tid;
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);
189             } else {
190                tid = NULL;
191             }
192             if ((bopen(ofd, ofile, mode, 0)) < 0) {
193                Jmsg2(jcr, M_ERROR, 0, _("Could not open %s: ERR=%s\n"), 
194                      ofile, berror(ofd));
195                stop_thread_timer(tid);
196                return CF_ERROR;
197             }
198             stop_thread_timer(tid);
199             return CF_EXTRACT;
200          }
201          Dmsg1(200, "FT_SPEC %s\n", ofile);
202          return CF_CREATED;
203
204       case FT_LNK:
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));
209             return CF_ERROR;
210          }
211          return CF_CREATED;
212
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));
218             return CF_ERROR;
219          }
220          return CF_CREATED;
221
222       } /* End inner switch */
223
224    case FT_DIR:
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) {
227          return CF_ERROR;
228       }
229       /*
230        * If we are using the Win32 Backup API, we open the
231        *   directory so that the security info will be read
232        *   and saved.
233        */
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"), 
237                   ofile, berror(ofd));
238             return CF_ERROR;
239          }
240          return CF_EXTRACT;
241       } else {
242          return CF_CREATED;
243       }
244
245    /* The following should not occur */
246    case FT_NOACCESS:
247    case FT_NOFOLLOW:
248    case FT_NOSTAT:
249    case FT_DIRNOCHG:
250    case FT_NOCHG:
251    case FT_ISARCH:
252    case FT_NORECURSE:
253    case FT_NOFSCHG:
254    case FT_NOOPEN:
255       Jmsg2(jcr, M_ERROR, 0, _("Original file %s not saved: type=%d\n"), fname, type);
256    default:
257       Jmsg2(jcr, M_ERROR, 0, _("Unknown file type %d; not restored: %s\n"), type, fname);
258    }
259    return CF_ERROR;
260 }
261
262 /*
263  *  Returns: > 0 index into path where last path char is.
264  *           0  no path
265  *           -1 filename is zero length
266  */ 
267 static int separate_path_and_file(JCR *jcr, char *fname, char *ofile)
268 {
269    char *f, *p;
270    int fnl, pnl;
271
272    /* Separate pathname and filename */
273    for (p=f=ofile; *p; p++) {
274       if (*p == '/') {
275          f = p;                    /* possible filename */
276       }
277    }
278    if (*f == '/') {
279       f++;
280    }
281
282    fnl = p - f;
283    if (fnl == 0) {
284       /* The filename length must not be zero here because we
285        *  are dealing with a file (i.e. FT_REGE or FT_REG).
286        */
287       Jmsg1(jcr, M_ERROR, 0, _("Zero length filename: %s\n"), fname);
288       return -1;
289    }
290    pnl = f - ofile - 1;    
291    return pnl;
292 }
293
294 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
295
296 /* 
297  * Primitive caching of path to prevent recreating a pathname 
298  *   each time as long as we remain in the same directory.
299  */
300 static int path_already_seen(char *path, int pnl)
301 {
302    static int cached_pnl = 0;
303    static char cached_path[1000];
304
305    P(mutex);
306    if (cached_pnl == pnl && strcmp(path, cached_path) == 0) {
307       V(mutex);
308       return 1;
309    }
310    if (pnl < (int)(sizeof(cached_path)-1)) {
311       strcpy(cached_path, path);
312       cached_pnl = pnl;
313    }
314    V(mutex);
315    return 0;
316 }