VirtualBox

source: vbox/trunk/src/VBox/Runtime/generic/RTDirCreateUniqueNumbered-generic.cpp@ 98278

Last change on this file since 98278 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: 5.2 KB
Line 
1/* $Id: RTDirCreateUniqueNumbered-generic.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - RTDirCreateUniqueNumbered, generic implementation.
4 */
5
6/*
7 * Copyright (C) 2011-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/path.h>
47#include <iprt/rand.h>
48#include <iprt/string.h>
49
50
51RTDECL(int) RTDirCreateUniqueNumbered(char *pszPath, size_t cbSize, RTFMODE fMode, size_t cchDigits, char chSep)
52{
53 /*
54 * Validate input.
55 */
56 AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
57 AssertReturn(cbSize, VERR_BUFFER_OVERFLOW);
58 AssertReturn(cchDigits > 0, VERR_INVALID_PARAMETER);
59 AssertReturn(cchDigits < 64, VERR_INVALID_PARAMETER);
60
61 /* Check that there is sufficient space. */
62 char *pszEnd = RTStrEnd(pszPath, cbSize);
63 AssertReturn(pszEnd, VERR_BUFFER_OVERFLOW);
64 size_t cbLeft = cbSize - (pszEnd - pszPath);
65 AssertReturn(cbLeft > (chSep ? 1U : 0U) + cchDigits, VERR_BUFFER_OVERFLOW);
66
67 /*
68 * First try the pretty name without any numbers appended.
69 */
70 int rc = RTDirCreate(pszPath, fMode, 0);
71 if (RT_SUCCESS(rc))
72 return rc;
73 if (rc == VERR_ALREADY_EXISTS)
74 {
75 /*
76 * Already exist, apply template specification.
77 */
78
79 /* Max 10000 tries (like RTDirCreateTemp), but stop earlier if we haven't got enough digits to work with. */
80 uint32_t cMaxTries;
81 switch (cchDigits)
82 {
83 case 1: cMaxTries = 40; break;
84 case 2: cMaxTries = 400; break;
85 case 3: cMaxTries = 4000; break;
86 default: cMaxTries = 10000; break;
87 }
88
89 static uint64_t const s_aEndSeqs[] =
90 {
91 UINT64_C(0),
92 UINT64_C(9),
93 UINT64_C(99),
94 UINT64_C(999),
95 UINT64_C(9999),
96 UINT64_C(99999),
97 UINT64_C(999999),
98 UINT64_C(9999999),
99 UINT64_C(99999999),
100 UINT64_C(999999999),
101 UINT64_C(9999999999),
102 UINT64_C(99999999999),
103 UINT64_C(999999999999),
104 UINT64_C(9999999999999),
105 UINT64_C(99999999999999),
106 UINT64_C(999999999999999),
107 UINT64_C(9999999999999999),
108 UINT64_C(99999999999999999),
109 UINT64_C(999999999999999999),
110 UINT64_C(9999999999999999999),
111 };
112 uint64_t const uEndSeq = cchDigits < RT_ELEMENTS(s_aEndSeqs) ? s_aEndSeqs[cchDigits] : UINT64_MAX;
113
114 /* Add separator if requested. */
115 if (chSep != '\0')
116 {
117 *pszEnd++ = chSep;
118 *pszEnd = '\0';
119 cbLeft--;
120 }
121
122 Assert(cbLeft > cchDigits);
123 for (uint32_t iTry = 0; iTry <= cMaxTries; iTry++)
124 {
125 /* Try sequentially first for a little bit, then switch to random numbers. */
126 uint64_t iSeq;
127 if (iTry > 20)
128 iSeq = RTRandU64Ex(0, uEndSeq);
129 else
130 {
131 iSeq = iTry;
132 if (uEndSeq < UINT64_MAX)
133 iSeq %= uEndSeq + 1;
134 }
135 ssize_t cchRet = RTStrFormatU64(pszEnd, cbLeft, iSeq, 10 /*uiBase*/,
136 (int)cchDigits /*cchWidth*/, 0 /*cchPrecision*/, RTSTR_F_WIDTH | RTSTR_F_ZEROPAD);
137 Assert((size_t)cchRet == cchDigits); NOREF(cchRet);
138
139 rc = RTDirCreate(pszPath, fMode, 0);
140 if (RT_SUCCESS(rc))
141 return rc;
142 if (rc != VERR_ALREADY_EXISTS)
143 break;
144 }
145 }
146
147 /* We've given up or failed. */
148 *pszPath = '\0';
149 return rc;
150}
151RT_EXPORT_SYMBOL(RTDirCreateUniqueNumbered);
152
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