VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/path.cpp@ 62571

Last change on this file since 62571 was 62477, checked in by vboxsync, 8 years ago

(C) 2016

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 5.6 KB
Line 
1/* $Id: path.cpp 62477 2016-07-22 18:27:37Z vboxsync $ */
2/** @file
3 * IPRT - Path Manipulation.
4 */
5
6/*
7 * Copyright (C) 2006-2016 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/assert.h>
33#include <iprt/env.h>
34#include <iprt/err.h>
35#include <iprt/path.h>
36#include <iprt/string.h>
37#include "internal/path.h"
38#include "internal/process.h"
39
40
41#ifdef RT_OS_SOLARIS
42/**
43 * Hack to strip of the architecture subdirectory from the exec dir.
44 *
45 * @returns See RTPathExecDir.
46 * @param pszPath See RTPathExecDir.
47 * @param cchPath See RTPathExecDir.
48 */
49DECLINLINE(int) rtPathSolarisArchHack(char *pszPath, size_t cchPath)
50{
51 int rc = RTPathExecDir(pszPath, cchPath);
52 if (RT_SUCCESS(rc))
53 {
54 const char *pszLast = RTPathFilename(pszPath);
55 if ( !strcmp(pszLast, "amd64")
56 || !strcmp(pszLast, "i386"))
57 RTPathStripFilename(pszPath);
58 }
59 return rc;
60}
61#endif
62
63
64RTDECL(int) RTPathExecDir(char *pszPath, size_t cchPath)
65{
66 AssertReturn(g_szrtProcExePath[0], VERR_WRONG_ORDER);
67
68 /*
69 * Calc the length and check if there is space before copying.
70 */
71 size_t cch = g_cchrtProcDir;
72 if (cch < cchPath)
73 {
74 memcpy(pszPath, g_szrtProcExePath, cch);
75 pszPath[cch] = '\0';
76 return VINF_SUCCESS;
77 }
78
79 AssertMsgFailed(("Buffer too small (%zu <= %zu)\n", cchPath, cch));
80 return VERR_BUFFER_OVERFLOW;
81}
82
83
84RTDECL(int) RTPathAppPrivateNoArch(char *pszPath, size_t cchPath)
85{
86#if !defined(RT_OS_WINDOWS) && defined(RTPATH_APP_PRIVATE)
87 return RTStrCopy(pszPath, cchPath, RTPATH_APP_PRIVATE);
88#elif defined(RT_OS_SOLARIS)
89 return rtPathSolarisArchHack(pszPath, cchPath);
90#else
91 return RTPathExecDir(pszPath, cchPath);
92#endif
93}
94
95
96RTDECL(int) RTPathAppPrivateArch(char *pszPath, size_t cchPath)
97{
98#if !defined(RT_OS_WINDOWS) && defined(RTPATH_APP_PRIVATE_ARCH)
99 return RTStrCopy(pszPath, cchPath, RTPATH_APP_PRIVATE_ARCH);
100#else
101 return RTPathExecDir(pszPath, cchPath);
102#endif
103}
104
105
106RTDECL(int) RTPathAppPrivateArchTop(char *pszPath, size_t cchPath)
107{
108#if !defined(RT_OS_WINDOWS) && defined(RTPATH_APP_PRIVATE_ARCH_TOP)
109 return RTStrCopy(pszPath, cchPath, RTPATH_APP_PRIVATE_ARCH_TOP);
110#elif !defined(RT_OS_WINDOWS) && defined(RTPATH_APP_PRIVATE_ARCH)
111 return RTStrCopy(pszPath, cchPath, RTPATH_APP_PRIVATE_ARCH);
112#elif defined(RT_OS_SOLARIS)
113 return rtPathSolarisArchHack(pszPath, cchPath);
114#else
115 int rc = RTPathExecDir(pszPath, cchPath);
116 return rc;
117#endif
118}
119
120
121RTDECL(int) RTPathSharedLibs(char *pszPath, size_t cchPath)
122{
123#if !defined(RT_OS_WINDOWS) && defined(RTPATH_SHARED_LIBS)
124 return RTStrCopy(pszPath, cchPath, RTPATH_SHARED_LIBS);
125#else
126 return RTPathExecDir(pszPath, cchPath);
127#endif
128}
129
130
131RTDECL(int) RTPathAppDocs(char *pszPath, size_t cchPath)
132{
133#if !defined(RT_OS_WINDOWS) && defined(RTPATH_APP_DOCS)
134 return RTStrCopy(pszPath, cchPath, RTPATH_APP_DOCS);
135#elif defined(RT_OS_SOLARIS)
136 return rtPathSolarisArchHack(pszPath, cchPath);
137#else
138 return RTPathExecDir(pszPath, cchPath);
139#endif
140}
141
142
143RTDECL(int) RTPathTemp(char *pszPath, size_t cchPath)
144{
145 /*
146 * Try get it from the environment first.
147 */
148 static const char * const s_apszVars[] =
149 {
150 "IPRT_TMPDIR"
151#if defined(RT_OS_WINDOWS)
152 , "TMP", "TEMP", "USERPROFILE"
153#elif defined(RT_OS_OS2)
154 , "TMP", "TEMP", "TMPDIR"
155#else
156 , "TMPDIR"
157#endif
158 };
159 for (size_t iVar = 0; iVar < RT_ELEMENTS(s_apszVars); iVar++)
160 {
161 int rc = RTEnvGetEx(RTENV_DEFAULT, s_apszVars[iVar], pszPath, cchPath, NULL);
162 if (rc != VERR_ENV_VAR_NOT_FOUND)
163 return rc;
164 }
165
166 /*
167 * Here we should use some sane system default, instead we just use
168 * the typical unix temp dir for now.
169 */
170 /** @todo Windows should default to the windows directory, see GetTempPath.
171 * Some unixes has path.h and _PATH_TMP. There is also a question about
172 * whether /var/tmp wouldn't be a better place... */
173 if (cchPath < sizeof("/tmp") )
174 return VERR_BUFFER_OVERFLOW;
175 memcpy(pszPath, "/tmp", sizeof("/tmp"));
176 return VINF_SUCCESS;
177}
178
179
180RTR3DECL(int) RTPathGetMode(const char *pszPath, PRTFMODE pfMode)
181{
182 AssertPtrReturn(pfMode, VERR_INVALID_POINTER);
183
184 RTFSOBJINFO ObjInfo;
185 int rc = RTPathQueryInfoEx(pszPath, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_FOLLOW_LINK);
186 if (RT_SUCCESS(rc))
187 *pfMode = ObjInfo.Attr.fMode;
188
189 return rc;
190}
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