int flock(int fd, int op) { HANDLE hdl = (HANDLE) _get_osfhandle(fd); DWORD low = 1, high = 0; OVERLAPPED offset = {0, 0, 0, 0, NULL}; if (hdl < 0) return -1; /* error in file descriptor */ /* bug for bug compatible with Unix */ UnlockFileEx(hdl, 0, low, high, &offset); switch (op & ~LOCK_NB) { /* translate to LockFileEx() op */ case LOCK_EX: /* exclusive */ if (LockFileEx(hdl, LOCKFILE_EXCLUSIVE_LOCK + ((op & LOCK_NB) ? LOCKFILE_FAIL_IMMEDIATELY : 0), 0, low, high, &offset)) return 0; break; case LOCK_SH: /* shared */ if (LockFileEx(hdl, ((op & LOCK_NB) ? LOCKFILE_FAIL_IMMEDIATELY : 0), 0, low, high, &offset)) return 0; break; case LOCK_UN: /* unlock */ return 0; /* always succeeds */ default: /* default */ break; } errno = EINVAL; /* bad call */ return -1; }
int flock(int fd, int oper) { OVERLAPPED o; int i = -1; HANDLE fh; fh = (HANDLE)_get_osfhandle(fd); memset(&o, 0, sizeof(o)); switch(oper) { case LOCK_SH: /* shared lock */ LK_ERR(LockFileEx(fh, 0, 0, LK_LEN, 0, &o),i); break; case LOCK_EX: /* exclusive lock */ LK_ERR(LockFileEx(fh, LOCKFILE_EXCLUSIVE_LOCK, 0, LK_LEN, 0, &o),i); break; case LOCK_SH|LOCK_NB: /* non-blocking shared lock */ LK_ERR(LockFileEx(fh, LOCKFILE_FAIL_IMMEDIATELY, 0, LK_LEN, 0, &o),i); break; case LOCK_EX|LOCK_NB: /* non-blocking exclusive lock */ LK_ERR(LockFileEx(fh,LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,0, LK_LEN, 0, &o),i); break; case LOCK_UN: /* unlock lock */ LK_ERR(UnlockFileEx(fh, 0, LK_LEN, 0, &o),i); break; default: /* unknown */ //errno = EINVAL; // i heard that on some versions errno is a function (win32 MT lib?) break; } return i; }
/* use LockFileEx/UnlockFileEx */ int acl_myflock(ACL_FILE_HANDLE fd, int lock_style acl_unused, int operation) { const char *myname = "acl_myflock"; DWORD size = 0xFFFFFFFF; char ebuf[256]; unsigned char lock_op; OVERLAPPED ovlp; DWORD flags; if ((operation & (ACL_FLOCK_OP_BITS)) != operation) acl_msg_panic("myflock: improper operation type: 0x%x", operation); memset(&ovlp, 0, sizeof(ovlp)); ovlp.Offset = 0; lock_op = operation & ~ACL_FLOCK_OP_NOWAIT; if (lock_op == ACL_FLOCK_OP_NONE) { if(UnlockFileEx(fd, 0, 1, 0, &ovlp)) return (0); acl_msg_error("%s(%d): unlock error(%s)", myname, __LINE__, acl_last_strerror(ebuf,sizeof(ebuf))); return (-1); } else if (lock_op == ACL_FLOCK_OP_SHARED) { while (1) { flags = 0; if ((operation & ACL_FLOCK_OP_NOWAIT)) flags |= LOCKFILE_FAIL_IMMEDIATELY; if(LockFileEx(fd, flags, 0, 1, 0, &ovlp)) return (0); if ((operation & ACL_FLOCK_OP_NOWAIT)) { acl_msg_error("%s(%d): lock error(%s)", myname, __LINE__, acl_last_strerror(ebuf,sizeof(ebuf))); return (-1); } sleep(1); } } else if (lock_op == ACL_FLOCK_OP_EXCLUSIVE) { while (1) { flags = LOCKFILE_EXCLUSIVE_LOCK; if ((operation & ACL_FLOCK_OP_NOWAIT)) flags |= LOCKFILE_FAIL_IMMEDIATELY; if(LockFileEx(fd, flags, 0, 1, 0, &ovlp)) return (0); if ((operation & ACL_FLOCK_OP_NOWAIT)) { acl_msg_error("%s(%d): lock error(%s)", myname, __LINE__, acl_last_strerror(ebuf,sizeof(ebuf))); return (-1); } sleep(1); } } acl_msg_error("%s(%d): invalid lock_op(%d)", myname, __LINE__, lock_op); return (-1); }
/* Locks a file. */ static MVMint64 lock(MVMThreadContext *tc, MVMOSHandle *h, MVMint64 flag) { MVMIOFileData *data = (MVMIOFileData *)h->body.data; #ifdef _WIN32 const DWORD len = 0xffffffff; const HANDLE hf = (HANDLE)_get_osfhandle(data->fd); OVERLAPPED offset; if (hf == INVALID_HANDLE_VALUE) { MVM_exception_throw_adhoc(tc, "Failed to lock filehandle: bad file descriptor"); } flag = ((flag & MVM_FILE_FLOCK_NONBLOCK) ? LOCKFILE_FAIL_IMMEDIATELY : 0) + ((flag & MVM_FILE_FLOCK_TYPEMASK) == MVM_FILE_FLOCK_SHARED ? 0 : LOCKFILE_EXCLUSIVE_LOCK); memset (&offset, 0, sizeof(offset)); MVM_gc_mark_thread_blocked(tc); if (LockFileEx(hf, flag, 0, len, len, &offset)) { MVM_gc_mark_thread_unblocked(tc); return 1; } MVM_gc_mark_thread_unblocked(tc); MVM_exception_throw_adhoc(tc, "Failed to lock filehandle: %d", GetLastError()); return 0; #else struct flock l; ssize_t r; int fc; const int fd = data->fd; l.l_whence = SEEK_SET; l.l_start = 0; l.l_len = 0; if ((flag & MVM_FILE_FLOCK_TYPEMASK) == MVM_FILE_FLOCK_SHARED) l.l_type = F_RDLCK; else l.l_type = F_WRLCK; fc = (flag & MVM_FILE_FLOCK_NONBLOCK) ? F_SETLK : F_SETLKW; do { MVM_gc_mark_thread_blocked(tc); r = fcntl(fd, fc, &l); MVM_gc_mark_thread_unblocked(tc); } while (r == -1 && errno == EINTR); if (r == -1) { MVM_exception_throw_adhoc(tc, "Failed to lock filehandle: %d", errno); } return 1; #endif }
static_inline int _VLockFd(FD_t handle, afs_uint32 offset, int locktype, int nonblock) { DWORD flags = 0; OVERLAPPED lap; if (locktype == WRITE_LOCK) { flags |= LOCKFILE_EXCLUSIVE_LOCK; } if (nonblock) { flags |= LOCKFILE_FAIL_IMMEDIATELY; } memset(&lap, 0, sizeof(lap)); lap.Offset = offset; if (!LockFileEx(handle, flags, 0, 1, 0, &lap)) { if (GetLastError() == ERROR_LOCK_VIOLATION) { return EBUSY; } return EIO; } return 0; }
void FileStream::lock(bool nb) { if(locked) return; #ifdef WIN32 OVERLAPPED olp; memset(&olp, 0, sizeof(OVERLAPPED)); olp.Offset = ~(DWORD)0; olp.OffsetHigh = ~(DWORD)0; if(!LockFileEx((HANDLE)_get_osfhandle(fileno(fp)), LOCKFILE_EXCLUSIVE_LOCK | (nb ? LOCKFILE_FAIL_IMMEDIATELY : 0), 0, 1, 0, &olp)) { const uint32 ec = GetLastError(); throw MDFN_Error((ec == ERROR_LOCK_VIOLATION) ? EWOULDBLOCK : 0, _("Error locking opened file \"%s\": %s"), path_save.c_str(), Win32Common::ErrCodeToString(ec).c_str()); } #else if(flock(fileno(fp), LOCK_EX | (nb ? LOCK_NB : 0)) == -1) { ErrnoHolder ene(errno); throw MDFN_Error(ene.Errno(), _("Error locking opened file \"%s\": %s"), path_save.c_str(), ene.StrError()); } #endif locked = true; }
AutoFileLock::AutoFileLock(const char* fileName) { // Repeat createFile, if there is a sharing violation. do { _hFile = CreateFile (fileName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); }while ((GetLastError() == ERROR_SHARING_VIOLATION)); // If this conditon succeeds, There is an error opening the file. Hence // returning from here as Lock can not be acquired. if((_hFile == INVALID_HANDLE_VALUE) && (GetLastError() != ERROR_ALREADY_EXISTS) && (GetLastError() != ERROR_SUCCESS)) { PEG_TRACE((TRC_DISCARDED_DATA, Tracer::LEVEL1, "AutoFileLock: Failed to open lock file '%s', error code %d.", fileName, GetLastError())); return; } OVERLAPPED l={0,0,0,0,0}; if(LockFileEx(_hFile,LOCKFILE_EXCLUSIVE_LOCK, 0, 0, 0, &l) == 0) { PEG_TRACE((TRC_DISCARDED_DATA, Tracer::LEVEL1, "AutoFileLock: Failed to Acquire lock on file %s, error code %d.", fileName, GetLastError())); CloseHandle(_hFile); _hFile = INVALID_HANDLE_VALUE; } }
void CJaxerLog::LogStr(eLogLevel eLevel, const char* pszStr, int len) { if (eLevel < m_eDefault) return; if (!m_fp) return; time_t rawtime; struct tm * timeinfo; char szTimeBuf[0x64]; time ( &rawtime ); timeinfo = localtime ( &rawtime ); sprintf(szTimeBuf, "%02d:%02d:%02d %02d/%02d/%04d [%s] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, timeinfo->tm_mon+1, timeinfo->tm_mday, timeinfo->tm_year+1900, sLogLevel[eLevel]); OVERLAPPED ovlp; memset(&ovlp, 0, sizeof(ovlp)); LockFileEx(m_fp, LOCKFILE_EXCLUSIVE_LOCK, 0, 1, 0, &ovlp); DWORD nBytes; SetFilePointer(m_fp, 0, 0, FILE_END); WriteFile(m_fp, szTimeBuf, (DWORD)strlen(szTimeBuf), &nBytes, NULL); WriteFile(m_fp, pszStr, (len==-1)? (DWORD)strlen(pszStr) : len, &nBytes, NULL); WriteFile(m_fp, "\r\n", 2, &nBytes, NULL); UnlockFileEx(m_fp, 0, 1, 0, &ovlp); }
RTR3DECL(int) RTFileLock(RTFILE hFile, unsigned fLock, int64_t offLock, uint64_t cbLock) { Assert(offLock >= 0); /* Check arguments. */ if (fLock & ~RTFILE_LOCK_MASK) { AssertMsgFailed(("Invalid fLock=%08X\n", fLock)); return VERR_INVALID_PARAMETER; } /* Prepare flags. */ Assert(RTFILE_LOCK_WRITE); DWORD dwFlags = (fLock & RTFILE_LOCK_WRITE) ? LOCKFILE_EXCLUSIVE_LOCK : 0; Assert(RTFILE_LOCK_WAIT); if (!(fLock & RTFILE_LOCK_WAIT)) dwFlags |= LOCKFILE_FAIL_IMMEDIATELY; /* Windows structure. */ OVERLAPPED Overlapped; memset(&Overlapped, 0, sizeof(Overlapped)); Overlapped.Offset = LOW_DWORD(offLock); Overlapped.OffsetHigh = HIGH_DWORD(offLock); /* Note: according to Microsoft, LockFileEx API call is available starting from NT 3.5 */ if (LockFileEx((HANDLE)RTFileToNative(hFile), dwFlags, 0, LOW_DWORD(cbLock), HIGH_DWORD(cbLock), &Overlapped)) return VINF_SUCCESS; return RTErrConvertFromWin32(GetLastError()); }
static int file_lock(lua_State *L, FILE *f, const char *mode, long offset, long length) { static const ULARGE_INTEGER zero_len; static const OVERLAPPED zero_ov; HANDLE h = file_handle(f); ULARGE_INTEGER len = zero_len; OVERLAPPED ov = zero_ov; DWORD flags = 0; BOOL ret; if (length) len.LowPart = length; else len.LowPart = len.HighPart = -1; ov.Offset = offset; switch (*mode) { case 'w': flags = LOCKFILE_EXCLUSIVE_LOCK; /*FALLTHRU*/ case 'r': flags |= LOCKFILE_FAIL_IMMEDIATELY; ret = LockFileEx(h, flags, 0, len.LowPart, len.HighPart, &ov); break; case 'u': ret = UnlockFileEx(h, 0, len.LowPart, len.HighPart, &ov); break; default: return luaL_error(L, "invalid mode"); } if (!ret) return push_error(L); /* return the file */ lua_settop(L, 1); return 1; }
static int lock_out(HANDLE hFile , int op) { DWORD dwNumBytesWritten; BOOL fSuccess; OVERLAPPED sOverlapped; char buf[PBS_MAXHOSTNAME + 10]; if (op == F_WRLCK) { sOverlapped.Offset = 0; sOverlapped.OffsetHigh = 100; sOverlapped.hEvent = 0; fSuccess = LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0, 0, 1000, &sOverlapped); if (fSuccess) { /* if write-lock, record hostname and pid in file */ (void) sprintf(buf, "%s:%d\n", thishost, getpid()); fSuccess = WriteFile(hFile, buf, strlen(buf), &dwNumBytesWritten, NULL); return 0; } } else { /* unlock and return */ fSuccess = UnlockFileEx(hFile, 0, 0, 1000, &sOverlapped); if (fSuccess) return 0; } return 1; }
int pgut_flock(int fd, int operation) { BOOL ret; HANDLE handle = (HANDLE) _get_osfhandle(fd); DWORD lo = 0; DWORD hi = 0; if (operation & LOCK_UN) { ret = UnlockFileEx(handle, 0, lo, hi, NULL); } else { DWORD flags = 0; if (operation & LOCK_EX) flags |= LOCKFILE_EXCLUSIVE_LOCK; if (operation & LOCK_NB) flags |= LOCKFILE_FAIL_IMMEDIATELY; ret = LockFileEx(handle, flags, 0, lo, hi, NULL); } if (!ret) { _dosmaperr(GetLastError()); return -1; } return 0; }
int flock (int fd,int op) { HANDLE hdl = (HANDLE) _get_osfhandle (fd); DWORD flags = (op & LOCK_NB) ? LOCKFILE_FAIL_IMMEDIATELY : 0; OVERLAPPED offset = {NIL,NIL,0,0,NIL}; int ret = -1; blocknotify_t bn = (blocknotify_t) ((op & LOCK_NB) ? NIL : mail_parameters (NIL,GET_BLOCKNOTIFY,NIL)); if (hdl < 0) errno = EBADF; /* error in file descriptor */ else switch (op & ~LOCK_NB) { /* translate to LockFileEx() op */ case LOCK_EX: /* exclusive */ flags |= LOCKFILE_EXCLUSIVE_LOCK; case LOCK_SH: /* shared */ if (!check_nt ()) return 0; /* always succeeds if not NT */ if (bn) (*bn) (BLOCK_FILELOCK,NIL); /* bug for bug compatible with Unix */ UnlockFileEx (hdl,NIL,1,0,&offset); /* lock the file as requested */ if (LockFileEx (hdl,flags,NIL,1,0,&offset)) ret = 0; if (bn) (*bn) (BLOCK_NONE,NIL); /* if failed */ if (ret) errno = (op & LOCK_NB) ? EAGAIN : EBADF; break; case LOCK_UN: /* unlock */ if (check_nt ()) UnlockFileEx (hdl,NIL,1,0,&offset); ret = 0; /* always succeeds */ default: /* default */ errno = EINVAL; /* bad call */ break; } return ret; }
static FILE *diskcache_openLockedFile(const char *path, int write) { FILE *f; if (!file_doesFileExist(path)) { // if we don't want to write, this means failure. if (!write) { return 0; } // create file. f = fopen(path, "wb"); if (!f) { return 0; } } else { if (write) { // open up file in append mode for writing f = fopen(path, "ab"); } else { // .. otherwise in read mode f = fopen(path, "rb"); } if (!f) { return 0; } } #ifdef UNIX #ifndef MAC // for non-Mac OS X Unix (Linux/BSD), we use flock int operation = LOCK_SH; if (write) { operation = LOCK_EX; } if (flock(fileno(f), operation) == 0) { return f; } fclose(f); return 0; #else // for Mac OS X, we currently use nothing! return f; #endif #else // on windows, we use LockFileEx HANDLE h = (HANDLE)_get_osfhandle(fileno(f)); int flags = 0; if (write) { flags |= LOCKFILE_EXCLUSIVE_LOCK; } OVERLAPPED ov; memset(&ov, 0, sizeof(ov)); if (LockFileEx(h, flags, 0, 0, 0, &ov)) { _close((int)h); return f; } _close((int)h); fclose(f); return 0; #endif }
bool tr_sys_file_lock(tr_sys_file_t handle, int operation, tr_error** error) { TR_ASSERT(handle != TR_BAD_SYS_FILE); TR_ASSERT((operation & ~(TR_SYS_FILE_LOCK_SH | TR_SYS_FILE_LOCK_EX | TR_SYS_FILE_LOCK_NB | TR_SYS_FILE_LOCK_UN)) == 0); TR_ASSERT(!!(operation & TR_SYS_FILE_LOCK_SH) + !!(operation & TR_SYS_FILE_LOCK_EX) + !!(operation & TR_SYS_FILE_LOCK_UN) == 1); bool ret; OVERLAPPED overlapped = { .Pointer = 0, .hEvent = NULL }; if ((operation & TR_SYS_FILE_LOCK_UN) == 0) { DWORD native_flags = 0; if ((operation & TR_SYS_FILE_LOCK_EX) != 0) { native_flags |= LOCKFILE_EXCLUSIVE_LOCK; } if ((operation & TR_SYS_FILE_LOCK_NB) != 0) { native_flags |= LOCKFILE_FAIL_IMMEDIATELY; } ret = LockFileEx(handle, native_flags, 0, MAXDWORD, MAXDWORD, &overlapped) != FALSE; } else { ret = UnlockFileEx(handle, 0, MAXDWORD, MAXDWORD, &overlapped) != FALSE; } if (!ret) { set_system_error(error, GetLastError()); } return ret; } char* tr_sys_dir_get_current(tr_error** error) { char* ret = NULL; wchar_t* wide_ret = NULL; DWORD size; size = GetCurrentDirectoryW(0, NULL); if (size != 0) { wide_ret = tr_new(wchar_t, size); if (GetCurrentDirectoryW(size, wide_ret) != 0) { ret = tr_win32_native_to_utf8(wide_ret, size); } }
bool Log::LogFile::lock(void) { OVERLAPPED ov; bool ret; memset(&ov, 0, sizeof (ov)); ret = LockFileEx(fd, LOCKFILE_EXCLUSIVE_LOCK, 0, 1, 0, &ov) == TRUE; SetFilePointer((HANDLE)fd, 0, 0, FILE_END); return ret; }
static int flock(int fd, int operation) { OVERLAPPED ov; HANDLE h = (HANDLE)_get_osfhandle(fd); DWORD flags; flags = ((operation & LOCK_NB) ? LOCKFILE_FAIL_IMMEDIATELY : 0) | ((operation & LOCK_SH) ? LOCKFILE_EXCLUSIVE_LOCK : 0); memset(&ov, 0, sizeof(ov)); return LockFileEx(h, flags, 0, 0xffffffff, 0xffffffff, &ov) ? 0 : -1; }
static __forceinline void doLockFile(HANDLE &fileHandle, const QString &filePath, QFile *const outFile) { bool success = false; fileHandle = INVALID_HANDLE_VALUE; //Try to open the file! for(int i = 0; i < 64; i++) { const HANDLE hTemp = CreateFileW(MUTILS_WCHR(QDir::toNativeSeparators(filePath)), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL); if(VALID_HANDLE(hTemp)) { PROTECT_HANDLE(fileHandle = hTemp, true); break; /*file opened successfully*/ } if(i == 0) { qWarning("Failed to open file on first attemp, retrying..."); } Sleep(1); } //Now try to actually lock the file! if (VALID_HANDLE(fileHandle)) { for (int i = 0; i < 64; i++) { LARGE_INTEGER fileSize; if (GetFileSizeEx(fileHandle, &fileSize)) { OVERLAPPED overlapped = { 0U, 0U, 0U, 0U, 0U }; if (LockFileEx(fileHandle, LOCKFILE_FAIL_IMMEDIATELY, 0, fileSize.LowPart, fileSize.HighPart, &overlapped)) { success = true; break; /*file locked successfully*/ } Sleep(1); } if (i == 0) { qWarning("Failed to lock file on first attemp, retrying..."); } } } //Locked successfully? if(!success) { CLOSE_HANDLE(fileHandle); if(outFile) { QFile::remove(QFileInfo(*outFile).canonicalFilePath()); } MUTILS_THROW_FMT("File '%s' could not be locked!", MUTILS_UTF8(QFileInfo(filePath).fileName())); } }
int tup_flock(tup_lock_t fd) { OVERLAPPED wtf; memset(&wtf, 0, sizeof(wtf)); if(LockFileEx(fd, LOCKFILE_EXCLUSIVE_LOCK, 0, 1, 0, &wtf) == 0) { errno = EIO; return -1; } return 0; }
/* Windows specific code of make_dotlock. Returns 0 on success and -1 on error. */ static int dotlock_take_w32 (dotlock_t h, long timeout) { int wtime = 0; int w32err; OVERLAPPED ovl; again: /* Lock one byte at offset 0. The offset is given by OVL. */ memset (&ovl, 0, sizeof ovl); if (LockFileEx (h->lockhd, (LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY), 0, 1, 0, &ovl)) { h->locked = 1; return 0; /* okay */ } w32err = GetLastError (); if (w32err != ERROR_LOCK_VIOLATION) { my_error_2 (_("lock `%s' not made: %s\n"), h->lockname, w32_strerror (w32err)); return -1; } if (timeout) { /* Wait until lock has been released. We use retry intervals of 50ms, 100ms, 200ms, 400ms, 800ms, 2s, 4s and 8s. */ if (!wtime) wtime = 50; else if (wtime < 800) wtime *= 2; else if (wtime == 800) wtime = 2000; else if (wtime < 8000) wtime *= 2; if (timeout > 0) { if (wtime > timeout) wtime = timeout; timeout -= wtime; } if (wtime >= 800) my_info_1 (_("waiting for lock %s...\n"), h->lockname); Sleep (wtime); goto again; } return -1; }
static inline int dolock(xc_fcntl_mutex_t *fcntl_mutex, int type) { static OVERLAPPED offset = {0, 0, 0, 0, NULL}; if (type == LCK_UN) { return UnlockFileEx(fcntl_mutex->fd, 0, 1, 0, &offset); } else { return LockFileEx(fcntl_mutex->fd, type, 0, 1, 0, &offset); } }
static int zend_file_cache_flock(int fd, int op) { OVERLAPPED offset = {0,0,0,0,NULL}; if (op == LOCK_EX) { if (LockFileEx((HANDLE)_get_osfhandle(fd), LOCKFILE_EXCLUSIVE_LOCK, 0, 1, 0, &offset) == TRUE) { return 0; } } else if (op == LOCK_SH) { if (LockFileEx((HANDLE)_get_osfhandle(fd), 0, 0, 1, 0, &offset) == TRUE) { return 0; } } else if (op == LOCK_UN) { if (UnlockFileEx((HANDLE)_get_osfhandle(fd), 0, 1, 0, &offset) == TRUE) { return 0; } } return -1; }
axStatus axFile::_os_lock( DWORD flags ) { if( ! isValid() ) { assert(false); return axStatus_Std::not_initialized; } OVERLAPPED offset; ax_memset( offset, 0 ); const DWORD len = 0xffffffff; if ( ! LockFileEx( h_, flags, 0, len, len, &offset ) ) { return axStatus_Std::File_lock_error; } return 0; }
int _qdbm_win32_fcntl(int fd, int cmd, struct flock *lock){ HANDLE fh; OVERLAPPED ol; fh = (HANDLE)_get_osfhandle(fd); memset(&ol, 0, sizeof(OVERLAPPED)); ol.Offset = 0; ol.OffsetHigh = 0; ol.hEvent = 0; if(!LockFileEx(fh, lock->l_type == F_WRLCK ? LOCKFILE_EXCLUSIVE_LOCK : 0, 0, 0, 1, &ol)) return -1; return 0; }
bool FileLock::TryLock() { if (hFile == INVALID_HANDLE_VALUE) { return false; } _OVERLAPPED overlapped = {0}; if (!LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0, 0, 0, &overlapped)) { reason = GetErrorReason(); return false; } return true; }
static int alock_test_lock ( int fd, int slot ) { int res; #if defined( HAVE_LOCKF ) res = lseek (fd, (off_t) (ALOCK_SLOT_SIZE * slot), SEEK_SET); if (res == -1) return -1; res = lockf (fd, F_TEST, (off_t) ALOCK_SLOT_SIZE); if (res == -1) { if (errno == EACCES || errno == EAGAIN) { return ALOCK_LOCKED; } else { return -1; } } #elif defined( HAVE_FCNTL ) struct flock lock_info; (void) memset ((void *) &lock_info, 0, sizeof (struct flock)); lock_info.l_type = F_WRLCK; lock_info.l_whence = SEEK_SET; lock_info.l_start = (off_t) (ALOCK_SLOT_SIZE * slot); lock_info.l_len = (off_t) ALOCK_SLOT_SIZE; res = fcntl (fd, F_GETLK, &lock_info); if (res == -1) return -1; if (lock_info.l_type != F_UNLCK) return ALOCK_LOCKED; #elif defined( _WIN32 ) OVERLAPPED ov; HANDLE hh = _get_osfhandle ( fd ); ov.hEvent = 0; ov.Offset = ALOCK_SLOT_SIZE*slot; ov.OffsetHigh = 0; if( !LockFileEx( hh, LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY, 0, ALOCK_SLOT_SIZE, 0, &ov )) { int err = GetLastError(); if ( err == ERROR_LOCK_VIOLATION ) return ALOCK_LOCKED; else return -1; } #else # error alock needs lockf, fcntl, or LockFile #endif return 0; }
bool CDiskFile::Lock(uint32 nLock, int32 nSize) { if(m_hHandle == (ulong)(-1)) return false; #if defined(WINDOWS) BOOL bRet; switch(nLock) { default: return false; case FOCP_FILE_LOCK: bRet = LockFile((HANDLE)m_hHandle, GetPosition(), 0, nSize, 0); break; case FOCP_FILE_TLOCK: bRet = LockFileEx((HANDLE)m_hHandle, LOCKFILE_FAIL_IMMEDIATELY, 0, nSize, 0, NULL); break; case FOCP_FILE_ULOCK: bRet = UnlockFile((HANDLE)m_hHandle, GetPosition(), 0, nSize, 0); break; } if(bRet) return true; return false; #else int32 nRet; switch(nLock) { default: return false; case FOCP_FILE_LOCK: nLock = F_LOCK; break; case FOCP_FILE_TLOCK: nLock = F_TLOCK; break; case FOCP_FILE_ULOCK: nLock = F_ULOCK; break; } loop: nRet = lockf((int32)m_hHandle, nLock, nSize); if(nRet) { if(EINTR == errno) goto loop; return false; } return true; #endif }
static int flock(int fd, int op) { HANDLE h = (HANDLE) _get_osfhandle(fd); OVERLAPPED offset; if (h < 0) return -1; memset(&offset, 0, sizeof(offset)); switch (op) { case LOCK_EX: return (LockFileEx(h, LOCKFILE_EXCLUSIVE_LOCK, 0, 1, 0, &offset)) ? 0 : -1; case LOCK_UN: UnlockFileEx(h, 0, 1, 0, &offset); return 0; } return -1; }
int qemu_create_pidfile(const char *filename) { char buffer[128]; int len; #ifndef _WIN32 int fd; fd = open(filename, O_RDWR | O_CREAT, 0600); if (fd == -1) return -1; if (lockf(fd, F_TLOCK, 0) == -1) return -1; len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid()); if (write(fd, buffer, len) != len) return -1; #else HANDLE file; DWORD flags; OVERLAPPED overlap; BOOL ret; /* Open for writing with no sharing. */ file = CreateFile(filename, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (file == INVALID_HANDLE_VALUE) return -1; flags = LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY; overlap.hEvent = 0; /* Lock 1 byte. */ ret = LockFileEx(file, flags, 0, 0, 1, &overlap); if (ret == 0) return -1; /* Write PID to file. */ len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid()); ret = WriteFileEx(file, (LPCVOID)buffer, (DWORD)len, &overlap, NULL); if (ret == 0) return -1; #endif return 0; }
HRESULT FileLockBytesImpl_LockRegionSync(ILockBytes* iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb) { FileLockBytesImpl* This = impl_from_ILockBytes(iface); OVERLAPPED ol; if (iface->lpVtbl != &FileLockBytesImpl_Vtbl) return E_NOTIMPL; ol.hEvent = 0; ol.u.s.Offset = libOffset.u.LowPart; ol.u.s.OffsetHigh = libOffset.u.HighPart; if (LockFileEx(This->hfile, LOCKFILE_EXCLUSIVE_LOCK, 0, cb.u.LowPart, cb.u.HighPart, &ol)) return S_OK; return get_lock_error(); }