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