1 | /* $Id: RTPathAbsEx.cpp 21675 2009-07-17 12:18:30Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTPathAbsEx
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #include "internal/iprt.h"
|
---|
36 | #include <iprt/path.h>
|
---|
37 | #include <iprt/err.h>
|
---|
38 | #include <iprt/param.h>
|
---|
39 | #include <iprt/string.h>
|
---|
40 | #include "internal/path.h"
|
---|
41 |
|
---|
42 |
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Get the absolute path (no symlinks, no . or .. components), assuming the
|
---|
46 | * given base path as the current directory. The resulting path doesn't have
|
---|
47 | * to exist.
|
---|
48 | *
|
---|
49 | * @returns iprt status code.
|
---|
50 | * @param pszBase The base path to act like a current directory.
|
---|
51 | * When NULL, the actual cwd is used (i.e. the call
|
---|
52 | * is equivalent to RTPathAbs(pszPath, ...).
|
---|
53 | * @param pszPath The path to resolve.
|
---|
54 | * @param pszAbsPath Where to store the absolute path.
|
---|
55 | * @param cchAbsPath Size of the buffer.
|
---|
56 | */
|
---|
57 | RTDECL(int) RTPathAbsEx(const char *pszBase, const char *pszPath, char *pszAbsPath, size_t cchAbsPath)
|
---|
58 | {
|
---|
59 | if (pszBase && pszPath && !rtPathVolumeSpecLen(pszPath))
|
---|
60 | {
|
---|
61 | #if defined(RT_OS_WINDOWS)
|
---|
62 | /* The format for very long paths is not supported. */
|
---|
63 | if ( (pszBase[0] == '/' || pszBase[0] == '\\')
|
---|
64 | && (pszBase[1] == '/' || pszBase[1] == '\\')
|
---|
65 | && pszBase[2] == '?'
|
---|
66 | && (pszBase[3] == '/' || pszBase[3] == '\\'))
|
---|
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 work for now - I don't have time to fix it right now.
|
---|
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 | * 3) The lengths total doesn't have to be less than max length
|
---|
77 | * if the pszPath starts with a slash.
|
---|
78 | */
|
---|
79 | size_t cchBase = strlen(pszBase);
|
---|
80 | size_t cchPath = strlen(pszPath);
|
---|
81 | if (cchBase + cchPath >= RTPATH_MAX)
|
---|
82 | return VERR_FILENAME_TOO_LONG;
|
---|
83 |
|
---|
84 | bool fRootSpec = pszPath[0] == '/'
|
---|
85 | #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
|
---|
86 | || pszPath[0] == '\\'
|
---|
87 | #endif
|
---|
88 | ;
|
---|
89 | size_t cchVolSpec = rtPathVolumeSpecLen(pszBase);
|
---|
90 | char szPath[RTPATH_MAX];
|
---|
91 | if (fRootSpec)
|
---|
92 | {
|
---|
93 | /* join the disk name from base and the path */
|
---|
94 | memcpy(szPath, pszBase, cchVolSpec);
|
---|
95 | strcpy(&szPath[cchVolSpec], pszPath);
|
---|
96 | }
|
---|
97 | else
|
---|
98 | {
|
---|
99 | /* join the base path and the path */
|
---|
100 | strcpy(szPath, pszBase);
|
---|
101 | szPath[cchBase] = RTPATH_DELIMITER;
|
---|
102 | strcpy(&szPath[cchBase + 1], pszPath);
|
---|
103 | }
|
---|
104 | return RTPathAbs(szPath, pszAbsPath, cchAbsPath);
|
---|
105 | }
|
---|
106 |
|
---|
107 | /* Fallback to the non *Ex version */
|
---|
108 | return RTPathAbs(pszPath, pszAbsPath, cchAbsPath);
|
---|
109 | }
|
---|
110 |
|
---|