From: Robert Nelson Date: Tue, 21 Nov 2006 16:45:13 +0000 (+0000) Subject: Replace explicit checks for "/" with calls to IsPathSeparator, strchr with first_path... X-Git-Tag: Release-7.0.0~7362 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=d2623318ddf378c21bf842fc7aafe6eb143933e0;p=bacula%2Fbacula Replace explicit checks for "/" with calls to IsPathSeparator, strchr with first_path_separator and strrchr with last_path_separator. Use serialization routines for crypto length fix. Fix 64 bit offset problems on Windows by replacing off_t with new boffset_t. git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@3671 91ce42f0-d328-0410-95d8-f526ca767f89 --- diff --git a/bacula/src/baconfig.h b/bacula/src/baconfig.h index 4debd4f444..d417d77c8a 100644 --- a/bacula/src/baconfig.h +++ b/bacula/src/baconfig.h @@ -324,7 +324,13 @@ typedef int (INTHANDLER)(); #define MODE_RW 0666 #endif -#ifdef DEBUG_MUTEX +#if defined(HAVE_WIN32) +typedef int64_t boffset_t; +#else +typedef off_t boffset_t; +#endif + +#if defined(DEBUG_MUTEX) extern void _p(char *file, int line, pthread_mutex_t *m); extern void _v(char *file, int line, pthread_mutex_t *m); @@ -538,7 +544,7 @@ int m_msg(const char *file, int line, POOLMEM *&pool_buf, const char *fmt, ...) #define REPLACE_NEVER 'n' #define REPLACE_IFOLDER 'o' -/* This probably should be done on a machine by machine basic, but it works */ +/* This probably should be done on a machine by machine basis, but it works */ /* This is critical for the smartalloc routines to properly align memory */ #define ALIGN_SIZE (sizeof(double)) #define BALIGN(x) (((x) + ALIGN_SIZE - 1) & ~(ALIGN_SIZE -1)) @@ -595,15 +601,25 @@ extern "C" int mknod ( const char *path, int mode, dev_t device ); #endif +#if defined(HAVE_WIN32) +#define DEFAULT_CONFIGDIR "C:\\Documents and Settings\\All Users\\Application Data\\Bacula" + +inline bool IsPathSeparator(int ch) { return ch == '/' || ch == '\\'; } +inline char *first_path_separator(char *path) { return strpbrk(path, ":/\\"); } +inline const char *first_path_separator(const char *path) { return strpbrk(path, ":/\\"); } + +#else /* Define Winsock functions if we aren't on Windows */ -#if !defined HAVE_WIN32 + #define WSA_Init() 0 /* 0 = success */ #define WSACleanup() 0 /* 0 = success */ -#endif -#ifdef HAVE_AIX_OS +inline bool IsPathSeparator(int ch) { return ch == '/'; } +inline char *first_path_separator(char *path) { return strchr(path, '/'); } +inline const char *first_path_separator(const char *path) { return strchr(path, '/'); } #endif + /* HP-UX 11 specific workarounds */ #ifdef HAVE_HPUX_OS @@ -627,12 +643,6 @@ extern "C" long gethostid(void); #endif -/* Added by KES to deal with Win32 systems */ -#ifndef S_ISWIN32 -#define S_ISWIN32 020000 -#endif - - /* Disabled because it breaks internationalisation... #undef HAVE_SETLOCALE #ifdef HAVE_SETLOCALE diff --git a/bacula/src/cats/bdb.c b/bacula/src/cats/bdb.c index b5979f18c9..6ef501f16d 100644 --- a/bacula/src/cats/bdb.c +++ b/bacula/src/cats/bdb.c @@ -65,7 +65,7 @@ static POOLMEM *make_filename(B_DB *mdb, char *name) POOLMEM *dbf; dbf = get_pool_memory(PM_FNAME); - if (working_directory[strlen(working_directory)-1] == '/') { + if (IsPathSeparator(working_directory[strlen(working_directory)-1])) { sep = 0; } else { sep = '/'; diff --git a/bacula/src/cats/sql.c b/bacula/src/cats/sql.c index dc84a65941..02bc8ca048 100644 --- a/bacula/src/cats/sql.c +++ b/bacula/src/cats/sql.c @@ -387,11 +387,11 @@ void split_path_and_file(JCR *jcr, B_DB *mdb, const char *fname) * must be a path name (e.g. c:). */ for (p=f=fname; *p; p++) { - if (*p == '/') { + if (IsPathSeparator(*p)) { f = p; /* set pos of last slash */ } } - if (*f == '/') { /* did we find a slash? */ + if (IsPathSeparator(*f)) { /* did we find a slash? */ f++; /* yes, point to filename */ } else { /* no, whole thing must be path name */ f = p; diff --git a/bacula/src/dird/inc_conf.c b/bacula/src/dird/inc_conf.c index dc7a2ba74c..3052449d11 100644 --- a/bacula/src/dird/inc_conf.c +++ b/bacula/src/dird/inc_conf.c @@ -508,7 +508,7 @@ static void store_wild(LEX *lc, RES_ITEM *item, int index, int pass) res_incexe.current_opts->wilddir.append(bstrdup(lc->str)); newsize = res_incexe.current_opts->wilddir.size(); } else if (item->code == 2) { - if (strchr(lc->str, '/') != NULL) { + if (strpbrk(lc->str, "/\\") != NULL) { type = "wildfile"; res_incexe.current_opts->wildfile.append(bstrdup(lc->str)); newsize = res_incexe.current_opts->wildfile.size(); diff --git a/bacula/src/dird/ua_restore.c b/bacula/src/dird/ua_restore.c index b20fb4b9f3..104dca9db5 100644 --- a/bacula/src/dird/ua_restore.c +++ b/bacula/src/dird/ua_restore.c @@ -613,7 +613,7 @@ static int user_select_jobids_or_files(UAContext *ua, RESTORE_CTX *rx) break; } /* Add trailing slash to end of directory names */ - if (ua->cmd[0] != '<' && ua->cmd[len-1] != '/') { + if (ua->cmd[0] != '<' && !IsPathSeparator(ua->cmd[len-1])) { strcat(ua->cmd, "/"); } insert_one_file_or_dir(ua, rx, date, true); @@ -825,11 +825,11 @@ static void split_path_and_filename(RESTORE_CTX *rx, char *name) * must be a path name (e.g. c:). */ for (p=f=name; *p; p++) { - if (*p == '/') { + if (IsPathSeparator(*p)) { f = p; /* set pos of last slash */ } } - if (*f == '/') { /* did we find a slash? */ + if (IsPathSeparator(*f)) { /* did we find a slash? */ f++; /* yes, point to filename */ } else { /* no, whole thing must be path name */ f = p; diff --git a/bacula/src/dird/ua_run.c b/bacula/src/dird/ua_run.c index a37fd9a9a1..55b60907b9 100644 --- a/bacula/src/dird/ua_run.c +++ b/bacula/src/dird/ua_run.c @@ -907,7 +907,7 @@ try_again: free(jcr->where); jcr->where = NULL; } - if (ua->cmd[0] == '/' && ua->cmd[1] == 0) { + if (IsPathSeparator(ua->cmd[0]) && ua->cmd[1] == '\0') { ua->cmd[0] = 0; } jcr->where = bstrdup(ua->cmd); diff --git a/bacula/src/dird/ua_tree.c b/bacula/src/dird/ua_tree.c index 0c232a8be4..f1b4330309 100644 --- a/bacula/src/dird/ua_tree.c +++ b/bacula/src/dird/ua_tree.c @@ -174,8 +174,8 @@ int insert_tree_handler(void *ctx, int num_fields, char **row) // Dmsg4(000, "Path=%s%s FI=%s JobId=%s\n", row[0], row[1], // row[2], row[3]); - if (*row[1] == 0) { /* no filename => directory */ - if (*row[0] != '/') { /* Must be Win32 directory */ + if (*row[1] == 0) { /* no filename => directory */ + if (!IsPathSeparator(*row[0])) { /* Must be Win32 directory */ type = TN_DIR_NLS; } else { type = TN_DIR; diff --git a/bacula/src/filed/acl.c b/bacula/src/filed/acl.c index cd72701625..6829cf6466 100644 --- a/bacula/src/filed/acl.c +++ b/bacula/src/filed/acl.c @@ -431,7 +431,7 @@ int main(int argc, char **argv) return EXIT_FAILURE; } - prgname = strrchr(argv[0], '/'); + prgname = last_path_separator(argv[0]); if (prgname == NULL || *++prgname == '\0') { prgname = argv[0]; } diff --git a/bacula/src/filed/backup.c b/bacula/src/filed/backup.c index d811abe5d4..eee6590ea6 100644 --- a/bacula/src/filed/backup.c +++ b/bacula/src/filed/backup.c @@ -736,8 +736,7 @@ int send_data(JCR *jcr, int stream, FF_PKT *ff_pkt, DIGEST *digest, #ifdef HAVE_LIBZ /* Do compression if turned on */ if (!sparseBlock && (ff_pkt->flags & FO_GZIP) && jcr->pZLIB_compress_workset) { - Dmsg4(400, "cbuf=0x%x len=%u rbuf=0x%x len=%u\n", cbuf, compress_len, - rbuf, sd->msglen); + Dmsg3(400, "cbuf=0x%x rbuf=0x%x len=%u\n", cbuf, rbuf, sd->msglen); ((z_stream*)jcr->pZLIB_compress_workset)->next_in = (Bytef *)rbuf; ((z_stream*)jcr->pZLIB_compress_workset)->avail_in = sd->msglen; @@ -767,16 +766,20 @@ int send_data(JCR *jcr, int stream, FF_PKT *ff_pkt, DIGEST *digest, if (!sparseBlock && (ff_pkt->flags & FO_ENCRYPT)) { uint32_t initial_len = 0; + ser_declare; if (ff_pkt->flags & FO_SPARSE) { cipher_input_len += SPARSE_FADDR_SIZE; } /* Encrypt the length of the input block */ - uint32_t packet_len = htonl(cipher_input_len); + uint8_t packet_len[sizeof(uint32_t)]; + + ser_begin(packet_len, sizeof(uint32_t)); + ser_uint32(cipher_input_len); /* store fileAddr in begin of buffer */ - if (!crypto_cipher_update(cipher_ctx, (const u_int8_t *)&packet_len, - sizeof(packet_len), (u_int8_t *)jcr->crypto_buf, &initial_len)) { + if (!crypto_cipher_update(cipher_ctx, packet_len, sizeof(packet_len), + (u_int8_t *)jcr->crypto_buf, &initial_len)) { /* Encryption failed. Shouldn't happen. */ Jmsg(jcr, M_FATAL, 0, _("Encryption error\n")); goto err; diff --git a/bacula/src/filed/job.c b/bacula/src/filed/job.c index 3182907008..14adba986e 100644 --- a/bacula/src/filed/job.c +++ b/bacula/src/filed/job.c @@ -1565,8 +1565,8 @@ static int restore_cmd(JCR *jcr) *where = 0; } /* Turn / into nothing */ - if (where[0] == '/' && where[1] == 0) { - where[0] = 0; + if (IsPathSeparator(where[0]) && where[1] == '\0') { + where[0] = '\0'; } Dmsg2(150, "Got replace %c, where=%s\n", replace, where); diff --git a/bacula/src/filed/restore.c b/bacula/src/filed/restore.c index 5f9238609f..160f854c68 100644 --- a/bacula/src/filed/restore.c +++ b/bacula/src/filed/restore.c @@ -56,10 +56,10 @@ bool flush_cipher(JCR *jcr, BFILE *bfd, int flags, CIPHER_CONTEXT *cipher, uint3 * Close a bfd check that we are at the expected file offset. * Makes some code in set_attributes(). */ -int bclose_chksize(JCR *jcr, BFILE *bfd, off_t osize) +int bclose_chksize(JCR *jcr, BFILE *bfd, boffset_t osize) { char ec1[50], ec2[50]; - off_t fsize; + boffset_t fsize; fsize = blseek(bfd, 0, SEEK_CUR); bclose(bfd); /* first close file */ @@ -808,6 +808,7 @@ int32_t extract_data(JCR *jcr, BFILE *bfd, POOLMEM *buf, int32_t buflen, if (flags & FO_ENCRYPT) { ASSERT(cipher); + unser_declare; while (jcr->crypto_size > 0 && jcr->crypto_count > 0 && wsize > 0) { uint32_t chunk_size = 16; @@ -892,7 +893,9 @@ int32_t extract_data(JCR *jcr, BFILE *bfd, POOLMEM *buf, int32_t buflen, jcr->crypto_count += decrypted_len; if (jcr->crypto_size == 0 && jcr->crypto_count >= 4) { - jcr->crypto_size = ntohl(*(uint32_t *)&jcr->crypto_buf[0]) + 4; + unser_begin(&jcr->crypto_buf[0], sizeof(uint32_t)); + unser_uint32(jcr->crypto_size); + jcr->crypto_size += 4; } if (jcr->crypto_size == 0 || jcr->crypto_count < jcr->crypto_size) { @@ -904,14 +907,14 @@ int32_t extract_data(JCR *jcr, BFILE *bfd, POOLMEM *buf, int32_t buflen, } if (flags & FO_SPARSE) { - ser_declare; + unser_declare; uint64_t faddr; char ec1[50]; - ser_begin(wbuf, SPARSE_FADDR_SIZE); + unser_begin(wbuf, SPARSE_FADDR_SIZE); unser_uint64(faddr); if (*addr != faddr) { *addr = faddr; - if (blseek(bfd, (off_t)*addr, SEEK_SET) < 0) { + if (blseek(bfd, (boffset_t)*addr, SEEK_SET) < 0) { berrno be; Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"), edit_uint64(*addr, ec1), jcr->last_fname, diff --git a/bacula/src/filed/status.c b/bacula/src/filed/status.c index af81a8d63b..85f1565b3f 100755 --- a/bacula/src/filed/status.c +++ b/bacula/src/filed/status.c @@ -117,8 +117,8 @@ void output_status(void sendit(const char *msg, int len, void *sarg), void *arg) edit_uint64_with_commas(sm_buffers, b3), edit_uint64_with_commas(sm_max_buffers, b4)); sendit(msg, len, arg); - len = Mmsg(msg, _(" Sizeof: off_t=%d size_t=%d debug=%d trace=%d\n"), - sizeof(off_t), sizeof(size_t), debug_level, get_trace()); + len = Mmsg(msg, _(" Sizeof: boffset_t=%d size_t=%d debug=%d trace=%d\n"), + sizeof(boffset_t), sizeof(size_t), debug_level, get_trace()); sendit(msg, len, arg); /* diff --git a/bacula/src/findlib/attribs.c b/bacula/src/findlib/attribs.c index ce8fc960fc..25b15180f6 100755 --- a/bacula/src/findlib/attribs.c +++ b/bacula/src/findlib/attribs.c @@ -357,7 +357,7 @@ bool set_attributes(JCR *jcr, ATTR *attr, BFILE *ofd) struct utimbuf ut; mode_t old_mask; bool ok = true; - off_t fsize; + boffset_t fsize; #if defined(HAVE_WIN32) if (attr->stream == STREAM_UNIX_ATTRIBUTES_EX && @@ -390,7 +390,7 @@ bool set_attributes(JCR *jcr, ATTR *attr, BFILE *ofd) char ec1[50], ec2[50]; fsize = blseek(ofd, 0, SEEK_END); bclose(ofd); /* first close file */ - if (attr->type == FT_REG && fsize > 0 && fsize != (off_t)attr->statp.st_size) { + if (attr->type == FT_REG && fsize > 0 && fsize != (boffset_t)attr->statp.st_size) { Jmsg3(jcr, M_ERROR, 0, _("File size of restored file %s not correct. Original %s, restored %s.\n"), attr->ofname, edit_uint64(attr->statp.st_size, ec1), edit_uint64(fsize, ec2)); diff --git a/bacula/src/findlib/bfile.c b/bacula/src/findlib/bfile.c index 8f930ed5d2..d593cd9711 100644 --- a/bacula/src/findlib/bfile.c +++ b/bacula/src/findlib/bfile.c @@ -148,7 +148,7 @@ void int32_LE2BE(int32_t* pBE, const int32_t v) bool processWin32BackupAPIBlock (BFILE *bfd, void *pBuffer, ssize_t dwSize) -{ +{ /* pByte contains the buffer dwSize the len to be processed. function assumes to be called in successive incremental order over the complete @@ -165,12 +165,11 @@ bool processWin32BackupAPIBlock (BFILE *bfd, void *pBuffer, ssize_t dwSize) */ int32_t dwSizeHeader = 20; - do { - if (pContext->liNextHeader >= dwSize) { + do { + if (pContext->liNextHeader >= dwSize) { dwDataLen = dwSize-dwDataOffset; bContinue = false; /* 1 iteration is enough */ - } - else { + } else { dwDataLen = pContext->liNextHeader-dwDataOffset; bContinue = true; /* multiple iterations may be necessary */ } @@ -179,34 +178,34 @@ bool processWin32BackupAPIBlock (BFILE *bfd, void *pBuffer, ssize_t dwSize) /* copy block of real DATA */ if (pContext->bIsInData) { if (bwrite(bfd, ((char *)pBuffer)+dwDataOffset, dwDataLen) != (ssize_t)dwDataLen) - return false; + return false; } if (pContext->liNextHeader < dwSize) {/* is a header in this block ? */ int32_t dwOffsetTarget; int32_t dwOffsetSource; - + if (pContext->liNextHeader < 0) { /* start of header was before this block, so we * continue with the part in the current block */ - dwOffsetTarget = -pContext->liNextHeader; - dwOffsetSource = 0; + dwOffsetTarget = -pContext->liNextHeader; + dwOffsetSource = 0; } else { /* start of header is inside of this block */ dwOffsetTarget = 0; - dwOffsetSource = pContext->liNextHeader; + dwOffsetSource = pContext->liNextHeader; } int32_t dwHeaderPartLen = dwSizeHeader-dwOffsetTarget; bool bHeaderIsComplete; - if (dwHeaderPartLen <= dwSize-dwOffsetSource) + if (dwHeaderPartLen <= dwSize-dwOffsetSource) { /* header (or rest of header) is completely available in current block */ bHeaderIsComplete = true; - else { + } else { /* header will continue in next block */ bHeaderIsComplete = false; dwHeaderPartLen = dwSize-dwOffsetSource; @@ -221,22 +220,21 @@ bool processWin32BackupAPIBlock (BFILE *bfd, void *pBuffer, ssize_t dwSize) int32_t dwNameSize; int32_LE2BE (&dwNameSize, pContext->header_stream.dwStreamNameSize); dwDataOffset = dwNameSize+pContext->liNextHeader+dwSizeHeader; - + /* convert stream size (64 bit little endian) to machine type */ int64_LE2BE (&(pContext->liNextHeader), pContext->header_stream.Size); pContext->liNextHeader += dwDataOffset; pContext->bIsInData = pContext->header_stream.dwStreamId == WIN32_BACKUP_DATA; if (dwDataOffset == dwSize) - bContinue = false; - } - else { + bContinue = false; + } else { /* stop and continue with next block */ bContinue = false; pContext->bIsInData = false; } - } - } while (bContinue); + } + } while (bContinue); /* set "NextHeader" relative to the beginning of the next block */ pContext->liNextHeader-= dwSize; @@ -259,7 +257,6 @@ void unix_name_to_win32(POOLMEM **win32_name, char *name); extern "C" HANDLE get_osfhandle(int fd); - void binit(BFILE *bfd) { memset(bfd, 0, sizeof(BFILE)); @@ -309,7 +306,6 @@ bool have_win32_api() } - /* * Return true if we support the stream * false if we do not support the stream @@ -385,7 +381,7 @@ int bopen(BFILE *bfd, const char *fname, int flags, mode_t mode) if (!(p_CreateFileA || p_CreateFileW)) return 0; - if (p_CreateFileW && p_MultiByteToWideChar) + if (p_CreateFileW && p_MultiByteToWideChar) make_win32_path_UTF8_2_wchar(&win32_fname_wchar, fname); if (flags & O_CREAT) { /* Create */ @@ -620,10 +616,19 @@ bool is_bopen(BFILE *bfd) return bfd->mode != BF_CLOSED; } -off_t blseek(BFILE *bfd, off_t offset, int whence) +boffset_t blseek(BFILE *bfd, boffset_t offset, int whence) { - /* ****FIXME**** this must be implemented if we want to read Win32 Archives */ - return -1; + LONG offset_low = (LONG)offset; + LONG offset_high = (LONG)(offset >> 32); + DWORD dwResult; + + dwResult = SetFilePointer(bfd->fh, offset_low, &offset_high, whence); + + if (dwResult == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) { + return (boffset_t)-1; + } + + return ((boffset_t)offset_high << 32) | dwResult; } #else /* Unix systems */ @@ -854,10 +859,10 @@ bool is_bopen(BFILE *bfd) return bfd->fid >= 0; } -off_t blseek(BFILE *bfd, off_t offset, int whence) +boffset_t blseek(BFILE *bfd, boffset_t offset, int whence) { - off_t pos; - pos = lseek(bfd->fid, offset, whence); + boffset_t pos; + pos = (boffset_t)lseek(bfd->fid, (off_t)offset, whence); bfd->berrno = errno; return pos; } diff --git a/bacula/src/findlib/bfile.h b/bacula/src/findlib/bfile.h index e6d4c34a06..6bc5ad5f21 100644 --- a/bacula/src/findlib/bfile.h +++ b/bacula/src/findlib/bfile.h @@ -133,7 +133,7 @@ int bopen_rsrc(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); +boffset_t blseek(BFILE *bfd, boffset_t offset, int whence); const char *stream_to_ascii(int stream); bool processWin32BackupAPIBlock (BFILE *bfd, void *pBuffer, ssize_t dwSize); diff --git a/bacula/src/findlib/create_file.c b/bacula/src/findlib/create_file.c index 3f845c1508..7460549b55 100644 --- a/bacula/src/findlib/create_file.c +++ b/bacula/src/findlib/create_file.c @@ -304,7 +304,9 @@ int create_file(JCR *jcr, ATTR *attr, BFILE *bfd, int replace) be.set_errno(bfd->berrno); #ifdef HAVE_WIN32 /* Check for trying to create a drive, if so, skip */ - if (attr->ofname[1] == ':' && attr->ofname[2] == '/' && attr->ofname[3] == 0) { + if (attr->ofname[1] == ':' && + IsPathSeparator(attr->ofname[2]) && + attr->ofname[3] == '\0') { return CF_SKIP; } #endif @@ -349,22 +351,22 @@ static int separate_path_and_file(JCR *jcr, char *fname, char *ofile) /* Separate pathname and filename */ for (q=p=f=ofile; *p; p++) { #ifdef HAVE_WIN32 - if (*p == '\\' || *p == '/') { + if (IsPathSeparator(*p)) { f = q; - if (p[1] == '\\' || p[1] == '/') { + if (IsPathSeparator(p[1])) { p++; } } *q++ = *p; /* copy data */ #else - if (*p == '/') { + if (IsPathSeparator(*p)) { f = q; /* possible filename */ } q++; #endif } - if (*f == '/') { + if (IsPathSeparator(*f)) { f++; } *q = 0; /* terminate string */ diff --git a/bacula/src/findlib/find.c b/bacula/src/findlib/find.c index 95ee92e149..90b66bcc94 100644 --- a/bacula/src/findlib/find.c +++ b/bacula/src/findlib/find.c @@ -200,7 +200,7 @@ static bool accept_file(FF_PKT *ff) if (ff->flags & FO_ENHANCEDWILD) { match_func = enh_fnmatch; - if ((basename = strrchr(ff->fname, '/')) != NULL) + if ((basename = last_path_separator(ff->fname)) != NULL) basename++; else basename = ff->fname; @@ -209,7 +209,7 @@ static bool accept_file(FF_PKT *ff) basename = ff->fname; } - for (j=0; jopts_list.size(); j++) { + for (j = 0; j < incexe->opts_list.size(); j++) { findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j); ff->flags = fo->flags; ff->GZIP_level = fo->GZIP_level; diff --git a/bacula/src/findlib/find_one.c b/bacula/src/findlib/find_one.c index 152cda924b..c05a489c59 100755 --- a/bacula/src/findlib/find_one.c +++ b/bacula/src/findlib/find_one.c @@ -335,7 +335,7 @@ find_one_file(JCR *jcr, FF_PKT *ff_pkt, /* This is not a link to a previously dumped file, so dump it. */ if (S_ISREG(ff_pkt->statp.st_mode)) { - off_t sizeleft; + boffset_t sizeleft; sizeleft = ff_pkt->statp.st_size; @@ -416,7 +416,7 @@ find_one_file(JCR *jcr, FF_PKT *ff_pkt, link = (char *)bmalloc(link_len + 2); bstrncpy(link, fname, link_len); /* Strip all trailing slashes */ - while (len >= 1 && link[len - 1] == '/') + while (len >= 1 && IsPathSeparator(link[len - 1])) len--; link[len++] = '/'; /* add back one */ link[len] = 0; diff --git a/bacula/src/findlib/makepath.c b/bacula/src/findlib/makepath.c index dbb873b2ba..36660a0d26 100644 --- a/bacula/src/findlib/makepath.c +++ b/bacula/src/findlib/makepath.c @@ -137,9 +137,9 @@ int isAbsolute(const char *path) { #if defined(HAVE_WIN32) - return path[1] == ':' || *path == '/' || *path == '\\'; /* drivespec:/blah is absolute */ + return path[1] == ':' || IsPathSeparator(*path); /* drivespec:/blah is absolute */ #else - return *path == '/'; + return IsPathSeparator(*path); #endif } @@ -255,31 +255,19 @@ make_path( } /* Skip over leading slashes. */ -#if defined(HAVE_WIN32) - while (*slash == '/' || *slash == '\\') - slash++; -#else - while (*slash == '/') + while (IsPathSeparator(*slash)) slash++; -#endif - while (1) { + + for ( ; ; ) { int newly_created_dir; int fail; /* slash points to the leftmost unprocessed component of dirpath. */ basename_dir = slash; - -#if defined(HAVE_WIN32) - slash = strpbrk(slash, ":/\\"); - if (slash == NULL) { - break; - } -#else - slash = strchr (slash, '/'); + slash = first_path_separator(slash); if (slash == NULL) { break; } -#endif /* If we're *not* doing chdir before each mkdir, then we have to refer to the target using the full (multi-component) directory name. */ @@ -338,13 +326,8 @@ make_path( /* Avoid unnecessary calls to `stat' when given pathnames containing multiple adjacent slashes. */ -#if defined(HAVE_WIN32) - while (*slash == '/' || *slash == '\\') - slash++; -#else - while (*slash == '/') + while (IsPathSeparator(*slash)) slash++; -#endif } /* end while (1) */ if (!cwd.do_chdir) { diff --git a/bacula/src/findlib/match.c b/bacula/src/findlib/match.c index 2e5bc26240..9b187c0f96 100644 --- a/bacula/src/findlib/match.c +++ b/bacula/src/findlib/match.c @@ -199,7 +199,7 @@ void add_fname_to_include_list(FF_PKT *ff, int prefixed, const char *fname) len = strlen(p); /* Zap trailing slashes. */ p += len - 1; - while (p > inc->fname && *p == '/') { + while (p > inc->fname && IsPathSeparator(*p)) { *p-- = 0; len--; } @@ -246,11 +246,7 @@ void add_fname_to_exclude_list(FF_PKT *ff, const char *fname) Dmsg1(20, "Add name to exclude: %s\n", fname); -#if defined(HAVE_WIN32) - if (strchr(fname, '/') || strchr(fname, '\\')) { -#else - if (strchr(fname, '/')) { -#endif + if (first_path_separator(fname) != NULL) { list = &ff->excluded_paths_list; } else { list = &ff->excluded_files_list; @@ -322,11 +318,11 @@ int file_is_included(FF_PKT *ff, const char *file) if (inc->len == len && strcmp(inc->fname, file) == 0) { return 1; } - if (inc->len < len && file[inc->len] == '/' && + if (inc->len < len && IsPathSeparator(file[inc->len]) && strncmp(inc->fname, file, inc->len) == 0) { return 1; } - if (inc->len == 1 && inc->fname[0] == '/') { + if (inc->len == 1 && IsPathSeparator(inc->fname[0])) { return 1; } } @@ -382,7 +378,7 @@ int file_is_excluded(FF_PKT *ff, const char *file) /* Try each component */ for (p = file; *p; p++) { /* Match from the beginning of a component only */ - if ((p == file || (*p != '/' && *(p-1) == '/')) + if ((p == file || (!IsPathSeparator(*p) && IsPathSeparator(p[-1]))) && file_in_excluded_list(ff->excluded_files_list, p)) { return 1; } diff --git a/bacula/src/lib/attr.c b/bacula/src/lib/attr.c index ae6ca36454..47a5ee1b9e 100644 --- a/bacula/src/lib/attr.c +++ b/bacula/src/lib/attr.c @@ -121,11 +121,11 @@ static void strip_double_slashes(char *fname) { char *p = fname; while (p && *p) { - p = strchr(p, '/'); - if (p && p[1] == '/') { - strcpy(p, p+1); - } - if (p) { + p = strpbrk(p, "/\\"); + if (p != NULL) { + if (IsPathSeparator(p[1])) { + strcpy(p, p+1); + } p++; } } @@ -162,7 +162,7 @@ void build_attr_output_fnames(JCR *jcr, ATTR *attr) #endif fn = attr->fname; /* take whole name */ /* Ensure where is terminated with a slash */ - if (jcr->where[wherelen-1] != '/' && fn[0] != '/') { + if (!IsPathSeparator(jcr->where[wherelen-1]) && !IsPathSeparator(fn[0])) { pm_strcat(attr->ofname, "/"); } pm_strcat(attr->ofname, fn); /* copy rest of name */ @@ -174,7 +174,7 @@ void build_attr_output_fnames(JCR *jcr, ATTR *attr) /* Always add prefix to hard links (FT_LNKSAVED) and * on user request to soft links */ - if (attr->lname[0] == '/' && + if (IsPathSeparator(attr->lname[0]) && (attr->type == FT_LNKSAVED || jcr->prefix_links)) { pm_strcpy(attr->olname, jcr->where); add_link = true; @@ -190,15 +190,17 @@ void build_attr_output_fnames(JCR *jcr, ATTR *attr) #endif fn = attr->lname; /* take whole name */ /* Ensure where is terminated with a slash */ - if (add_link && jcr->where[wherelen-1] != '/' && fn[0] != '/') { + if (add_link && + !IsPathSeparator(jcr->where[wherelen-1]) && + !IsPathSeparator(fn[0])) { pm_strcat(attr->olname, "/"); } pm_strcat(attr->olname, fn); /* copy rest of link */ } } #if defined(HAVE_WIN32) - strip_double_slashes(attr->ofname); - strip_double_slashes(attr->olname); + strip_double_slashes(attr->ofname); + strip_double_slashes(attr->olname); #endif } diff --git a/bacula/src/lib/enh_fnmatch.c b/bacula/src/lib/enh_fnmatch.c index 090f68efdb..b3e79b47ee 100644 --- a/bacula/src/lib/enh_fnmatch.c +++ b/bacula/src/lib/enh_fnmatch.c @@ -42,10 +42,10 @@ enh_fnmatch_sub(const char *pattern, const char *string, int patternlen, int fla case '?': if (*n == '\0') return 0; - else if ((flags & FNM_FILE_NAME) && *n == '/') + else if ((flags & FNM_FILE_NAME) && IsPathSeparator(*n)) return 0; else if ((flags & FNM_PERIOD) && *n == '.' && - (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/'))) + (n == string || ((flags & FNM_FILE_NAME) && IsPathSeparator(n[-1])))) return 0; break; @@ -65,7 +65,7 @@ enh_fnmatch_sub(const char *pattern, const char *string, int patternlen, int fla case '*': if ((flags & FNM_PERIOD) && *n == '.' && - (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/'))) + (n == string || ((flags & FNM_FILE_NAME) && IsPathSeparator(n[-1])))) return FNM_NOMATCH; if ((p - pattern) >= patternlen) @@ -73,7 +73,7 @@ enh_fnmatch_sub(const char *pattern, const char *string, int patternlen, int fla for (c = *p++; ((p - pattern) <= patternlen) && (c == '?' || c == '*'); c = *p++) { - if ((flags & FNM_FILE_NAME) && *n == '/') + if ((flags & FNM_FILE_NAME) && IsPathSeparator(*n)) /* A slash does not match a wildcard under FNM_FILE_NAME. */ return 0; else if (c == '?') @@ -109,7 +109,7 @@ enh_fnmatch_sub(const char *pattern, const char *string, int patternlen, int fla } else { - if ((flags & FNM_FILE_NAME) && *n == '/') + if ((flags & FNM_FILE_NAME) && IsPathSeparator(*n)) return 0; /* A slash does not match a wildcard under FNM_FILE_NAME. */ } @@ -179,7 +179,7 @@ enh_fnmatch_sub(const char *pattern, const char *string, int patternlen, int fla return 0; if ((flags & FNM_PERIOD) && *n == '.' && - (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/'))) + (n == string || ((flags & FNM_FILE_NAME) && IsPathSeparator(n[-1])))) return 0; nnot = (*p == '!' || *p == '^'); @@ -205,7 +205,7 @@ enh_fnmatch_sub(const char *pattern, const char *string, int patternlen, int fla c = *p++; c = FOLD (c); - if ((flags & FNM_FILE_NAME) && c == '/') + if ((flags & FNM_FILE_NAME) && IsPathSeparator(c)) /* [/] can never match. */ return 0; @@ -277,7 +277,7 @@ enh_fnmatch(const char *pattern, const char *string, int flags) if (string[matchlen] == '\0') return 0; - if ((flags & FNM_LEADING_DIR) && string[matchlen] == '/') + if ((flags & FNM_LEADING_DIR) && IsPathSeparator(string[matchlen])) /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */ return 0; diff --git a/bacula/src/lib/fnmatch.c b/bacula/src/lib/fnmatch.c index ef1fab559c..29dad5ac11 100644 --- a/bacula/src/lib/fnmatch.c +++ b/bacula/src/lib/fnmatch.c @@ -41,10 +41,10 @@ fnmatch (const char *pattern, const char *string, int flags) case '?': if (*n == '\0') return FNM_NOMATCH; - else if ((flags & FNM_FILE_NAME) && *n == '/') + else if ((flags & FNM_FILE_NAME) && IsPathSeparator(*n)) return FNM_NOMATCH; else if ((flags & FNM_PERIOD) && *n == '.' && - (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/'))) + (n == string || ((flags & FNM_FILE_NAME) && IsPathSeparator(n[-1])))) return FNM_NOMATCH; break; @@ -63,12 +63,12 @@ fnmatch (const char *pattern, const char *string, int flags) case '*': if ((flags & FNM_PERIOD) && *n == '.' && - (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/'))) + (n == string || ((flags & FNM_FILE_NAME) && IsPathSeparator(n[-1])))) return FNM_NOMATCH; for (c = *p++; c == '?' || c == '*'; c = *p++) { - if ((flags & FNM_FILE_NAME) && *n == '/') + if ((flags & FNM_FILE_NAME) && IsPathSeparator(*n)) /* A slash does not match a wildcard under FNM_FILE_NAME. */ return FNM_NOMATCH; else if (c == '?') @@ -107,7 +107,7 @@ fnmatch (const char *pattern, const char *string, int flags) return FNM_NOMATCH; if ((flags & FNM_PERIOD) && *n == '.' && - (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/'))) + (n == string || ((flags & FNM_FILE_NAME) && IsPathSeparator(n[-1])))) return FNM_NOMATCH; nnot = (*p == '!' || *p == '^'); @@ -135,7 +135,7 @@ fnmatch (const char *pattern, const char *string, int flags) c = *p++; c = FOLD (c); - if ((flags & FNM_FILE_NAME) && c == '/') + if ((flags & FNM_FILE_NAME) && IsPathSeparator(c)) /* [/] can never match. */ return FNM_NOMATCH; @@ -194,7 +194,7 @@ fnmatch (const char *pattern, const char *string, int flags) if (*n == '\0') return 0; - if ((flags & FNM_LEADING_DIR) && *n == '/') + if ((flags & FNM_LEADING_DIR) && IsPathSeparator(*n)) /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */ return 0; diff --git a/bacula/src/lib/idcache.c b/bacula/src/lib/idcache.c index 99c7362b05..d3e78f8762 100644 --- a/bacula/src/lib/idcache.c +++ b/bacula/src/lib/idcache.c @@ -37,40 +37,43 @@ static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; char *getuser(uid_t uid, char *name, int len) { - register struct userid *tail; - struct passwd *pwent; - char usernum_string[20]; - - P(mutex); - for (tail = user_alist; tail; tail = tail->next) { - if (tail->id.u == uid) { - goto uid_done; - } - } + register struct userid *tail; + char usernum_string[20]; + + P(mutex); + for (tail = user_alist; tail; tail = tail->next) { + if (tail->id.u == uid) { + goto uid_done; + } + } + + tail = (struct userid *)malloc(sizeof (struct userid)); + tail->id.u = uid; + tail->name = NULL; + +#if !defined(HAVE_WIN32) + { + struct passwd *pwent = getpwuid(uid); + + if (pwent != NULL && strcmp(pwent->pw_name, "????????") != 0) { + tail->name = bstrdup(pwent->pw_name); + } + } +#endif - pwent = getpwuid(uid); - tail = (struct userid *)malloc(sizeof (struct userid)); - tail->id.u = uid; -#ifndef HAVE_WIN32 - if (pwent == NULL || strcmp(pwent->pw_name, "????????") == 0) { + if (tail->name == NULL) { sprintf(usernum_string, "%u", (uint32_t)uid); tail->name = bstrdup(usernum_string); - } else { - tail->name = bstrdup(pwent->pw_name); - } -#else - sprintf(usernum_string, "%u", (uint32_t)uid); - tail->name = bstrdup(usernum_string); -#endif + } - /* Add to the head of the list, so most recently used is first. */ - tail->next = user_alist; - user_alist = tail; + /* Add to the head of the list, so most recently used is first. */ + tail->next = user_alist; + user_alist = tail; uid_done: - bstrncpy(name, tail->name, len); - V(mutex); - return name; + bstrncpy(name, tail->name, len); + V(mutex); + return name; } void free_getuser_cache() @@ -91,42 +94,46 @@ void free_getuser_cache() /* Translate GID to a group name or a stringified number, - with cache. */ + with cache. */ char *getgroup(gid_t gid, char *name, int len) { - register struct userid *tail; - struct group *grent; - char groupnum_string[20]; - - P(mutex); - for (tail = group_alist; tail; tail = tail->next) { - if (tail->id.g == gid) { - goto gid_done; - } - } + register struct userid *tail; + char groupnum_string[20]; + + P(mutex); + for (tail = group_alist; tail; tail = tail->next) { + if (tail->id.g == gid) { + goto gid_done; + } + } + + tail = (struct userid *)malloc(sizeof (struct userid)); + tail->id.g = gid; + tail->name = NULL; + +#if !defined(HAVE_WIN32) + { + struct group *grent = getgrgid(gid); + + if (grent != NULL && strcmp(grent->gr_name, "????????") != 0) { + tail->name = bstrdup(grent->gr_name); + } + } +#endif - grent = getgrgid(gid); - tail = (struct userid *)malloc(sizeof (struct userid)); - tail->id.g = gid; -#ifndef HAVE_WIN32 - if (grent == NULL || strcmp(grent->gr_name, "????????") == 0) { + if (tail->name == NULL) { sprintf (groupnum_string, "%u", (uint32_t)gid); tail->name = bstrdup(groupnum_string); - } else { - tail->name = bstrdup(grent->gr_name); - } -#else - sprintf (groupnum_string, "%u", (uint32_t)gid); - tail->name = bstrdup(groupnum_string); -#endif - /* Add to the head of the list, so most recently used is first. */ - tail->next = group_alist; - group_alist = tail; + } + + /* Add to the head of the list, so most recently used is first. */ + tail->next = group_alist; + group_alist = tail; gid_done: - bstrncpy(name, tail->name, len); - V(mutex); - return name; + bstrncpy(name, tail->name, len); + V(mutex); + return name; } void free_getgroup_cache() diff --git a/bacula/src/lib/message.c b/bacula/src/lib/message.c index f786d781a7..dc91de9e47 100755 --- a/bacula/src/lib/message.c +++ b/bacula/src/lib/message.c @@ -105,11 +105,11 @@ void my_name_is(int argc, char *argv[], const char *name) if (argc>0 && argv && argv[0]) { /* strip trailing filename and save exepath */ for (l=p=argv[0]; *p; p++) { - if (*p == '/') { + if (IsPathSeparator(*p)) { l = p; /* set pos of last slash */ } } - if (*l == '/') { + if (IsPathSeparator(*l)) { l++; } else { l = argv[0]; @@ -135,7 +135,7 @@ void my_name_is(int argc, char *argv[], const char *name) *q++ = *p++; } *q = 0; - if (strchr(exepath, '.') || exepath[0] != '/') { + if (strchr(exepath, '.') || !IsPathSeparator(exepath[0])) { if (getcwd(cpath, sizeof(cpath))) { free(exepath); exepath = (char *)malloc(strlen(cpath) + 1 + len); diff --git a/bacula/src/lib/parse_conf.c b/bacula/src/lib/parse_conf.c index a76aacc396..ecce0a4cfd 100755 --- a/bacula/src/lib/parse_conf.c +++ b/bacula/src/lib/parse_conf.c @@ -929,8 +929,6 @@ parse_config(const char *cf, LEX_ERROR_HANDLER *scan_error, int err_type) const char *get_default_configdir() { #if defined(HAVE_WIN32) -#define DEFAULT_CONFIGDIR "C:\\Documents and Settings\\All Users\\Application Data\\Bacula" - HRESULT hr; static char szConfigDir[MAX_PATH + 1] = { 0 }; @@ -952,15 +950,9 @@ const char *get_default_configdir() bool find_config_file(const char *config_file, char *full_path) { -#if defined(HAVE_WIN32) - if (strpbrk(config_file, ":/\\") != NULL) { - return false; - } -#else - if (strchr(config_file, '/') != NULL) { + if (first_path_separator(config_file) != NULL) { return false; } -#endif struct stat st; @@ -978,8 +970,7 @@ find_config_file(const char *config_file, char *full_path) memcpy(full_path, config_dir, dir_length + 1); - if (full_path[dir_length - 1] != '/' && - full_path[dir_length - 1] != '\\') { + if (!IsPathSeparator(full_path[dir_length - 1])) { full_path[dir_length++] = '/'; } diff --git a/bacula/src/lib/protos.h b/bacula/src/lib/protos.h index 20e11a5841..5b2d8d4e0a 100644 --- a/bacula/src/lib/protos.h +++ b/bacula/src/lib/protos.h @@ -311,8 +311,9 @@ 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); +POOLMEM * edit_job_codes (JCR *jcr, char *omsg, char *imsg, const char *to); +void set_working_directory (char *wd); +const char * last_path_separator (const char *str); /* watchdog.c */ diff --git a/bacula/src/lib/scan.c b/bacula/src/lib/scan.c index d4b4fab169..bbbe9913b3 100644 --- a/bacula/src/lib/scan.c +++ b/bacula/src/lib/scan.c @@ -79,7 +79,7 @@ void strip_trailing_slashes(char *dir) p = dir + strlen(dir) - 1; /* strip trailing slashes */ - while ((p >= dir) && (*p == '/')) + while (p >= dir && IsPathSeparator(*p)) *p-- = 0; } @@ -288,16 +288,16 @@ void split_path_and_filename(const char *fname, POOLMEM **path, int *pnl, */ f = fname + len - 1; /* "strip" any trailing slashes */ - while (slen > 1 && *f == '/') { + while (slen > 1 && IsPathSeparator(*f)) { slen--; f--; } /* Walk back to last slash -- begin of filename */ - while (slen > 0 && *f != '/') { + while (slen > 0 && !IsPathSeparator(*f)) { slen--; f--; } - if (*f == '/') { /* did we find a slash? */ + if (IsPathSeparator(*f)) { /* did we find a slash? */ f++; /* yes, point to filename */ } else { /* no, whole thing must be path name */ f = fname; diff --git a/bacula/src/lib/signal.c b/bacula/src/lib/signal.c index 50bfcc21b9..18b4e797e9 100644 --- a/bacula/src/lib/signal.c +++ b/bacula/src/lib/signal.c @@ -112,12 +112,12 @@ extern "C" void signal_handler(int sig) bstrncpy(btpath, "btraceback", sizeof(btpath)); } else { bstrncpy(btpath, exepath, sizeof(btpath)); - if (btpath[exelen-1] == '/') { + if (IsPathSeparator(btpath[exelen-1])) { btpath[exelen-1] = 0; } bstrncat(btpath, "/btraceback", sizeof(btpath)); } - if (exepath[exelen-1] != '/') { + if (!IsPathSeparator(exepath[exelen - 1])) { strcat(exepath, "/"); } strcat(exepath, exename); diff --git a/bacula/src/lib/tree.c b/bacula/src/lib/tree.c index 62b9aa154d..0d8f24db8c 100755 --- a/bacula/src/lib/tree.c +++ b/bacula/src/lib/tree.c @@ -191,7 +191,7 @@ TREE_NODE *insert_tree_node(char *path, char *fname, int type, */ if (path_len > 0) { q = path + path_len - 1; - if (*q == '/') { + if (IsPathSeparator(*q)) { *q = 0; /* strip trailing slash */ } else { q = NULL; /* no trailing slash */ @@ -201,10 +201,10 @@ TREE_NODE *insert_tree_node(char *path, char *fname, int type, } /* If no filename, strip last component of path as "filename" */ if (*fname == 0) { - p = strrchr(path, '/'); /* separate path and filename */ + p = (char *)last_path_separator(path); /* separate path and filename */ if (p) { fname = p + 1; /* set new filename */ - *p = 0; /* terminate new path */ + *p = '\0'; /* terminate new path */ } } else { p = NULL; @@ -258,7 +258,7 @@ TREE_NODE *make_tree_path(char *path, TREE_ROOT *root) Dmsg0(100, "make_tree_path: parent=*root*\n"); return (TREE_NODE *)root; } - p = strrchr(path, '/'); /* get last dir component of path */ + p = (char *)last_path_separator(path); /* get last dir component of path */ if (p) { fname = p + 1; *p = 0; /* terminate path */ @@ -332,15 +332,15 @@ int tree_getpath(TREE_NODE *node, char *buf, int buf_size) * there is only a / in the buffer, remove it since * win32 names don't generally start with / */ - if (node->type == TN_DIR_NLS && buf[0] == '/' && buf[1] == 0) { - buf[0] = 0; + if (node->type == TN_DIR_NLS && IsPathSeparator(buf[0]) && buf[1] == '\0') { + buf[0] = '\0'; } bstrncat(buf, node->fname, buf_size); /* Add a slash for all directories unless we are at the root, * also add a slash to a soft linked file if it has children * i.e. it is linked to a directory. */ - if ((node->type != TN_FILE && !(buf[0] == '/' && buf[1] == 0)) || + if ((node->type != TN_FILE && !(IsPathSeparator(buf[0]) && buf[1] == '\0')) || (node->soft_link && tree_node_has_child(node))) { bstrncat(buf, "/", buf_size); } @@ -364,7 +364,7 @@ TREE_NODE *tree_cwd(char *path, TREE_ROOT *root, TREE_NODE *node) return tree_cwd(path+3, root, parent); } } - if (path[0] == '/') { + if (IsPathSeparator(path[0])) { Dmsg0(100, "Doing absolute lookup.\n"); return tree_relcwd(path+1, root, (TREE_NODE *)root); } @@ -386,8 +386,7 @@ TREE_NODE *tree_relcwd(char *path, TREE_ROOT *root, TREE_NODE *node) return node; } /* Check the current segment only */ - p = strchr(path, '/'); - if (p) { + if ((p = first_path_separator(path)) != NULL) { len = p - path; } else { len = strlen(path); diff --git a/bacula/src/lib/util.c b/bacula/src/lib/util.c index 154965b9d5..0519cb5d53 100644 --- a/bacula/src/lib/util.c +++ b/bacula/src/lib/util.c @@ -700,3 +700,16 @@ void set_working_directory(char *wd) } working_directory = wd; /* set global */ } + +const char *last_path_separator(const char *str) +{ + if (*str != '\0') { + for (const char *p = &str[strlen(str) - 1]; p >= str; p--) { + if (IsPathSeparator(*p)) { + return p; + } + } + } + return NULL; +} + diff --git a/bacula/src/stored/bextract.c b/bacula/src/stored/bextract.c index 9b6c1213b2..428e9a04bb 100644 --- a/bacula/src/stored/bextract.c +++ b/bacula/src/stored/bextract.c @@ -339,7 +339,7 @@ static bool record_cb(DCR *dcr, DEV_RECORD *rec) unser_uint64(faddr); if (fileAddr != faddr) { fileAddr = faddr; - if (blseek(&bfd, (off_t)fileAddr, SEEK_SET) < 0) { + if (blseek(&bfd, (boffset_t)fileAddr, SEEK_SET) < 0) { berrno be; Emsg2(M_ERROR_TERM, 0, _("Seek error on %s: %s\n"), attr->ofname, be.strerror()); @@ -379,7 +379,7 @@ static bool record_cb(DCR *dcr, DEV_RECORD *rec) unser_uint64(faddr); if (fileAddr != faddr) { fileAddr = faddr; - if (blseek(&bfd, (off_t)fileAddr, SEEK_SET) < 0) { + if (blseek(&bfd, (boffset_t)fileAddr, SEEK_SET) < 0) { berrno be; Emsg3(M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"), edit_uint64(fileAddr, ec1), attr->ofname, be.strerror()); diff --git a/bacula/src/stored/block.c b/bacula/src/stored/block.c index 5e380b7a30..bb675a2008 100644 --- a/bacula/src/stored/block.c +++ b/bacula/src/stored/block.c @@ -1042,7 +1042,7 @@ reread: } } else { Dmsg0(200, "Seek to beginning of block for reread.\n"); - off_t pos = dev->lseek(dcr, (off_t)0, SEEK_CUR); /* get curr pos */ + boffset_t pos = dev->lseek(dcr, (boffset_t)0, SEEK_CUR); /* get curr pos */ pos -= block->read_len; dev->lseek(dcr, pos, SEEK_SET); dev->file_addr = pos; @@ -1114,7 +1114,7 @@ reread: Dmsg0(200, "At end of read block\n"); if (block->read_len > block->block_len && !dev->is_tape()) { char ed1[50]; - off_t pos = dev->lseek(dcr, (off_t)0, SEEK_CUR); /* get curr pos */ + boffset_t pos = dev->lseek(dcr, (boffset_t)0, SEEK_CUR); /* get curr pos */ Dmsg1(200, "Current lseek pos=%s\n", edit_int64(pos, ed1)); pos -= (block->read_len - block->block_len); dev->lseek(dcr, pos, SEEK_SET); diff --git a/bacula/src/stored/btape.c b/bacula/src/stored/btape.c index 3cd41fa656..50e30914f3 100644 --- a/bacula/src/stored/btape.c +++ b/bacula/src/stored/btape.c @@ -163,9 +163,9 @@ int main(int margc, char *margv[]) if (TAPE_BSIZE != (1 << (ffs(TAPE_BSIZE)-1))) { Emsg1(M_ABORT, 0, _("Tape block size (%d) is not a power of 2\n"), TAPE_BSIZE); } - if (sizeof(off_t) < 8) { - Pmsg1(-1, _("\n\n!!!! Warning large disk addressing disabled. off_t=%d should be 8 or more !!!!!\n\n\n"), - sizeof(off_t)); + if (sizeof(boffset_t) < 8) { + Pmsg1(-1, _("\n\n!!!! Warning large disk addressing disabled. boffset_t=%d should be 8 or more !!!!!\n\n\n"), + sizeof(boffset_t)); } x32 = 123456789; bsnprintf(buf, sizeof(buf), "%u", x32); @@ -2100,7 +2100,7 @@ static void unfillcmd() static void do_unfill() { DEV_BLOCK *block = dcr->block; - bool autochanger; + int autochanger; dumped = 0; VolBytes = 0; @@ -2143,7 +2143,7 @@ static void do_unfill() dev->offline(); } autochanger = autoload_device(dcr, 1, NULL); - if (!autochanger) { + if (autochanger != 1) { dev->close(); get_cmd(_("Mount first tape. Press enter when ready: ")); } @@ -2205,7 +2205,7 @@ static void do_unfill() set_volume_name("TestVolume2", 2); autochanger = autoload_device(dcr, 1, NULL); - if (!autochanger) { + if (autochanger != 1) { dev->close(); get_cmd(_("Mount second tape. Press enter when ready: ")); } @@ -2726,7 +2726,7 @@ bool dir_ask_sysop_to_mount_volume(DCR *dcr) bool dir_ask_sysop_to_create_appendable_volume(DCR *dcr) { - bool autochanger; + int autochanger; DEVICE *dev = dcr->dev; Dmsg0(20, "Enter dir_ask_sysop_to_create_appendable_volume\n"); if (stop == 0) { @@ -2739,7 +2739,7 @@ bool dir_ask_sysop_to_create_appendable_volume(DCR *dcr) dev->offline(); } autochanger = autoload_device(dcr, 1, NULL); - if (!autochanger) { + if (autochanger != 1) { fprintf(stderr, _("Mount blank Volume on device %s and press return when ready: "), dev->print_name()); dev->close(); diff --git a/bacula/src/stored/butil.c b/bacula/src/stored/butil.c index 43a8088a8f..f281756248 100644 --- a/bacula/src/stored/butil.c +++ b/bacula/src/stored/butil.c @@ -140,9 +140,9 @@ static DCR *setup_to_access_device(JCR *jcr, char *dev_name, /* Try stripping file part */ p = dev_name + strlen(dev_name); - while (p >= dev_name && *p != '/') + while (p >= dev_name && !IsPathSeparator(*p)) p--; - if (*p == '/') { + if (IsPathSeparator(*p)) { bstrncpy(VolName, p+1, sizeof(VolName)); *p = 0; } diff --git a/bacula/src/stored/dev.c b/bacula/src/stored/dev.c index 231f3c1566..ffb02c9641 100644 --- a/bacula/src/stored/dev.c +++ b/bacula/src/stored/dev.c @@ -447,7 +447,7 @@ void DEVICE::open_file_device(DCR *dcr, int omode) return; } - if (archive_name.c_str()[strlen(archive_name.c_str())-1] != '/') { + if (!IsPathSeparator(archive_name.c_str()[strlen(archive_name.c_str())-1])) { pm_strcat(archive_name, "/"); } pm_strcat(archive_name, VolCatInfo.VolCatName); @@ -752,7 +752,7 @@ bool DEVICE::rewind(DCR *dcr) break; } } else if (is_file() || is_dvd()) { - if (lseek(dcr, (off_t)0, SEEK_SET) < 0) { + if (lseek(dcr, (boffset_t)0, SEEK_SET) < 0) { berrno be; dev_errno = errno; Mmsg2(errmsg, _("lseek error on %s. ERR=%s.\n"), @@ -835,7 +835,7 @@ bool DEVICE::eod(DCR *dcr) struct mtop mt_com; struct mtget mt_stat; bool ok = true; - off_t pos; + boffset_t pos; if (fd < 0) { dev_errno = EBADF; @@ -859,7 +859,7 @@ bool DEVICE::eod(DCR *dcr) return true; } if (!is_tape()) { - pos = lseek(dcr, (off_t)0, SEEK_END); + pos = lseek(dcr, (boffset_t)0, SEEK_END); // Dmsg1(100, "====== Seek to %lld\n", pos); if (pos >= 0) { update_pos(dcr); @@ -985,7 +985,7 @@ bool DEVICE::eod(DCR *dcr) */ bool DEVICE::update_pos(DCR *dcr) { - off_t pos; + boffset_t pos; bool ok = true; if (!is_open()) { @@ -999,7 +999,7 @@ bool DEVICE::update_pos(DCR *dcr) if (is_file() || is_dvd()) { file = 0; file_addr = 0; - pos = lseek(dcr, (off_t)0, SEEK_CUR); + pos = lseek(dcr, (boffset_t)0, SEEK_CUR); if (pos < 0) { berrno be; dev_errno = errno; @@ -1568,9 +1568,9 @@ bool DEVICE::reposition(DCR *dcr, uint32_t rfile, uint32_t rblock) } if (!is_tape()) { - off_t pos = (((off_t)rfile)<<32) + (off_t)rblock; + boffset_t pos = (((boffset_t)rfile)<<32) | rblock; Dmsg1(100, "===== lseek to %d\n", (int)pos); - if (lseek(dcr, pos, SEEK_SET) == (off_t)-1) { + if (lseek(dcr, pos, SEEK_SET) == (boffset_t)-1) { berrno be; dev_errno = errno; Mmsg2(errmsg, _("lseek error on %s. ERR=%s.\n"), @@ -1893,14 +1893,18 @@ void DEVICE::close_part(DCR *dcr) dcr->VolCatInfo = saveVolCatInfo; /* structure assignment */ } -off_t DEVICE::lseek(DCR *dcr, off_t offset, int whence) +boffset_t DEVICE::lseek(DCR *dcr, boffset_t offset, int whence) { switch (dev_type) { case B_DVD_DEV: return lseek_dvd(dcr, offset, whence); case B_FILE_DEV: - return ::lseek(fd, offset, whence); - } +#if defined(HAVE_WIN32) + return ::_lseeki64(fd, (__int64)offset, whence); +#else + return ::lseek(fd, (off_t)offset, whence); +#endif + } return -1; } diff --git a/bacula/src/stored/dev.h b/bacula/src/stored/dev.h index dfc4cd7e46..c6ffc63e1c 100644 --- a/bacula/src/stored/dev.h +++ b/bacula/src/stored/dev.h @@ -368,7 +368,7 @@ public: bool scan_dir_for_volume(DCR *dcr); /* in scan.c */ bool reposition(DCR *dcr, uint32_t rfile, uint32_t rblock); /* in dev.c */ void clrerror(int func); /* in dev.c */ - off_t lseek(DCR *dcr, off_t offset, int whence); /* in dev.c */ + boffset_t lseek(DCR *dcr, boffset_t offset, int whence); /* in dev.c */ bool update_pos(DCR *dcr); /* in dev.c */ bool update_freespace(); /* in dvd.c */ diff --git a/bacula/src/stored/dvd.c b/bacula/src/stored/dvd.c index 5fd7ea7d75..8a45fd78c3 100644 --- a/bacula/src/stored/dvd.c +++ b/bacula/src/stored/dvd.c @@ -52,7 +52,7 @@ static void add_file_and_part_name(DEVICE *dev, POOL_MEM &archive_name) { char partnumber[20]; - if (archive_name.c_str()[strlen(archive_name.c_str())-1] != '/') { + if (!IsPathSeparator(archive_name.c_str()[strlen(archive_name.c_str())-1])) { pm_strcat(archive_name, "/"); } @@ -416,10 +416,10 @@ static bool dvd_open_first_part(DCR *dcr, int mode) /* * Do an lseek on a DVD handling all the different parts */ -off_t lseek_dvd(DCR *dcr, off_t offset, int whence) +boffset_t lseek_dvd(DCR *dcr, boffset_t offset, int whence) { DEVICE *dev = dcr->dev; - off_t pos; + boffset_t pos; char ed1[50], ed2[50]; Dmsg5(400, "Enter lseek_dvd fd=%d off=%s w=%d part=%d nparts=%d\n", dev->fd, @@ -433,7 +433,12 @@ off_t lseek_dvd(DCR *dcr, off_t offset, int whence) if ((uint64_t)offset == dev->part_start || (uint64_t)offset < dev->part_start+dev->part_size) { /* We are staying in the current part, just seek */ - if ((pos = lseek(dev->fd, offset-dev->part_start, SEEK_SET)) < 0) { +#if defined(HAVE_WIN32) + pos = _lseeki64(dev->fd, offset-dev->part_start, SEEK_SET); +#else + pos = lseek(dev->fd, offset-dev->part_start, SEEK_SET); +#endif + if (pos < 0) { return pos; } else { return pos + dev->part_start; @@ -721,7 +726,7 @@ bool check_can_write_on_non_blank_dvd(DCR *dcr) /* Found a file, checking it is empty */ POOL_MEM filename(PM_FNAME); pm_strcpy(filename, dev->device->mount_point); - if (filename.c_str()[strlen(filename.c_str())-1] != '/') { + if (!IsPathSeparator(filename.c_str()[strlen(filename.c_str())-1])) { pm_strcat(filename, "/"); } pm_strcat(filename, result->d_name); diff --git a/bacula/src/stored/protos.h b/bacula/src/stored/protos.h index e31d8f98e6..0bab20269f 100644 --- a/bacula/src/stored/protos.h +++ b/bacula/src/stored/protos.h @@ -106,7 +106,7 @@ void make_spooled_dvd_filename(DEVICE *dev, POOL_MEM &archive_name); bool truncate_dvd(DCR *dcr); bool check_can_write_on_non_blank_dvd(DCR *dcr); int find_num_dvd_parts(DCR *dcr); -off_t lseek_dvd(DCR *dcr, off_t offset, int whence); +boffset_t lseek_dvd(DCR *dcr, boffset_t offset, int whence); void dvd_remove_empty_part(DCR *dcr); /* From device.c */ diff --git a/bacula/src/stored/scan.c b/bacula/src/stored/scan.c index 2be050fa6d..80f8f76957 100644 --- a/bacula/src/stored/scan.c +++ b/bacula/src/stored/scan.c @@ -64,7 +64,7 @@ bool DEVICE::scan_dir_for_volume(DCR *dcr) len = strlen(mount_point); if (len > 0) { - need_slash = mount_point[len - 1] != '/'; + need_slash = !IsPathSeparator(mount_point[len - 1]); } entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000); for ( ;; ) { diff --git a/bacula/src/stored/spool.c b/bacula/src/stored/spool.c index a896cbae03..74c89ae9b4 100644 --- a/bacula/src/stored/spool.c +++ b/bacula/src/stored/spool.c @@ -462,7 +462,12 @@ static bool write_spool_header(DCR *dcr) if (stat != (ssize_t)sizeof(hdr)) { /* If we wrote something, truncate it, then despool */ if (stat != -1) { - if (ftruncate(dcr->spool_fd, lseek(dcr->spool_fd, (off_t)0, SEEK_CUR) - stat) != 0) { +#if defined(HAVE_WIN32) + boffset_t pos = _lseeki64(dcr->spool_fd, (__int64)0, SEEK_CUR); +#else + boffset_t pos = lseek(dcr->spool_fd, (off_t)0, SEEK_CUR); +#endif + if (ftruncate(dcr->spool_fd, pos - stat) != 0) { berrno be; Jmsg(dcr->jcr, M_FATAL, 0, _("Ftruncate spool file failed: ERR=%s\n"), be.strerror()); @@ -499,8 +504,12 @@ static bool write_spool_data(DCR *dcr) * If we wrote something, truncate it and the header, then despool */ if (stat != -1) { - if (ftruncate(dcr->spool_fd, lseek(dcr->spool_fd, (off_t)0, SEEK_CUR) - - stat - sizeof(spool_hdr)) != 0) { +#if defined(HAVE_WIN32) + boffset_t pos = _lseeki64(dcr->spool_fd, (__int64)0, SEEK_CUR); +#else + boffset_t pos = lseek(dcr->spool_fd, (off_t)0, SEEK_CUR); +#endif + if (ftruncate(dcr->spool_fd, pos - stat - sizeof(spool_hdr)) != 0) { berrno be; Jmsg(dcr->jcr, M_FATAL, 0, _("Ftruncate spool file failed: ERR=%s\n"), be.strerror()); diff --git a/bacula/src/stored/stored.c b/bacula/src/stored/stored.c index aa6c47ad07..df2648c35a 100644 --- a/bacula/src/stored/stored.c +++ b/bacula/src/stored/stored.c @@ -424,11 +424,7 @@ static void cleanup_old_files() pm_strcpy(cleanup, "/bin/rm -f "); #endif pm_strcat(cleanup, me->working_directory); - if (len > 0 && me->working_directory[len-1] != '/' -#if defined(HAVE_WIN32) - && me->working_directory[len-1] != '\\' -#endif - ) { + if (len > 0 && !IsPathSeparator(me->working_directory[len-1])) { pm_strcat(cleanup, "/"); } pm_strcat(cleanup, my_name); diff --git a/bacula/src/tools/dbcheck.c b/bacula/src/tools/dbcheck.c index d473899c36..b9e83fd7ad 100644 --- a/bacula/src/tools/dbcheck.c +++ b/bacula/src/tools/dbcheck.c @@ -1035,7 +1035,7 @@ static void repair_bad_filenames() printf("%s\n", db_strerror(db)); } /* Strip trailing slash(es) */ - for (len=strlen(name); len > 0 && name[len-1]=='/'; len--) + for (len=strlen(name); len > 0 && IsPathSeparator(name[len-1]); len--) { } if (len == 0) { len = 1; diff --git a/bacula/src/tools/testfind.c b/bacula/src/tools/testfind.c index 3b7876912f..441c1f2a87 100644 --- a/bacula/src/tools/testfind.c +++ b/bacula/src/tools/testfind.c @@ -338,11 +338,11 @@ static void count_files(FF_PKT *ar) * must be a path name (e.g. c:). */ for (p=l=ar->fname; *p; p++) { - if (*p == '/') { + if (IsPathSeparator(*p)) { l = p; /* set pos of last slash */ } } - if (*l == '/') { /* did we find a slash? */ + if (IsPathSeparator(*l)) { /* did we find a slash? */ l++; /* yes, point to filename */ } else { /* no, whole thing must be path name */ l = p;