VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/os2/filelock-os2.cpp@ 24006

Last change on this file since 24006 was 8245, checked in by vboxsync, 17 years ago

rebranding: IPRT files again.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Revision Author Id
File size: 5.6 KB
Line 
1/* $Id: filelock-os2.cpp 8245 2008-04-21 17:24:28Z vboxsync $ */
2/** @file
3 * IPRT - File Locking, OS/2.
4 */
5
6/*
7 * Copyright (C) 2008 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#define LOG_GROUP RTLOGGROUP_FILE
36
37#include <errno.h>
38#include <sys/types.h>
39#include <sys/ioctl.h>
40#include <sys/fcntl.h>
41#include <fcntl.h>
42#include <unistd.h>
43#include <sys/time.h>
44
45#include <iprt/file.h>
46#include <iprt/assert.h>
47#include <iprt/string.h>
48#include <iprt/err.h>
49#include <iprt/log.h>
50#include "internal/file.h"
51#include "internal/fs.h"
52
53
54
55
56RTR3DECL(int) RTFileLock(RTFILE File, unsigned fLock, int64_t offLock, uint64_t cbLock)
57{
58 Assert(offLock >= 0);
59
60 /* Check arguments. */
61 if (fLock & ~RTFILE_LOCK_MASK)
62 {
63 AssertMsgFailed(("Invalid fLock=%08X\n", fLock));
64 return VERR_INVALID_PARAMETER;
65 }
66
67 /*
68 * Validate offset.
69 */
70 if ( sizeof(off_t) < sizeof(cbLock)
71 && ( (offLock >> 32) != 0
72 || (cbLock >> 32) != 0
73 || ((offLock + cbLock) >> 32) != 0))
74 {
75 AssertMsgFailed(("64-bit file i/o not supported! offLock=%lld cbLock=%lld\n", offLock, cbLock));
76 return VERR_NOT_SUPPORTED;
77 }
78
79 /* Prepare flock structure. */
80 struct flock fl;
81 Assert(RTFILE_LOCK_WRITE);
82 fl.l_type = (fLock & RTFILE_LOCK_WRITE) ? F_WRLCK : F_RDLCK;
83 fl.l_whence = SEEK_SET;
84 fl.l_start = (off_t)offLock;
85 fl.l_len = (off_t)cbLock;
86 fl.l_pid = 0;
87
88 Assert(RTFILE_LOCK_WAIT);
89 if (fcntl(File, (fLock & RTFILE_LOCK_WAIT) ? F_SETLKW : F_SETLK, &fl) >= 0)
90 return VINF_SUCCESS;
91
92 int iErr = errno;
93 if ( iErr == EAGAIN
94 || iErr == EACCES)
95 return VERR_FILE_LOCK_VIOLATION;
96
97 return RTErrConvertFromErrno(iErr);
98}
99
100
101RTR3DECL(int) RTFileChangeLock(RTFILE File, unsigned fLock, int64_t offLock, uint64_t cbLock)
102{
103 /** @todo copied from ../win/fileio-win.cpp for now but a proper solution
104 * would probably be to modify kLIBC so that __fcntl_locking() first
105 * assmues a change lock request is made (e.g. the same region was
106 * previously F_RDLCK'ed and now needs to be F_WRLCK'ed or vice versa) and
107 * tries to use atomic locking, and only if it fails, it does the regular
108 * lock procedure. The alternative is to use DosSetFileLocks directly here
109 * which basically means copy-pasting the __fcntl_locking() source
110 * code :) Note that the first attempt to call RTFileLock() below assumes
111 * that kLIBC is patched as described above one day and gives it a chance;
112 * on failure, we fall back to the Win-like unlock-then-lock approach. */
113
114 int rc = RTFileLock(File, fLock, offLock, cbLock);
115 if (RT_FAILURE(rc) && rc != VERR_FILE_LOCK_VIOLATION)
116 return rc;
117
118 /* Check arguments. */
119 if (fLock & ~RTFILE_LOCK_MASK)
120 {
121 AssertMsgFailed(("Invalid fLock=%08X\n", fLock));
122 return VERR_INVALID_PARAMETER;
123 }
124
125 /* Remove old lock. */
126 rc = RTFileUnlock(File, offLock, cbLock);
127 if (RT_FAILURE(rc))
128 return rc;
129
130 /* Set new lock. */
131 rc = RTFileLock(File, fLock, offLock, cbLock);
132 if (RT_SUCCESS(rc))
133 return rc;
134
135 /* Try to restore old lock. */
136 unsigned fLockOld = (fLock & RTFILE_LOCK_WRITE) ? fLock & ~RTFILE_LOCK_WRITE : fLock | RTFILE_LOCK_WRITE;
137 rc = RTFileLock(File, fLockOld, offLock, cbLock);
138 if (RT_SUCCESS(rc))
139 return VERR_FILE_LOCK_VIOLATION;
140 else
141 return VERR_FILE_LOCK_LOST;
142}
143
144
145RTR3DECL(int) RTFileUnlock(RTFILE File, int64_t offLock, uint64_t cbLock)
146{
147 Assert(offLock >= 0);
148
149 /*
150 * Validate offset.
151 */
152 if ( sizeof(off_t) < sizeof(cbLock)
153 && ( (offLock >> 32) != 0
154 || (cbLock >> 32) != 0
155 || ((offLock + cbLock) >> 32) != 0))
156 {
157 AssertMsgFailed(("64-bit file i/o not supported! offLock=%lld cbLock=%lld\n", offLock, cbLock));
158 return VERR_NOT_SUPPORTED;
159 }
160
161 /* Prepare flock structure. */
162 struct flock fl;
163 fl.l_type = F_UNLCK;
164 fl.l_whence = SEEK_SET;
165 fl.l_start = (off_t)offLock;
166 fl.l_len = (off_t)cbLock;
167 fl.l_pid = 0;
168
169 if (fcntl(File, F_SETLK, &fl) >= 0)
170 return VINF_SUCCESS;
171
172 /** @todo check error codes for non existing lock. */
173 int iErr = errno;
174 if ( iErr == EAGAIN
175 || iErr == EACCES)
176 return VERR_FILE_LOCK_VIOLATION;
177
178 return RTErrConvertFromErrno(iErr);
179}
180
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