[1] | 1 | /* $Id: path.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[8245] | 3 | * IPRT - Path Manipulation.
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[1] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[5999] | 11 | *
|
---|
[96407] | 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
[5999] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
| 28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
[5999] | 29 | * CDDL are applicable instead of those of the GPL.
|
---|
| 30 | *
|
---|
| 31 | * You may elect to license modified versions of this file under the
|
---|
| 32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[1] | 35 | */
|
---|
| 36 |
|
---|
| 37 |
|
---|
[57358] | 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[21673] | 41 | #include "internal/iprt.h"
|
---|
[83991] | 42 | #include <iprt/path.h>
|
---|
| 43 |
|
---|
[21673] | 44 | #include <iprt/assert.h>
|
---|
[83991] | 45 | #include <iprt/errcore.h>
|
---|
[1] | 46 | #include <iprt/string.h>
|
---|
[21677] | 47 | #include "internal/path.h"
|
---|
[11836] | 48 | #include "internal/process.h"
|
---|
[1] | 49 |
|
---|
| 50 |
|
---|
[35218] | 51 | #ifdef RT_OS_SOLARIS
|
---|
| 52 | /**
|
---|
| 53 | * Hack to strip of the architecture subdirectory from the exec dir.
|
---|
| 54 | *
|
---|
| 55 | * @returns See RTPathExecDir.
|
---|
| 56 | * @param pszPath See RTPathExecDir.
|
---|
| 57 | * @param cchPath See RTPathExecDir.
|
---|
| 58 | */
|
---|
[35225] | 59 | DECLINLINE(int) rtPathSolarisArchHack(char *pszPath, size_t cchPath)
|
---|
[35218] | 60 | {
|
---|
| 61 | int rc = RTPathExecDir(pszPath, cchPath);
|
---|
| 62 | if (RT_SUCCESS(rc))
|
---|
| 63 | {
|
---|
| 64 | const char *pszLast = RTPathFilename(pszPath);
|
---|
| 65 | if ( !strcmp(pszLast, "amd64")
|
---|
| 66 | || !strcmp(pszLast, "i386"))
|
---|
| 67 | RTPathStripFilename(pszPath);
|
---|
| 68 | }
|
---|
| 69 | return rc;
|
---|
| 70 | }
|
---|
| 71 | #endif
|
---|
| 72 |
|
---|
| 73 |
|
---|
[19924] | 74 | RTDECL(int) RTPathExecDir(char *pszPath, size_t cchPath)
|
---|
[11836] | 75 | {
|
---|
| 76 | AssertReturn(g_szrtProcExePath[0], VERR_WRONG_ORDER);
|
---|
| 77 |
|
---|
| 78 | /*
|
---|
| 79 | * Calc the length and check if there is space before copying.
|
---|
| 80 | */
|
---|
[96442] | 81 | size_t cch = g_cchrtProcExeDir;
|
---|
[33454] | 82 | if (cch < cchPath)
|
---|
[11836] | 83 | {
|
---|
| 84 | memcpy(pszPath, g_szrtProcExePath, cch);
|
---|
| 85 | pszPath[cch] = '\0';
|
---|
| 86 | return VINF_SUCCESS;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | AssertMsgFailed(("Buffer too small (%zu <= %zu)\n", cchPath, cch));
|
---|
| 90 | return VERR_BUFFER_OVERFLOW;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 |
|
---|
[14050] | 94 | RTDECL(int) RTPathAppPrivateNoArch(char *pszPath, size_t cchPath)
|
---|
[3860] | 95 | {
|
---|
[3866] | 96 | #if !defined(RT_OS_WINDOWS) && defined(RTPATH_APP_PRIVATE)
|
---|
[28916] | 97 | return RTStrCopy(pszPath, cchPath, RTPATH_APP_PRIVATE);
|
---|
[35222] | 98 | #elif defined(RT_OS_SOLARIS)
|
---|
[35218] | 99 | return rtPathSolarisArchHack(pszPath, cchPath);
|
---|
[3860] | 100 | #else
|
---|
[19924] | 101 | return RTPathExecDir(pszPath, cchPath);
|
---|
[3860] | 102 | #endif
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 |
|
---|
[14050] | 106 | RTDECL(int) RTPathAppPrivateArch(char *pszPath, size_t cchPath)
|
---|
[3860] | 107 | {
|
---|
[3866] | 108 | #if !defined(RT_OS_WINDOWS) && defined(RTPATH_APP_PRIVATE_ARCH)
|
---|
[28916] | 109 | return RTStrCopy(pszPath, cchPath, RTPATH_APP_PRIVATE_ARCH);
|
---|
[3860] | 110 | #else
|
---|
[19924] | 111 | return RTPathExecDir(pszPath, cchPath);
|
---|
[3860] | 112 | #endif
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 |
|
---|
[35218] | 116 | RTDECL(int) RTPathAppPrivateArchTop(char *pszPath, size_t cchPath)
|
---|
| 117 | {
|
---|
[35225] | 118 | #if !defined(RT_OS_WINDOWS) && defined(RTPATH_APP_PRIVATE_ARCH_TOP)
|
---|
| 119 | return RTStrCopy(pszPath, cchPath, RTPATH_APP_PRIVATE_ARCH_TOP);
|
---|
| 120 | #elif !defined(RT_OS_WINDOWS) && defined(RTPATH_APP_PRIVATE_ARCH)
|
---|
[35218] | 121 | return RTStrCopy(pszPath, cchPath, RTPATH_APP_PRIVATE_ARCH);
|
---|
| 122 | #elif defined(RT_OS_SOLARIS)
|
---|
| 123 | return rtPathSolarisArchHack(pszPath, cchPath);
|
---|
| 124 | #else
|
---|
[35225] | 125 | int rc = RTPathExecDir(pszPath, cchPath);
|
---|
| 126 | return rc;
|
---|
[35218] | 127 | #endif
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 |
|
---|
[14050] | 131 | RTDECL(int) RTPathSharedLibs(char *pszPath, size_t cchPath)
|
---|
[3860] | 132 | {
|
---|
[3866] | 133 | #if !defined(RT_OS_WINDOWS) && defined(RTPATH_SHARED_LIBS)
|
---|
[28916] | 134 | return RTStrCopy(pszPath, cchPath, RTPATH_SHARED_LIBS);
|
---|
[3860] | 135 | #else
|
---|
[19924] | 136 | return RTPathExecDir(pszPath, cchPath);
|
---|
[3860] | 137 | #endif
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 |
|
---|
[14052] | 141 | RTDECL(int) RTPathAppDocs(char *pszPath, size_t cchPath)
|
---|
[3860] | 142 | {
|
---|
[3866] | 143 | #if !defined(RT_OS_WINDOWS) && defined(RTPATH_APP_DOCS)
|
---|
[28916] | 144 | return RTStrCopy(pszPath, cchPath, RTPATH_APP_DOCS);
|
---|
[35218] | 145 | #elif defined(RT_OS_SOLARIS)
|
---|
| 146 | return rtPathSolarisArchHack(pszPath, cchPath);
|
---|
[3860] | 147 | #else
|
---|
[19924] | 148 | return RTPathExecDir(pszPath, cchPath);
|
---|
[3860] | 149 | #endif
|
---|
| 150 | }
|
---|
[6536] | 151 |
|
---|
[20101] | 152 |
|
---|
[31304] | 153 | RTR3DECL(int) RTPathGetMode(const char *pszPath, PRTFMODE pfMode)
|
---|
| 154 | {
|
---|
| 155 | AssertPtrReturn(pfMode, VERR_INVALID_POINTER);
|
---|
| 156 |
|
---|
[31404] | 157 | RTFSOBJINFO ObjInfo;
|
---|
[31405] | 158 | int rc = RTPathQueryInfoEx(pszPath, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_FOLLOW_LINK);
|
---|
[31304] | 159 | if (RT_SUCCESS(rc))
|
---|
[31404] | 160 | *pfMode = ObjInfo.Attr.fMode;
|
---|
[31304] | 161 |
|
---|
| 162 | return rc;
|
---|
| 163 | }
|
---|