VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/misc/RTFileModeToFlags.cpp@ 99248

Last change on this file since 99248 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: 11.7 KB
Line 
1/* $Id: RTFileModeToFlags.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - RTFileModeToFlags.
4 */
5
6/*
7 * Copyright (C) 2013-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/assert.h>
42#include <iprt/errcore.h>
43#include <iprt/file.h>
44#include <iprt/string.h>
45#include "internal/iprt.h"
46
47
48RTR3DECL(int) RTFileModeToFlags(const char *pszMode, uint64_t *pfMode)
49{
50 AssertPtrReturn(pszMode, VERR_INVALID_POINTER);
51 AssertPtrReturn(pfMode, VERR_INVALID_POINTER);
52
53 const char *pszCur = pszMode;
54 if (*pszCur == '\0')
55 return VERR_INVALID_PARAMETER;
56
57 uint64_t fMode = 0;
58 char chPrev = '\0';
59 while ( pszCur
60 && *pszCur != '\0')
61 {
62 bool fSkip = false;
63 switch (*pszCur)
64 {
65 /* Opens an existing file for writing and places the
66 * file pointer at the end of the file. The file is
67 * created if it does not exist. */
68 case 'a':
69 if ((fMode & RTFILE_O_ACTION_MASK) == 0)
70 fMode |= RTFILE_O_OPEN_CREATE
71 | RTFILE_O_WRITE
72 | RTFILE_O_APPEND;
73 else
74 return VERR_INVALID_PARAMETER;
75 break;
76
77 case 'b': /* Binary mode. */
78 /* Just skip as being valid. */
79 fSkip = true;
80 break;
81
82 /* Creates a file or open an existing one for
83 * writing only. The file pointer will be placed
84 * at the beginning of the file.*/
85 case 'c':
86 if ((fMode & RTFILE_O_ACTION_MASK) == 0)
87 fMode |= RTFILE_O_OPEN_CREATE
88 | RTFILE_O_WRITE;
89 else
90 return VERR_INVALID_PARAMETER;
91 break;
92
93 /* Opens an existing file for reading and places the
94 * file pointer at the beginning of the file. If the
95 * file does not exist an error will be returned. */
96 case 'r':
97 if ((fMode & RTFILE_O_ACTION_MASK) == 0)
98 fMode |= RTFILE_O_OPEN
99 | RTFILE_O_READ;
100 else
101 return VERR_INVALID_PARAMETER;
102 break;
103
104 case 't': /* Text mode. */
105 /* Just skip as being valid. */
106 fSkip = true;
107 break;
108
109 /* Creates a new file or replaces an existing one
110 * for writing. Places the file pointer at the beginning.
111 * An existing file will be truncated to 0 bytes. */
112 case 'w':
113 if ((fMode & RTFILE_O_ACTION_MASK) == 0)
114 fMode |= RTFILE_O_CREATE_REPLACE
115 | RTFILE_O_WRITE
116 | RTFILE_O_TRUNCATE;
117 else
118 return VERR_INVALID_PARAMETER;
119 break;
120
121 /* Creates a new file and opens it for writing. Places
122 * the file pointer at the beginning. If the file
123 * exists an error will be returned. */
124 case 'x':
125 if ((fMode & RTFILE_O_ACTION_MASK) == 0)
126 fMode |= RTFILE_O_CREATE
127 | RTFILE_O_WRITE;
128 else
129 return VERR_INVALID_PARAMETER;
130 break;
131
132 case '+':
133 {
134 switch (chPrev)
135 {
136 case 'a':
137 case 'c':
138 case 'w':
139 case 'x':
140 /* Also open / create file with read access. */
141 fMode |= RTFILE_O_READ;
142 break;
143
144 case 'r':
145 /* Also open / create file with write access. */
146 fMode |= RTFILE_O_WRITE;
147 break;
148
149 case 'b':
150 case 't':
151 /* Silently eat skipped parameters. */
152 fSkip = true;
153 break;
154
155 case 0: /* No previous character yet. */
156 case '+':
157 /* Eat plusses which don't belong to a command. */
158 fSkip = true;
159 break;
160
161 default:
162 return VERR_INVALID_PARAMETER;
163 }
164
165 break;
166 }
167
168 default:
169 return VERR_INVALID_PARAMETER;
170 }
171
172 if (!fSkip)
173 chPrev = *pszCur;
174 pszCur++;
175 }
176
177 /* No action mask set? */
178 if ((fMode & RTFILE_O_ACTION_MASK) == 0)
179 return VERR_INVALID_PARAMETER;
180
181 /** @todo Handle sharing mode */
182 fMode |= RTFILE_O_DENY_NONE;
183
184 /* Return. */
185 *pfMode = fMode;
186 return VINF_SUCCESS;
187}
188RT_EXPORT_SYMBOL(RTFileModeToFlags);
189
190
191RTR3DECL(int) RTFileModeToFlagsEx(const char *pszAccess, const char *pszDisposition,
192 const char *pszSharing, uint64_t *pfMode)
193{
194 AssertPtrReturn(pszAccess, VERR_INVALID_POINTER);
195 AssertPtrReturn(pszDisposition, VERR_INVALID_POINTER);
196 AssertPtrNullReturn(pszSharing, VERR_INVALID_POINTER);
197 AssertPtrReturn(pfMode, VERR_INVALID_POINTER);
198
199 const char *pszCur = pszAccess;
200 if (*pszCur == '\0')
201 return VERR_INVALID_PARAMETER;
202
203 /*
204 * Handle access mode.
205 */
206 uint64_t fMode = 0;
207 char chPrev = '\0';
208 while ( pszCur
209 && *pszCur != '\0')
210 {
211 bool fSkip = false;
212 switch (*pszCur)
213 {
214 case 'b': /* Binary mode. */
215 /* Just skip as being valid. */
216 fSkip = true;
217 break;
218
219 case 'r': /* Read. */
220 fMode |= RTFILE_O_READ;
221 break;
222
223 case 't': /* Text mode. */
224 /* Just skip as being valid. */
225 fSkip = true;
226 break;
227
228 case 'w': /* Write. */
229 fMode |= RTFILE_O_WRITE;
230 break;
231
232 case 'a': /* Append. */
233 fMode |= RTFILE_O_WRITE | RTFILE_O_APPEND;
234 break;
235
236 case '+':
237 {
238 switch (chPrev)
239 {
240 case 'w':
241 case 'a':
242 /* Also use read access in write mode. */
243 fMode |= RTFILE_O_READ;
244 break;
245
246 case 'r':
247 /* Also use write access in read mode. */
248 fMode |= RTFILE_O_WRITE;
249 break;
250
251 case 'b':
252 case 't':
253 /* Silently eat skipped parameters. */
254 fSkip = true;
255 break;
256
257 case 0: /* No previous character yet. */
258 case '+':
259 /* Eat plusses which don't belong to a command. */
260 fSkip = true;
261 break;
262
263 default:
264 return VERR_INVALID_PARAMETER;
265 }
266
267 break;
268 }
269
270 default:
271 return VERR_INVALID_PARAMETER;
272 }
273
274 if (!fSkip)
275 chPrev = *pszCur;
276 pszCur++;
277 }
278
279 /*
280 * Handle disposition.
281 */
282 pszCur = pszDisposition;
283
284 /* Create a new file, always, overwrite an existing file. */
285 if ( !RTStrCmp(pszCur, "ca")
286 || !RTStrCmp(pszCur, "create-replace"))
287 fMode |= RTFILE_O_CREATE_REPLACE;
288 /* Create a new file if it does not exist, fail if exist. */
289 else if ( !RTStrCmp(pszCur, "ce")
290 || !RTStrCmp(pszCur, "create"))
291 fMode |= RTFILE_O_CREATE;
292 /* Open existing file, create file if does not exist. */
293 else if ( !RTStrCmp(pszCur, "oc")
294 || !RTStrCmp(pszCur, "open-create"))
295 fMode |= RTFILE_O_OPEN_CREATE;
296 /* Open existing file and place the file pointer at the end of the file, if
297 * opened with write access. Create the file if does not exist.
298 * Note! This mode is ill conceived as the appending is a accesss mode not open disposition. */
299 else if ( !RTStrCmp(pszCur, "oa")
300 || !RTStrCmp(pszCur, "open-append"))
301 fMode |= RTFILE_O_OPEN_CREATE | RTFILE_O_APPEND;
302 /* Open existing, fail if does not exist. */
303 else if ( !RTStrCmp(pszCur, "oe")
304 || !RTStrCmp(pszCur, "open"))
305 fMode |= RTFILE_O_OPEN;
306 /* Open and truncate existing, fail of not exist. */
307 else if ( !RTStrCmp(pszCur, "ot")
308 || !RTStrCmp(pszCur, "open-truncate"))
309 fMode |= RTFILE_O_OPEN | RTFILE_O_TRUNCATE;
310 else
311 return VERR_INVALID_PARAMETER;
312
313 /* No action mask set? */
314 if ((fMode & RTFILE_O_ACTION_MASK) == 0)
315 return VERR_INVALID_PARAMETER;
316
317 /*
318 * Sharing mode.
319 */
320 if (!pszSharing || !*pszSharing)
321 fMode |= RTFILE_O_DENY_NONE;
322 else
323 {
324 do
325 {
326 if (pszSharing[0] == 'n')
327 {
328 if (pszSharing[1] == 'r') /* nr (no other readers) */
329 {
330 if (pszSharing[2] == 'w') /* nrw (no other readers or writers) */
331 {
332 fMode |= RTFILE_O_DENY_READWRITE;
333 pszSharing += 3;
334 }
335 else
336 {
337 fMode |= RTFILE_O_DENY_READ;
338 pszSharing += 2;
339 }
340 }
341 else if (pszSharing[1] == 'w') /* nw (no other writers) */
342 {
343 fMode |= RTFILE_O_DENY_WRITE;
344 pszSharing += 2;
345 }
346 else
347 return VERR_INVALID_PARAMETER;
348 }
349 else if (pszSharing[0] == 'd') /* d (don't deny delete) */
350 {
351 fMode |= RTFILE_O_DENY_WRITE;
352 pszSharing++;
353 }
354 else
355 return VERR_INVALID_PARAMETER;
356 } while (*pszSharing != '\0');
357 }
358
359 /* Return. */
360 *pfMode = fMode;
361 return VINF_SUCCESS;
362}
363RT_EXPORT_SYMBOL(RTFileModeToFlagsEx);
364
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