2 * Create a file, and reset the modes
4 * Kern Sibbald, November MM
10 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
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.
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.
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,
33 #define S_IRWXUGO (S_IRWXU | S_IRWXG | S_IRWXO)
43 * Create the file, or the directory
45 * fname is the original filename
46 * ofile is the output filename (may be in a different directory)
48 * Returns: CF_SKIP if file should be skipped
50 * CF_EXTRACT file created and data to restore
51 * CF_CREATED file created no data to restore
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
57 * So, we return with the file descriptor open for normal
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)
65 int new_mode, parent_mode, mode;
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;
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) {
83 if (lstat(ofile, &mstatp) == 0) {
86 if (statp->st_mtime <= mstatp.st_mtime) {
87 Jmsg(jcr, M_SKIPPED, 0, _("File skipped. Not newer: %s\n"), ofile);
92 if (statp->st_mtime >= mstatp.st_mtime) {
93 Jmsg(jcr, M_SKIPPED, 0, _("File skipped. Not older: %s\n"), ofile);
98 Jmsg(jcr, M_SKIPPED, 0, _("File skipped. Already exists: %s\n"), ofile);
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));
112 case FT_REGE: /* empty file */
113 case FT_REG: /* regular file */
114 /* Separate pathname and filename */
115 for (p=f=ofile; *p; p++) {
117 f = p; /* possible filename */
126 Jmsg1(jcr, M_ERROR, 0, _("Zero length filename: %s\n"), fname);
132 Jmsg1(jcr, M_ERROR, 0, _("Zero length path: %s\n"), fname);
135 savechr = ofile[pnl];
136 ofile[pnl] = 0; /* terminate path */
138 Dmsg1(50, "Make path %s\n", ofile);
140 * If we need to make the directory, ensure that it is with
141 * execute bit set (i.e. parent_mode), and preserve what already
142 * exists. Normally, this should do nothing.
144 stat = !make_path(jcr, ofile, parent_mode, parent_mode, uid, gid, 1, NULL);
146 Dmsg1(0, "Could not make path. %s\n", ofile);
150 ofile[pnl] = savechr; /* restore full name */
151 Dmsg1(100, "Create file %s\n", ofile);
152 mode = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY;
153 if (IS_CTG(statp->st_mode)) {
154 mode |= O_CTG; /* set contiguous bit if needed */
156 Dmsg1(50, "Create file: %s\n", ofile);
157 if ((*ofd = open(ofile, mode, S_IRUSR | S_IWUSR)) < 0) {
158 Jmsg2(jcr, M_ERROR, 0, _("Could not create %s: ERR=%s\n"), ofile, strerror(errno));
163 Dmsg2(130, "FT_LNK should restore: %s -> %s\n", ofile, lname);
164 if (symlink(lname, ofile) != 0 && errno != EEXIST) {
165 Jmsg3(jcr, M_ERROR, 0, _("Could not symlink %s -> %s: ERR=%s\n"),
166 ofile, lname, strerror(errno));
171 Dmsg2(300, "Make dir mode=%o dir=%s\n", new_mode, ofile);
172 if (make_path(jcr, ofile, new_mode, parent_mode, uid, gid, 0, NULL) != 0) {
173 Jmsg1(jcr, M_ERROR, 0, _("Could not make directory: %s\n"), ofile);
178 if (S_ISFIFO(statp->st_mode)) {
179 Dmsg1(200, "Restore fifo: %s\n", ofile);
180 if (mkfifo(ofile, statp->st_mode) != 0) {
181 Jmsg2(jcr, M_ERROR, 0, _("Cannot make fifo %s: ERR=%s\n"), ofile, strerror(errno));
185 Dmsg1(200, "Restore node: %s\n", ofile);
186 if (mknod(ofile, statp->st_mode, statp->st_rdev) != 0) {
187 Jmsg2(jcr, M_ERROR, 0, _("Cannot make node %s: ERR=%s\n"), ofile, strerror(errno));
191 Dmsg1(200, "FT_SPEC %s\n", ofile);
194 /* The following should not occur */
204 Jmsg2(jcr, M_ERROR, 0, _("Original file %s not saved. Stat=%d\n"), fname, type);
206 Jmsg2(jcr, M_ERROR, 0, _("Unknown file type %d; not restored: %s\n"), type, fname);