VirtualBox

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

Last change on this file since 62514 was 62477, checked in by vboxsync, 8 years ago

(C) 2016

  • 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 62477 2016-07-22 18:27:37Z vboxsync $ */
2/** @file
3 * IPRT - File Locking, OS/2.
4 */
5
6/*
7 * Copyright (C) 2008-2016 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(RTFileToNative(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 copied from ../win/fileio-win.cpp for now but a proper solution
100 * would probably be to modify kLIBC so that __fcntl_locking() first
101 * assumes a change lock request is made (e.g. the same region was
102 * previously F_RDLCK'ed and now needs to be F_WRLCK'ed or vice versa) and
103 * tries to use atomic locking, and only if it fails, it does the regular
104 * lock procedure. The alternative is to use DosSetFileLocks directly here
105 * which basically means copy-pasting the __fcntl_locking() source
106 * code :) Note that the first attempt to call RTFileLock() below assumes
107 * that kLIBC is patched as described above one day and gives it a chance;
108 * on failure, we fall back to the Win-like unlock-then-lock approach. */
109
110 int rc = RTFileLock(File, fLock, offLock, cbLock);
111 if (RT_FAILURE(rc) && rc != VERR_FILE_LOCK_VIOLATION)
112 return rc;
113
114 /* Check arguments. */
115 if (fLock & ~RTFILE_LOCK_MASK)
116 {
117 AssertMsgFailed(("Invalid fLock=%08X\n", fLock));
118 return VERR_INVALID_PARAMETER;
119 }
120
121 /* Remove old lock. */
122 rc = RTFileUnlock(File, offLock, cbLock);
123 if (RT_FAILURE(rc))
124 return rc;
125
126 /* Set new lock. */
127 rc = RTFileLock(File, fLock, offLock, cbLock);
128 if (RT_SUCCESS(rc))
129 return rc;
130
131 /* Try to restore old lock. */
132 unsigned fLockOld = (fLock & RTFILE_LOCK_WRITE) ? fLock & ~RTFILE_LOCK_WRITE : fLock | RTFILE_LOCK_WRITE;
133 rc = RTFileLock(File, fLockOld, offLock, cbLock);
134 if (RT_SUCCESS(rc))
135 return VERR_FILE_LOCK_VIOLATION;
136 else
137 return VERR_FILE_LOCK_LOST;
138}
139
140
141RTR3DECL(int) RTFileUnlock(RTFILE File, int64_t offLock, uint64_t cbLock)
142{
143 Assert(offLock >= 0);
144
145 /*
146 * Validate offset.
147 */
148 if ( sizeof(off_t) < sizeof(cbLock)
149 && ( (offLock >> 32) != 0
150 || (cbLock >> 32) != 0
151 || ((offLock + cbLock) >> 32) != 0))
152 {
153 AssertMsgFailed(("64-bit file i/o not supported! offLock=%lld cbLock=%lld\n", offLock, cbLock));
154 return VERR_NOT_SUPPORTED;
155 }
156
157 /* Prepare flock structure. */
158 struct flock fl;
159 fl.l_type = F_UNLCK;
160 fl.l_whence = SEEK_SET;
161 fl.l_start = (off_t)offLock;
162 fl.l_len = (off_t)cbLock;
163 fl.l_pid = 0;
164
165 if (fcntl(RTFileToNative(File), F_SETLK, &fl) >= 0)
166 return VINF_SUCCESS;
167
168 /** @todo check error codes for non existing lock. */
169 int iErr = errno;
170 if ( iErr == EAGAIN
171 || iErr == EACCES)
172 return VERR_FILE_LOCK_VIOLATION;
173
174 return RTErrConvertFromErrno(iErr);
175}
176
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