filelock-os2.cpp revision 4eb60291a3736df8e5c706848a0c5dfa75a5814f
5b281ba489ca18f0380d7efc7a5108b606cce449vboxsync * IPRT - File Locking, OS/2.
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync * Copyright (C) 2008 Oracle Corporation
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * available from http://www.virtualbox.org. This file is free software;
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * you can redistribute it and/or modify it under the terms of the GNU
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * General Public License (GPL) as published by the Free Software
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * The contents of this file may alternatively be used under the terms
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * of the Common Development and Distribution License Version 1.0
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * VirtualBox OSE distribution, in which case the provisions of the
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * CDDL are applicable instead of those of the GPL.
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * You may elect to license modified versions of this file under the
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * terms and conditions of either the GPL or the CDDL or both.
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync/*******************************************************************************
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync* Header Files *
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync*******************************************************************************/
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsyncRTR3DECL(int) RTFileLock(RTFILE File, unsigned fLock, int64_t offLock, uint64_t cbLock)
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync /* Check arguments. */
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * Validate offset.
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync AssertMsgFailed(("64-bit file i/o not supported! offLock=%lld cbLock=%lld\n", offLock, cbLock));
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync /* Prepare flock structure. */
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync fl.l_type = (fLock & RTFILE_LOCK_WRITE) ? F_WRLCK : F_RDLCK;
4eb60291a3736df8e5c706848a0c5dfa75a5814fvboxsync if (fcntl(RTFileToNative(File), (fLock & RTFILE_LOCK_WAIT) ? F_SETLKW : F_SETLK, &fl) >= 0)
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsyncRTR3DECL(int) RTFileChangeLock(RTFILE File, unsigned fLock, int64_t offLock, uint64_t cbLock)
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync /** @todo copied from ../win/fileio-win.cpp for now but a proper solution
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * would probably be to modify kLIBC so that __fcntl_locking() first
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync * assumes a change lock request is made (e.g. the same region was
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * previously F_RDLCK'ed and now needs to be F_WRLCK'ed or vice versa) and
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * tries to use atomic locking, and only if it fails, it does the regular
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * lock procedure. The alternative is to use DosSetFileLocks directly here
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * which basically means copy-pasting the __fcntl_locking() source
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * code :) Note that the first attempt to call RTFileLock() below assumes
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * that kLIBC is patched as described above one day and gives it a chance;
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * on failure, we fall back to the Win-like unlock-then-lock approach. */
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync if (RT_FAILURE(rc) && rc != VERR_FILE_LOCK_VIOLATION)
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync /* Check arguments. */
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync /* Remove old lock. */
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync /* Set new lock. */
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync /* Try to restore old lock. */
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync unsigned fLockOld = (fLock & RTFILE_LOCK_WRITE) ? fLock & ~RTFILE_LOCK_WRITE : fLock | RTFILE_LOCK_WRITE;
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsyncRTR3DECL(int) RTFileUnlock(RTFILE File, int64_t offLock, uint64_t cbLock)
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync * Validate offset.
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync AssertMsgFailed(("64-bit file i/o not supported! offLock=%lld cbLock=%lld\n", offLock, cbLock));
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync /* Prepare flock structure. */
4eb60291a3736df8e5c706848a0c5dfa75a5814fvboxsync if (fcntl(RTFileToNative(File), F_SETLK, &fl) >= 0)
1d7987915cc0b7628d6711d280f7d48d7192124dvboxsync /** @todo check error codes for non existing lock. */