VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/posix/filelock-posix.cpp@ 78253

Last change on this file since 78253 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

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