diff options
author | Marko Mäkelä <marko.makela@mariadb.com> | 2022-12-13 14:39:18 +0200 |
---|---|---|
committer | Marko Mäkelä <marko.makela@mariadb.com> | 2022-12-13 14:39:18 +0200 |
commit | 1dc2f35598193fc52b79061c286b61f01c617374 (patch) | |
tree | 30e3972564ec174de0432e429f5f0897bf0b41a7 /mysys/my_winfile.c | |
parent | da5d3499353036d39c3a4bcd1f0466f7de3fc263 (diff) | |
parent | fdf43b5c78c4aeb26efdbef3172746e007ab6f1d (diff) | |
download | mariadb-git-1dc2f35598193fc52b79061c286b61f01c617374.tar.gz |
Merge 10.4 into 10.5
Diffstat (limited to 'mysys/my_winfile.c')
-rw-r--r-- | mysys/my_winfile.c | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/mysys/my_winfile.c b/mysys/my_winfile.c index 2c02e4285e6..b3840affb2d 100644 --- a/mysys/my_winfile.c +++ b/mysys/my_winfile.c @@ -102,6 +102,42 @@ static int my_get_open_flags(File fd) DBUG_RETURN(my_file_info[fd].oflag); } +/* + CreateFile with retry logic. + + Uses retries, to avoid or reduce CreateFile errors + with ERROR_SHARING_VIOLATION, in case the file is opened + by another process, which used incompatible sharing + flags when opening. + + See Windows' CreateFile() documentation for details. +*/ +static HANDLE my_create_file_with_retries( + LPCSTR lpFileName, DWORD dwDesiredAccess, + DWORD dwShareMode, + LPSECURITY_ATTRIBUTES lpSecurityAttributes, + DWORD dwCreationDisposition, + DWORD dwFlagsAndAttributes, + HANDLE hTemplateFile) +{ + int retries; + DBUG_INJECT_FILE_SHARING_VIOLATION(lpFileName); + + for (retries = FILE_SHARING_VIOLATION_RETRIES;;) + { + HANDLE h= CreateFile(lpFileName, dwDesiredAccess, dwShareMode, + lpSecurityAttributes, dwCreationDisposition, + dwFlagsAndAttributes, hTemplateFile); + DBUG_CLEAR_FILE_SHARING_VIOLATION(); + + if (h != INVALID_HANDLE_VALUE || + GetLastError() != ERROR_SHARING_VIOLATION || --retries == 0) + return h; + + Sleep(FILE_SHARING_VIOLATION_DELAY_MS); + } + return INVALID_HANDLE_VALUE; +} /* Open a file with sharing. Similar to _sopen() from libc, but allows managing @@ -247,7 +283,7 @@ File my_win_sopen(const char *path, int oflag, int shflag, int pmode) fileattrib|= FILE_FLAG_RANDOM_ACCESS; /* try to open/create the file */ - if ((osfh= CreateFile(path, fileaccess, fileshare, &SecurityAttributes, + if ((osfh= my_create_file_with_retries(path, fileaccess, fileshare, &SecurityAttributes, filecreate, fileattrib, NULL)) == INVALID_HANDLE_VALUE) { DWORD last_error= GetLastError(); |