1 | /* $Id: filelock-darwin.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - File Locking, POSIX.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2019 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 |
|
---|
52 | RTR3DECL(int) RTFileLock(RTFILE hFile, 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(hFile), (fLock & RTFILE_LOCK_WAIT) ? F_SETLKW : F_SETLK, &fl) >= 0)
|
---|
86 | return VINF_SUCCESS;
|
---|
87 | int iErr = errno;
|
---|
88 | if (iErr == ENOTSUP)
|
---|
89 | {
|
---|
90 | /*
|
---|
91 | * This is really bad hack for getting VDIs to work somewhat
|
---|
92 | * safely on SMB mounts.
|
---|
93 | */
|
---|
94 | /** @todo we need to keep track of these locks really. Anyone requiring to lock more
|
---|
95 | * than one part of a file will have to fix this. */
|
---|
96 | unsigned f = 0;
|
---|
97 | Assert(RTFILE_LOCK_WAIT);
|
---|
98 | if (fLock & RTFILE_LOCK_WAIT)
|
---|
99 | f |= LOCK_NB;
|
---|
100 | if (fLock & RTFILE_LOCK_WRITE)
|
---|
101 | f |= LOCK_EX;
|
---|
102 | else
|
---|
103 | f |= LOCK_SH;
|
---|
104 | if (!flock(RTFileToNative(hFile), f))
|
---|
105 | return VINF_SUCCESS;
|
---|
106 | iErr = errno;
|
---|
107 | if (iErr == EWOULDBLOCK)
|
---|
108 | return VERR_FILE_LOCK_VIOLATION;
|
---|
109 | }
|
---|
110 |
|
---|
111 | if ( iErr == EAGAIN
|
---|
112 | || iErr == EACCES)
|
---|
113 | return VERR_FILE_LOCK_VIOLATION;
|
---|
114 |
|
---|
115 | return RTErrConvertFromErrno(iErr);
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | RTR3DECL(int) RTFileChangeLock(RTFILE hFile, unsigned fLock, int64_t offLock, uint64_t cbLock)
|
---|
120 | {
|
---|
121 | /** @todo We never returns VERR_FILE_NOT_LOCKED for now. */
|
---|
122 | return RTFileLock(hFile, fLock, offLock, cbLock);
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | RTR3DECL(int) RTFileUnlock(RTFILE hFile, int64_t offLock, uint64_t cbLock)
|
---|
127 | {
|
---|
128 | Assert(offLock >= 0);
|
---|
129 |
|
---|
130 | /*
|
---|
131 | * Validate offset.
|
---|
132 | */
|
---|
133 | if ( sizeof(off_t) < sizeof(cbLock)
|
---|
134 | && ( (offLock >> 32) != 0
|
---|
135 | || (cbLock >> 32) != 0
|
---|
136 | || ((offLock + cbLock) >> 32) != 0))
|
---|
137 | {
|
---|
138 | AssertMsgFailed(("64-bit file i/o not supported! offLock=%lld cbLock=%lld\n", offLock, cbLock));
|
---|
139 | return VERR_NOT_SUPPORTED;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /* Prepare flock structure. */
|
---|
143 | struct flock fl;
|
---|
144 | fl.l_type = F_UNLCK;
|
---|
145 | fl.l_whence = SEEK_SET;
|
---|
146 | fl.l_start = (off_t)offLock;
|
---|
147 | fl.l_len = (off_t)cbLock;
|
---|
148 | fl.l_pid = 0;
|
---|
149 |
|
---|
150 | if (fcntl(RTFileToNative(hFile), F_SETLK, &fl) >= 0)
|
---|
151 | return VINF_SUCCESS;
|
---|
152 |
|
---|
153 | int iErr = errno;
|
---|
154 | if (iErr == ENOTSUP)
|
---|
155 | {
|
---|
156 | /* A SMB hack, see RTFileLock. */
|
---|
157 | if (!flock(RTFileToNative(hFile), LOCK_UN))
|
---|
158 | return VINF_SUCCESS;
|
---|
159 | }
|
---|
160 |
|
---|
161 | /** @todo check error codes for non existing lock. */
|
---|
162 | if ( iErr == EAGAIN
|
---|
163 | || iErr == EACCES)
|
---|
164 | return VERR_FILE_LOCK_VIOLATION;
|
---|
165 |
|
---|
166 | return RTErrConvertFromErrno(iErr);
|
---|
167 | }
|
---|
168 |
|
---|