1684N/A/* $Id$ */
1684N/A/** @file
1684N/A * IPRT - File Locking, POSIX.
1684N/A */
1684N/A
1684N/A/*
1684N/A * Copyright (C) 2006-2012 Oracle Corporation
1684N/A *
1684N/A * This file is part of VirtualBox Open Source Edition (OSE), as
1684N/A * available from http://www.virtualbox.org. This file is free software;
1684N/A * you can redistribute it and/or modify it under the terms of the GNU
1684N/A * General Public License (GPL) as published by the Free Software
1684N/A * Foundation, in version 2 as it comes in the "COPYING" file of the
1684N/A * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
1684N/A * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
1684N/A *
1684N/A * The contents of this file may alternatively be used under the terms
1684N/A * of the Common Development and Distribution License Version 1.0
1684N/A * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
1684N/A * VirtualBox OSE distribution, in which case the provisions of the
1684N/A * CDDL are applicable instead of those of the GPL.
1684N/A *
3232N/A * You may elect to license modified versions of this file under the
1684N/A * terms and conditions of either the GPL or the CDDL or both.
1684N/A */
1684N/A
1684N/A
1684N/A/*******************************************************************************
1684N/A* Header Files *
1684N/A*******************************************************************************/
1684N/A#define LOG_GROUP RTLOGGROUP_FILE
1684N/A
1684N/A#include <errno.h>
1684N/A#include <sys/types.h>
1684N/A#include <sys/ioctl.h>
1684N/A#include <fcntl.h>
1684N/A#include <unistd.h>
1684N/A#include <sys/time.h>
1684N/A
1684N/A#include <iprt/file.h>
1684N/A#include <iprt/assert.h>
1684N/A#include <iprt/string.h>
1684N/A#include <iprt/err.h>
1684N/A#include <iprt/log.h>
1684N/A#include "internal/file.h"
1684N/A#include "internal/fs.h"
1684N/A
1684N/A
1684N/A
1684N/A
1684N/ARTR3DECL(int) RTFileLock(RTFILE hFile, unsigned fLock, int64_t offLock, uint64_t cbLock)
1684N/A{
1684N/A Assert(offLock >= 0);
1684N/A
1684N/A /* Check arguments. */
1684N/A if (fLock & ~RTFILE_LOCK_MASK)
1684N/A {
1684N/A AssertMsgFailed(("Invalid fLock=%08X\n", fLock));
1684N/A return VERR_INVALID_PARAMETER;
1684N/A }
1684N/A
1684N/A /*
1684N/A * Validate offset.
1684N/A */
1684N/A if ( sizeof(off_t) < sizeof(cbLock)
1684N/A && ( (offLock >> 32) != 0
1684N/A || (cbLock >> 32) != 0
1684N/A || ((offLock + cbLock) >> 32) != 0))
1684N/A {
1684N/A AssertMsgFailed(("64-bit file i/o not supported! offLock=%lld cbLock=%lld\n", offLock, cbLock));
1684N/A return VERR_NOT_SUPPORTED;
1684N/A }
1684N/A
1684N/A /* Prepare flock structure. */
1684N/A struct flock fl;
1684N/A Assert(RTFILE_LOCK_WRITE);
1684N/A fl.l_type = (fLock & RTFILE_LOCK_WRITE) ? F_WRLCK : F_RDLCK;
1684N/A fl.l_whence = SEEK_SET;
1684N/A fl.l_start = (off_t)offLock;
1684N/A fl.l_len = (off_t)cbLock;
1684N/A fl.l_pid = 0;
1684N/A
1684N/A Assert(RTFILE_LOCK_WAIT);
1684N/A if (fcntl(RTFileToNative(hFile), (fLock & RTFILE_LOCK_WAIT) ? F_SETLKW : F_SETLK, &fl) >= 0)
1684N/A return VINF_SUCCESS;
1684N/A
1684N/A int iErr = errno;
1684N/A if ( iErr == EAGAIN
1684N/A || iErr == EACCES)
1684N/A return VERR_FILE_LOCK_VIOLATION;
1684N/A
1684N/A return RTErrConvertFromErrno(iErr);
1684N/A}
1684N/A
1684N/A
1684N/ARTR3DECL(int) RTFileChangeLock(RTFILE hFile, unsigned fLock, int64_t offLock, uint64_t cbLock)
1684N/A{
1684N/A /** @todo We never returns VERR_FILE_NOT_LOCKED for now. */
1684N/A return RTFileLock(hFile, fLock, offLock, cbLock);
1684N/A}
1684N/A
1684N/A
1684N/ARTR3DECL(int) RTFileUnlock(RTFILE hFile, int64_t offLock, uint64_t cbLock)
1684N/A{
1684N/A Assert(offLock >= 0);
1684N/A
1684N/A /*
1684N/A * Validate offset.
1684N/A */
1684N/A if ( sizeof(off_t) < sizeof(cbLock)
1684N/A && ( (offLock >> 32) != 0
1684N/A || (cbLock >> 32) != 0
1684N/A || ((offLock + cbLock) >> 32) != 0))
1684N/A {
1684N/A AssertMsgFailed(("64-bit file i/o not supported! offLock=%lld cbLock=%lld\n", offLock, cbLock));
1684N/A return VERR_NOT_SUPPORTED;
1684N/A }
1684N/A
1684N/A /* Prepare flock structure. */
1684N/A struct flock fl;
1684N/A fl.l_type = F_UNLCK;
1684N/A fl.l_whence = SEEK_SET;
1684N/A fl.l_start = (off_t)offLock;
1684N/A fl.l_len = (off_t)cbLock;
1684N/A fl.l_pid = 0;
1684N/A
1684N/A if (fcntl(RTFileToNative(hFile), F_SETLK, &fl) >= 0)
1684N/A return VINF_SUCCESS;
1684N/A
1684N/A /* @todo check error codes for non existing lock. */
1684N/A int iErr = errno;
1684N/A if ( iErr == EAGAIN
1684N/A || iErr == EACCES)
1684N/A return VERR_FILE_LOCK_VIOLATION;
1684N/A
1684N/A return RTErrConvertFromErrno(iErr);
1684N/A}
1684N/A
1684N/A