]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/create_file.c
Allow Nul Where on restore
[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, 2001, 2002 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:  1 on success
49  *           0 on failure
50  *
51  *   Note, we create the file here, except for special files,
52  *     we do not set the attributes because we want to first 
53  *     write the file, then when the writing is done, set the
54  *     attributes.
55  *   So, we return with the file descriptor open for normal 
56  *     files.
57  *
58  */
59 int create_file(void *jcr, char *fname, char *ofile, char *lname,
60                 int type, int stream, struct stat *statp, 
61                 char *attribsEx, int *ofd, int replace)
62 {
63    int new_mode, parent_mode, mode;
64    uid_t uid;
65    gid_t gid;
66    int stat = 0;
67    int fnl, pnl;
68    char *f, *p, savechr;
69
70    *ofd = -1;
71    new_mode = statp->st_mode;
72    Dmsg2(300, "newmode=%x file=%s\n", new_mode, ofile);
73    parent_mode = S_IWUSR | S_IXUSR | new_mode;
74    gid = statp->st_gid;
75    uid = statp->st_uid;
76
77    switch (type) {
78    case FT_LNKSAVED:                  /* Hard linked, file already saved */
79       Dmsg2(130, "Hard link %s => %s\n", ofile, lname);
80       if (link(lname, ofile) != 0) {
81          Jmsg3(jcr, M_ERROR, 0, _("Could not hard link %s ==> %s: ERR=%s\n"), 
82                ofile, lname, strerror(errno));
83       }
84       break;
85    case FT_REGE:                      /* empty file */
86    case FT_REG:                       /* regular file */
87       /* If not always replacing, do a stat and decide */
88       if (replace != REPLACE_ALWAYS) {
89          struct stat mstatp;
90          if (lstat(ofile, &mstatp) == 0) {
91             switch (replace) {
92             case REPLACE_IFNEWER:
93                if (statp->st_mtime < mstatp.st_mtime) {
94                   Jmsg1(jcr, M_INFO, 0, _("File %s skipped. Not newer.\n"), ofile);
95                   return 0;
96                }
97                break;
98             case REPLACE_IFOLDER:
99                if (statp->st_mtime > mstatp.st_mtime) {
100                   Jmsg1(jcr, M_INFO, 0, _("File %s skipped. Not older.\n"), ofile);
101                   return 0;
102                }
103                break;
104             case REPLACE_NEVER:
105                Jmsg1(jcr, M_INFO, 0, _("File %s skipped. Already exists.\n"), ofile);
106                return 0;
107             }
108          }
109       }
110       /* Separate pathname and filename */
111       for (p=f=ofile; *p; p++) {
112          if (*p == '/') {
113             f = p;                    /* possible filename */
114          }
115       }
116       if (*f == '/') {
117          f++;
118       }
119
120       fnl = p - f;
121       if (fnl == 0) {
122          Jmsg1(jcr, M_ERROR, 0, _("Zero length filename: %s\n"), fname);
123          return 0;
124       }
125
126       pnl = f - ofile - 1;    
127       if (pnl <= 0) {
128          Jmsg1(jcr, M_ERROR, 0, _("Zero length path: %s\n"), fname);
129          return 0;
130       }
131       savechr = ofile[pnl];
132       ofile[pnl] = 0;                 /* terminate path */
133
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          return 0;
144       }
145       
146       ofile[pnl] = savechr;           /* restore full name */
147       Dmsg1(100, "Create file %s\n", ofile);
148       mode =  O_WRONLY | O_CREAT | O_TRUNC | O_BINARY;
149       if (IS_CTG(statp->st_mode)) {
150          mode |= O_CTG;               /* set contiguous bit if needed */
151       }
152       Dmsg1(50, "Create file: %s\n", ofile);
153       if ((*ofd = open(ofile, mode, S_IRUSR | S_IWUSR)) < 0) {
154          Jmsg2(jcr, M_ERROR, 0, _("Could not create %s: ERR=%s\n"), ofile, strerror(errno));
155          return 0;
156       }
157       return 1;
158    case FT_LNK:
159       Dmsg2(130, "FT_LNK should restore: %s -> %s\n", ofile, lname);
160       if (symlink(lname, ofile) != 0 && errno != EEXIST) {
161          Jmsg3(jcr, M_ERROR, 0, _("Could not symlink %s -> %s: ERR=%s\n"), 
162             ofile, lname, strerror(errno));
163       }
164       return 0;
165    case FT_DIR:
166       Dmsg2(300, "Make dir mode=%o dir=%s\n", new_mode, ofile);
167       if (make_path(jcr, ofile, new_mode, parent_mode, uid, gid, 0, NULL) != 0) {
168          Jmsg1(jcr, M_ERROR, 0, _("Could not make directory: %s\n"), ofile);
169       }
170       return 0;
171    case FT_SPEC:
172       if (S_ISFIFO(statp->st_mode)) {
173          Dmsg1(200, "Restore fifo: %s\n", ofile);
174          if (mkfifo(ofile, statp->st_mode) != 0) {
175             Jmsg2(jcr, M_ERROR, 0, _("Cannot make fifo %s: ERR=%s\n"), ofile, strerror(errno));
176             return 0;
177          }
178       } else {          
179          Dmsg1(200, "Restore node: %s\n", ofile);
180          if (mknod(ofile, statp->st_mode, statp->st_rdev) != 0) {
181             Jmsg2(jcr, M_ERROR, 0, _("Cannot make node %s: ERR=%s\n"), ofile, strerror(errno));
182             return 0;
183          }
184       }       
185       Dmsg1(200, "FT_SPEC %s\n", ofile);
186       return 0;
187
188    /* The following should not occur */
189    case FT_NOACCESS:
190    case FT_NOFOLLOW:
191    case FT_NOSTAT:
192    case FT_DIRNOCHG:
193    case FT_NOCHG:
194    case FT_ISARCH:
195    case FT_NORECURSE:
196    case FT_NOFSCHG:
197    case FT_NOOPEN:
198       Jmsg2(jcr, M_ERROR, 0, _("Original file %s not saved. Stat=%d\n"), fname, type);
199       return 0;
200    default:
201       Jmsg2(jcr, M_ERROR, 0, _("Unknown file type %d; not restored: %s\n"), type, fname);
202       return 0;
203    }
204
205    return 0;
206 }