From: Eric Bollengier Date: Mon, 20 Apr 2020 14:39:20 +0000 (+0200) Subject: BEE Backport bacula/src/findlib/bfile.c X-Git-Tag: Release-11.3.2~1835 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=25be00a0f4891cb3a785e7d3b35f61871b292b9c;p=thirdparty%2Fbacula.git BEE Backport bacula/src/findlib/bfile.c This commit is the result of the squash of the following main commits: Author: Eric Bollengier Date: Fri Sep 27 14:49:44 2019 +0200 win32: Fix "BackupWrite failed. ERR=The handle is invalid." and "ERR=The process cannot access the file because it is being used by another" with directories The PluginIO::open() call always returns io->status=0, the code cannot detect that we cannot open a file for restore. We use share flags to open directories, we cannot use the .restore hive trick with directories. Author: Eric Bollengier Date: Fri Jul 13 14:00:14 2018 +0200 Fix GCC 8 compiler warnings with memset() on objects Author: Alain Spineux Date: Tue Apr 24 18:48:25 2018 +0200 win32: remove FF_PKT->sys_fname attribute and unix_name_to_win32() function - sys_fname attribute is only used for win32 and only for debugging - f_name can be used instead - unix_name_to_win32() is only used for debugging purpose - sys_fname & unix_name_to_win32() are not appropriate anymore --- diff --git a/bacula/src/findlib/bfile.c b/bacula/src/findlib/bfile.c index 99d22e2272..549b51cd63 100644 --- a/bacula/src/findlib/bfile.c +++ b/bacula/src/findlib/bfile.c @@ -67,7 +67,7 @@ void pause_msg(const char *file, const char *func, int line, const char *msg) bool is_win32_stream(int stream) { - switch (stream) { + switch (stream & STREAMMASK_TYPE) { case STREAM_WIN32_DATA: case STREAM_WIN32_GZIP_DATA: case STREAM_WIN32_COMPRESSED_DATA: @@ -504,7 +504,6 @@ static int encrypt_bclose(BFILE *bfd) /* Windows */ int bopen(BFILE *bfd, const char *fname, uint64_t flags, mode_t mode) { - POOLMEM *win32_fname; POOLMEM *win32_fname_wchar; DWORD dwaccess, dwflags, dwshare; @@ -514,11 +513,8 @@ int bopen(BFILE *bfd, const char *fname, uint64_t flags, mode_t mode) } /* Convert to Windows path format */ - win32_fname = get_pool_memory(PM_FNAME); win32_fname_wchar = get_pool_memory(PM_FNAME); - unix_name_to_win32(&win32_fname, (char *)fname); - if (bfd->cmd_plugin && plugin_bopen) { int rtnstat; Dmsg1(50, "call plugin_bopen fname=%s\n", fname); @@ -536,10 +532,9 @@ int bopen(BFILE *bfd, const char *fname, uint64_t flags, mode_t mode) } else { bfd->mode = BF_CLOSED; bfd->fid = -1; - Dmsg1(000, "==== plugin_bopen returned bad status=%d\n", rtnstat); + Dmsg1(10, "==== plugin_bopen returned bad status=%d\n", rtnstat); } free_pool_memory(win32_fname_wchar); - free_pool_memory(win32_fname); return bfd->mode == BF_CLOSED ? -1 : 1; } Dmsg0(100, "=== NO plugin\n"); @@ -562,35 +557,32 @@ int bopen(BFILE *bfd, const char *fname, uint64_t flags, mode_t mode) dwflags = 0; } - if (p_CreateFileW && p_MultiByteToWideChar) { - // unicode open for create write - Dmsg1(100, "Create CreateFileW=%ls\n", win32_fname_wchar); - bfd->fh = p_CreateFileW((LPCWSTR)win32_fname_wchar, - dwaccess, /* Requested access */ - 0, /* Shared mode */ - NULL, /* SecurityAttributes */ - CREATE_ALWAYS, /* CreationDisposition */ - dwflags, /* Flags and attributes */ - NULL); /* TemplateFile */ - } else { - // ascii open - Dmsg1(100, "Create CreateFileA=%s\n", win32_fname); - bfd->fh = p_CreateFileA(win32_fname, - dwaccess, /* Requested access */ - 0, /* Shared mode */ - NULL, /* SecurityAttributes */ - CREATE_ALWAYS, /* CreationDisposition */ - dwflags, /* Flags and attributes */ - NULL); /* TemplateFile */ - } + // unicode open for create write + Dmsg1(100, "Create CreateFileW=%ls\n", win32_fname_wchar); + bfd->fh = p_CreateFileW((LPCWSTR)win32_fname_wchar, + dwaccess, /* Requested access */ + 0, /* Shared mode */ + NULL, /* SecurityAttributes */ + CREATE_ALWAYS, /* CreationDisposition */ + dwflags, /* Flags and attributes */ + NULL); /* TemplateFile */ bfd->mode = BF_WRITE; } else if (flags & O_WRONLY) { /* Open existing for write */ if (bfd->use_backup_api) { dwaccess = GENERIC_READ|GENERIC_WRITE|WRITE_OWNER|WRITE_DAC; - /* If deduped we do not want to open the reparse point */ - if (bfd->fattrs & FILE_ATTRIBUTE_DEDUP) { + + if (bfd->fattrs & FILE_ATTRIBUTE_DIRECTORY) { + /* Avoid issues with directories + * ERR=The process cannot access the file because it is being used by another + */ + dwshare = FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE; + } else { + dwshare = 0; + } + /* If deduped, we do not want to open the reparse point */ + if (bfd->fattrs & FILE_ATTRIBUTE_DEDUP) { /* Maybe check for bfd->reparse_point */ dwflags = FILE_FLAG_BACKUP_SEMANTICS; } else { dwflags = FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT; @@ -598,30 +590,18 @@ int bopen(BFILE *bfd, const char *fname, uint64_t flags, mode_t mode) } else { dwaccess = GENERIC_READ|GENERIC_WRITE; dwflags = 0; + dwshare = 0; } - if (p_CreateFileW && p_MultiByteToWideChar) { - // unicode open for open existing write - Dmsg1(100, "Write only CreateFileW=%s\n", win32_fname); - bfd->fh = p_CreateFileW((LPCWSTR)win32_fname_wchar, - dwaccess, /* Requested access */ - 0, /* Shared mode */ - NULL, /* SecurityAttributes */ - OPEN_EXISTING, /* CreationDisposition */ - dwflags, /* Flags and attributes */ - NULL); /* TemplateFile */ - } else { - // ascii open - Dmsg1(100, "Write only CreateFileA=%s\n", win32_fname); - bfd->fh = p_CreateFileA(win32_fname, - dwaccess, /* Requested access */ - 0, /* Shared mode */ - NULL, /* SecurityAttributes */ - OPEN_EXISTING, /* CreationDisposition */ - dwflags, /* Flags and attributes */ - NULL); /* TemplateFile */ - - } + // unicode open for open existing write + Dmsg1(100, "Write only CreateFileW=%ls\n", win32_fname_wchar); + bfd->fh = p_CreateFileW((LPCWSTR)win32_fname_wchar, + dwaccess, /* Requested access */ + dwshare, /* Shared mode */ + NULL, /* SecurityAttributes */ + OPEN_EXISTING, /* CreationDisposition */ + dwflags, /* Flags and attributes */ + NULL); /* TemplateFile */ bfd->mode = BF_WRITE; @@ -637,27 +617,15 @@ int bopen(BFILE *bfd, const char *fname, uint64_t flags, mode_t mode) dwshare = FILE_SHARE_READ|FILE_SHARE_WRITE; } - if (p_CreateFileW && p_MultiByteToWideChar) { - // unicode open for open existing read - Dmsg1(100, "Read CreateFileW=%ls\n", win32_fname_wchar); - bfd->fh = p_CreateFileW((LPCWSTR)win32_fname_wchar, - dwaccess, /* Requested access */ - dwshare, /* Share modes */ - NULL, /* SecurityAttributes */ - OPEN_EXISTING, /* CreationDisposition */ - dwflags, /* Flags and attributes */ - NULL); /* TemplateFile */ - } else { - // ascii open - Dmsg1(100, "Read CreateFileA=%s\n", win32_fname); - bfd->fh = p_CreateFileA(win32_fname, - dwaccess, /* Requested access */ - dwshare, /* Share modes */ - NULL, /* SecurityAttributes */ - OPEN_EXISTING, /* CreationDisposition */ - dwflags, /* Flags and attributes */ - NULL); /* TemplateFile */ - } + // unicode open for open existing read + Dmsg1(100, "Read CreateFileW=%ls\n", win32_fname_wchar); + bfd->fh = p_CreateFileW((LPCWSTR)win32_fname_wchar, + dwaccess, /* Requested access */ + dwshare, /* Share modes */ + NULL, /* SecurityAttributes */ + OPEN_EXISTING, /* CreationDisposition */ + dwflags, /* Flags and attributes */ + NULL); /* TemplateFile */ bfd->mode = BF_READ; } @@ -676,7 +644,6 @@ int bopen(BFILE *bfd, const char *fname, uint64_t flags, mode_t mode) bfd->lpContext = NULL; bfd->win32filter.init(); free_pool_memory(win32_fname_wchar); - free_pool_memory(win32_fname); bfd->fid = (bfd->mode == BF_CLOSED) ? -1 : 0; return bfd->mode == BF_CLOSED ? -1 : 1; } @@ -880,7 +847,7 @@ boffset_t blseek(BFILE *bfd, boffset_t offset, int whence) /* Unix */ void binit(BFILE *bfd) { - memset(bfd, 0, sizeof(BFILE)); + bmemset(bfd, 0, sizeof(BFILE)); bfd->fid = -1; }