VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/posix/filelock-posix.cpp@ 28800

Last change on this file since 28800 was 28800, checked in by vboxsync, 14 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.0 KB
Line 
1/* $Id: filelock-posix.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
2/** @file
3 * IPRT - File Locking, POSIX.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#define LOG_GROUP RTLOGGROUP_FILE
32
33#include <errno.h>
34#include <sys/types.h>
35#include <sys/ioctl.h>
36#include <sys/fcntl.h>
37#include <fcntl.h>
38#include <unistd.h>
39#include <sys/time.h>
40
41#include <iprt/file.h>
42#include <iprt/assert.h>
43#include <iprt/string.h>
44#include <iprt/err.h>
45#include <iprt/log.h>
46#include "internal/file.h"
47#include "internal/fs.h"
48
49
50
51
52RTR3DECL(int) RTFileLock(RTFILE File, unsigned fLock, int64_t offLock, uint64_t cbLock)
53{
54 Assert(offLock >= 0);
55
56 /* Check arguments. */
57 if (fLock & ~RTFILE_LOCK_MASK)
58 {
59 AssertMsgFailed(("Invalid fLock=%08X\n", fLock));
60 return VERR_INVALID_PARAMETER;
61 }
62
63 /*
64 * Validate offset.
65 */
66 if ( sizeof(off_t) < sizeof(cbLock)
67 && ( (offLock >> 32) != 0
68 || (cbLock >> 32) != 0
69 || ((offLock + cbLock) >> 32) != 0))
70 {
71 AssertMsgFailed(("64-bit file i/o not supported! offLock=%lld cbLock=%lld\n", offLock, cbLock));
72 return VERR_NOT_SUPPORTED;
73 }
74
75 /* Prepare flock structure. */
76 struct flock fl;
77 Assert(RTFILE_LOCK_WRITE);
78 fl.l_type = (fLock & RTFILE_LOCK_WRITE) ? F_WRLCK : F_RDLCK;
79 fl.l_whence = SEEK_SET;
80 fl.l_start = (off_t)offLock;
81 fl.l_len = (off_t)cbLock;
82 fl.l_pid = 0;
83
84 Assert(RTFILE_LOCK_WAIT);
85 if (fcntl(File, (fLock & RTFILE_LOCK_WAIT) ? F_SETLKW : F_SETLK, &fl) >= 0)
86 return VINF_SUCCESS;
87
88 int iErr = errno;
89 if ( iErr == EAGAIN
90 || iErr == EACCES)
91 return VERR_FILE_LOCK_VIOLATION;
92
93 return RTErrConvertFromErrno(iErr);
94}
95
96
97RTR3DECL(int) RTFileChangeLock(RTFILE File, unsigned fLock, int64_t offLock, uint64_t cbLock)
98{
99 /** @todo We never returns VERR_FILE_NOT_LOCKED for now. */
100 return RTFileLock(File, fLock, offLock, cbLock);
101}
102
103
104RTR3DECL(int) RTFileUnlock(RTFILE File, int64_t offLock, uint64_t cbLock)
105{
106 Assert(offLock >= 0);
107
108 /*
109 * Validate offset.
110 */
111 if ( sizeof(off_t) < sizeof(cbLock)
112 && ( (offLock >> 32) != 0
113 || (cbLock >> 32) != 0
114 || ((offLock + cbLock) >> 32) != 0))
115 {
116 AssertMsgFailed(("64-bit file i/o not supported! offLock=%lld cbLock=%lld\n", offLock, cbLock));
117 return VERR_NOT_SUPPORTED;
118 }
119
120 /* Prepare flock structure. */
121 struct flock fl;
122 fl.l_type = F_UNLCK;
123 fl.l_whence = SEEK_SET;
124 fl.l_start = (off_t)offLock;
125 fl.l_len = (off_t)cbLock;
126 fl.l_pid = 0;
127
128 if (fcntl(File, F_SETLK, &fl) >= 0)
129 return VINF_SUCCESS;
130
131 /* @todo check error codes for non existing lock. */
132 int iErr = errno;
133 if ( iErr == EAGAIN
134 || iErr == EACCES)
135 return VERR_FILE_LOCK_VIOLATION;
136
137 return RTErrConvertFromErrno(iErr);
138}
139
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette