VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/darwin/filelock-darwin.cpp@ 4512

Last change on this file since 4512 was 4071, checked in by vboxsync, 17 years ago

Biggest check-in ever. New source code headers for all (C) innotek files.

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