1 | /* $Id: filelock-darwin.cpp 2981 2007-06-01 16:01:28Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * innotek Portable Runtime - File Locking, POSIX.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * If you received this file as part of a commercial VirtualBox
|
---|
18 | * distribution, then only the terms of your commercial VirtualBox
|
---|
19 | * license agreement apply instead of the previous paragraph.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #define LOG_GROUP RTLOGGROUP_FILE
|
---|
27 |
|
---|
28 | #include <errno.h>
|
---|
29 | #include <sys/types.h>
|
---|
30 | #include <sys/ioctl.h>
|
---|
31 | #include <sys/fcntl.h>
|
---|
32 | #include <fcntl.h>
|
---|
33 | #include <unistd.h>
|
---|
34 | #include <sys/time.h>
|
---|
35 |
|
---|
36 | #include <iprt/file.h>
|
---|
37 | #include <iprt/assert.h>
|
---|
38 | #include <iprt/string.h>
|
---|
39 | #include <iprt/err.h>
|
---|
40 | #include <iprt/log.h>
|
---|
41 | #include "internal/file.h"
|
---|
42 | #include "internal/fs.h"
|
---|
43 |
|
---|
44 |
|
---|
45 |
|
---|
46 |
|
---|
47 | RTR3DECL(int) RTFileLock(RTFILE File, unsigned fLock, int64_t offLock, uint64_t cbLock)
|
---|
48 | {
|
---|
49 | Assert(offLock >= 0);
|
---|
50 |
|
---|
51 | /* Check arguments. */
|
---|
52 | if (fLock & ~RTFILE_LOCK_MASK)
|
---|
53 | {
|
---|
54 | AssertMsgFailed(("Invalid fLock=%08X\n", fLock));
|
---|
55 | return VERR_INVALID_PARAMETER;
|
---|
56 | }
|
---|
57 |
|
---|
58 | /*
|
---|
59 | * Validate offset.
|
---|
60 | */
|
---|
61 | if ( sizeof(off_t) < sizeof(cbLock)
|
---|
62 | && ( (offLock >> 32) != 0
|
---|
63 | || (cbLock >> 32) != 0
|
---|
64 | || ((offLock + cbLock) >> 32) != 0))
|
---|
65 | {
|
---|
66 | AssertMsgFailed(("64-bit file i/o not supported! offLock=%lld cbLock=%lld\n", offLock, cbLock));
|
---|
67 | return VERR_NOT_SUPPORTED;
|
---|
68 | }
|
---|
69 |
|
---|
70 | /* Prepare flock structure. */
|
---|
71 | struct flock fl;
|
---|
72 | Assert(RTFILE_LOCK_WRITE);
|
---|
73 | fl.l_type = (fLock & RTFILE_LOCK_WRITE) ? F_WRLCK : F_RDLCK;
|
---|
74 | fl.l_whence = SEEK_SET;
|
---|
75 | fl.l_start = (off_t)offLock;
|
---|
76 | fl.l_len = (off_t)cbLock;
|
---|
77 | fl.l_pid = 0;
|
---|
78 |
|
---|
79 | Assert(RTFILE_LOCK_WAIT);
|
---|
80 | if (fcntl(File, (fLock & RTFILE_LOCK_WAIT) ? F_SETLKW : F_SETLK, &fl) >= 0)
|
---|
81 | return VINF_SUCCESS;
|
---|
82 | int iErr = errno;
|
---|
83 | if (iErr == ENOTSUP)
|
---|
84 | {
|
---|
85 | /*
|
---|
86 | * This is really bad hack for getting VDIs to work somewhat
|
---|
87 | * safely on SMB mounts.
|
---|
88 | */
|
---|
89 | /** @todo we need to keep track of these locks really. Anyone requiring to lock more
|
---|
90 | * than one part of a file will have to fix this. */
|
---|
91 | unsigned f = 0;
|
---|
92 | Assert(RTFILE_LOCK_WAIT);
|
---|
93 | if (fLock & RTFILE_LOCK_WAIT)
|
---|
94 | f |= LOCK_NB;
|
---|
95 | if (fLock & RTFILE_LOCK_WRITE)
|
---|
96 | f |= LOCK_EX;
|
---|
97 | else
|
---|
98 | f |= LOCK_SH;
|
---|
99 | if (!flock(File, f))
|
---|
100 | return VINF_SUCCESS;
|
---|
101 | iErr = errno;
|
---|
102 | if (iErr == EWOULDBLOCK)
|
---|
103 | return VERR_FILE_LOCK_VIOLATION;
|
---|
104 | }
|
---|
105 |
|
---|
106 | if ( iErr == EAGAIN
|
---|
107 | || iErr == EACCES)
|
---|
108 | return VERR_FILE_LOCK_VIOLATION;
|
---|
109 |
|
---|
110 | return RTErrConvertFromErrno(iErr);
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | RTR3DECL(int) RTFileChangeLock(RTFILE File, unsigned fLock, int64_t offLock, uint64_t cbLock)
|
---|
115 | {
|
---|
116 | /** @todo We never returns VERR_FILE_NOT_LOCKED for now. */
|
---|
117 | return RTFileLock(File, fLock, offLock, cbLock);
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | RTR3DECL(int) RTFileUnlock(RTFILE File, int64_t offLock, uint64_t cbLock)
|
---|
122 | {
|
---|
123 | Assert(offLock >= 0);
|
---|
124 |
|
---|
125 | /*
|
---|
126 | * Validate offset.
|
---|
127 | */
|
---|
128 | if ( sizeof(off_t) < sizeof(cbLock)
|
---|
129 | && ( (offLock >> 32) != 0
|
---|
130 | || (cbLock >> 32) != 0
|
---|
131 | || ((offLock + cbLock) >> 32) != 0))
|
---|
132 | {
|
---|
133 | AssertMsgFailed(("64-bit file i/o not supported! offLock=%lld cbLock=%lld\n", offLock, cbLock));
|
---|
134 | return VERR_NOT_SUPPORTED;
|
---|
135 | }
|
---|
136 |
|
---|
137 | /* Prepare flock structure. */
|
---|
138 | struct flock fl;
|
---|
139 | fl.l_type = F_UNLCK;
|
---|
140 | fl.l_whence = SEEK_SET;
|
---|
141 | fl.l_start = (off_t)offLock;
|
---|
142 | fl.l_len = (off_t)cbLock;
|
---|
143 | fl.l_pid = 0;
|
---|
144 |
|
---|
145 | if (fcntl(File, F_SETLK, &fl) >= 0)
|
---|
146 | return VINF_SUCCESS;
|
---|
147 |
|
---|
148 | int iErr = errno;
|
---|
149 | if (iErr == ENOTSUP)
|
---|
150 | {
|
---|
151 | /* A SMB hack, see RTFileLock. */
|
---|
152 | if (!flock(File, LOCK_UN))
|
---|
153 | return VINF_SUCCESS;
|
---|
154 | }
|
---|
155 |
|
---|
156 | /** @todo check error codes for non existing lock. */
|
---|
157 | if ( iErr == EAGAIN
|
---|
158 | || iErr == EACCES)
|
---|
159 | return VERR_FILE_LOCK_VIOLATION;
|
---|
160 |
|
---|
161 | return RTErrConvertFromErrno(iErr);
|
---|
162 | }
|
---|
163 |
|
---|