1 | /* $Id: filelock-posix.cpp 8245 2008-04-21 17:24:28Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - File Locking, POSIX.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 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 |
|
---|
56 | RTR3DECL(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 |
|
---|
101 | RTR3DECL(int) RTFileChangeLock(RTFILE File, unsigned fLock, int64_t offLock, uint64_t cbLock)
|
---|
102 | {
|
---|
103 | /** @todo We never returns VERR_FILE_NOT_LOCKED for now. */
|
---|
104 | return RTFileLock(File, fLock, offLock, cbLock);
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | RTR3DECL(int) RTFileUnlock(RTFILE File, int64_t offLock, uint64_t cbLock)
|
---|
109 | {
|
---|
110 | Assert(offLock >= 0);
|
---|
111 |
|
---|
112 | /*
|
---|
113 | * Validate offset.
|
---|
114 | */
|
---|
115 | if ( sizeof(off_t) < sizeof(cbLock)
|
---|
116 | && ( (offLock >> 32) != 0
|
---|
117 | || (cbLock >> 32) != 0
|
---|
118 | || ((offLock + cbLock) >> 32) != 0))
|
---|
119 | {
|
---|
120 | AssertMsgFailed(("64-bit file i/o not supported! offLock=%lld cbLock=%lld\n", offLock, cbLock));
|
---|
121 | return VERR_NOT_SUPPORTED;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /* Prepare flock structure. */
|
---|
125 | struct flock fl;
|
---|
126 | fl.l_type = F_UNLCK;
|
---|
127 | fl.l_whence = SEEK_SET;
|
---|
128 | fl.l_start = (off_t)offLock;
|
---|
129 | fl.l_len = (off_t)cbLock;
|
---|
130 | fl.l_pid = 0;
|
---|
131 |
|
---|
132 | if (fcntl(File, F_SETLK, &fl) >= 0)
|
---|
133 | return VINF_SUCCESS;
|
---|
134 |
|
---|
135 | /* @todo check error codes for non existing lock. */
|
---|
136 | int iErr = errno;
|
---|
137 | if ( iErr == EAGAIN
|
---|
138 | || iErr == EACCES)
|
---|
139 | return VERR_FILE_LOCK_VIOLATION;
|
---|
140 |
|
---|
141 | return RTErrConvertFromErrno(iErr);
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|