]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/create_file.c
Misc -- see kes-1.30
[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
42 /*
43  * Create the file, or the directory
44  *
45  *  fname is the original filename  
46  *  ofile is the output filename (may be in a different directory)
47  *
48  * Returns:  CF_SKIP     if file should be skipped
49  *           CF_ERROR    on error
50  *           CF_EXTRACT  file created and data to restore  
51  *           CF_CREATED  file created no data to restore
52  *
53  *   Note, we create the file here, except for special files,
54  *     we do not set the attributes because we want to first 
55  *     write the file, then when the writing is done, set the
56  *     attributes.
57  *   So, we return with the file descriptor open for normal 
58  *     files.
59  *
60  */
61 int create_file(void *jcr, char *fname, char *ofile, char *lname,
62                 int type, int stream, struct stat *statp, 
63                 char *attribsEx, int *ofd, int replace)
64 {
65    int new_mode, parent_mode, mode;
66    uid_t uid;
67    gid_t gid;
68    int stat = 0;
69    int fnl, pnl;
70    char *f, *p, savechr;
71
72    *ofd = -1;
73    new_mode = statp->st_mode;
74    Dmsg2(300, "newmode=%x file=%s\n", new_mode, ofile);
75    parent_mode = S_IWUSR | S_IXUSR | new_mode;
76    gid = statp->st_gid;
77    uid = statp->st_uid;
78
79    Dmsg2(400, "Replace=%c %d\n", (char)replace, replace);
80    /* If not always replacing, do a stat and decide */
81    if (replace != REPLACE_ALWAYS) {
82       struct stat mstatp;
83       if (lstat(ofile, &mstatp) == 0) {
84          switch (replace) {
85          case REPLACE_IFNEWER:
86             if (statp->st_mtime <= mstatp.st_mtime) {
87                Jmsg(jcr, M_SKIPPED, 0, _("File skipped. Not newer: %s\n"), ofile);
88                return CF_SKIP;
89             }
90             break;
91          case REPLACE_IFOLDER:
92             if (statp->st_mtime >= mstatp.st_mtime) {
93                Jmsg(jcr, M_SKIPPED, 0, _("File skipped. Not older: %s\n"), ofile);
94                return CF_SKIP;
95             }
96             break;
97          case REPLACE_NEVER:
98             Jmsg(jcr, M_SKIPPED, 0, _("File skipped. Already exists: %s\n"), ofile);
99             return CF_SKIP;
100          }
101       }
102    }
103    switch (type) {
104    case FT_LNKSAVED:                  /* Hard linked, file already saved */
105       Dmsg2(130, "Hard link %s => %s\n", ofile, lname);
106       if (link(lname, ofile) != 0) {
107          Jmsg3(jcr, M_ERROR, 0, _("Could not hard link %s ==> %s: ERR=%s\n"), 
108                ofile, lname, strerror(errno));
109          return CF_ERROR;
110       }
111       break;
112    case FT_REGE:                      /* empty file */
113    case FT_REG:                       /* regular file */
114       /* Separate pathname and filename */
115       for (p=f=ofile; *p; p++) {
116          if (*p == '/') {
117             f = p;                    /* possible filename */
118          }
119       }
120       if (*f == '/') {
121          f++;
122       }
123
124       fnl = p - f;
125       if (fnl == 0) {
126          /* The filename length must not be zero here because we
127           *  are dealing with a file (i.e. FT_REGE or FT_REG).
128           */
129          Jmsg1(jcr, M_ERROR, 0, _("Zero length filename: %s\n"), fname);
130          return CF_ERROR;
131       }
132
133       pnl = f - ofile - 1;    
134
135       /*
136        * If path length is <= 0 we are making a file in the root
137        *  directory. Assume that the directory already exists.
138        */
139       if (pnl > 0) {
140          savechr = ofile[pnl];
141          ofile[pnl] = 0;                 /* terminate path */
142
143          Dmsg1(50, "Make path %s\n", ofile);
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          stat = !make_path(jcr, ofile, parent_mode, parent_mode, uid, gid, 1, NULL);
150          if (stat == 0) {
151             Dmsg1(0, "Could not make path. %s\n", ofile);
152             return CF_ERROR;
153          }
154       
155          ofile[pnl] = savechr;           /* restore full name */
156       }
157
158       Dmsg1(100, "Create file %s\n", ofile);
159       mode =  O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_NOFOLLOW;
160       if (IS_CTG(statp->st_mode)) {
161          mode |= O_CTG;               /* set contiguous bit if needed */
162       }
163       Dmsg1(50, "Create file: %s\n", ofile);
164       if ((*ofd = open(ofile, mode, S_IRUSR | S_IWUSR)) < 0) {
165          Jmsg2(jcr, M_ERROR, 0, _("Could not create %s: ERR=%s\n"), ofile, strerror(errno));
166          return CF_ERROR;
167       }
168       return CF_EXTRACT;
169    case FT_LNK:
170       Dmsg2(130, "FT_LNK should restore: %s -> %s\n", ofile, lname);
171       if (symlink(lname, ofile) != 0 && errno != EEXIST) {
172          Jmsg3(jcr, M_ERROR, 0, _("Could not symlink %s -> %s: ERR=%s\n"), 
173             ofile, lname, strerror(errno));
174          return CF_ERROR;
175       }
176       return CF_CREATED;
177    case FT_DIR:
178       Dmsg2(300, "Make dir mode=%o dir=%s\n", new_mode, ofile);
179       if (make_path(jcr, ofile, new_mode, parent_mode, uid, gid, 0, NULL) != 0) {
180          Jmsg1(jcr, M_ERROR, 0, _("Could not make directory: %s\n"), ofile);
181          return CF_ERROR;
182       }
183       return CF_CREATED;
184    case FT_RAW:
185    case FT_FIFO:
186    case FT_SPEC:
187       if (S_ISFIFO(statp->st_mode)) {
188          Dmsg1(200, "Restore fifo: %s\n", ofile);
189          if (mkfifo(ofile, statp->st_mode) != 0 && errno != EEXIST) {
190             Jmsg2(jcr, M_ERROR, 0, _("Cannot make fifo %s: ERR=%s\n"), ofile, strerror(errno));
191             return CF_ERROR;
192          }
193       } else {          
194          Dmsg1(200, "Restore node: %s\n", ofile);
195          if (mknod(ofile, statp->st_mode, statp->st_rdev) != 0 && errno != EEXIST) {
196             Jmsg2(jcr, M_ERROR, 0, _("Cannot make node %s: ERR=%s\n"), ofile, strerror(errno));
197             return CF_ERROR;
198          }
199       }       
200       if (type == FT_RAW || type == FT_FIFO) {
201          btimer_id tid;
202          Dmsg1(200, "FT_RAW|FT_FIFO %s\n", ofile);
203          mode =  O_WRONLY | O_BINARY;
204          /* Timeout open() in 60 seconds */
205          if (type == FT_FIFO) {
206             tid = start_thread_timer(pthread_self(), 60);
207          } else {
208             tid = NULL;
209          }
210          if ((*ofd = open(ofile, mode)) < 0) {
211             Jmsg2(jcr, M_ERROR, 0, _("Could not open %s: ERR=%s\n"), ofile, strerror(errno));
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", ofile);
219       return CF_CREATED;
220
221    /* The following should not occur */
222    case FT_NOACCESS:
223    case FT_NOFOLLOW:
224    case FT_NOSTAT:
225    case FT_DIRNOCHG:
226    case FT_NOCHG:
227    case FT_ISARCH:
228    case FT_NORECURSE:
229    case FT_NOFSCHG:
230    case FT_NOOPEN:
231       Jmsg2(jcr, M_ERROR, 0, _("Original file %s not saved. Stat=%d\n"), fname, type);
232    default:
233       Jmsg2(jcr, M_ERROR, 0, _("Unknown file type %d; not restored: %s\n"), type, fname);
234    }
235    return CF_ERROR;
236 }