VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/path/RTPathAbsEx.cpp@ 78048

Last change on this file since 78048 was 78048, checked in by vboxsync, 6 years ago

IPRT: Working on new RTPathAbsEx implementation, temporarily called RTPathAbsExEx. This should fix most of the bugs in the current RTPathAbs/Ex code and do away with the fixed path buffer limitations. Also introduces a RTPATH_BIG_MAX, given that RTPATH_MAX is just a reasonable and not absolute MAX value. The new one is more or less absolute and must never be used for stack buffers. bugref:9172

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 4.2 KB
Line 
1/* $Id: RTPathAbsEx.cpp 78048 2019-04-09 01:21:09Z vboxsync $ */
2/** @file
3 * IPRT - RTPathAbsEx
4 */
5
6/*
7 * Copyright (C) 2006-2019 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 "internal/iprt.h"
32#include <iprt/path.h>
33#include <iprt/err.h>
34#include <iprt/param.h>
35#include <iprt/string.h>
36#include "internal/path.h"
37
38
39
40/**
41 * Get the absolute path (no symlinks, no . or .. components), assuming the
42 * given base path as the current directory. The resulting path doesn't have
43 * to exist.
44 *
45 * @returns iprt status code.
46 * @param pszBase The base path to act like a current directory.
47 * When NULL, the actual cwd is used (i.e. the call
48 * is equivalent to RTPathAbs(pszPath, ...).
49 * @param pszPath The path to resolve.
50 * @param pszAbsPath Where to store the absolute path.
51 * @param cbAbsPath Size of the buffer.
52 */
53RTDECL(int) RTPathAbsEx(const char *pszBase, const char *pszPath, char *pszAbsPath, size_t cbAbsPath)
54{
55#if 1
56 if ( pszBase
57 && pszPath
58 && !rtPathVolumeSpecLen(pszPath)
59 )
60 {
61# if defined(RT_OS_WINDOWS)
62 /* The format for very long paths is not supported. */
63 if ( RTPATH_IS_SLASH(pszBase[0])
64 && RTPATH_IS_SLASH(pszBase[1])
65 && pszBase[2] == '?'
66 && RTPATH_IS_SLASH(pszBase[3])
67 )
68 return VERR_INVALID_NAME;
69# endif
70
71 /** @todo there are a couple of things which isn't 100% correct, although the
72 * current code will have to do for now, no time to fix.
73 *
74 * 1) On Windows & OS/2 we confuse '/' with an abspath spec and will
75 * not necessarily resolve it on the right drive.
76 * 2) A trailing slash in the base might cause UNC names to be created.
77 */
78 size_t const cchPath = strlen(pszPath);
79 char szTmpPath[RTPATH_MAX];
80 if (RTPATH_IS_SLASH(pszPath[0]))
81 {
82 /* join the disk name from base and the path (DOS systems only) */
83 size_t const cchVolSpec = rtPathVolumeSpecLen(pszBase);
84 if (cchVolSpec + cchPath + 1 > sizeof(szTmpPath))
85 return VERR_FILENAME_TOO_LONG;
86 memcpy(szTmpPath, pszBase, cchVolSpec);
87 memcpy(&szTmpPath[cchVolSpec], pszPath, cchPath + 1);
88 }
89 else
90 {
91 /* join the base path and the path */
92 size_t const cchBase = strlen(pszBase);
93 if (cchBase + 1 + cchPath + 1 > sizeof(szTmpPath))
94 return VERR_FILENAME_TOO_LONG;
95 memcpy(szTmpPath, pszBase, cchBase);
96 szTmpPath[cchBase] = RTPATH_DELIMITER;
97 memcpy(&szTmpPath[cchBase + 1], pszPath, cchPath + 1);
98 }
99 return RTPathAbs(szTmpPath, pszAbsPath, cbAbsPath);
100 }
101
102 /* Fallback to the non *Ex version */
103 return RTPathAbs(pszPath, pszAbsPath, cbAbsPath);
104#else
105 return RTPathAbsExEx(pszBase, pszPath, RTPATH_STR_F_STYLE_HOST, pszAbsPath, &cbAbsPath);
106#endif
107}
108
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