From: Kern Sibbald Date: Tue, 27 Jul 2004 21:00:34 +0000 (+0000) Subject: First cut berrno unified Unix/Win32 errno handling X-Git-Tag: Release-1.35.1~41 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=2b34c8473bab755f8b319b1c289569a13ea6283c;p=bacula%2Fbacula First cut berrno unified Unix/Win32 errno handling git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@1493 91ce42f0-d328-0410-95d8-f526ca767f89 --- diff --git a/bacula/src/dird/Makefile.in b/bacula/src/dird/Makefile.in index 005c756198..1ee51c39ba 100644 --- a/bacula/src/dird/Makefile.in +++ b/bacula/src/dird/Makefile.in @@ -66,7 +66,7 @@ all: Makefile bacula-dir @STATIC_DIR@ bacula-dir: $(SVROBJS) ../lib/libbac.a ../cats/libsql.a ../findlib/libfind.a $(CXX) $(WLDFLAGS) $(LDFLAGS) -L../lib -L../cats -L../findlib -o $@ $(SVROBJS) \ - -lsql -lbac -lfind -lm $(DLIB) $(DB_LIBS) $(LIBS) + -lsql -lfind -lbac -lm $(DLIB) $(DB_LIBS) $(LIBS) static-bacula-dir: $(SVROBJS) ../lib/libbac.a ../cats/libsql.a ../findlib/libfind.a $(CXX) $(WLDFLAGS) $(LDFLAGS) -static -L../lib -L../cats -L../findlib -o $@ $(SVROBJS) \ diff --git a/bacula/src/filed/backup.c b/bacula/src/filed/backup.c index aa9d4a1916..b558480f29 100644 --- a/bacula/src/filed/backup.c +++ b/bacula/src/filed/backup.c @@ -173,21 +173,30 @@ static int save_file(FF_PKT *ff_pkt, void *vjcr) case FT_FIFO: Dmsg1(130, "FT_FIFO saving: %s\n", ff_pkt->fname); break; - case FT_NOACCESS: + case FT_NOACCESS: { + berrno be; + be.set_errno(ff_pkt->ff_errno); Jmsg(jcr, M_NOTSAVED, -1, _(" Could not access %s: ERR=%s\n"), ff_pkt->fname, - strerror(ff_pkt->ff_errno)); + be.strerror()); jcr->Errors++; return 1; - case FT_NOFOLLOW: + } + case FT_NOFOLLOW: { + berrno be; + be.set_errno(ff_pkt->ff_errno); Jmsg(jcr, M_NOTSAVED, -1, _(" Could not follow link %s: ERR=%s\n"), ff_pkt->fname, - strerror(ff_pkt->ff_errno)); + be.strerror()); jcr->Errors++; return 1; - case FT_NOSTAT: + } + case FT_NOSTAT: { + berrno be; + be.set_errno(ff_pkt->ff_errno); Jmsg(jcr, M_NOTSAVED, -1, _(" Could not stat %s: ERR=%s\n"), ff_pkt->fname, - strerror(ff_pkt->ff_errno)); + be.strerror()); jcr->Errors++; return 1; + } case FT_DIRNOCHG: case FT_NOCHG: Jmsg(jcr, M_SKIPPED, -1, _(" Unchanged file skipped: %s\n"), ff_pkt->fname); @@ -203,11 +212,14 @@ static int save_file(FF_PKT *ff_pkt, void *vjcr) Jmsg(jcr, M_SKIPPED, -1, _(" File system change prohibited. Directory skipped. %s\n"), ff_pkt->fname); return 1; - case FT_NOOPEN: + case FT_NOOPEN: { + berrno be; + be.set_errno(ff_pkt->ff_errno); Jmsg(jcr, M_NOTSAVED, -1, _(" Could not open directory %s: ERR=%s\n"), ff_pkt->fname, - strerror(ff_pkt->ff_errno)); + be.strerror()); jcr->Errors++; return 1; + } default: Jmsg(jcr, M_NOTSAVED, 0, _(" Unknown file type %d; not saved: %s\n"), ff_pkt->type, ff_pkt->fname); jcr->Errors++; @@ -236,8 +248,9 @@ static int save_file(FF_PKT *ff_pkt, void *vjcr) } if (bopen(&ff_pkt->bfd, ff_pkt->fname, O_RDONLY | O_BINARY, 0) < 0) { ff_pkt->ff_errno = errno; + berrno be; Jmsg(jcr, M_NOTSAVED, -1, _(" Cannot open %s: ERR=%s.\n"), ff_pkt->fname, - berror(&ff_pkt->bfd)); + be.strerror()); jcr->Errors++; if (tid) { stop_thread_timer(tid); @@ -464,8 +477,10 @@ static int save_file(FF_PKT *ff_pkt, void *vjcr) if (sd->msglen < 0) { + berrno be; + be.set_errno(ff_pkt->bfd.berrno); Jmsg(jcr, M_ERROR, 0, _("Read error on file %s. ERR=%s\n"), - ff_pkt->fname, berror(&ff_pkt->bfd)); + ff_pkt->fname, be.strerror()); } bclose(&ff_pkt->bfd); /* close file */ diff --git a/bacula/src/filed/job.c b/bacula/src/filed/job.c index 591a1d6ac2..f75a4c3226 100644 --- a/bacula/src/filed/job.c +++ b/bacula/src/filed/job.c @@ -523,8 +523,9 @@ static void add_fname_to_list(JCR *jcr, char *fname, int list) case '<': p++; /* skip over < */ if ((ffd = fopen(p, "r")) == NULL) { + berrno be; Jmsg(jcr, M_FATAL, 0, _("Cannot open %s file: %s. ERR=%s\n"), - list==INC_LIST?"included":"excluded", p, strerror(errno)); + list==INC_LIST?"included":"excluded", p, be.strerror()); return; } /* Copy File options */ @@ -654,8 +655,9 @@ static void add_file_to_fileset(JCR *jcr, const char *fname, findFILESET *filese case '<': p++; /* skip over < */ if ((ffd = fopen(p, "r")) == NULL) { + berrno be; Jmsg(jcr, M_FATAL, 0, _("Cannot open FileSet input file: %s. ERR=%s\n"), - p, strerror(errno)); + p, be.strerror()); return; } while (fgets(buf, sizeof(buf), ffd)) { @@ -932,6 +934,7 @@ static int bootstrap_cmd(JCR *jcr) jcr->RestoreBootstrap = fname; bs = fopen(fname, "a+"); /* create file */ if (!bs) { + berrno be; /* * Suck up what he is sending to us so that he will then * read our error message. @@ -940,7 +943,7 @@ static int bootstrap_cmd(JCR *jcr) { } Jmsg(jcr, M_FATAL, 0, _("Could not create bootstrap file %s: ERR=%s\n"), - jcr->RestoreBootstrap, strerror(errno)); + jcr->RestoreBootstrap, be.strerror()); free_pool_memory(jcr->RestoreBootstrap); jcr->RestoreBootstrap = NULL; set_jcr_job_status(jcr, JS_ErrorTerminated); @@ -1548,8 +1551,9 @@ static int send_bootstrap_file(JCR *jcr) } bs = fopen(jcr->RestoreBootstrap, "r"); if (!bs) { + berrno be; Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"), - jcr->RestoreBootstrap, strerror(errno)); + jcr->RestoreBootstrap, be.strerror()); set_jcr_job_status(jcr, JS_ErrorTerminated); goto bail_out; } diff --git a/bacula/src/filed/restore.c b/bacula/src/filed/restore.c index af7e6bdfdd..405c37b97e 100644 --- a/bacula/src/filed/restore.c +++ b/bacula/src/filed/restore.c @@ -224,8 +224,10 @@ void do_restore(JCR *jcr) if (fileAddr != faddr) { fileAddr = faddr; if (blseek(&bfd, (off_t)fileAddr, SEEK_SET) < 0) { + berrno be; + be.set_errno(bfd.berrno); Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"), - edit_uint64(fileAddr, ec1), attr->ofname, berror(&bfd)); + edit_uint64(fileAddr, ec1), attr->ofname, be.strerror()); extract = false; bclose(&bfd); continue; @@ -238,7 +240,10 @@ void do_restore(JCR *jcr) Dmsg2(30, "Write %u bytes, total before write=%u\n", wsize, total); if ((uint32_t)bwrite(&bfd, wbuf, wsize) != wsize) { Dmsg0(0, "===Write error===\n"); - Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: ERR=%s\n"), attr->ofname, berror(&bfd)); + berrno be; + be.set_errno(bfd.berrno); + Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: ERR=%s\n"), attr->ofname, + be.strerror()); extract = false; bclose(&bfd); continue; @@ -270,8 +275,10 @@ void do_restore(JCR *jcr) if (fileAddr != faddr) { fileAddr = faddr; if (blseek(&bfd, (off_t)fileAddr, SEEK_SET) < 0) { + berrno be; + be.set_errno(bfd.berrno); Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"), - edit_uint64(fileAddr, ec1), attr->ofname, berror(&bfd)); + edit_uint64(fileAddr, ec1), attr->ofname, be.strerror()); extract = false; bclose(&bfd); continue; @@ -295,7 +302,9 @@ void do_restore(JCR *jcr) Dmsg2(100, "Write uncompressed %d bytes, total before write=%d\n", compress_len, total); if ((uLong)bwrite(&bfd, jcr->compress_buf, compress_len) != compress_len) { Dmsg0(0, "===Write error===\n"); - Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: %s\n"), attr->ofname, berror(&bfd)); + berrno be; + be.set_errno(bfd.berrno); + Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: %s\n"), attr->ofname, be.strerror()); extract = false; bclose(&bfd); continue; diff --git a/bacula/src/filed/verify.c b/bacula/src/filed/verify.c index 5ae74dd90b..8b58dc5694 100644 --- a/bacula/src/filed/verify.c +++ b/bacula/src/filed/verify.c @@ -108,18 +108,27 @@ static int verify_file(FF_PKT *ff_pkt, void *pkt) case FT_FIFO: Dmsg1(30, "FT_FIFO saving: %s\n", ff_pkt->fname); break; - case FT_NOACCESS: - Jmsg(jcr, M_NOTSAVED, -1, _(" Could not access %s: ERR=%s\n"), ff_pkt->fname, strerror(ff_pkt->ff_errno)); + case FT_NOACCESS: { + berrno be; + be.set_errno(ff_pkt->ff_errno); + Jmsg(jcr, M_NOTSAVED, -1, _(" Could not access %s: ERR=%s\n"), ff_pkt->fname, be.strerror()); jcr->Errors++; return 1; - case FT_NOFOLLOW: - Jmsg(jcr, M_NOTSAVED, -1, _(" Could not follow link %s: ERR=%s\n"), ff_pkt->fname, strerror(ff_pkt->ff_errno)); + } + case FT_NOFOLLOW: { + berrno be; + be.set_errno(ff_pkt->ff_errno); + Jmsg(jcr, M_NOTSAVED, -1, _(" Could not follow link %s: ERR=%s\n"), ff_pkt->fname, be.strerror()); jcr->Errors++; return 1; - case FT_NOSTAT: - Jmsg(jcr, M_NOTSAVED, -1, _(" Could not stat %s: ERR=%s\n"), ff_pkt->fname, strerror(ff_pkt->ff_errno)); + } + case FT_NOSTAT: { + berrno be; + be.set_errno(ff_pkt->ff_errno); + Jmsg(jcr, M_NOTSAVED, -1, _(" Could not stat %s: ERR=%s\n"), ff_pkt->fname, be.strerror()); jcr->Errors++; return 1; + } case FT_DIRNOCHG: case FT_NOCHG: Jmsg(jcr, M_SKIPPED, -1, _(" Unchanged file skipped: %s\n"), ff_pkt->fname); @@ -133,10 +142,13 @@ static int verify_file(FF_PKT *ff_pkt, void *pkt) case FT_NOFSCHG: Jmsg(jcr, M_SKIPPED, -1, _(" File system change prohibited. Directory skipped: %s\n"), ff_pkt->fname); return 1; - case FT_NOOPEN: - Jmsg(jcr, M_NOTSAVED, -1, _(" Could not open directory %s: ERR=%s\n"), ff_pkt->fname, strerror(ff_pkt->ff_errno)); + case FT_NOOPEN: { + berrno be; + be.set_errno(ff_pkt->ff_errno); + Jmsg(jcr, M_NOTSAVED, -1, _(" Could not open directory %s: ERR=%s\n"), ff_pkt->fname, be.strerror()); jcr->Errors++; return 1; + } default: Jmsg(jcr, M_NOTSAVED, 0, _(" Unknown file type %d: %s\n"), ff_pkt->type, ff_pkt->fname); jcr->Errors++; @@ -150,8 +162,10 @@ static int verify_file(FF_PKT *ff_pkt, void *pkt) ff_pkt->type == FT_RAW || ff_pkt->type == FT_FIFO) { if ((bopen(&bfd, ff_pkt->fname, O_RDONLY | O_BINARY, 0)) < 0) { ff_pkt->ff_errno = errno; + berrno be; + be.set_errno(bfd.berrno); Jmsg(jcr, M_NOTSAVED, -1, _(" Cannot open %s: ERR=%s.\n"), - ff_pkt->fname, berror(&bfd)); + ff_pkt->fname, be.strerror()); jcr->Errors++; return 1; } @@ -210,8 +224,10 @@ static int verify_file(FF_PKT *ff_pkt, void *pkt) jcr->ReadBytes += n; } if (n < 0) { + berrno be; + be.set_errno(bfd.berrno); Jmsg(jcr, M_ERROR, -1, _("Error reading file %s: ERR=%s\n"), - ff_pkt->fname, berror(&bfd)); + ff_pkt->fname, be.strerror()); jcr->Errors++; } MD5Final(signature, &md5c); @@ -230,8 +246,10 @@ static int verify_file(FF_PKT *ff_pkt, void *pkt) jcr->ReadBytes += n; } if (n < 0) { + berrno be; + be.set_errno(bfd.berrno); Jmsg(jcr, M_ERROR, -1, _("Error reading file %s: ERR=%s\n"), - ff_pkt->fname, berror(&bfd)); + ff_pkt->fname, be.strerror()); jcr->Errors++; } SHA1Final(&sha1c, signature); diff --git a/bacula/src/findlib/attribs.c b/bacula/src/findlib/attribs.c index 492e5b8024..aad35e1d08 100755 --- a/bacula/src/findlib/attribs.c +++ b/bacula/src/findlib/attribs.c @@ -335,19 +335,22 @@ bool set_attributes(JCR *jcr, ATTR *attr, BFILE *ofd) if (attr->type == FT_LNK) { /* Change owner of link, not of real file */ if (lchown(attr->ofname, attr->statp.st_uid, attr->statp.st_gid) < 0) { + berrno be; Jmsg2(jcr, M_ERROR, 0, _("Unable to set file owner %s: ERR=%s\n"), - attr->ofname, strerror(errno)); + attr->ofname, be.strerror()); ok = false; } } else { if (chown(attr->ofname, attr->statp.st_uid, attr->statp.st_gid) < 0) { + berrno be; Jmsg2(jcr, M_ERROR, 0, _("Unable to set file owner %s: ERR=%s\n"), - attr->ofname, strerror(errno)); + attr->ofname, be.strerror()); ok = false; } if (chmod(attr->ofname, attr->statp.st_mode) < 0) { + berrno be; Jmsg2(jcr, M_ERROR, 0, _("Unable to set file modes %s: ERR=%s\n"), - attr->ofname, strerror(errno)); + attr->ofname, be.strerror()); ok = false; } @@ -355,8 +358,9 @@ bool set_attributes(JCR *jcr, ATTR *attr, BFILE *ofd) * Reset file times. */ if (utime(attr->ofname, &ut) < 0) { + berrno be; Jmsg2(jcr, M_ERROR, 0, _("Unable to set file times %s: ERR=%s\n"), - attr->ofname, strerror(errno)); + attr->ofname, be.strerror()); ok = false; } #ifdef HAVE_CHFLAGS @@ -368,8 +372,9 @@ bool set_attributes(JCR *jcr, ATTR *attr, BFILE *ofd) * fail. */ if (chflags(attr->ofname, attr->statp.st_flags) < 0) { + berrno be; Jmsg2(jcr, M_ERROR, 0, _("Unable to set file flags %s: ERR=%s\n"), - attr->ofname, strerror(errno)); + attr->ofname, be.strerror()); ok = false; } #endif diff --git a/bacula/src/findlib/bfile.c b/bacula/src/findlib/bfile.c index 2ca9bd0337..f2ff68dbaa 100644 --- a/bacula/src/findlib/bfile.c +++ b/bacula/src/findlib/bfile.c @@ -108,6 +108,7 @@ void binit(BFILE *bfd) bfd->errmsg = NULL; bfd->lpContext = NULL; bfd->lerror = 0; + bfd->berrno = 0; } /* @@ -253,6 +254,7 @@ int bopen(BFILE *bfd, const char *fname, int flags, mode_t mode) if (bfd->fh == INVALID_HANDLE_VALUE) { bfd->lerror = GetLastError(); + bfd->berrno = b_errno_win32; bfd->mode = BF_CLOSED; } bfd->errmsg = NULL; @@ -348,6 +350,7 @@ ssize_t bread(BFILE *bfd, void *buf, size_t count) 1, /* Process Security */ &bfd->lpContext)) { /* Context */ bfd->lerror = GetLastError(); + bfd->berrno = b_errno_win32; return -1; } } else { @@ -357,6 +360,7 @@ ssize_t bread(BFILE *bfd, void *buf, size_t count) &bfd->rw_bytes, NULL)) { bfd->lerror = GetLastError(); + bfd->berrno = b_errno_win32; return -1; } } @@ -377,6 +381,7 @@ ssize_t bwrite(BFILE *bfd, void *buf, size_t count) 1, /* Process Security */ &bfd->lpContext)) { /* Context */ bfd->lerror = GetLastError(); + bfd->berrno = b_errno_win32; return -1; } } else { @@ -386,6 +391,7 @@ ssize_t bwrite(BFILE *bfd, void *buf, size_t count) &bfd->rw_bytes, NULL)) { bfd->lerror = GetLastError(); + bfd->berrno = b_errno_win32; return -1; } } diff --git a/bacula/src/findlib/bfile.h b/bacula/src/findlib/bfile.h index 390df8e2b8..2282010e81 100644 --- a/bacula/src/findlib/bfile.h +++ b/bacula/src/findlib/bfile.h @@ -41,27 +41,28 @@ enum { BF_CLOSED, - BF_READ, /* BackupRead */ - BF_WRITE /* BackupWrite */ + BF_READ, /* BackupRead */ + BF_WRITE /* BackupWrite */ }; /* In bfile.c */ /* Basic low level I/O file packet */ struct BFILE { - int use_backup_api; /* set if using BackupRead/Write */ - int mode; /* set if file is open */ - HANDLE fh; /* Win32 file handle */ - int fid; /* fd if doing Unix style */ - LPVOID lpContext; /* BackupRead/Write context */ - POOLMEM *errmsg; /* error message buffer */ - DWORD rw_bytes; /* Bytes read or written */ - DWORD lerror; /* Last error code */ -}; + int use_backup_api; /* set if using BackupRead/Write */ + int mode; /* set if file is open */ + HANDLE fh; /* Win32 file handle */ + int fid; /* fd if doing Unix style */ + LPVOID lpContext; /* BackupRead/Write context */ + POOLMEM *errmsg; /* error message buffer */ + DWORD rw_bytes; /* Bytes read or written */ + DWORD lerror; /* Last error code */ + int berrno; /* errno */ +}; HANDLE bget_handle(BFILE *bfd); -#else /* Linux/Unix systems */ +#else /* Linux/Unix systems */ /* ======================================================= * @@ -72,26 +73,26 @@ HANDLE bget_handle(BFILE *bfd); /* Basic low level I/O file packet */ struct BFILE { - int fid; /* file id on Unix */ + int fid; /* file id on Unix */ int berrno; -}; +}; #endif -void binit(BFILE *bfd); -int is_bopen(BFILE *bfd); -int set_win32_backup(BFILE *bfd); -int set_portable_backup(BFILE *bfd); -int have_win32_api(); -int is_portable_backup(BFILE *bfd); -int is_stream_supported(int stream); -int is_win32_stream(int stream); +void binit(BFILE *bfd); +int is_bopen(BFILE *bfd); +int set_win32_backup(BFILE *bfd); +int set_portable_backup(BFILE *bfd); +int have_win32_api(); +int is_portable_backup(BFILE *bfd); +int is_stream_supported(int stream); +int is_win32_stream(int stream); char *berror(BFILE *bfd); -int bopen(BFILE *bfd, const char *fname, int flags, mode_t mode); -int bclose(BFILE *bfd); +int bopen(BFILE *bfd, const char *fname, int flags, mode_t mode); +int bclose(BFILE *bfd); ssize_t bread(BFILE *bfd, void *buf, size_t count); ssize_t bwrite(BFILE *bfd, void *buf, size_t count); -off_t blseek(BFILE *bfd, off_t offset, int whence); +off_t blseek(BFILE *bfd, off_t offset, int whence); const char *stream_to_ascii(int stream); #endif /* __BFILE_H */ diff --git a/bacula/src/findlib/create_file.c b/bacula/src/findlib/create_file.c index 7f06d8bdcd..0a64a564dc 100644 --- a/bacula/src/findlib/create_file.c +++ b/bacula/src/findlib/create_file.c @@ -119,8 +119,9 @@ int create_file(JCR *jcr, ATTR *attr, BFILE *bfd, int replace) if (exists) { /* Get rid of old copy */ if (unlink(attr->ofname) == -1) { + berrno be; Jmsg(jcr, M_ERROR, 0, _("File %s already exists and could not be replaced. ERR=%s.\n"), - attr->ofname, strerror(errno)); + attr->ofname, be.strerror()); /* Continue despite error */ } } @@ -175,8 +176,10 @@ int create_file(JCR *jcr, ATTR *attr, BFILE *bfd, int replace) bclose(bfd); } if ((bopen(bfd, attr->ofname, mode, S_IRUSR | S_IWUSR)) < 0) { + berrno be; + be.set_errno(bfd->berrno); Jmsg2(jcr, M_ERROR, 0, _("Could not create %s: ERR=%s\n"), - attr->ofname, berror(bfd)); + attr->ofname, be.strerror()); return CF_ERROR; } return CF_EXTRACT; @@ -187,15 +190,17 @@ int create_file(JCR *jcr, ATTR *attr, BFILE *bfd, int replace) 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, strerror(errno)); + 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, strerror(errno)); + attr->ofname, be.strerror()); return CF_ERROR; } } @@ -213,8 +218,10 @@ int create_file(JCR *jcr, ATTR *attr, BFILE *bfd, int replace) 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, berror(bfd)); + attr->ofname, be.strerror()); stop_thread_timer(tid); return CF_ERROR; } @@ -227,8 +234,9 @@ int create_file(JCR *jcr, ATTR *attr, BFILE *bfd, int replace) case FT_LNK: Dmsg2(130, "FT_LNK should restore: %s -> %s\n", attr->ofname, attr->olname); if (symlink(attr->olname, attr->ofname) != 0 && errno != EEXIST) { + berrno be; Jmsg3(jcr, M_ERROR, 0, _("Could not symlink %s -> %s: ERR=%s\n"), - attr->ofname, attr->olname, strerror(errno)); + attr->ofname, attr->olname, be.strerror()); return CF_ERROR; } return CF_CREATED; @@ -236,8 +244,9 @@ int create_file(JCR *jcr, ATTR *attr, BFILE *bfd, int replace) case FT_LNKSAVED: /* Hard linked, file already saved */ Dmsg2(130, "Hard link %s => %s\n", attr->ofname, attr->olname); if (link(attr->olname, attr->ofname) != 0) { + berrno be; Jmsg3(jcr, M_ERROR, 0, _("Could not hard link %s -> %s: ERR=%s\n"), - attr->ofname, attr->olname, strerror(errno)); + attr->ofname, attr->olname, be.strerror()); return CF_ERROR; } return CF_CREATED; @@ -260,8 +269,10 @@ int create_file(JCR *jcr, ATTR *attr, BFILE *bfd, int replace) Jmsg1(jcr, M_ERROR, 0, "bpkt already open fid=%d\n", bfd->fid); } if ((bopen(bfd, attr->ofname, O_WRONLY|O_BINARY, 0)) < 0) { + berrno be; + be.set_errno(bfd->berrno); Jmsg2(jcr, M_ERROR, 0, _("Could not open %s: ERR=%s\n"), - attr->ofname, berror(bfd)); + attr->ofname, be.strerror()); return CF_ERROR; } return CF_EXTRACT; diff --git a/bacula/src/findlib/makepath.c b/bacula/src/findlib/makepath.c index 69ef9c9e97..a971373c80 100644 --- a/bacula/src/findlib/makepath.c +++ b/bacula/src/findlib/makepath.c @@ -112,8 +112,10 @@ make_dir(JCR *jcr, const char *dir, const char *dirpath, mode_t mode, int *creat would fail with EEXIST. */ if (stat(dir, &stats)) { + berrno be; + be.set_errno(save_errno); Jmsg(jcr, M_ERROR, 0, _("Cannot create directory %s: ERR=%s\n"), - dirpath, strerror(save_errno)); + dirpath, be.strerror()); fail = 1; } else if (!S_ISDIR(stats.st_mode)) { Jmsg(jcr, M_ERROR, 0, _("%s exists but is not a directory\n"), quote(dirpath)); @@ -271,8 +273,9 @@ make_path( #endif ) { /* Note, if we are restoring as NON-root, this may not be fatal */ + berrno be; Jmsg(jcr, M_ERROR, 0, _("Cannot change owner and/or group of %s: ERR=%s\n"), - quote(dirpath), strerror(errno)); + quote(dirpath), be.strerror()); } Dmsg0(300, "Chown done.\n"); @@ -291,8 +294,9 @@ make_path( creating an entry in that directory. This avoids making stat and mkdir process O(n^2) file name components. */ if (cwd.do_chdir && chdir(basename_dir) < 0) { + berrno be; Jmsg(jcr, M_ERROR, 0, _("Cannot chdir to directory, %s: ERR=%s\n"), - quote(dirpath), strerror(errno)); + quote(dirpath), be.strerror()); umask(oldmask); cleanup(&cwd); return 1; @@ -330,8 +334,9 @@ make_path( #endif ) { + berrno be; Jmsg(jcr, M_WARNING, 0, _("Cannot change owner and/or group of %s: ERR=%s\n"), - quote(dirpath), strerror(errno)); + quote(dirpath), be.strerror()); } } @@ -344,8 +349,9 @@ make_path( Dmsg1(300, "Final chmod mode=%o\n", mode); } if ((mode & ~S_IRWXUGO) && chmod(basename_dir, mode)) { + berrno be; Jmsg(jcr, M_WARNING, 0, _("Cannot change permissions of %s: ERR=%s\n"), - quote(dirpath), strerror(errno)); + quote(dirpath), be.strerror()); } if (cleanup(&cwd)) { @@ -359,8 +365,9 @@ make_path( *(p->dirname_end) = '\0'; Dmsg2(300, "Reset parent mode=%o dir=%s\n", parent_mode, dirpath); if (chmod(dirpath, parent_mode)) { + berrno be; Jmsg(jcr, M_WARNING, 0, _("Cannot change permissions of %s: ERR=%s\n"), - quote(dirpath), strerror(errno)); + quote(dirpath), be.strerror()); } } } else { @@ -387,12 +394,14 @@ make_path( && errno != EPERM #endif ) { + berrno be; Jmsg(jcr, M_WARNING, 0, _("Cannot change owner and/or group of %s: ERR=%s\n"), - quote(dirpath), strerror(errno)); + quote(dirpath), be.strerror()); } if (chmod(dirpath, mode)) { + berrno be; Jmsg(jcr, M_WARNING, 0, _("Cannot change permissions of %s: ERR=%s\n"), - quote(dirpath), strerror(errno)); + quote(dirpath), be.strerror()); } Dmsg2(300, "pathexists chmod mode=%o dir=%s\n", mode, dirpath); } diff --git a/bacula/src/findlib/save-cwd.c b/bacula/src/findlib/save-cwd.c index cd68d39443..8d6d75080a 100644 --- a/bacula/src/findlib/save-cwd.c +++ b/bacula/src/findlib/save-cwd.c @@ -44,9 +44,10 @@ save_cwd(struct saved_cwd *cwd) #if HAVE_FCHDIR cwd->desc = open(".", O_RDONLY); if (cwd->desc < 0) { - Emsg1(M_ERROR, 0, "Cannot open current directory: %s\n", strerror(errno)); - return 1; - } + berrno be; + Emsg1(M_ERROR, 0, "Cannot open current directory: %s\n", be.strerror()); + return 1; + } # if __sun__ || sun /* On SunOS 4, fchdir returns EINVAL if accounting is enabled, @@ -57,7 +58,8 @@ save_cwd(struct saved_cwd *cwd) cwd->desc = -1; have_working_fchdir = 0; } else { - Emsg1(M_ERROR, 0, "Current directory: %s\n", strerror(errno)); + berrno be; + Emsg1(M_ERROR, 0, "Current directory: %s\n", be.strerror()); close(cwd->desc); cwd->desc = -1; return 1; @@ -78,9 +80,10 @@ save_cwd(struct saved_cwd *cwd) #endif cwd->name = (POOLMEM *)getcwd(buf, sizeof_pool_memory(buf)); if (cwd->name == NULL) { - Emsg1(M_ERROR, 0, "Cannot get current directory: %s\n", strerror(errno)); - free_pool_memory(buf); - return 1; + berrno be; + Emsg1(M_ERROR, 0, "Cannot get current directory: %s\n", be.strerror()); + free_pool_memory(buf); + return 1; } } return 0; @@ -96,14 +99,16 @@ restore_cwd(const struct saved_cwd *cwd, const char *dest, const char *from) int fail = 0; if (cwd->desc >= 0) { if (fchdir(cwd->desc)) { - Emsg4(M_ERROR, 0, "Cannot return to %s%s%s: %s\n", + berrno be; + Emsg4(M_ERROR, 0, "Cannot return to %s%s%s: %s\n", (dest ? dest : "saved working directory"), (from ? " from " : ""), - (from ? from : ""), strerror(errno)); - fail = 1; + (from ? from : ""), be.strerror()); + fail = 1; } } else if (chdir(cwd->name) < 0) { - Emsg2(M_ERROR, 0, "%s: %s\n", cwd->name, strerror(errno)); + berrno be; + Emsg2(M_ERROR, 0, "%s: %s\n", cwd->name, be.strerror()); fail = 1; } return fail; diff --git a/bacula/src/lib/Makefile.in b/bacula/src/lib/Makefile.in index f3fcaa480f..6b23aa48d7 100644 --- a/bacula/src/lib/Makefile.in +++ b/bacula/src/lib/Makefile.in @@ -31,7 +31,7 @@ GMP_INC=@GMP_INC@ first_rule: all dummy: -LIBSRCS = alloc.c attr.c base64.c bsys.c bget_msg.c \ +LIBSRCS = alloc.c attr.c base64.c berrno.c bsys.c bget_msg.c \ bnet.c bnet_server.c \ bpipe.c bshm.c btime.c \ cram-md5.c crc32.c daemon.c edit.c fnmatch.c \ @@ -42,7 +42,7 @@ LIBSRCS = alloc.c attr.c base64.c bsys.c bget_msg.c \ util.c var.c watchdog.c workq.c btimers.c -LIBOBJS = alloc.o attr.o base64.o bsys.o bget_msg.o \ +LIBOBJS = alloc.o attr.o base64.o berrno.o bsys.o bget_msg.o \ bnet.o bnet_server.o \ bpipe.o bshm.o btime.o \ cram-md5.o crc32.o daemon.o edit.o fnmatch.o \ diff --git a/bacula/src/lib/alist.h b/bacula/src/lib/alist.h index 5f02222ced..cf8fbf5bc5 100644 --- a/bacula/src/lib/alist.h +++ b/bacula/src/lib/alist.h @@ -3,7 +3,7 @@ */ /* - Copyright (C) 2000-2003 Kern Sibbald and John Walker + Copyright (C) 2003-2004 Kern Sibbald and John Walker This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as diff --git a/bacula/src/lib/bsys.c b/bacula/src/lib/bsys.c index bb36376c16..5b32e330bd 100644 --- a/bacula/src/lib/bsys.c +++ b/bacula/src/lib/bsys.c @@ -201,8 +201,8 @@ int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result) #endif #endif /* HAVE_READDIR_R */ -#ifdef xxxxxxxxxx_STRERROR_R -int strerror_r(int errnum, char *buf, size_t bufsiz) + +int bstrerror(int errnum, char *buf, size_t bufsiz) { static pthread_mutex_t mutex; static int first = 1; @@ -224,7 +224,6 @@ int strerror_r(int errnum, char *buf, size_t bufsiz) V(mutex); return stat; } -#endif /* HAVE_STRERROR_R */ /* * These are mutex routines that do error checking diff --git a/bacula/src/lib/lib.h b/bacula/src/lib/lib.h index 89d9c2948d..76dfc8aa2b 100644 --- a/bacula/src/lib/lib.h +++ b/bacula/src/lib/lib.h @@ -52,6 +52,7 @@ #include "tree.h" #include "watchdog.h" #include "btimers.h" +#include "berrno.h" #include "bpipe.h" #include "attr.h" #include "var.h" diff --git a/bacula/src/lib/protos.h b/bacula/src/lib/protos.h index eec978c01d..440de0e2d9 100644 --- a/bacula/src/lib/protos.h +++ b/bacula/src/lib/protos.h @@ -26,99 +26,100 @@ struct JCR; /* attr.c */ -ATTR *new_attr(); -void free_attr(ATTR *attr); -int unpack_attributes_record(JCR *jcr, int32_t stream, char *rec, ATTR *attr); -void build_attr_output_fnames(JCR *jcr, ATTR *attr); -void print_ls_output(JCR *jcr, ATTR *attr); +ATTR *new_attr(); +void free_attr(ATTR *attr); +int unpack_attributes_record(JCR *jcr, int32_t stream, char *rec, ATTR *attr); +void build_attr_output_fnames(JCR *jcr, ATTR *attr); +void print_ls_output(JCR *jcr, ATTR *attr); /* base64.c */ -void base64_init (void); -int to_base64 (intmax_t value, char *where); -int from_base64 (intmax_t *value, char *where); -int bin_to_base64 (char *buf, char *bin, int len); +void base64_init (void); +int to_base64 (intmax_t value, char *where); +int from_base64 (intmax_t *value, char *where); +int bin_to_base64 (char *buf, char *bin, int len); /* bsys.c */ -char *bstrncpy (char *dest, const char *src, int maxlen); -char *bstrncat (char *dest, const char *src, int maxlen); -void *b_malloc (const char *file, int line, size_t size); +char *bstrncpy (char *dest, const char *src, int maxlen); +char *bstrncat (char *dest, const char *src, int maxlen); +void *b_malloc (const char *file, int line, size_t size); #ifndef DEBUG -void *bmalloc (size_t size); +void *bmalloc (size_t size); #endif -void *brealloc (void *buf, size_t size); -void *bcalloc (size_t size1, size_t size2); -int bsnprintf (char *str, int32_t size, const char *format, ...); -int bvsnprintf (char *str, int32_t size, const char *format, va_list ap); -int pool_sprintf (char *pool_buf, const char *fmt, ...); -void create_pid_file (char *dir, const char *progname, int port); -int delete_pid_file (char *dir, const char *progname, int port); -void drop (char *uid, char *gid); -int bmicrosleep (time_t sec, long usec); -char *bfgets (char *s, int size, FILE *fd); -void make_unique_filename (POOLMEM **name, int Id, char *what); +void *brealloc (void *buf, size_t size); +void *bcalloc (size_t size1, size_t size2); +int bsnprintf (char *str, int32_t size, const char *format, ...); +int bvsnprintf (char *str, int32_t size, const char *format, va_list ap); +int pool_sprintf (char *pool_buf, const char *fmt, ...); +void create_pid_file (char *dir, const char *progname, int port); +int delete_pid_file (char *dir, const char *progname, int port); +void drop (char *uid, char *gid); +int bmicrosleep (time_t sec, long usec); +char *bfgets (char *s, int size, FILE *fd); +void make_unique_filename (POOLMEM **name, int Id, char *what); #ifndef HAVE_STRTOLL -long long int strtoll (const char *ptr, char **endptr, int base); +long long int strtoll (const char *ptr, char **endptr, int base); #endif -void read_state_file(char *dir, const char *progname, int port); +void read_state_file(char *dir, const char *progname, int port); +int bstrerror(int errnum, char *buf, size_t bufsiz); /* bnet.c */ -int32_t bnet_recv (BSOCK *bsock); -bool bnet_send (BSOCK *bsock); -bool bnet_fsend (BSOCK *bs, const char *fmt, ...); -bool bnet_set_buffer_size (BSOCK *bs, uint32_t size, int rw); -bool bnet_sig (BSOCK *bs, int sig); -int bnet_ssl_server (BSOCK *bsock, char *password, int ssl_need, int ssl_has); -int bnet_ssl_client (BSOCK *bsock, char *password, int ssl_need); -BSOCK * bnet_connect (JCR *jcr, int retry_interval, - int max_retry_time, const char *name, char *host, char *service, - int port, int verbose); -void bnet_close (BSOCK *bsock); -BSOCK * init_bsock (JCR *jcr, int sockfd, const char *who, const char *ip, - int port, struct sockaddr_in *client_addr); -BSOCK * dup_bsock (BSOCK *bsock); -void term_bsock (BSOCK *bsock); -char * bnet_strerror (BSOCK *bsock); -const char *bnet_sig_to_ascii (BSOCK *bsock); -int bnet_wait_data (BSOCK *bsock, int sec); -int bnet_wait_data_intr (BSOCK *bsock, int sec); -int bnet_despool_to_bsock (BSOCK *bsock, void update(ssize_t size), ssize_t size); -bool is_bnet_stop (BSOCK *bsock); -int is_bnet_error (BSOCK *bsock); -void bnet_suppress_error_messages(BSOCK *bsock, bool flag); +int32_t bnet_recv (BSOCK *bsock); +bool bnet_send (BSOCK *bsock); +bool bnet_fsend (BSOCK *bs, const char *fmt, ...); +bool bnet_set_buffer_size (BSOCK *bs, uint32_t size, int rw); +bool bnet_sig (BSOCK *bs, int sig); +int bnet_ssl_server (BSOCK *bsock, char *password, int ssl_need, int ssl_has); +int bnet_ssl_client (BSOCK *bsock, char *password, int ssl_need); +BSOCK * bnet_connect (JCR *jcr, int retry_interval, + int max_retry_time, const char *name, char *host, char *service, + int port, int verbose); +void bnet_close (BSOCK *bsock); +BSOCK * init_bsock (JCR *jcr, int sockfd, const char *who, const char *ip, + int port, struct sockaddr_in *client_addr); +BSOCK * dup_bsock (BSOCK *bsock); +void term_bsock (BSOCK *bsock); +char * bnet_strerror (BSOCK *bsock); +const char *bnet_sig_to_ascii (BSOCK *bsock); +int bnet_wait_data (BSOCK *bsock, int sec); +int bnet_wait_data_intr (BSOCK *bsock, int sec); +int bnet_despool_to_bsock (BSOCK *bsock, void update(ssize_t size), ssize_t size); +bool is_bnet_stop (BSOCK *bsock); +int is_bnet_error (BSOCK *bsock); +void bnet_suppress_error_messages(BSOCK *bsock, bool flag); /* bget_msg.c */ -int bget_msg(BSOCK *sock); +int bget_msg(BSOCK *sock); /* bpipe.c */ -BPIPE * open_bpipe(char *prog, int wait, const char *mode); -int close_wpipe(BPIPE *bpipe); -int close_bpipe(BPIPE *bpipe); +BPIPE * open_bpipe(char *prog, int wait, const char *mode); +int close_wpipe(BPIPE *bpipe); +int close_bpipe(BPIPE *bpipe); /* cram-md5.c */ int cram_md5_get_auth(BSOCK *bs, char *password, int ssl_need); int cram_md5_auth(BSOCK *bs, char *password, int ssl_need); void hmac_md5(uint8_t* text, int text_len, uint8_t* key, - int key_len, uint8_t *hmac); + int key_len, uint8_t *hmac); /* crc32.c */ uint32_t bcrc32(uint8_t *buf, int len); /* daemon.c */ -void daemon_start (); +void daemon_start (); /* edit.c */ -uint64_t str_to_uint64(char *str); -int64_t str_to_int64(char *str); -char * edit_uint64_with_commas (uint64_t val, char *buf); -char * add_commas (char *val, char *buf); -char * edit_uint64 (uint64_t val, char *buf); -int duration_to_utime (char *str, utime_t *value); -int size_to_uint64(char *str, int str_len, uint64_t *rtn_value); -char *edit_utime (utime_t val, char *buf); -int is_a_number (const char *num); -int is_an_integer (const char *n); -bool is_name_valid (char *name, POOLMEM **msg); +uint64_t str_to_uint64(char *str); +int64_t str_to_int64(char *str); +char * edit_uint64_with_commas (uint64_t val, char *buf); +char * add_commas (char *val, char *buf); +char * edit_uint64 (uint64_t val, char *buf); +int duration_to_utime (char *str, utime_t *value); +int size_to_uint64(char *str, int str_len, uint64_t *rtn_value); +char *edit_utime (utime_t val, char *buf); +int is_a_number (const char *num); +int is_an_integer (const char *n); +bool is_name_valid (char *name, POOLMEM **msg); /* jcr.c (most definitions are in src/jcr.h) */ void init_last_jobs_list(); @@ -132,36 +133,36 @@ void job_end_push(JCR *jcr, void job_end_cb(JCR *jcr,void *), void *ctx); /* lex.c */ -LEX * lex_close_file (LEX *lf); -LEX * lex_open_file (LEX *lf, const char *fname, LEX_ERROR_HANDLER *scan_error); -int lex_get_char (LEX *lf); -void lex_unget_char (LEX *lf); -const char * lex_tok_to_str (int token); -int lex_get_token (LEX *lf, int expect); +LEX * lex_close_file (LEX *lf); +LEX * lex_open_file (LEX *lf, const char *fname, LEX_ERROR_HANDLER *scan_error); +int lex_get_char (LEX *lf); +void lex_unget_char (LEX *lf); +const char * lex_tok_to_str (int token); +int lex_get_token (LEX *lf, int expect); /* message.c */ -void my_name_is (int argc, char *argv[], const char *name); -void init_msg (JCR *jcr, MSGS *msg); -void term_msg (void); -void close_msg (JCR *jcr); -void add_msg_dest (MSGS *msg, int dest, int type, char *where, char *dest_code); -void rem_msg_dest (MSGS *msg, int dest, int type, char *where); -void Jmsg (JCR *jcr, int type, int level, const char *fmt, ...); -void dispatch_message (JCR *jcr, int type, int level, char *buf); -void init_console_msg (const char *wd); -void free_msgs_res (MSGS *msgs); -void dequeue_messages (JCR *jcr); -void set_trace (int trace_flag); -void set_exit_on_error (int value); +void my_name_is (int argc, char *argv[], const char *name); +void init_msg (JCR *jcr, MSGS *msg); +void term_msg (void); +void close_msg (JCR *jcr); +void add_msg_dest (MSGS *msg, int dest, int type, char *where, char *dest_code); +void rem_msg_dest (MSGS *msg, int dest, int type, char *where); +void Jmsg (JCR *jcr, int type, int level, const char *fmt, ...); +void dispatch_message (JCR *jcr, int type, int level, char *buf); +void init_console_msg (const char *wd); +void free_msgs_res (MSGS *msgs); +void dequeue_messages (JCR *jcr); +void set_trace (int trace_flag); +void set_exit_on_error (int value); /* bnet_server.c */ -void bnet_thread_server(char *bind_addr, int port, int max_clients, workq_t *client_wq, - void *handle_client_request(void *bsock)); -void bnet_stop_thread_server(pthread_t tid); -void bnet_server (int port, void handle_client_request(BSOCK *bsock)); -int net_connect (int port); -BSOCK * bnet_bind (int port); -BSOCK * bnet_accept (BSOCK *bsock, char *who); +void bnet_thread_server(char *bind_addr, int port, int max_clients, workq_t *client_wq, + void *handle_client_request(void *bsock)); +void bnet_stop_thread_server(pthread_t tid); +void bnet_server (int port, void handle_client_request(BSOCK *bsock)); +int net_connect (int port); +BSOCK * bnet_bind (int port); +BSOCK * bnet_accept (BSOCK *bsock, char *who); /* idcache.c */ char *getuser(uid_t uid); @@ -171,41 +172,41 @@ void free_getgroup_cache(); /* signal.c */ -void init_signals (void terminate(int sig)); -void init_stack_dump (void); +void init_signals (void terminate(int sig)); +void init_stack_dump (void); /* scan.c */ -void strip_trailing_junk (char *str); -void strip_trailing_slashes (char *dir); -bool skip_spaces (char **msg); -bool skip_nonspaces (char **msg); -int fstrsch (const char *a, const char *b); -char *next_arg(char **s); -int parse_args(POOLMEM *cmd, POOLMEM **args, int *argc, - char **argk, char **argv, int max_args); -void split_path_and_filename(const char *fname, POOLMEM **path, - int *pnl, POOLMEM **file, int *fnl); -int bsscanf(const char *buf, const char *fmt, ...); +void strip_trailing_junk (char *str); +void strip_trailing_slashes (char *dir); +bool skip_spaces (char **msg); +bool skip_nonspaces (char **msg); +int fstrsch (const char *a, const char *b); +char *next_arg(char **s); +int parse_args(POOLMEM *cmd, POOLMEM **args, int *argc, + char **argk, char **argv, int max_args); +void split_path_and_filename(const char *fname, POOLMEM **path, + int *pnl, POOLMEM **file, int *fnl); +int bsscanf(const char *buf, const char *fmt, ...); /* util.c */ -int is_buf_zero (char *buf, int len); -void lcase (char *str); -void bash_spaces (char *str); -void unbash_spaces (char *str); -char * encode_time (time_t time, char *buf); -char * encode_mode (mode_t mode, char *buf); -int do_shell_expansion (char *name, int name_len); -void jobstatus_to_ascii (int JobStatus, char *msg, int maxlen); -int pm_strcat (POOLMEM **pm, const char *str); -int pm_strcpy (POOLMEM **pm, const char *str); -int run_program (char *prog, int wait, POOLMEM *results); -const char * job_type_to_str (int type); -const char * job_status_to_str (int stat); -const char * job_level_to_str (int level); -void make_session_key (char *key, char *seed, int mode); -POOLMEM *edit_job_codes(JCR *jcr, char *omsg, char *imsg, const char *to); -void set_working_directory(char *wd); +int is_buf_zero (char *buf, int len); +void lcase (char *str); +void bash_spaces (char *str); +void unbash_spaces (char *str); +char * encode_time (time_t time, char *buf); +char * encode_mode (mode_t mode, char *buf); +int do_shell_expansion (char *name, int name_len); +void jobstatus_to_ascii (int JobStatus, char *msg, int maxlen); +int pm_strcat (POOLMEM **pm, const char *str); +int pm_strcpy (POOLMEM **pm, const char *str); +int run_program (char *prog, int wait, POOLMEM *results); +const char * job_type_to_str (int type); +const char * job_status_to_str (int stat); +const char * job_level_to_str (int level); +void make_session_key (char *key, char *seed, int mode); +POOLMEM *edit_job_codes(JCR *jcr, char *omsg, char *imsg, const char *to); +void set_working_directory(char *wd); /* watchdog.c */ diff --git a/bacula/src/win32/compat/compat.cpp b/bacula/src/win32/compat/compat.cpp index 7e0fd1e39a..e9cc05cc9a 100644 --- a/bacula/src/win32/compat/compat.cpp +++ b/bacula/src/win32/compat/compat.cpp @@ -167,19 +167,19 @@ cvt_ftime_to_utime(const FILETIME &time) static const char * errorString(void) { - LPVOID lpMsgBuf; - - FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, - GetLastError(), - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default lang - (LPTSTR) &lpMsgBuf, - 0, - NULL); - - return (const char *) lpMsgBuf; + LPVOID lpMsgBuf; + + FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + GetLastError(), + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default lang + (LPTSTR) &lpMsgBuf, + 0, + NULL); + + return (const char *) lpMsgBuf; } #ifndef HAVE_MINGW @@ -204,7 +204,7 @@ statDir(const char *file, struct stat *sb) const char *err = errorString(); d_msg(__FILE__, __LINE__, 99, "FindFirstFile(%s):%s\n", file, err); LocalFree((void *)err); - errno = GetLastError(); + errno = b_errno_win32; return -1; } @@ -247,7 +247,7 @@ stat2(const char *file, struct stat *sb) d_msg(__FILE__, __LINE__, 99, "GetFileAttrubtes(%s): %s\n", tmpbuf, err); LocalFree((void *)err); - errno = GetLastError(); + errno = b_errno_win32; return -1; } @@ -263,7 +263,7 @@ stat2(const char *file, struct stat *sb) "Cannot open file for stat (%s):%s\n", tmpbuf, err); LocalFree((void *)err); rval = -1; - errno = GetLastError(); + errno = b_errno_win32; goto error; } @@ -273,7 +273,7 @@ stat2(const char *file, struct stat *sb) "GetfileInformationByHandle(%s): %s\n", tmpbuf, err); LocalFree((void *)err); rval = -1; - errno = GetLastError(); + errno = b_errno_win32; goto error; } @@ -382,6 +382,7 @@ geteuid(void) int execvp(const char *, char *[]) { + errno = ENOSYS; return -1; } @@ -389,24 +390,28 @@ execvp(const char *, char *[]) { int fork(void) { + errno = ENOSYS; return -1; } int pipe(int[]) { + errno = ENOSYS; return -1; } int waitpid(int, int*, int) { + errno = ENOSYS; return -1; } int readlink(const char *, char *, int) { + errno = ENOSYS; return -1; } @@ -455,7 +460,7 @@ strncasecmp(const char *s1, const char *s2, int len) if (ch1 == 0 || tolower(ch1) != tolower(ch2)) break; } - return(ch1 - ch2); + return (ch1 - ch2); } int @@ -465,8 +470,14 @@ gettimeofday(struct timeval *tv, struct timezone *) FILETIME tmp; GetSystemTime(&now); - if (tv == NULL) return -1; - if (!SystemTimeToFileTime(&now, &tmp)) return -1; + if (tv == NULL) { + errno = EINVAL; + return -1; + } + if (!SystemTimeToFileTime(&now, &tmp)) { + errno = b_errno_win32; + return -1; + } int64_t _100nsec = tmp.dwHighDateTime; _100nsec <<= 32; @@ -515,12 +526,15 @@ opendir(const char *path) { int max_len = strlen(path) + 16; _dir *rval = NULL; - if (path == NULL) return NULL; + if (path == NULL) { + errno = ENOENT; + return NULL; + } rval = (_dir *)malloc(sizeof(_dir)); if (rval == NULL) return NULL; char *tspec = (char *)malloc(max_len); - if (tspec == NULL) goto err1; + if (tspec == NULL) return NULL; if (g_platform_id != VER_PLATFORM_WIN32_WINDOWS) { // allow path to be 32767 bytes @@ -549,10 +563,11 @@ opendir(const char *path) d_msg(__FILE__, __LINE__, 99, "\tFirstFile=%s\n", rval->data.cFileName); return (DIR *)rval; + err: free((void *)rval->spec); -err1: free(rval); + errno = b_errno_win32; return NULL; } @@ -609,6 +624,7 @@ readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result) dirp, entry->d_name, entry->d_reclen, entry->d_off); } else { // d_msg(__FILE__, __LINE__, 99, "readdir_r !valid\n"); + errno = b_errno_win32; return -1; } dp->valid = FindNextFile(dp->dirh, &dp->data); @@ -671,7 +687,7 @@ pathconf(const char *path, int name) case _PC_NAME_MAX : return 255; } - + errno = ENOSYS; return -1; } @@ -684,9 +700,9 @@ WSA_Init(void) int err = WSAStartup(wVersionRequested, &wsaData); - if (err != 0) - { + if (err != 0) { printf("Can not start Windows Sockets\n"); + errno = ENOSYS; return -1; } @@ -697,7 +713,10 @@ WSA_Init(void) int win32_chdir(const char *dir) { - if (0 == SetCurrentDirectory(dir)) return -1; + if (0 == SetCurrentDirectory(dir)) { + errno = b_errno_win32; + return -1; + } return 0; } @@ -713,7 +732,6 @@ win32_getcwd(char *buf, int maxlen) buf[n] = '\\'; buf[n+1] = 0; } - return buf; } @@ -1108,7 +1126,7 @@ close_wpipe(BPIPE *bpipe) fflush(bpipe->wfd); if (fclose(bpipe->wfd) != 0) { stat = 0; - } + } bpipe->wfd = NULL; } return stat; @@ -1140,13 +1158,12 @@ utime(const char *fname, struct utimbuf *times) d_msg(__FILE__, __LINE__, 99, "Cannot open file for utime(%s,...):%s\n", tmpbuf, err); LocalFree((void *)err); + errno = b_errno_win32; return -1; } int rval = SetFileTime(h, NULL, &acc, &mod) ? 0 : -1; - CloseHandle(h); - return rval; } @@ -1219,16 +1236,16 @@ open(const char *file, int flags, int mode) if (!(flags & O_EXCL)) shareMode = FILE_SHARE_DELETE|FILE_SHARE_READ|FILE_SHARE_WRITE; - if (flags & O_APPEND) - { + if (flags & O_APPEND) { printf("open...APPEND not implemented yet."); exit(-1); } foo = CreateFile(file, access, shareMode, NULL, create, msflags, NULL); - if (INVALID_HANDLE_VALUE == foo) + if (INVALID_HANDLE_VALUE == foo) { + errno = b_errno_win32; return(int) -1; - + } return (int)foo; } @@ -1237,8 +1254,10 @@ open(const char *file, int flags, int mode) int close(int fd) { - if (!CloseHandle((HANDLE)fd)) + if (!CloseHandle((HANDLE)fd)) { + errno = b_errno_win32; return -1; + } return 0; } @@ -1250,6 +1269,7 @@ write(int fd, const void *data, ssize_t len) DWORD bwrite; status = WriteFile((HANDLE)fd, data, len, &bwrite, NULL); if (status) return bwrite; + errno = b_errno_win32; return -1; } @@ -1262,6 +1282,7 @@ read(int fd, void *data, ssize_t len) status = ReadFile((HANDLE)fd, data, len, &bread, NULL); if (status) return bread; + errno = b_errno_win32; return -1; } @@ -1269,6 +1290,7 @@ off_t lseek(int fd, off_t offset, int whence) { DWORD method = 0; + DWORD val; switch (whence) { case SEEK_SET : method = FILE_BEGIN; @@ -1280,15 +1302,22 @@ lseek(int fd, off_t offset, int whence) method = FILE_END; break; default: + errno = EINVAL; return -1; } - return SetFilePointer((HANDLE)fd, (DWORD)offset, NULL, method); + if ((val=SetFilePointer((HANDLE)fd, (DWORD)offset, NULL, method)) == INVALID_SET_FILE_POINTER) { + errno = b_errno_win32; + return -1; + } + /* ***FIXME*** I doubt this works right */ + return val; } int dup2(int, int) { + errno = ENOSYS; return -1; }