[20104] | 1 | /* $Id: createtemp-generic.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[42712] | 3 | * IPRT - temporary file and directory creation, generic implementation.
|
---|
[20104] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2009-2023 Oracle and/or its affiliates.
|
---|
[20104] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[20104] | 11 | *
|
---|
[96407] | 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 | *
|
---|
[20104] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 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
|
---|
[20104] | 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.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[20104] | 35 | */
|
---|
| 36 |
|
---|
| 37 |
|
---|
[57358] | 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[20104] | 41 | #include <iprt/dir.h>
|
---|
[21337] | 42 | #include "internal/iprt.h"
|
---|
[20104] | 43 |
|
---|
| 44 | #include <iprt/assert.h>
|
---|
| 45 | #include <iprt/err.h>
|
---|
[42712] | 46 | #include <iprt/file.h>
|
---|
[20111] | 47 | #include <iprt/path.h>
|
---|
[20104] | 48 | #include <iprt/rand.h>
|
---|
| 49 | #include <iprt/string.h>
|
---|
| 50 |
|
---|
| 51 |
|
---|
[79200] | 52 | /**
|
---|
| 53 | * The X'es may be trailing, or they may be a cluster of 3 or more inside
|
---|
| 54 | * the file name.
|
---|
| 55 | */
|
---|
| 56 | static int rtCreateTempValidateTemplate(char *pszTemplate, char **ppszX, unsigned *pcXes)
|
---|
[20104] | 57 | {
|
---|
| 58 | AssertPtr(pszTemplate);
|
---|
[42692] | 59 | AssertPtr(ppszX);
|
---|
| 60 | AssertPtr(pcXes);
|
---|
[79200] | 61 |
|
---|
[20104] | 62 | unsigned cXes = 0;
|
---|
| 63 | char *pszX = strchr(pszTemplate, '\0');
|
---|
[20111] | 64 | if ( pszX != pszTemplate
|
---|
| 65 | && pszX[-1] != 'X')
|
---|
[20104] | 66 | {
|
---|
[20111] | 67 | /* look inside the file name. */
|
---|
| 68 | char *pszFilename = RTPathFilename(pszTemplate);
|
---|
| 69 | if ( pszFilename
|
---|
| 70 | && (size_t)(pszX - pszFilename) > 3)
|
---|
| 71 | {
|
---|
| 72 | char *pszXEnd = pszX - 1;
|
---|
| 73 | pszFilename += 3;
|
---|
| 74 | do
|
---|
| 75 | {
|
---|
[79200] | 76 | if ( pszXEnd[-1] == 'X'
|
---|
| 77 | && pszXEnd[-2] == 'X'
|
---|
| 78 | && pszXEnd[-3] == 'X')
|
---|
[20111] | 79 | {
|
---|
| 80 | pszX = pszXEnd - 3;
|
---|
| 81 | cXes = 3;
|
---|
| 82 | break;
|
---|
| 83 | }
|
---|
| 84 | } while (pszXEnd-- != pszFilename);
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | /* count them */
|
---|
| 89 | while ( pszX != pszTemplate
|
---|
| 90 | && pszX[-1] == 'X')
|
---|
| 91 | {
|
---|
[20104] | 92 | pszX--;
|
---|
| 93 | cXes++;
|
---|
| 94 | }
|
---|
[20111] | 95 |
|
---|
| 96 | /* fail if none found. */
|
---|
[42692] | 97 | *ppszX = pszX;
|
---|
| 98 | *pcXes = cXes;
|
---|
[79200] | 99 | AssertReturn(cXes > 0, VERR_INVALID_PARAMETER);
|
---|
[42692] | 100 | return VINF_SUCCESS;
|
---|
| 101 | }
|
---|
[20104] | 102 |
|
---|
[42692] | 103 |
|
---|
| 104 | static void rtCreateTempFillTemplate(char *pszX, unsigned cXes)
|
---|
| 105 | {
|
---|
| 106 | static char const s_sz[] = "0123456789abcdefghijklmnopqrstuvwxyz";
|
---|
| 107 | unsigned j = cXes;
|
---|
| 108 | while (j-- > 0)
|
---|
| 109 | pszX[j] = s_sz[RTRandU32Ex(0, RT_ELEMENTS(s_sz) - 2)];
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 |
|
---|
[42708] | 113 | RTDECL(int) RTDirCreateTemp(char *pszTemplate, RTFMODE fMode)
|
---|
[42692] | 114 | {
|
---|
[42712] | 115 | char *pszX = NULL;
|
---|
[42709] | 116 | unsigned cXes = 0;
|
---|
[42692] | 117 | int rc = rtCreateTempValidateTemplate(pszTemplate, &pszX, &cXes);
|
---|
| 118 | if (RT_FAILURE(rc))
|
---|
| 119 | {
|
---|
| 120 | *pszTemplate = '\0';
|
---|
| 121 | return rc;
|
---|
| 122 | }
|
---|
[20104] | 123 | /*
|
---|
| 124 | * Try ten thousand times.
|
---|
| 125 | */
|
---|
| 126 | int i = 10000;
|
---|
| 127 | while (i-- > 0)
|
---|
| 128 | {
|
---|
[42692] | 129 | rtCreateTempFillTemplate(pszX, cXes);
|
---|
[42708] | 130 | rc = RTDirCreate(pszTemplate, fMode, 0);
|
---|
[20104] | 131 | if (RT_SUCCESS(rc))
|
---|
| 132 | return rc;
|
---|
| 133 | if (rc != VERR_ALREADY_EXISTS)
|
---|
| 134 | {
|
---|
| 135 | *pszTemplate = '\0';
|
---|
| 136 | return rc;
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | /* we've given up. */
|
---|
| 141 | *pszTemplate = '\0';
|
---|
| 142 | return VERR_ALREADY_EXISTS;
|
---|
| 143 | }
|
---|
[21337] | 144 | RT_EXPORT_SYMBOL(RTDirCreateTemp);
|
---|
[20104] | 145 |
|
---|
[42712] | 146 |
|
---|
| 147 | /** @todo Test case for this once it is implemented. */
|
---|
| 148 | RTDECL(int) RTDirCreateTempSecure(char *pszTemplate)
|
---|
| 149 | {
|
---|
| 150 | /* bool fSafe; */
|
---|
| 151 |
|
---|
| 152 | /* Temporarily convert pszTemplate to a path. */
|
---|
[79200] | 153 | size_t cchDir = 0;
|
---|
[45375] | 154 | RTPathParseSimple(pszTemplate, &cchDir, NULL, NULL);
|
---|
[79200] | 155 | char chOld = pszTemplate[cchDir];
|
---|
[42712] | 156 | pszTemplate[cchDir] = '\0';
|
---|
| 157 | /** @todo Implement this. */
|
---|
[79200] | 158 | int rc = /* RTPathIsSecure(pszTemplate, &fSafe) */ VERR_NOT_SUPPORTED;
|
---|
[42712] | 159 | pszTemplate[cchDir] = chOld;
|
---|
| 160 | if (RT_SUCCESS(rc) /* && fSafe */)
|
---|
| 161 | return RTDirCreateTemp(pszTemplate, 0700);
|
---|
[79200] | 162 |
|
---|
| 163 | *pszTemplate = '\0';
|
---|
| 164 | /** @todo Replace VERR_PERMISSION_DENIED. VERR_INSECURE? */
|
---|
| 165 | return RT_FAILURE(rc) ? rc : VERR_PERMISSION_DENIED;
|
---|
[42712] | 166 | }
|
---|
| 167 | RT_EXPORT_SYMBOL(RTDirCreateTempSecure);
|
---|
| 168 |
|
---|
| 169 |
|
---|
[94311] | 170 | RTDECL(int) RTFileCreateUnique(PRTFILE phFile, char *pszTemplate, uint64_t fOpen)
|
---|
[42712] | 171 | {
|
---|
[94311] | 172 | /*
|
---|
| 173 | * Validate input.
|
---|
| 174 | */
|
---|
| 175 | *phFile = NIL_RTFILE;
|
---|
| 176 | AssertReturn((fOpen & RTFILE_O_ACTION_MASK) == RTFILE_O_CREATE, VERR_INVALID_FLAGS);
|
---|
[42712] | 177 | char *pszX = NULL;
|
---|
| 178 | unsigned cXes = 0;
|
---|
| 179 | int rc = rtCreateTempValidateTemplate(pszTemplate, &pszX, &cXes);
|
---|
[94311] | 180 | if (RT_SUCCESS(rc))
|
---|
[42712] | 181 | {
|
---|
[94311] | 182 | /*
|
---|
| 183 | * Try ten thousand times.
|
---|
| 184 | */
|
---|
| 185 | int i = 10000;
|
---|
| 186 | while (i-- > 0)
|
---|
[42712] | 187 | {
|
---|
[94311] | 188 | rtCreateTempFillTemplate(pszX, cXes);
|
---|
| 189 | RTFILE hFile = NIL_RTFILE;
|
---|
| 190 | rc = RTFileOpen(&hFile, pszTemplate, fOpen);
|
---|
| 191 | if (RT_SUCCESS(rc))
|
---|
| 192 | {
|
---|
| 193 | *phFile = hFile;
|
---|
| 194 | return rc;
|
---|
| 195 | }
|
---|
| 196 | /** @todo Anything else to consider? */
|
---|
| 197 | if (rc != VERR_ALREADY_EXISTS)
|
---|
| 198 | {
|
---|
| 199 | *pszTemplate = '\0';
|
---|
| 200 | return rc;
|
---|
| 201 | }
|
---|
[42712] | 202 | }
|
---|
[94311] | 203 |
|
---|
| 204 | /* we've given up. */
|
---|
| 205 | rc = VERR_ALREADY_EXISTS;
|
---|
[42712] | 206 | }
|
---|
| 207 | *pszTemplate = '\0';
|
---|
[94311] | 208 | return rc;
|
---|
[42712] | 209 | }
|
---|
[94311] | 210 | RT_EXPORT_SYMBOL(RTFileCreateUnique);
|
---|
| 211 |
|
---|
| 212 |
|
---|
| 213 | RTDECL(int) RTFileCreateTemp(char *pszTemplate, RTFMODE fMode)
|
---|
| 214 | {
|
---|
| 215 | RTFILE hFile = NIL_RTFILE;
|
---|
| 216 | int rc = RTFileCreateUnique(&hFile, pszTemplate,
|
---|
| 217 | RTFILE_O_WRITE | RTFILE_O_DENY_ALL | RTFILE_O_CREATE | RTFILE_O_NOT_CONTENT_INDEXED
|
---|
| 218 | | fMode << RTFILE_O_CREATE_MODE_SHIFT);
|
---|
| 219 | if (RT_SUCCESS(rc))
|
---|
| 220 | RTFileClose(hFile);
|
---|
| 221 | return rc;
|
---|
| 222 | }
|
---|
[42712] | 223 | RT_EXPORT_SYMBOL(RTFileCreateTemp);
|
---|
| 224 |
|
---|
| 225 |
|
---|
| 226 | /** @todo Test case for this once it is implemented. */
|
---|
| 227 | RTDECL(int) RTFileCreateTempSecure(char *pszTemplate)
|
---|
| 228 | {
|
---|
| 229 | /* bool fSafe; */
|
---|
| 230 |
|
---|
| 231 | /* Temporarily convert pszTemplate to a path. */
|
---|
[79200] | 232 | size_t cchDir = 0;
|
---|
[45375] | 233 | RTPathParseSimple(pszTemplate, &cchDir, NULL, NULL);
|
---|
[79200] | 234 | char chOld = pszTemplate[cchDir];
|
---|
[42712] | 235 | pszTemplate[cchDir] = '\0';
|
---|
| 236 | /** @todo Implement this. */
|
---|
[79200] | 237 | int rc = /* RTPathIsSecure(pszTemplate, &fSafe) */ VERR_NOT_SUPPORTED;
|
---|
[42712] | 238 | pszTemplate[cchDir] = chOld;
|
---|
| 239 | if (RT_SUCCESS(rc) /* && fSafe */)
|
---|
| 240 | return RTFileCreateTemp(pszTemplate, 0600);
|
---|
[79200] | 241 |
|
---|
| 242 | *pszTemplate = '\0';
|
---|
| 243 | /** @todo Replace VERR_PERMISSION_DENIED. VERR_INSECURE? */
|
---|
| 244 | return RT_FAILURE(rc) ? rc : VERR_PERMISSION_DENIED;
|
---|
[42712] | 245 | }
|
---|
| 246 | RT_EXPORT_SYMBOL(RTFileCreateTempSecure);
|
---|
[57613] | 247 |
|
---|
| 248 |
|
---|
| 249 | RTDECL(int) RTFileOpenTemp(PRTFILE phFile, char *pszFilename, size_t cbFilename, uint64_t fOpen)
|
---|
| 250 | {
|
---|
| 251 | AssertReturn((fOpen & RTFILE_O_ACTION_MASK) == RTFILE_O_CREATE, VERR_INVALID_FLAGS);
|
---|
| 252 | AssertReturn(fOpen & RTFILE_O_WRITE, VERR_INVALID_FLAGS);
|
---|
| 253 |
|
---|
| 254 | /*
|
---|
| 255 | * Start by obtaining the path to the temporary directory.
|
---|
| 256 | */
|
---|
| 257 | int rc = RTPathTemp(pszFilename, cbFilename);
|
---|
| 258 | if (RT_SUCCESS(rc))
|
---|
| 259 | {
|
---|
| 260 | /*
|
---|
| 261 | * Add a filename pattern.
|
---|
| 262 | */
|
---|
| 263 | static char const s_szTemplate[] = "IPRT-XXXXXXXXXXXX.tmp";
|
---|
| 264 | rc = RTPathAppend(pszFilename, cbFilename, s_szTemplate);
|
---|
| 265 | if (RT_SUCCESS(rc))
|
---|
| 266 | {
|
---|
| 267 | char * const pszX = RTStrEnd(pszFilename, cbFilename) - (sizeof(s_szTemplate) - 1) + 5;
|
---|
| 268 | unsigned cXes = sizeof(s_szTemplate) - 1 - 4 - 5;
|
---|
| 269 | Assert(pszX[0] == 'X'); Assert(pszX[-1] == '-'); Assert(pszX[cXes] == '.');
|
---|
| 270 |
|
---|
| 271 | /*
|
---|
| 272 | * Try 10000 times with random names.
|
---|
| 273 | */
|
---|
| 274 | unsigned cTriesLeft = 10000;
|
---|
| 275 | while (cTriesLeft-- > 0)
|
---|
| 276 | {
|
---|
| 277 | rtCreateTempFillTemplate(pszX, cXes);
|
---|
| 278 | rc = RTFileOpen(phFile, pszFilename, fOpen);
|
---|
| 279 | if (RT_SUCCESS(rc))
|
---|
| 280 | return rc;
|
---|
| 281 | }
|
---|
| 282 | }
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | if (cbFilename)
|
---|
| 286 | *pszFilename = '\0';
|
---|
| 287 | *phFile = NIL_RTFILE;
|
---|
| 288 | return rc;
|
---|
| 289 | }
|
---|
| 290 | RT_EXPORT_SYMBOL(RTFileOpenTemp);
|
---|
| 291 |
|
---|