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