VirtualBox

source: vbox/trunk/src/VBox/Runtime/nt/fileioutils-nt.cpp@ 104700

Last change on this file since 104700 was 98103, checked in by vboxsync, 2 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.9 KB
Line 
1/* $Id: fileioutils-nt.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - File I/O, common NT helpers.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/nt/nt.h>
42#include "internal/iprt.h"
43
44#include <iprt/errcore.h>
45#include "internal/file.h"
46
47
48
49/**
50 * Helper for converting RTFILE_O_XXX to the various NtCreateFile flags.
51 *
52 * @returns IPRT status code
53 * @param fOpen The RTFILE_O_XXX flags to convert.
54 * @param pfDesiredAccess Where to return the desired access mask.
55 * @param pfObjAttribs Where to return the NT object attributes.
56 * @param pfFileAttribs Where to return the file attributes (create).
57 * @param pfShareAccess Where to return the file sharing access mask.
58 * @param pfCreateDisposition Where to return the file create disposition.
59 * @param pfCreateOptions Where to return the file open/create options.
60 */
61DECLHIDDEN(int) rtFileNtValidateAndConvertFlags(uint64_t fOpen, uint32_t *pfDesiredAccess, uint32_t *pfObjAttribs,
62 uint32_t *pfFileAttribs, uint32_t *pfShareAccess, uint32_t *pfCreateDisposition,
63 uint32_t *pfCreateOptions)
64{
65 /*
66 * Merge forced open flags and validate them.
67 */
68 int rc = rtFileRecalcAndValidateFlags(&fOpen);
69 if (RT_FAILURE(rc))
70 return rc;
71
72 /*
73 * Determine disposition, access, share mode, creation flags, and security attributes
74 * for the CreateFile API call.
75 */
76 ULONG fCreateDisposition;
77 switch (fOpen & RTFILE_O_ACTION_MASK)
78 {
79 case RTFILE_O_OPEN:
80 fCreateDisposition = fOpen & RTFILE_O_TRUNCATE ? FILE_OVERWRITE : FILE_OPEN;
81 break;
82 case RTFILE_O_OPEN_CREATE:
83 fCreateDisposition = FILE_OPEN_IF;
84 break;
85 case RTFILE_O_CREATE:
86 fCreateDisposition = FILE_CREATE;
87 break;
88 case RTFILE_O_CREATE_REPLACE:
89 fCreateDisposition = FILE_SUPERSEDE;
90 break;
91 default:
92 AssertMsgFailed(("Impossible fOpen=%#llx\n", fOpen));
93 return VERR_INVALID_PARAMETER;
94 }
95
96 ACCESS_MASK fDesiredAccess;
97 switch (fOpen & RTFILE_O_ACCESS_MASK)
98 {
99 case RTFILE_O_READ:
100 fDesiredAccess = FILE_GENERIC_READ; /* RTFILE_O_APPEND is ignored. */
101 break;
102 case RTFILE_O_WRITE:
103 fDesiredAccess = fOpen & RTFILE_O_APPEND
104 ? FILE_GENERIC_WRITE & ~FILE_WRITE_DATA
105 : FILE_GENERIC_WRITE;
106 break;
107 case RTFILE_O_READWRITE:
108 fDesiredAccess = fOpen & RTFILE_O_APPEND
109 ? FILE_GENERIC_READ | (FILE_GENERIC_WRITE & ~FILE_WRITE_DATA)
110 : FILE_GENERIC_READ | FILE_GENERIC_WRITE;
111 break;
112 case RTFILE_O_ATTR_ONLY:
113 if (fOpen & RTFILE_O_ACCESS_ATTR_MASK)
114 {
115 fDesiredAccess = 0;
116 break;
117 }
118 RT_FALL_THRU();
119 default:
120 AssertMsgFailed(("Impossible fOpen=%#llx\n", fOpen));
121 return VERR_INVALID_PARAMETER;
122 }
123 if (fCreateDisposition == FILE_OVERWRITE)
124 /* Required for truncating the file (see MSDN), it is *NOT* part of FILE_GENERIC_WRITE. */
125 fDesiredAccess |= GENERIC_WRITE;
126
127 /* RTFileSetMode needs following rights as well. */
128 switch (fOpen & RTFILE_O_ACCESS_ATTR_MASK)
129 {
130 case RTFILE_O_ACCESS_ATTR_READ: fDesiredAccess |= FILE_READ_ATTRIBUTES | SYNCHRONIZE; break;
131 case RTFILE_O_ACCESS_ATTR_WRITE: fDesiredAccess |= FILE_WRITE_ATTRIBUTES | SYNCHRONIZE; break;
132 case RTFILE_O_ACCESS_ATTR_READWRITE: fDesiredAccess |= FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES | SYNCHRONIZE; break;
133 default:
134 /* Attributes access is the same as the file access. */
135 switch (fOpen & RTFILE_O_ACCESS_MASK)
136 {
137 case RTFILE_O_READ: fDesiredAccess |= FILE_READ_ATTRIBUTES | SYNCHRONIZE; break;
138 case RTFILE_O_WRITE: fDesiredAccess |= FILE_WRITE_ATTRIBUTES | SYNCHRONIZE; break;
139 case RTFILE_O_READWRITE: fDesiredAccess |= FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES | SYNCHRONIZE; break;
140 default:
141 AssertMsgFailed(("Impossible fOpen=%#llx\n", fOpen));
142 return VERR_INVALID_PARAMETER;
143 }
144 }
145
146 ULONG fShareAccess;
147 switch (fOpen & RTFILE_O_DENY_MASK)
148 {
149 case RTFILE_O_DENY_NONE: fShareAccess = FILE_SHARE_READ | FILE_SHARE_WRITE; break;
150 case RTFILE_O_DENY_READ: fShareAccess = FILE_SHARE_WRITE; break;
151 case RTFILE_O_DENY_WRITE: fShareAccess = FILE_SHARE_READ; break;
152 case RTFILE_O_DENY_READWRITE: fShareAccess = 0; break;
153
154 case RTFILE_O_DENY_NOT_DELETE: fShareAccess = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE; break;
155 case RTFILE_O_DENY_NOT_DELETE | RTFILE_O_DENY_READ: fShareAccess = FILE_SHARE_DELETE | FILE_SHARE_WRITE; break;
156 case RTFILE_O_DENY_NOT_DELETE | RTFILE_O_DENY_WRITE: fShareAccess = FILE_SHARE_DELETE | FILE_SHARE_READ; break;
157 case RTFILE_O_DENY_NOT_DELETE | RTFILE_O_DENY_READWRITE:fShareAccess = FILE_SHARE_DELETE; break;
158 default:
159 AssertMsgFailed(("Impossible fOpen=%#llx\n", fOpen));
160 return VERR_INVALID_PARAMETER;
161 }
162
163 ULONG fObjAttribs = 0;
164 if (fOpen & RTFILE_O_INHERIT)
165 fObjAttribs = OBJ_INHERIT;
166
167 ULONG fCreateOptions = FILE_NON_DIRECTORY_FILE;
168 if (fOpen & RTFILE_O_WRITE_THROUGH)
169 fCreateOptions |= FILE_WRITE_THROUGH;
170 if (!(fOpen & RTFILE_O_ASYNC_IO))
171 fCreateOptions |= FILE_SYNCHRONOUS_IO_NONALERT;
172 if (fOpen & RTFILE_O_NO_CACHE)
173 {
174 fCreateOptions |= FILE_NO_INTERMEDIATE_BUFFERING;
175 fDesiredAccess &= ~FILE_APPEND_DATA;
176 }
177
178 /*
179 * Done.
180 */
181 *pfDesiredAccess = fDesiredAccess;
182 *pfObjAttribs = fObjAttribs;
183 *pfFileAttribs = FILE_ATTRIBUTE_NORMAL;
184 *pfShareAccess = fShareAccess;
185 *pfCreateDisposition = fCreateDisposition;
186 *pfCreateOptions = fCreateOptions;
187 return VINF_SUCCESS;
188}
189
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