summaryrefslogtreecommitdiffstats
path: root/src/VBox/Runtime/common/misc/RTFileModeToFlags.cpp
blob: bb6f33f006b53db7e1cf8a2355ee9fd08ca0e2ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/* $Id: RTFileModeToFlags.cpp $ */
/** @file
 * IPRT - RTFileModeToFlags.
 */

/*
 * Copyright (C) 2013-2019 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 *
 * The contents of this file may alternatively be used under the terms
 * of the Common Development and Distribution License Version 1.0
 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
 * VirtualBox OSE distribution, in which case the provisions of the
 * CDDL are applicable instead of those of the GPL.
 *
 * You may elect to license modified versions of this file under the
 * terms and conditions of either the GPL or the CDDL or both.
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#include <iprt/assert.h>
#include <iprt/errcore.h>
#include <iprt/file.h>
#include <iprt/string.h>
#include "internal/iprt.h"


RTR3DECL(int) RTFileModeToFlags(const char *pszMode, uint64_t *pfMode)
{
    AssertPtrReturn(pszMode, VERR_INVALID_POINTER);
    AssertPtrReturn(pfMode, VERR_INVALID_POINTER);

    const char *pszCur = pszMode;
    if (*pszCur == '\0')
        return VERR_INVALID_PARAMETER;

    uint64_t    fMode  = 0;
    char        chPrev = '\0';
    while (    pszCur
           && *pszCur != '\0')
    {
        bool fSkip = false;
        switch (*pszCur)
        {
            /* Opens an existing file for writing and places the
             * file pointer at the end of the file. The file is
             * created if it does not exist. */
            case 'a':
                if ((fMode & RTFILE_O_ACTION_MASK) == 0)
                    fMode |= RTFILE_O_OPEN_CREATE
                           | RTFILE_O_WRITE
                           | RTFILE_O_APPEND;
                else
                    return VERR_INVALID_PARAMETER;
                break;

            case 'b': /* Binary mode. */
                /* Just skip as being valid. */
                fSkip = true;
                break;

            /* Creates a file or open an existing one for
             * writing only. The file pointer will be placed
             * at the beginning of the file.*/
            case 'c':
                if ((fMode & RTFILE_O_ACTION_MASK) == 0)
                    fMode |= RTFILE_O_OPEN_CREATE
                           | RTFILE_O_WRITE;
                else
                    return VERR_INVALID_PARAMETER;
                break;

            /* Opens an existing file for reading and places the
             * file pointer at the beginning of the file. If the
             * file does not exist an error will be returned. */
            case 'r':
                if ((fMode & RTFILE_O_ACTION_MASK) == 0)
                    fMode |=  RTFILE_O_OPEN
                           | RTFILE_O_READ;
                else
                    return VERR_INVALID_PARAMETER;
                break;

            case 't': /* Text mode. */
                /* Just skip as being valid. */
                fSkip = true;
                break;

            /* Creates a new file or replaces an existing one
             * for writing. Places the file pointer at the beginning.
             * An existing file will be truncated to 0 bytes. */
            case 'w':
                if ((fMode & RTFILE_O_ACTION_MASK) == 0)
                    fMode |= RTFILE_O_CREATE_REPLACE
                           | RTFILE_O_WRITE
                           | RTFILE_O_TRUNCATE;
                else
                    return VERR_INVALID_PARAMETER;
                break;

            /* Creates a new file and opens it for writing. Places
             * the file pointer at the beginning. If the file
             * exists an error will be returned. */
            case 'x':
                if ((fMode & RTFILE_O_ACTION_MASK) == 0)
                    fMode |= RTFILE_O_CREATE
                           | RTFILE_O_WRITE;
                else
                    return VERR_INVALID_PARAMETER;
                break;

            case '+':
            {
                switch (chPrev)
                {
                    case 'a':
                    case 'c':
                    case 'w':
                    case 'x':
                        /* Also open / create file with read access. */
                        fMode |= RTFILE_O_READ;
                        break;

                    case 'r':
                        /* Also open / create file with write access. */
                        fMode |= RTFILE_O_WRITE;
                        break;

                    case 'b':
                    case 't':
                        /* Silently eat skipped parameters. */
                        fSkip = true;
                        break;

                    case 0: /* No previous character yet. */
                    case '+':
                        /* Eat plusses which don't belong to a command. */
                        fSkip = true;
                        break;

                    default:
                        return VERR_INVALID_PARAMETER;
                }

                break;
            }

            default:
                return VERR_INVALID_PARAMETER;
        }

        if (!fSkip)
            chPrev = *pszCur;
        pszCur++;
    }

    /* No action mask set? */
    if ((fMode & RTFILE_O_ACTION_MASK) == 0)
        return VERR_INVALID_PARAMETER;

    /** @todo Handle sharing mode */
    fMode |= RTFILE_O_DENY_NONE;

    /* Return. */
    *pfMode = fMode;
    return VINF_SUCCESS;
}
RT_EXPORT_SYMBOL(RTFileModeToFlags);


RTR3DECL(int) RTFileModeToFlagsEx(const char *pszAccess, const char *pszDisposition,
                                  const char *pszSharing, uint64_t *pfMode)
{
    AssertPtrReturn(pszAccess, VERR_INVALID_POINTER);
    AssertPtrReturn(pszDisposition, VERR_INVALID_POINTER);
    AssertPtrNullReturn(pszSharing, VERR_INVALID_POINTER);
    AssertPtrReturn(pfMode, VERR_INVALID_POINTER);

    const char *pszCur = pszAccess;
    if (*pszCur == '\0')
        return VERR_INVALID_PARAMETER;

    /*
     * Handle access mode.
     */
    uint64_t fMode  = 0;
    char     chPrev = '\0';
    while (    pszCur
           && *pszCur != '\0')
    {
        bool fSkip = false;
        switch (*pszCur)
        {
            case 'b': /* Binary mode. */
                /* Just skip as being valid. */
                fSkip = true;
                break;

            case 'r': /* Read. */
                fMode |= RTFILE_O_READ;
                break;

            case 't': /* Text mode. */
                /* Just skip as being valid. */
                fSkip = true;
                break;

            case 'w': /* Write. */
                fMode |= RTFILE_O_WRITE;
                break;

            case '+':
            {
                switch (chPrev)
                {
                    case 'w':
                        /* Also use read access in write mode. */
                        fMode |= RTFILE_O_READ;
                        break;

                    case 'r':
                        /* Also use write access in read mode. */
                        fMode |= RTFILE_O_WRITE;
                        break;

                    case 'b':
                    case 't':
                        /* Silently eat skipped parameters. */
                        fSkip = true;
                        break;

                    case 0: /* No previous character yet. */
                    case '+':
                        /* Eat plusses which don't belong to a command. */
                        fSkip = true;
                        break;

                    default:
                        return VERR_INVALID_PARAMETER;
                }

                break;
            }

            default:
                return VERR_INVALID_PARAMETER;
        }

        if (!fSkip)
            chPrev = *pszCur;
        pszCur++;
    }

    /*
     * Handle disposition.
     */
    pszCur = pszDisposition;

    /* Create a new file, always, overwrite an existing file. */
    if (   !RTStrCmp(pszCur, "ca")
        || !RTStrCmp(pszCur, "create-replace"))
        fMode |= RTFILE_O_CREATE_REPLACE;
    /* Create a new file if it does not exist, fail if exist. */
    else if (   !RTStrCmp(pszCur, "ce")
             || !RTStrCmp(pszCur, "create"))
        fMode |= RTFILE_O_CREATE;
    /* Open existing file, create file if does not exist. */
    else if (   !RTStrCmp(pszCur, "oc")
             || !RTStrCmp(pszCur, "open-create"))
        fMode |= RTFILE_O_OPEN_CREATE;
    /* Open existing file and place the file pointer at
     * the end of the file, if opened with write access.
     * Create the file if does not exist. */
    else if (   !RTStrCmp(pszCur, "oa")
             || !RTStrCmp(pszCur, "open-append"))
        fMode |= RTFILE_O_OPEN_CREATE | RTFILE_O_APPEND;
    /* Open existing, fail if does not exist. */
    else if (   !RTStrCmp(pszCur, "oe")
             || !RTStrCmp(pszCur, "open"))
        fMode |= RTFILE_O_OPEN;
    /* Open and truncate existing, fail of not exist. */
    else if (   !RTStrCmp(pszCur, "ot")
             || !RTStrCmp(pszCur, "open-truncate"))
        fMode |= RTFILE_O_OPEN | RTFILE_O_TRUNCATE;
    else
        return VERR_INVALID_PARAMETER;

    /* No action mask set? */
    if ((fMode & RTFILE_O_ACTION_MASK) == 0)
        return VERR_INVALID_PARAMETER;

    /*
     * Sharing mode.
     */
    if (!pszSharing || !*pszSharing)
        fMode |= RTFILE_O_DENY_NONE;
    else
    {
        do
        {
            if (pszSharing[0] == 'n')
            {
                if (pszSharing[1] == 'r') /* nr (no other readers) */
                {
                    if (pszSharing[2] == 'w') /* nrw (no other readers or writers) */
                    {
                        fMode |= RTFILE_O_DENY_READWRITE;
                        pszSharing += 3;
                    }
                    else
                    {
                        fMode |= RTFILE_O_DENY_READ;
                        pszSharing += 2;
                    }
                }
                else if (pszSharing[1] == 'w') /* nw (no other writers) */
                {
                    fMode |= RTFILE_O_DENY_WRITE;
                    pszSharing += 2;
                }
                else
                    return VERR_INVALID_PARAMETER;
            }
            else if (pszSharing[0] == 'd') /* d (don't deny delete) */
            {
                fMode |= RTFILE_O_DENY_WRITE;
                pszSharing++;
            }
            else
                return VERR_INVALID_PARAMETER;
        } while (*pszSharing != '\0');
    }

    /* Return. */
    *pfMode = fMode;
    return VINF_SUCCESS;
}
RT_EXPORT_SYMBOL(RTFileModeToFlagsEx);