1 | /* $Id: filelock-posix.cpp 4071 2007-08-07 17:07:59Z 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 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #define LOG_GROUP RTLOGGROUP_FILE
|
---|
23 |
|
---|
24 | #include <errno.h>
|
---|
25 | #include <sys/types.h>
|
---|
26 | #include <sys/ioctl.h>
|
---|
27 | #include <sys/fcntl.h>
|
---|
28 | #include <fcntl.h>
|
---|
29 | #include <unistd.h>
|
---|
30 | #include <sys/time.h>
|
---|
31 |
|
---|
32 | #include <iprt/file.h>
|
---|
33 | #include <iprt/assert.h>
|
---|
34 | #include <iprt/string.h>
|
---|
35 | #include <iprt/err.h>
|
---|
36 | #include <iprt/log.h>
|
---|
37 | #include "internal/file.h"
|
---|
38 | #include "internal/fs.h"
|
---|
39 |
|
---|
40 |
|
---|
41 |
|
---|
42 |
|
---|
43 | RTR3DECL(int) RTFileLock(RTFILE File, unsigned fLock, int64_t offLock, uint64_t cbLock)
|
---|
44 | {
|
---|
45 | Assert(offLock >= 0);
|
---|
46 |
|
---|
47 | /* Check arguments. */
|
---|
48 | if (fLock & ~RTFILE_LOCK_MASK)
|
---|
49 | {
|
---|
50 | AssertMsgFailed(("Invalid fLock=%08X\n", fLock));
|
---|
51 | return VERR_INVALID_PARAMETER;
|
---|
52 | }
|
---|
53 |
|
---|
54 | /*
|
---|
55 | * Validate offset.
|
---|
56 | */
|
---|
57 | if ( sizeof(off_t) < sizeof(cbLock)
|
---|
58 | && ( (offLock >> 32) != 0
|
---|
59 | || (cbLock >> 32) != 0
|
---|
60 | || ((offLock + cbLock) >> 32) != 0))
|
---|
61 | {
|
---|
62 | AssertMsgFailed(("64-bit file i/o not supported! offLock=%lld cbLock=%lld\n", offLock, cbLock));
|
---|
63 | return VERR_NOT_SUPPORTED;
|
---|
64 | }
|
---|
65 |
|
---|
66 | /* Prepare flock structure. */
|
---|
67 | struct flock fl;
|
---|
68 | Assert(RTFILE_LOCK_WRITE);
|
---|
69 | fl.l_type = (fLock & RTFILE_LOCK_WRITE) ? F_WRLCK : F_RDLCK;
|
---|
70 | fl.l_whence = SEEK_SET;
|
---|
71 | fl.l_start = (off_t)offLock;
|
---|
72 | fl.l_len = (off_t)cbLock;
|
---|
73 | fl.l_pid = 0;
|
---|
74 |
|
---|
75 | Assert(RTFILE_LOCK_WAIT);
|
---|
76 | if (fcntl(File, (fLock & RTFILE_LOCK_WAIT) ? F_SETLKW : F_SETLK, &fl) >= 0)
|
---|
77 | return VINF_SUCCESS;
|
---|
78 |
|
---|
79 | int iErr = errno;
|
---|
80 | if ( iErr == EAGAIN
|
---|
81 | || iErr == EACCES)
|
---|
82 | return VERR_FILE_LOCK_VIOLATION;
|
---|
83 |
|
---|
84 | return RTErrConvertFromErrno(iErr);
|
---|
85 | }
|
---|
86 |
|
---|
87 |
|
---|
88 | RTR3DECL(int) RTFileChangeLock(RTFILE File, unsigned fLock, int64_t offLock, uint64_t cbLock)
|
---|
89 | {
|
---|
90 | /** @todo We never returns VERR_FILE_NOT_LOCKED for now. */
|
---|
91 | return RTFileLock(File, fLock, offLock, cbLock);
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | RTR3DECL(int) RTFileUnlock(RTFILE File, int64_t offLock, uint64_t cbLock)
|
---|
96 | {
|
---|
97 | Assert(offLock >= 0);
|
---|
98 |
|
---|
99 | /*
|
---|
100 | * Validate offset.
|
---|
101 | */
|
---|
102 | if ( sizeof(off_t) < sizeof(cbLock)
|
---|
103 | && ( (offLock >> 32) != 0
|
---|
104 | || (cbLock >> 32) != 0
|
---|
105 | || ((offLock + cbLock) >> 32) != 0))
|
---|
106 | {
|
---|
107 | AssertMsgFailed(("64-bit file i/o not supported! offLock=%lld cbLock=%lld\n", offLock, cbLock));
|
---|
108 | return VERR_NOT_SUPPORTED;
|
---|
109 | }
|
---|
110 |
|
---|
111 | /* Prepare flock structure. */
|
---|
112 | struct flock fl;
|
---|
113 | fl.l_type = F_UNLCK;
|
---|
114 | fl.l_whence = SEEK_SET;
|
---|
115 | fl.l_start = (off_t)offLock;
|
---|
116 | fl.l_len = (off_t)cbLock;
|
---|
117 | fl.l_pid = 0;
|
---|
118 |
|
---|
119 | if (fcntl(File, F_SETLK, &fl) >= 0)
|
---|
120 | return VINF_SUCCESS;
|
---|
121 |
|
---|
122 | /* @todo check error codes for non existing lock. */
|
---|
123 | int iErr = errno;
|
---|
124 | if ( iErr == EAGAIN
|
---|
125 | || iErr == EACCES)
|
---|
126 | return VERR_FILE_LOCK_VIOLATION;
|
---|
127 |
|
---|
128 | return RTErrConvertFromErrno(iErr);
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|