VirtualBox

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

Last change on this file since 36586 was 36582, checked in by vboxsync, 14 years ago

iprt: next burn fix

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.0 KB
Line 
1/* $Id: RTDirCreateUniqueNumbered-generic.cpp 36582 2011-04-06 14:02:03Z vboxsync $ */
2/** @file
3 * IPRT - RTDirCreateUniqueNumbered, generic implementation.
4 */
5
6/*
7 * Copyright (C) 2011 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
39RTDECL(int) RTDirCreateUniqueNumbered(char *pszPath, size_t cbSize, RTFMODE fMode, signed int cchDigits, char chSep)
40{
41 /*
42 * Validate input.
43 */
44 AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
45 AssertReturn(cbSize, VERR_INVALID_PARAMETER);
46 AssertReturn(cchDigits, VERR_INVALID_PARAMETER);
47 /* Check for enough space. */
48 char *pszEnd = strchr(pszPath, '\0');
49 AssertReturn(cbSize - 1 - (pszEnd - pszPath) >= (size_t)cchDigits + (chSep ? 1 : 0), VERR_BUFFER_OVERFLOW);
50
51 /* First try is to create the path without any numbers. */
52 int rc = RTDirCreate(pszPath, fMode);
53 if ( RT_SUCCESS(rc)
54 || rc != VERR_ALREADY_EXISTS)
55 return rc;
56
57 /* If the separator value isn't zero, add it. */
58 if (chSep != '\0')
59 {
60 *pszEnd++ = chSep;
61 *pszEnd = '\0';
62 }
63
64 /* How many tries? */
65 size_t cMaxTries = 10;
66 for (size_t a = 0; a < (size_t)cchDigits - 1; ++a)
67 cMaxTries *= 10;
68
69 /* Try cMaxTries - 1 counts to create a directory with the appended number. */
70 size_t i = 1;
71 while (i < cMaxTries)
72 {
73 /* Format the number with leading zero's. */
74 rc = RTStrFormatNumber(pszEnd, i, 10, cchDigits, 0, RTSTR_F_WIDTH | RTSTR_F_ZEROPAD);
75 if (RT_FAILURE(rc))
76 {
77 *pszPath = '\0';
78 return rc;
79 }
80 rc = RTDirCreate(pszPath, fMode);
81 if (RT_SUCCESS(rc))
82 return rc;
83 ++i;
84 }
85
86 /* We've given up. */
87 *pszPath = '\0';
88 return VERR_ALREADY_EXISTS;
89}
90RT_EXPORT_SYMBOL(RTDirCreateUniqueNumbered);
91
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