1 | /* $Id: GetVBoxUserHomeDirectory.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * MS COM / XPCOM Abstraction Layer - GetVBoxUserHomeDirectory.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2005-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
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 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | /*********************************************************************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *********************************************************************************************************************************/
|
---|
32 | #define LOG_GROUP LOG_GROUP_MAIN
|
---|
33 | #include <VBox/com/utils.h>
|
---|
34 |
|
---|
35 | #include <iprt/env.h>
|
---|
36 | #include <iprt/dir.h>
|
---|
37 | #include <iprt/param.h>
|
---|
38 | #include <iprt/path.h>
|
---|
39 | #include <iprt/string.h>
|
---|
40 |
|
---|
41 | #include <VBox/err.h>
|
---|
42 | #include <VBox/log.h>
|
---|
43 |
|
---|
44 |
|
---|
45 |
|
---|
46 | /*********************************************************************************************************************************
|
---|
47 | * Global Variables *
|
---|
48 | *********************************************************************************************************************************/
|
---|
49 | #if !defined(RT_OS_DARWIN) && !defined(RT_OS_WINDOWS)
|
---|
50 | char g_szXdgConfigHome[RTPATH_MAX] = "";
|
---|
51 | #endif
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Possible locations for the VirtualBox user configuration folder,
|
---|
55 | * listed from oldest (as in legacy) to newest. These can be either
|
---|
56 | * absolute or relative to the home directory. We use the first entry
|
---|
57 | * of the list which corresponds to a real folder on storage, or
|
---|
58 | * create a folder corresponding to the last in the list (the least
|
---|
59 | * legacy) if none do.
|
---|
60 | */
|
---|
61 | const char * const g_apcszUserHome[] =
|
---|
62 | {
|
---|
63 | #ifdef RT_OS_DARWIN
|
---|
64 | "Library/VirtualBox",
|
---|
65 | #elif defined RT_OS_WINDOWS
|
---|
66 | ".VirtualBox",
|
---|
67 | #else
|
---|
68 | ".VirtualBox", g_szXdgConfigHome,
|
---|
69 | #endif
|
---|
70 | };
|
---|
71 |
|
---|
72 |
|
---|
73 | namespace com
|
---|
74 | {
|
---|
75 |
|
---|
76 | static int composeHomePath(char *aDir, size_t aDirLen, const char *pcszBase)
|
---|
77 | {
|
---|
78 | int vrc;
|
---|
79 | if (RTPathStartsWithRoot(pcszBase))
|
---|
80 | vrc = RTStrCopy(aDir, aDirLen, pcszBase);
|
---|
81 | else
|
---|
82 | {
|
---|
83 | /* compose the config directory (full path) */
|
---|
84 | /** @todo r=bird: RTPathUserHome doesn't necessarily return a
|
---|
85 | * full (abs) path like the comment above seems to indicate. */
|
---|
86 | vrc = RTPathUserHome(aDir, aDirLen);
|
---|
87 | if (RT_SUCCESS(vrc))
|
---|
88 | vrc = RTPathAppend(aDir, aDirLen, pcszBase);
|
---|
89 | }
|
---|
90 | return vrc;
|
---|
91 | }
|
---|
92 |
|
---|
93 | int GetVBoxUserHomeDirectory(char *aDir, size_t aDirLen, bool fCreateDir)
|
---|
94 | {
|
---|
95 | AssertReturn(aDir, VERR_INVALID_POINTER);
|
---|
96 | AssertReturn(aDirLen > 0, VERR_BUFFER_OVERFLOW);
|
---|
97 |
|
---|
98 | /* start with null */
|
---|
99 | *aDir = 0;
|
---|
100 |
|
---|
101 | char szTmp[RTPATH_MAX];
|
---|
102 | int vrc = RTEnvGetEx(RTENV_DEFAULT, "VBOX_USER_HOME", szTmp, sizeof(szTmp), NULL);
|
---|
103 | if (RT_SUCCESS(vrc) || vrc == VERR_ENV_VAR_NOT_FOUND)
|
---|
104 | {
|
---|
105 | bool fFound = false;
|
---|
106 | if (RT_SUCCESS(vrc))
|
---|
107 | {
|
---|
108 | /* get the full path name */
|
---|
109 | vrc = RTPathAbs(szTmp, aDir, aDirLen);
|
---|
110 | }
|
---|
111 | else
|
---|
112 | {
|
---|
113 | #if !defined(RT_OS_WINDOWS) && !defined(RT_OS_DARWIN)
|
---|
114 | vrc = RTEnvGetEx(RTENV_DEFAULT, "XDG_CONFIG_HOME", g_szXdgConfigHome, sizeof(g_szXdgConfigHome), NULL);
|
---|
115 | if (RT_SUCCESS(vrc))
|
---|
116 | vrc = RTPathAppend(g_szXdgConfigHome, sizeof(g_szXdgConfigHome), "VirtualBox");
|
---|
117 | AssertMsg(vrc == VINF_SUCCESS || vrc == VERR_ENV_VAR_NOT_FOUND, ("%Rrc\n", vrc));
|
---|
118 | if (RT_FAILURE_NP(vrc))
|
---|
119 | vrc = RTStrCopy(g_szXdgConfigHome, sizeof(g_szXdgConfigHome), ".config/VirtualBox");
|
---|
120 | #endif
|
---|
121 | for (unsigned i = 0; i < RT_ELEMENTS(g_apcszUserHome); ++i)
|
---|
122 | {
|
---|
123 | vrc = composeHomePath(aDir, aDirLen, g_apcszUserHome[i]);
|
---|
124 | if ( RT_SUCCESS(vrc)
|
---|
125 | && RTDirExists(aDir))
|
---|
126 | {
|
---|
127 | fFound = true;
|
---|
128 | break;
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | /* ensure the home directory exists */
|
---|
134 | if (RT_SUCCESS(vrc))
|
---|
135 | if (!fFound && fCreateDir)
|
---|
136 | vrc = RTDirCreateFullPath(aDir, 0700);
|
---|
137 | }
|
---|
138 |
|
---|
139 | return vrc;
|
---|
140 | }
|
---|
141 |
|
---|
142 | } /* namespace com */
|
---|