path.cpp revision 207e97003c797079efc85d3df4dfe832d5919860
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync/* $Id$ */
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync/** @file
5b281ba489ca18f0380d7efc7a5108b606cce449vboxsync * IPRT - Path Manipulation.
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync */
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync/*
c58f1213e628a545081c70e26c6b67a841cff880vboxsync * Copyright (C) 2006-2007 Oracle Corporation
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync *
804197ca1d56719343cd21af782ddc2c695a6760vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
804197ca1d56719343cd21af782ddc2c695a6760vboxsync * available from http://www.virtualbox.org. This file is free software;
804197ca1d56719343cd21af782ddc2c695a6760vboxsync * you can redistribute it and/or modify it under the terms of the GNU
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * General Public License (GPL) as published by the Free Software
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync *
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * The contents of this file may alternatively be used under the terms
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * of the Common Development and Distribution License Version 1.0
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * VirtualBox OSE distribution, in which case the provisions of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * CDDL are applicable instead of those of the GPL.
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync *
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * You may elect to license modified versions of this file under the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * terms and conditions of either the GPL or the CDDL or both.
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync */
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync/*******************************************************************************
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync* Header Files *
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync*******************************************************************************/
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync#include "internal/iprt.h"
aa4bcf0a4b2db3ac352b56a291d49cb8d4b66d32vboxsync#include <iprt/assert.h>
aa4bcf0a4b2db3ac352b56a291d49cb8d4b66d32vboxsync#include <iprt/env.h>
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync#include <iprt/err.h>
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync#include <iprt/path.h>
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync#include <iprt/string.h>
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync#include "internal/path.h"
98ac5f06a60dbc35241635f0d146c3d4ff55c727vboxsync#include "internal/process.h"
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsyncRTDECL(int) RTPathExecDir(char *pszPath, size_t cchPath)
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync{
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync AssertReturn(g_szrtProcExePath[0], VERR_WRONG_ORDER);
76fa7570c3b6268d459098f86d74d369c1d85f4cvboxsync
76fa7570c3b6268d459098f86d74d369c1d85f4cvboxsync /*
46fee3dbfe26f500824ef463ac239b6cb5d09522vboxsync * Calc the length and check if there is space before copying.
46fee3dbfe26f500824ef463ac239b6cb5d09522vboxsync */
46fee3dbfe26f500824ef463ac239b6cb5d09522vboxsync size_t cch = g_cchrtProcDir;
4157c84dffb94fe976739ad709e4fd95d22c6667vboxsync if (cch <= cchPath)
590bfe12ce22cd3716448fbb9f4dc51664bfe5e2vboxsync {
6988e736149e8800875f9fbb001a6018926d6562vboxsync memcpy(pszPath, g_szrtProcExePath, cch);
590bfe12ce22cd3716448fbb9f4dc51664bfe5e2vboxsync pszPath[cch] = '\0';
e7e8a6cb1597db3e0a1a83733e1d90e7a1688647vboxsync return VINF_SUCCESS;
e7e8a6cb1597db3e0a1a83733e1d90e7a1688647vboxsync }
aa4bcf0a4b2db3ac352b56a291d49cb8d4b66d32vboxsync
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync AssertMsgFailed(("Buffer too small (%zu <= %zu)\n", cchPath, cch));
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync return VERR_BUFFER_OVERFLOW;
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync}
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync/**
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * Gets the directory for architecture-independent application data, for
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * example NLS files, module sources, ...
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync *
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * Linux: /usr/shared/@<application@>
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * Windows: @<program files directory@>/@<application@>
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync * Old path: same as RTPathExecDir()
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync *
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync */
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsyncRTDECL(int) RTPathAppPrivateNoArch(char *pszPath, size_t cchPath)
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync{
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync#if !defined(RT_OS_WINDOWS) && defined(RTPATH_APP_PRIVATE)
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync return RTStrCopy(pszPath, cchPath, RTPATH_APP_PRIVATE);
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync#else
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync return RTPathExecDir(pszPath, cchPath);
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync#endif
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync}
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync/**
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * Gets the directory for architecture-dependent application data, for
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * example modules which can be loaded at runtime.
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync *
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * Linux: /usr/lib/@<application@>
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * Windows: @<program files directory@>/@<application@>
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * Old path: same as RTPathExecDir()
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync *
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync * @returns iprt status code.
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * @param pszPath Buffer where to store the path.
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * @param cchPath Buffer size in bytes.
db55f7b1060a6a72704b5369a8e776c59e5e4f64vboxsync */
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsyncRTDECL(int) RTPathAppPrivateArch(char *pszPath, size_t cchPath)
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync{
db55f7b1060a6a72704b5369a8e776c59e5e4f64vboxsync#if !defined(RT_OS_WINDOWS) && defined(RTPATH_APP_PRIVATE_ARCH)
db55f7b1060a6a72704b5369a8e776c59e5e4f64vboxsync return RTStrCopy(pszPath, cchPath, RTPATH_APP_PRIVATE_ARCH);
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync#else
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync return RTPathExecDir(pszPath, cchPath);
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync#endif
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync}
db55f7b1060a6a72704b5369a8e776c59e5e4f64vboxsync
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync/**
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * Gets the directory of shared libraries. This is not the same as
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync * RTPathAppPrivateArch() as Linux depends all shared libraries in
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync * a common global directory where ld.so can found them.
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync *
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * Linux: /usr/lib
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * Windows: @<program files directory@>/@<application@>
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * Old path: same as RTPathExecDir()
db55f7b1060a6a72704b5369a8e776c59e5e4f64vboxsync *
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * @returns iprt status code.
db55f7b1060a6a72704b5369a8e776c59e5e4f64vboxsync * @param pszPath Buffer where to store the path.
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * @param cchPath Buffer size in bytes.
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync */
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsyncRTDECL(int) RTPathSharedLibs(char *pszPath, size_t cchPath)
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync{
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync#if !defined(RT_OS_WINDOWS) && defined(RTPATH_SHARED_LIBS)
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync return RTStrCopy(pszPath, cchPath, RTPATH_SHARED_LIBS);
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync#else
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync return RTPathExecDir(pszPath, cchPath);
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync#endif
db55f7b1060a6a72704b5369a8e776c59e5e4f64vboxsync}
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync/**
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * Gets the directory for documentation.
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync *
db55f7b1060a6a72704b5369a8e776c59e5e4f64vboxsync * Linux: /usr/share/doc/@<application@>
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * Windows: @<program files directory@>/@<application@>
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync * Old path: same as RTPathExecDir()
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync *
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync * @returns iprt status code.
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync * @param pszPath Buffer where to store the path.
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * @param cchPath Buffer size in bytes.
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync */
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsyncRTDECL(int) RTPathAppDocs(char *pszPath, size_t cchPath)
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync{
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync#if !defined(RT_OS_WINDOWS) && defined(RTPATH_APP_DOCS)
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync return RTStrCopy(pszPath, cchPath, RTPATH_APP_DOCS);
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync#else
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync return RTPathExecDir(pszPath, cchPath);
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync#endif
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync}
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync/**
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync * Gets the temporary directory path.
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync *
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * @returns iprt status code.
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync *
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * @param pszPath Buffer where to store the path.
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * @param cchPath Buffer size in bytes.
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync */
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsyncRTDECL(int) RTPathTemp(char *pszPath, size_t cchPath)
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync{
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync /*
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync * Try get it from the environment first.
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync */
db55f7b1060a6a72704b5369a8e776c59e5e4f64vboxsync static const char * const s_apszVars[] =
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync {
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync "IPRT_TMPDIR"
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync#if defined(RT_OS_WINDOWS)
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync , "TMP", "TEMP", "USERPROFILE"
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync#elif defined(RT_OS_OS2)
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync , "TMP", "TEMP", "TMPDIR"
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync#else
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync , "TMPDIR"
dbc6ecd6a6e372b8dcdf31d68bdab2d5b5e604d2vboxsync#endif
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync };
aa4bcf0a4b2db3ac352b56a291d49cb8d4b66d32vboxsync for (size_t iVar = 0; iVar < RT_ELEMENTS(s_apszVars); iVar++)
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync {
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync int rc = RTEnvGetEx(RTENV_DEFAULT, s_apszVars[iVar], pszPath, cchPath, NULL);
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync if (rc != VERR_ENV_VAR_NOT_FOUND)
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync return rc;
db55f7b1060a6a72704b5369a8e776c59e5e4f64vboxsync }
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync /*
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * Here we should use some sane system default, instead we just use
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * the typical unix temp dir for now.
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync */
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync /** @todo Windows should default to the windows directory, see GetTempPath.
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * Some unixes has path.h and _PATH_TMP. There is also a question about
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync * whether /var/tmp wouldn't be a better place... */
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync if (cchPath < sizeof("/tmp") )
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync return VERR_BUFFER_OVERFLOW;
db55f7b1060a6a72704b5369a8e776c59e5e4f64vboxsync memcpy(pszPath, "/tmp", sizeof("/tmp"));
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync return VINF_SUCCESS;
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync}
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsyncRTR3DECL(int) RTPathGetMode(const char *pszPath, PRTFMODE pfMode)
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync{
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync AssertPtrReturn(pfMode, VERR_INVALID_POINTER);
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync char szPathReal[RTPATH_MAX];
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync int rc = RTPathReal(pszPath, szPathReal, sizeof(szPathReal));
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync if (RT_SUCCESS(rc))
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync {
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync RTFSOBJINFO objInfo;
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync rc = RTPathQueryInfo(szPathReal, &objInfo, RTFSOBJATTRADD_NOTHING);
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync if (RT_SUCCESS(rc))
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync *pfMode = objInfo.Attr.fMode;
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync }
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync return rc;
65a755c25b49d32c914e2d569fcdfb766e1ee879vboxsync}
ababd7e83ee3d23c5191a0d3802f10764df69e36vboxsync