VirtualBox

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

Last change on this file since 91586 was 82968, checked in by vboxsync, 5 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: 7.8 KB
Line 
1/* $Id: createtemp-generic.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * IPRT - temporary file and directory creation, generic implementation.
4 */
5
6/*
7 * Copyright (C) 2009-2020 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) RTFileCreateTemp(char *pszTemplate, RTFMODE fMode)
161{
162 char *pszX = NULL;
163 unsigned cXes = 0;
164 int rc = rtCreateTempValidateTemplate(pszTemplate, &pszX, &cXes);
165 if (RT_FAILURE(rc))
166 {
167 *pszTemplate = '\0';
168 return rc;
169 }
170
171 /*
172 * Try ten thousand times.
173 */
174 int i = 10000;
175 while (i-- > 0)
176 {
177 uint64_t fOpen = RTFILE_O_WRITE | RTFILE_O_DENY_ALL | RTFILE_O_CREATE | RTFILE_O_NOT_CONTENT_INDEXED
178 | fMode << RTFILE_O_CREATE_MODE_SHIFT;
179 rtCreateTempFillTemplate(pszX, cXes);
180 RTFILE hFile = NIL_RTFILE;
181 rc = RTFileOpen(&hFile, pszTemplate, fOpen);
182 if (RT_SUCCESS(rc))
183 {
184 RTFileClose(hFile);
185 return rc;
186 }
187 /** @todo Anything else to consider? */
188 if (rc != VERR_ALREADY_EXISTS)
189 {
190 *pszTemplate = '\0';
191 return rc;
192 }
193 }
194
195 /* we've given up. */
196 *pszTemplate = '\0';
197 return VERR_ALREADY_EXISTS;
198}
199RT_EXPORT_SYMBOL(RTFileCreateTemp);
200
201
202/** @todo Test case for this once it is implemented. */
203RTDECL(int) RTFileCreateTempSecure(char *pszTemplate)
204{
205 /* bool fSafe; */
206
207 /* Temporarily convert pszTemplate to a path. */
208 size_t cchDir = 0;
209 RTPathParseSimple(pszTemplate, &cchDir, NULL, NULL);
210 char chOld = pszTemplate[cchDir];
211 pszTemplate[cchDir] = '\0';
212 /** @todo Implement this. */
213 int rc = /* RTPathIsSecure(pszTemplate, &fSafe) */ VERR_NOT_SUPPORTED;
214 pszTemplate[cchDir] = chOld;
215 if (RT_SUCCESS(rc) /* && fSafe */)
216 return RTFileCreateTemp(pszTemplate, 0600);
217
218 *pszTemplate = '\0';
219 /** @todo Replace VERR_PERMISSION_DENIED. VERR_INSECURE? */
220 return RT_FAILURE(rc) ? rc : VERR_PERMISSION_DENIED;
221}
222RT_EXPORT_SYMBOL(RTFileCreateTempSecure);
223
224
225RTDECL(int) RTFileOpenTemp(PRTFILE phFile, char *pszFilename, size_t cbFilename, uint64_t fOpen)
226{
227 AssertReturn((fOpen & RTFILE_O_ACTION_MASK) == RTFILE_O_CREATE, VERR_INVALID_FLAGS);
228 AssertReturn(fOpen & RTFILE_O_WRITE, VERR_INVALID_FLAGS);
229
230 /*
231 * Start by obtaining the path to the temporary directory.
232 */
233 int rc = RTPathTemp(pszFilename, cbFilename);
234 if (RT_SUCCESS(rc))
235 {
236 /*
237 * Add a filename pattern.
238 */
239 static char const s_szTemplate[] = "IPRT-XXXXXXXXXXXX.tmp";
240 rc = RTPathAppend(pszFilename, cbFilename, s_szTemplate);
241 if (RT_SUCCESS(rc))
242 {
243 char * const pszX = RTStrEnd(pszFilename, cbFilename) - (sizeof(s_szTemplate) - 1) + 5;
244 unsigned cXes = sizeof(s_szTemplate) - 1 - 4 - 5;
245 Assert(pszX[0] == 'X'); Assert(pszX[-1] == '-'); Assert(pszX[cXes] == '.');
246
247 /*
248 * Try 10000 times with random names.
249 */
250 unsigned cTriesLeft = 10000;
251 while (cTriesLeft-- > 0)
252 {
253 rtCreateTempFillTemplate(pszX, cXes);
254 rc = RTFileOpen(phFile, pszFilename, fOpen);
255 if (RT_SUCCESS(rc))
256 return rc;
257 }
258 }
259 }
260
261 if (cbFilename)
262 *pszFilename = '\0';
263 *phFile = NIL_RTFILE;
264 return rc;
265}
266RT_EXPORT_SYMBOL(RTFileOpenTemp);
267
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