1 | /* $Id: RTDirCreateUniqueNumbered-generic.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTDirCreateUniqueNumbered, generic implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2015 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/string.h>
|
---|
38 |
|
---|
39 |
|
---|
40 | RTDECL(int) RTDirCreateUniqueNumbered(char *pszPath, size_t cbSize, RTFMODE fMode, signed int cchDigits, char chSep)
|
---|
41 | {
|
---|
42 | /*
|
---|
43 | * Validate input.
|
---|
44 | */
|
---|
45 | AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
|
---|
46 | AssertReturn(cbSize, VERR_BUFFER_OVERFLOW);
|
---|
47 | AssertReturn(cchDigits > 0, VERR_INVALID_PARAMETER);
|
---|
48 |
|
---|
49 | /* Check that there is sufficient space. */
|
---|
50 | char *pszEnd = RTStrEnd(pszPath, cbSize);
|
---|
51 | AssertReturn(pszEnd, VERR_BUFFER_OVERFLOW);
|
---|
52 | AssertReturn(cbSize - 1 - (pszEnd - pszPath) >= (size_t)cchDigits + (chSep ? 1 : 0), VERR_BUFFER_OVERFLOW);
|
---|
53 | size_t cbLeft = cbSize - (pszEnd - pszPath);
|
---|
54 |
|
---|
55 | /* First try is to create the path without any numbers. */
|
---|
56 | int rc = RTDirCreate(pszPath, fMode, 0);
|
---|
57 | if ( RT_SUCCESS(rc)
|
---|
58 | || rc != VERR_ALREADY_EXISTS)
|
---|
59 | return rc;
|
---|
60 |
|
---|
61 | /* If the separator value isn't zero, add it. */
|
---|
62 | if (chSep != '\0')
|
---|
63 | {
|
---|
64 | cbLeft--;
|
---|
65 | *pszEnd++ = chSep;
|
---|
66 | *pszEnd = '\0';
|
---|
67 | }
|
---|
68 |
|
---|
69 | /* How many tries? Stay within somewhat sane limits. */
|
---|
70 | uint32_t cMaxTries;
|
---|
71 | if (cchDigits >= 8)
|
---|
72 | cMaxTries = 100 * _1M;
|
---|
73 | else
|
---|
74 | {
|
---|
75 | cMaxTries = 10;
|
---|
76 | for (int a = 0; a < cchDigits - 1; ++a)
|
---|
77 | cMaxTries *= 10;
|
---|
78 | }
|
---|
79 |
|
---|
80 | /* Try cMaxTries - 1 times to create a directory with appended numbers. */
|
---|
81 | uint32_t i = 1;
|
---|
82 | while (i < cMaxTries)
|
---|
83 | {
|
---|
84 | /* Format the number with leading zero's. */
|
---|
85 | ssize_t rc2 = RTStrFormatU32(pszEnd, cbLeft, i, 10, cchDigits, 0, RTSTR_F_WIDTH | RTSTR_F_ZEROPAD);
|
---|
86 | if (RT_FAILURE((int) rc2))
|
---|
87 | {
|
---|
88 | *pszPath = '\0';
|
---|
89 | return (int)rc2;
|
---|
90 | }
|
---|
91 | rc = RTDirCreate(pszPath, fMode, 0);
|
---|
92 | if (RT_SUCCESS(rc))
|
---|
93 | return rc;
|
---|
94 | ++i;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /* We've given up. */
|
---|
98 | *pszPath = '\0';
|
---|
99 | return VERR_ALREADY_EXISTS;
|
---|
100 | }
|
---|
101 | RT_EXPORT_SYMBOL(RTDirCreateUniqueNumbered);
|
---|
102 |
|
---|