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