StandardDirectory.cxx 7.1 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "StandardDirectory.hxx"
#include "FileSystem.hxx"
22
#include "XDG.hxx"
23
#include "config.h"
24 25 26

#include <array>

27
#ifdef _WIN32
28 29 30 31 32 33 34 35 36
#include <windows.h>
#include <shlobj.h>
#else
#include <stdlib.h>
#include <unistd.h>
#include <pwd.h>
#endif

#ifdef USE_XDG
37
#include "util/StringStrip.hxx"
38
#include "util/StringCompare.hxx"
39
#include "io/TextFile.hxx"
40 41 42 43
#include <string.h>
#include <utility>
#endif

44 45 46
#ifdef ANDROID
#include "java/Global.hxx"
#include "android/Environment.hxx"
47 48
#include "android/Context.hxx"
#include "Main.hxx"
49 50
#endif

51
#if !defined(_WIN32) && !defined(ANDROID)
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
class PasswdEntry
{
#if defined(HAVE_GETPWNAM_R) || defined(HAVE_GETPWUID_R)
	std::array<char, 16 * 1024> buf;
	passwd pw;
#endif

	passwd *result;
public:
	PasswdEntry() : result(nullptr) { }

	bool ReadByName(const char *name) {
#ifdef HAVE_GETPWNAM_R
		getpwnam_r(name, &pw, buf.data(), buf.size(), &result);
#else
		result = getpwnam(name);
#endif
		return result != nullptr;
	}

	bool ReadByUid(uid_t uid) {
#ifdef HAVE_GETPWUID_R
		getpwuid_r(uid, &pw, buf.data(), buf.size(), &result);
#else
		result = getpwuid(uid);
#endif
		return result != nullptr;
	}

	const passwd *operator->() {
		assert(result != nullptr);
		return result;
	}
};
#endif

88
#ifndef ANDROID
89 90
static inline bool
IsValidPathString(PathTraitsFS::const_pointer_type path)
91 92 93 94
{
	return path != nullptr && *path != '\0';
}

95 96
static inline bool
IsValidDir(PathTraitsFS::const_pointer_type dir)
97 98 99 100 101
{
	return PathTraitsFS::IsAbsolute(dir) &&
	       DirectoryExists(Path::FromFS(dir));
}

102 103
static inline AllocatedPath
SafePathFromFS(PathTraitsFS::const_pointer_type dir)
104 105 106
{
	if (IsValidPathString(dir) && IsValidDir(dir))
		return AllocatedPath::FromFS(dir);
107
	return nullptr;
108
}
109
#endif
110

111
#ifdef _WIN32
112 113
static AllocatedPath GetStandardDir(int folder_id)
{
114
	std::array<PathTraitsFS::value_type, MAX_PATH> dir;
115 116 117
	auto ret = SHGetFolderPath(nullptr, folder_id | CSIDL_FLAG_DONT_VERIFY,
				   nullptr, SHGFP_TYPE_CURRENT, dir.data());
	if (FAILED(ret))
118
		return nullptr;
119 120 121 122 123 124 125 126
	return SafePathFromFS(dir.data());
}
#endif

#ifdef USE_XDG

static const char home_prefix[] = "$HOME/";

127 128
static bool
ParseConfigLine(char *line, const char *dir_name, AllocatedPath &result_dir)
129 130
{
	// strip leading white space
131
	line = StripLeft(line);
132 133 134 135 136 137 138 139 140 141 142

	// check for end-of-line or comment
	if (*line == '\0' || *line == '#')
		return false;

	// check if current setting is for requested dir
	if (!StringStartsWith(line, dir_name))
		return false;
	line += strlen(dir_name);

	// strip equals sign and spaces around it
143
	line = StripLeft(line);
144 145 146
	if (*line != '=')
		return false;
	++line;
147
	line = StripLeft(line);
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

	// check if path is quoted
	bool quoted = false;
	if (*line == '"') {
		++line;
		quoted = true;
	}

	// check if path is relative to $HOME
	bool home_relative = false;
	if (StringStartsWith(line, home_prefix)) {
		line += strlen(home_prefix);
		home_relative = true;
	}


164
	char *line_end;
165 166 167 168 169 170
	// find end of the string
	if (quoted) {
		line_end = strrchr(line, '"');
		if (line_end == nullptr)
			return true;
	} else {
171
		line_end = StripRight(line, line + strlen(line));
172 173 174 175 176 177
	}

	// check for empty result
	if (line == line_end)
		return true;

178 179 180
	*line_end = 0;

	// build the result path
181
	const auto path_fs = Path::FromFS(line);
182

183
	AllocatedPath result = nullptr;
184 185 186 187
	if (home_relative) {
		auto home = GetHomeDir();
		if (home.IsNull())
			return true;
188
		result = home / path_fs;
189
	} else {
190
		result = AllocatedPath(path_fs);
191 192 193 194 195 196 197 198 199
	}

	if (IsValidDir(result.c_str())) {
		result_dir = std::move(result);
		return true;
	}
	return true;
}

200 201
static AllocatedPath
GetUserDir(const char *name) noexcept
202
try {
203
	AllocatedPath result = nullptr;
204 205 206
	auto config_dir = GetUserConfigDir();
	if (config_dir.IsNull())
		return result;
207

208
	TextFile input(config_dir / Path::FromFS("user-dirs.dirs"));
209
	char *line;
210 211 212 213
	while ((line = input.ReadLine()) != nullptr)
		if (ParseConfigLine(line, name, result))
			return result;
	return result;
214
} catch (const std::exception &e) {
215
	return nullptr;
216 217 218 219
}

#endif

220 221
AllocatedPath
GetUserConfigDir() noexcept
222
{
223
#if defined(_WIN32)
224 225 226 227 228 229 230 231 232 233
	return GetStandardDir(CSIDL_LOCAL_APPDATA);
#elif defined(USE_XDG)
	// Check for $XDG_CONFIG_HOME
	auto config_home = getenv("XDG_CONFIG_HOME");
	if (IsValidPathString(config_home) && IsValidDir(config_home))
		return AllocatedPath::FromFS(config_home);

	// Check for $HOME/.config
	auto home = GetHomeDir();
	if (!home.IsNull()) {
234
		auto fallback = home / Path::FromFS(".config");
235 236 237 238
		if (IsValidDir(fallback.c_str()))
			return fallback;
	}

239
	return nullptr;
240
#else
241
	return nullptr;
242 243 244
#endif
}

245 246
AllocatedPath
GetUserMusicDir() noexcept
247
{
248
#if defined(_WIN32)
249 250 251
	return GetStandardDir(CSIDL_MYMUSIC);	
#elif defined(USE_XDG)
	return GetUserDir("XDG_MUSIC_DIR");
252
#elif defined(ANDROID)
253
	return Environment::getExternalStoragePublicDirectory("Music");
254
#else
255
	return nullptr;
256
#endif
257 258
}

259 260
AllocatedPath
GetUserCacheDir() noexcept
261 262
{
#ifdef USE_XDG
263 264 265 266 267 268 269 270
	// Check for $XDG_CACHE_HOME
	auto cache_home = getenv("XDG_CACHE_HOME");
	if (IsValidPathString(cache_home) && IsValidDir(cache_home))
		return AllocatedPath::FromFS(cache_home);

	// Check for $HOME/.cache
	auto home = GetHomeDir();
	if (!home.IsNull()) {
271
		auto fallback = home / Path::FromFS(".cache");
272 273 274 275
		if (IsValidDir(fallback.c_str()))
			return fallback;
	}

276
	return nullptr;
277 278 279
#elif defined(ANDROID)
	return context->GetCacheDir(Java::GetEnv());
#else
280
	return nullptr;
281
#endif
282 283
}

284
#ifdef _WIN32
285

286 287
AllocatedPath
GetSystemConfigDir() noexcept
288 289 290 291
{
	return GetStandardDir(CSIDL_COMMON_APPDATA);
}

292 293
AllocatedPath
GetAppBaseDir() noexcept
294
{
295
	std::array<PathTraitsFS::value_type, MAX_PATH> app;
296 297 298 299
	auto ret = GetModuleFileName(nullptr, app.data(), app.size());

	// Check for error
	if (ret == 0)
300
		return nullptr;
301 302 303

	// Check for truncation
	if (ret == app.size() && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
304
		return nullptr;
305 306 307 308 309 310 311

	auto app_path = AllocatedPath::FromFS(app.data());
	return app_path.GetDirectoryName().GetDirectoryName();
}

#else

312 313
AllocatedPath
GetHomeDir() noexcept
314
{
315
#ifndef ANDROID
316 317 318 319 320 321
	auto home = getenv("HOME");
	if (IsValidPathString(home) && IsValidDir(home))
		return AllocatedPath::FromFS(home);
	PasswdEntry pw;
	if (pw.ReadByUid(getuid()))
		return SafePathFromFS(pw->pw_dir);
322
#endif
323
	return nullptr;
324 325
}

326 327
AllocatedPath
GetHomeDir(const char *user_name) noexcept
328
{
329 330 331
#ifdef ANDROID
	(void)user_name;
#else
332 333 334 335
	assert(user_name != nullptr);
	PasswdEntry pw;
	if (pw.ReadByName(user_name))
		return SafePathFromFS(pw->pw_dir);
336
#endif
337
	return nullptr;
338 339 340
}

#endif