--- parser3/src/main/pa_os.C 2017/01/29 19:41:17 1.17 +++ parser3/src/main/pa_os.C 2026/04/25 13:38:46 1.26 @@ -1,20 +1,62 @@ /** @file Parser: commonly functions. - Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com) - Author: Alexandr Petrosian (http://paf.design.ru) + Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com) + Authors: Konstantin Morshnev , Alexandr Petrosian */ #include "pa_config_includes.h" #include "pa_os.h" -volatile const char * IDENT_PA_OS_C="$Id: pa_os.C,v 1.17 2017/01/29 19:41:17 moko Exp $" IDENT_PA_OS_H; +volatile const char * IDENT_PA_OS_C="$Id: pa_os.C,v 1.26 2026/04/25 13:38:46 moko Exp $" IDENT_PA_OS_H; unsigned int pa_lock_attempts=PA_LOCK_ATTEMPTS; #ifdef _MSC_VER #include -#endif + +#define PA_SH_LOCK 2 +#define PA_EX_LOCK 1 +#define PA_ULOCK 0 +#define FLOCK(operation) int status=pa_flock(fd, operation); +#define ERRNO pa_errno() + +int pa_flock(int fd, int operation) { + HANDLE hFile = (HANDLE)_get_osfhandle(fd); + if (hFile == INVALID_HANDLE_VALUE) { + return -1; + } + + OVERLAPPED overlapped = {0}; + + if (operation == PA_ULOCK) { + return UnlockFileEx(hFile, 0, MAXDWORD, MAXDWORD, &overlapped) ? 0 : -1; + } else { + DWORD flags = LOCKFILE_FAIL_IMMEDIATELY; + if (operation == PA_EX_LOCK) { + flags |= LOCKFILE_EXCLUSIVE_LOCK; + } + return LockFileEx(hFile, flags, 0, MAXDWORD, MAXDWORD, &overlapped) ? 0 : -1; + } +} + +int pa_errno() { + switch(GetLastError()) { + case ERROR_LOCK_VIOLATION: // real case: returning the same error as with _locking + return EACCES; + case ERROR_IO_PENDING: + return EAGAIN; + case ERROR_INVALID_HANDLE: + return EBADF; + case ERROR_NOT_LOCKED: + return ENOLCK; + } + return EACCES; +} + +#else + +#define ERRNO errno #ifdef HAVE_FLOCK @@ -24,14 +66,6 @@ unsigned int pa_lock_attempts=PA_LOCK_AT #define FLOCK(operation) int status=flock(fd, operation); #else -#ifdef HAVE__LOCKING - -#define PA_SH_LOCK _LK_NBLCK -#define PA_EX_LOCK _LK_NBLCK -#define PA_ULOCK _LK_UNLCK -#define FLOCK(operation) lseek(fd, 0, SEEK_SET); int status=_locking(fd, operation, 1); - -#else #ifdef HAVE_FCNTL #define PA_SH_LOCK F_RDLCK @@ -54,6 +88,7 @@ unsigned int pa_lock_attempts=PA_LOCK_AT #endif #endif #endif + #endif int pa_lock(int fd, int attempts, int operation){ @@ -62,7 +97,7 @@ int pa_lock(int fd, int attempts, int op if(status==0) return 0; if(--attempts<=0) - return errno; + return ERRNO; pa_sleep(PA_LOCK_WAIT_TIMEOUT_SECS, PA_LOCK_WAIT_TIMEOUT_USECS); } };