VirtualBox

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

Last change on this file since 95205 was 94311, checked in by vboxsync, 3 years ago

IPRT: Added RTFileCreateUnique, a saner version of RTFileCreateTemp in that it returns the file handle. bugref:10201

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