VirtualBox

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