VirtualBox

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

Last change on this file since 35222 was 35222, checked in by vboxsync, 14 years ago

Made RTPathAppPrivateNoArch return /opt/VirtualBox instead of /opt/VirtualBox/[amd64|i386] on solaris, moving the nls up to /opt/VirtualBox/ saving ~12KB.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.3 KB
Line 
1/* $Id: path.cpp 35222 2010-12-17 13:03:01Z vboxsync $ */
2/** @file
3 * IPRT - Path Manipulation.
4 */
5
6/*
7 * Copyright (C) 2006-2007 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 */
49static 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)
109 return RTStrCopy(pszPath, cchPath, RTPATH_APP_PRIVATE_ARCH);
110#elif defined(RT_OS_SOLARIS)
111 return rtPathSolarisArchHack(pszPath, cchPath);
112#else
113 return RTPathExecDir(pszPath, cchPath);
114#endif
115}
116
117
118RTDECL(int) RTPathSharedLibs(char *pszPath, size_t cchPath)
119{
120#if !defined(RT_OS_WINDOWS) && defined(RTPATH_SHARED_LIBS)
121 return RTStrCopy(pszPath, cchPath, RTPATH_SHARED_LIBS);
122#else
123 return RTPathExecDir(pszPath, cchPath);
124#endif
125}
126
127
128RTDECL(int) RTPathAppDocs(char *pszPath, size_t cchPath)
129{
130#if !defined(RT_OS_WINDOWS) && defined(RTPATH_APP_DOCS)
131 return RTStrCopy(pszPath, cchPath, RTPATH_APP_DOCS);
132#elif defined(RT_OS_SOLARIS)
133 return rtPathSolarisArchHack(pszPath, cchPath);
134#else
135 return RTPathExecDir(pszPath, cchPath);
136#endif
137}
138
139
140RTDECL(int) RTPathTemp(char *pszPath, size_t cchPath)
141{
142 /*
143 * Try get it from the environment first.
144 */
145 static const char * const s_apszVars[] =
146 {
147 "IPRT_TMPDIR"
148#if defined(RT_OS_WINDOWS)
149 , "TMP", "TEMP", "USERPROFILE"
150#elif defined(RT_OS_OS2)
151 , "TMP", "TEMP", "TMPDIR"
152#else
153 , "TMPDIR"
154#endif
155 };
156 for (size_t iVar = 0; iVar < RT_ELEMENTS(s_apszVars); iVar++)
157 {
158 int rc = RTEnvGetEx(RTENV_DEFAULT, s_apszVars[iVar], pszPath, cchPath, NULL);
159 if (rc != VERR_ENV_VAR_NOT_FOUND)
160 return rc;
161 }
162
163 /*
164 * Here we should use some sane system default, instead we just use
165 * the typical unix temp dir for now.
166 */
167 /** @todo Windows should default to the windows directory, see GetTempPath.
168 * Some unixes has path.h and _PATH_TMP. There is also a question about
169 * whether /var/tmp wouldn't be a better place... */
170 if (cchPath < sizeof("/tmp") )
171 return VERR_BUFFER_OVERFLOW;
172 memcpy(pszPath, "/tmp", sizeof("/tmp"));
173 return VINF_SUCCESS;
174}
175
176
177RTR3DECL(int) RTPathGetMode(const char *pszPath, PRTFMODE pfMode)
178{
179 AssertPtrReturn(pfMode, VERR_INVALID_POINTER);
180
181 RTFSOBJINFO ObjInfo;
182 int rc = RTPathQueryInfoEx(pszPath, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_FOLLOW_LINK);
183 if (RT_SUCCESS(rc))
184 *pfMode = ObjInfo.Attr.fMode;
185
186 return rc;
187}
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