- Dmsg1(100, "Create file %s\n", ofile);
- mode = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY; /* O_NOFOLLOW; */
- if (IS_CTG(statp->st_mode)) {
- mode |= O_CTG; /* set contiguous bit if needed */
- }
- Dmsg1(50, "Create file: %s\n", ofile);
- if ((bopen(ofd, ofile, mode, S_IRUSR | S_IWUSR)) < 0) {
- Jmsg2(jcr, M_ERROR, 0, _("Could not create %s: ERR=%s\n"), ofile, strerror(errno));
- return CF_ERROR;
- }
- return CF_EXTRACT;
-
- case FT_RAW: /* Bacula raw device e.g. /dev/sda1 */
- case FT_FIFO: /* Bacula fifo to save data */
- case FT_SPEC:
- if (S_ISFIFO(statp->st_mode)) {
- Dmsg1(200, "Restore fifo: %s\n", ofile);
- if (mkfifo(ofile, statp->st_mode) != 0 && errno != EEXIST) {
- Jmsg2(jcr, M_ERROR, 0, _("Cannot make fifo %s: ERR=%s\n"), ofile, strerror(errno));
- return CF_ERROR;
- }
- } else {
- Dmsg1(200, "Restore node: %s\n", ofile);
- if (mknod(ofile, statp->st_mode, statp->st_rdev) != 0 && errno != EEXIST) {
- Jmsg2(jcr, M_ERROR, 0, _("Cannot make node %s: ERR=%s\n"), ofile, strerror(errno));
- return CF_ERROR;
- }
- }
- if (type == FT_RAW || type == FT_FIFO) {
- btimer_id tid;
- Dmsg1(200, "FT_RAW|FT_FIFO %s\n", ofile);
- mode = O_WRONLY | O_BINARY;
- /* Timeout open() in 60 seconds */
- if (type == FT_FIFO) {
- tid = start_thread_timer(pthread_self(), 60);
- } else {
- tid = NULL;
- }
- if ((bopen(ofd, ofile, mode, 0)) < 0) {
- Jmsg2(jcr, M_ERROR, 0, _("Could not open %s: ERR=%s\n"), ofile, strerror(errno));
- stop_thread_timer(tid);
- return CF_ERROR;
- }
- stop_thread_timer(tid);
- return CF_EXTRACT;
- }
- Dmsg1(200, "FT_SPEC %s\n", ofile);
- return CF_CREATED;
+ Dmsg1(100, "Create file %s\n", attr->ofname);
+ mode = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY; /* O_NOFOLLOW; */
+ if (IS_CTG(attr->statp.st_mode)) {
+ mode |= O_CTG; /* set contiguous bit if needed */
+ }
+ Dmsg1(50, "Create file: %s\n", attr->ofname);
+ if (is_bopen(bfd)) {
+ Jmsg1(jcr, M_ERROR, 0, _("bpkt already open fid=%d\n"), bfd->fid);
+ bclose(bfd);
+ }
+ /*
+ * If the open fails, we attempt to cd into the directory
+ * and create the file with a relative path rather than
+ * the full absolute path. This is for Win32 where
+ * path names may be too long to create.
+ */
+ if ((bopen(bfd, attr->ofname, mode, S_IRUSR | S_IWUSR)) < 0) {
+ berrno be;
+ int stat;
+ Dmsg2(000, "bopen failed errno=%d: ERR=%s\n", bfd->berrno,
+ be.strerror(bfd->berrno));
+ if (strlen(attr->ofname) > 250) { /* Microsoft limitation */
+ char savechr;
+ char *p, *e;
+ struct saved_cwd cwd;
+ savechr = attr->ofname[pnl];
+ attr->ofname[pnl] = 0; /* terminate path */
+ Dmsg1(000, "Do chdir %s\n", attr->ofname);
+ if (save_cwd(&cwd) != 0) {
+ Jmsg0(jcr, M_ERROR, 0, _("Could not save_dirn"));
+ attr->ofname[pnl] = savechr;
+ return CF_ERROR;
+ }
+ p = attr->ofname;
+ while ((e = strchr(p, '/'))) {
+ *e = 0;
+ if (chdir(p) < 0) {
+ berrno be;
+ Jmsg2(jcr, M_ERROR, 0, _("Could not chdir to %s: ERR=%s\n"),
+ attr->ofname, be.strerror());
+ restore_cwd(&cwd, NULL, NULL);
+ free_cwd(&cwd);
+ attr->ofname[pnl] = savechr;
+ *e = '/';
+ return CF_ERROR;
+ }
+ *e = '/';
+ p = e + 1;
+ }
+ if (chdir(p) < 0) {
+ berrno be;
+ Jmsg2(jcr, M_ERROR, 0, _("Could not chdir to %s: ERR=%s\n"),
+ attr->ofname, be.strerror());
+ restore_cwd(&cwd, NULL, NULL);
+ free_cwd(&cwd);
+ attr->ofname[pnl] = savechr;
+ return CF_ERROR;
+ }
+ attr->ofname[pnl] = savechr;
+ Dmsg1(000, "Do open %s\n", &attr->ofname[pnl+1]);
+ if ((bopen(bfd, &attr->ofname[pnl+1], mode, S_IRUSR | S_IWUSR)) < 0) {
+ stat = CF_ERROR;
+ } else {
+ stat = CF_EXTRACT;
+ }
+ restore_cwd(&cwd, NULL, NULL);
+ free_cwd(&cwd);
+ if (stat == CF_EXTRACT) {
+ return CF_EXTRACT;
+ }
+ }
+ Jmsg2(jcr, M_ERROR, 0, _("Could not create %s: ERR=%s\n"),
+ attr->ofname, be.strerror(bfd->berrno));
+ return CF_ERROR;
+ }
+ return CF_EXTRACT;
+#ifndef HAVE_WIN32 // none of these exists on MS Windows
+ case FT_RAW: /* Bacula raw device e.g. /dev/sda1 */
+ case FT_FIFO: /* Bacula fifo to save data */
+ case FT_SPEC:
+ if (S_ISFIFO(attr->statp.st_mode)) {
+ Dmsg1(200, "Restore fifo: %s\n", attr->ofname);
+ if (mkfifo(attr->ofname, attr->statp.st_mode) != 0 && errno != EEXIST) {
+ berrno be;
+ Jmsg2(jcr, M_ERROR, 0, _("Cannot make fifo %s: ERR=%s\n"),
+ attr->ofname, be.strerror());
+ return CF_ERROR;
+ }
+ } else {
+ Dmsg1(200, "Restore node: %s\n", attr->ofname);
+ if (mknod(attr->ofname, attr->statp.st_mode, attr->statp.st_rdev) != 0 && errno != EEXIST) {
+ berrno be;
+ Jmsg2(jcr, M_ERROR, 0, _("Cannot make node %s: ERR=%s\n"),
+ attr->ofname, be.strerror());
+ return CF_ERROR;
+ }
+ }
+ if (attr->type == FT_RAW || attr->type == FT_FIFO) {
+ btimer_t *tid;
+ Dmsg1(200, "FT_RAW|FT_FIFO %s\n", attr->ofname);
+ mode = O_WRONLY | O_BINARY;
+ /* Timeout open() in 60 seconds */
+ if (attr->type == FT_FIFO) {
+ tid = start_thread_timer(pthread_self(), 60);
+ } else {
+ tid = NULL;
+ }
+ if (is_bopen(bfd)) {
+ Jmsg1(jcr, M_ERROR, 0, _("bpkt already open fid=%d\n"), bfd->fid);
+ }
+ if ((bopen(bfd, attr->ofname, mode, 0)) < 0) {
+ berrno be;
+ be.set_errno(bfd->berrno);
+ Jmsg2(jcr, M_ERROR, 0, _("Could not open %s: ERR=%s\n"),
+ attr->ofname, be.strerror());
+ stop_thread_timer(tid);
+ return CF_ERROR;
+ }
+ stop_thread_timer(tid);
+ return CF_EXTRACT;
+ }
+ Dmsg1(200, "FT_SPEC %s\n", attr->ofname);
+ return CF_CREATED;