1 | /* $Id: RTPathAbsEx.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTPathAbsEx
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-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 "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 cchAbsPath Size of the buffer.
|
---|
52 | */
|
---|
53 | RTDECL(int) RTPathAbsEx(const char *pszBase, const char *pszPath, char *pszAbsPath, size_t cchAbsPath)
|
---|
54 | {
|
---|
55 | if ( pszBase
|
---|
56 | && pszPath
|
---|
57 | && !rtPathVolumeSpecLen(pszPath)
|
---|
58 | )
|
---|
59 | {
|
---|
60 | #if defined(RT_OS_WINDOWS)
|
---|
61 | /* The format for very long paths is not supported. */
|
---|
62 | if ( RTPATH_IS_SLASH(pszBase[0])
|
---|
63 | && RTPATH_IS_SLASH(pszBase[1])
|
---|
64 | && pszBase[2] == '?'
|
---|
65 | && RTPATH_IS_SLASH(pszBase[3])
|
---|
66 | )
|
---|
67 | return VERR_INVALID_NAME;
|
---|
68 | #endif
|
---|
69 |
|
---|
70 | /** @todo there are a couple of things which isn't 100% correct, although the
|
---|
71 | * current code will have to do for now, no time to fix.
|
---|
72 | *
|
---|
73 | * 1) On Windows & OS/2 we confuse '/' with an abspath spec and will
|
---|
74 | * not necessarily resolve it on the right drive.
|
---|
75 | * 2) A trailing slash in the base might cause UNC names to be created.
|
---|
76 | */
|
---|
77 | size_t const cchPath = strlen(pszPath);
|
---|
78 | char szTmpPath[RTPATH_MAX];
|
---|
79 | if (RTPATH_IS_SLASH(pszPath[0]))
|
---|
80 | {
|
---|
81 | /* join the disk name from base and the path (DOS systems only) */
|
---|
82 | size_t const cchVolSpec = rtPathVolumeSpecLen(pszBase);
|
---|
83 | if (cchVolSpec + cchPath + 1 > sizeof(szTmpPath))
|
---|
84 | return VERR_FILENAME_TOO_LONG;
|
---|
85 | memcpy(szTmpPath, pszBase, cchVolSpec);
|
---|
86 | memcpy(&szTmpPath[cchVolSpec], pszPath, cchPath + 1);
|
---|
87 | }
|
---|
88 | else
|
---|
89 | {
|
---|
90 | /* join the base path and the path */
|
---|
91 | size_t const cchBase = strlen(pszBase);
|
---|
92 | if (cchBase + 1 + cchPath + 1 > sizeof(szTmpPath))
|
---|
93 | return VERR_FILENAME_TOO_LONG;
|
---|
94 | memcpy(szTmpPath, pszBase, cchBase);
|
---|
95 | szTmpPath[cchBase] = RTPATH_DELIMITER;
|
---|
96 | memcpy(&szTmpPath[cchBase + 1], pszPath, cchPath + 1);
|
---|
97 | }
|
---|
98 | return RTPathAbs(szTmpPath, pszAbsPath, cchAbsPath);
|
---|
99 | }
|
---|
100 |
|
---|
101 | /* Fallback to the non *Ex version */
|
---|
102 | return RTPathAbs(pszPath, pszAbsPath, cchAbsPath);
|
---|
103 | }
|
---|
104 |
|
---|