VirtualBox

source: vbox/trunk/src/VBox/Runtime/generic/createtemp-generic.cpp@ 100762

Last change on this file since 100762 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: 8.6 KB
Line 
1/* $Id: createtemp-generic.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - temporary file and directory creation, generic implementation.
4 */
5
6/*
7 * Copyright (C) 2009-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/dir.h>
42#include "internal/iprt.h"
43
44#include <iprt/assert.h>
45#include <iprt/err.h>
46#include <iprt/file.h>
47#include <iprt/path.h>
48#include <iprt/rand.h>
49#include <iprt/string.h>
50
51
52/**
53 * The X'es may be trailing, or they may be a cluster of 3 or more inside
54 * the file name.
55 */
56static int rtCreateTempValidateTemplate(char *pszTemplate, char **ppszX, unsigned *pcXes)
57{
58 AssertPtr(pszTemplate);
59 AssertPtr(ppszX);
60 AssertPtr(pcXes);
61
62 unsigned cXes = 0;
63 char *pszX = strchr(pszTemplate, '\0');
64 if ( pszX != pszTemplate
65 && pszX[-1] != 'X')
66 {
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 {
76 if ( pszXEnd[-1] == 'X'
77 && pszXEnd[-2] == 'X'
78 && pszXEnd[-3] == 'X')
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 {
92 pszX--;
93 cXes++;
94 }
95
96 /* fail if none found. */
97 *ppszX = pszX;
98 *pcXes = cXes;
99 AssertReturn(cXes > 0, VERR_INVALID_PARAMETER);
100 return VINF_SUCCESS;
101}
102
103
104static 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
113RTDECL(int) RTDirCreateTemp(char *pszTemplate, RTFMODE fMode)
114{
115 char *pszX = NULL;
116 unsigned cXes = 0;
117 int rc = rtCreateTempValidateTemplate(pszTemplate, &pszX, &cXes);
118 if (RT_FAILURE(rc))
119 {
120 *pszTemplate = '\0';
121 return rc;
122 }
123 /*
124 * Try ten thousand times.
125 */
126 int i = 10000;
127 while (i-- > 0)
128 {
129 rtCreateTempFillTemplate(pszX, cXes);
130 rc = RTDirCreate(pszTemplate, fMode, 0);
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}
144RT_EXPORT_SYMBOL(RTDirCreateTemp);
145
146
147/** @todo Test case for this once it is implemented. */
148RTDECL(int) RTDirCreateTempSecure(char *pszTemplate)
149{
150 /* bool fSafe; */
151
152 /* Temporarily convert pszTemplate to a path. */
153 size_t cchDir = 0;
154 RTPathParseSimple(pszTemplate, &cchDir, NULL, NULL);
155 char chOld = pszTemplate[cchDir];
156 pszTemplate[cchDir] = '\0';
157 /** @todo Implement this. */
158 int rc = /* RTPathIsSecure(pszTemplate, &fSafe) */ VERR_NOT_SUPPORTED;
159 pszTemplate[cchDir] = chOld;
160 if (RT_SUCCESS(rc) /* && fSafe */)
161 return RTDirCreateTemp(pszTemplate, 0700);
162
163 *pszTemplate = '\0';
164 /** @todo Replace VERR_PERMISSION_DENIED. VERR_INSECURE? */
165 return RT_FAILURE(rc) ? rc : VERR_PERMISSION_DENIED;
166}
167RT_EXPORT_SYMBOL(RTDirCreateTempSecure);
168
169
170RTDECL(int) RTFileCreateUnique(PRTFILE phFile, char *pszTemplate, uint64_t fOpen)
171{
172 /*
173 * Validate input.
174 */
175 *phFile = NIL_RTFILE;
176 AssertReturn((fOpen & RTFILE_O_ACTION_MASK) == RTFILE_O_CREATE, VERR_INVALID_FLAGS);
177 char *pszX = NULL;
178 unsigned cXes = 0;
179 int rc = rtCreateTempValidateTemplate(pszTemplate, &pszX, &cXes);
180 if (RT_SUCCESS(rc))
181 {
182 /*
183 * Try ten thousand times.
184 */
185 int i = 10000;
186 while (i-- > 0)
187 {
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 }
202 }
203
204 /* we've given up. */
205 rc = VERR_ALREADY_EXISTS;
206 }
207 *pszTemplate = '\0';
208 return rc;
209}
210RT_EXPORT_SYMBOL(RTFileCreateUnique);
211
212
213RTDECL(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}
223RT_EXPORT_SYMBOL(RTFileCreateTemp);
224
225
226/** @todo Test case for this once it is implemented. */
227RTDECL(int) RTFileCreateTempSecure(char *pszTemplate)
228{
229 /* bool fSafe; */
230
231 /* Temporarily convert pszTemplate to a path. */
232 size_t cchDir = 0;
233 RTPathParseSimple(pszTemplate, &cchDir, NULL, NULL);
234 char chOld = pszTemplate[cchDir];
235 pszTemplate[cchDir] = '\0';
236 /** @todo Implement this. */
237 int rc = /* RTPathIsSecure(pszTemplate, &fSafe) */ VERR_NOT_SUPPORTED;
238 pszTemplate[cchDir] = chOld;
239 if (RT_SUCCESS(rc) /* && fSafe */)
240 return RTFileCreateTemp(pszTemplate, 0600);
241
242 *pszTemplate = '\0';
243 /** @todo Replace VERR_PERMISSION_DENIED. VERR_INSECURE? */
244 return RT_FAILURE(rc) ? rc : VERR_PERMISSION_DENIED;
245}
246RT_EXPORT_SYMBOL(RTFileCreateTempSecure);
247
248
249RTDECL(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}
290RT_EXPORT_SYMBOL(RTFileOpenTemp);
291
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