ConfigPath.cxx 2.76 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3
 * http://www.musicpd.org
Warren Dukes's avatar
Warren Dukes committed
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
Warren Dukes's avatar
Warren Dukes committed
18 19
 */

20
#include "config.h"
Max Kellermann's avatar
Max Kellermann committed
21
#include "ConfigPath.hxx"
22
#include "fs/AllocatedPath.hxx"
23
#include "fs/Traits.hxx"
24
#include "fs/Domain.hxx"
25
#include "fs/StandardDirectory.hxx"
26
#include "util/RuntimeError.hxx"
27
#include "ConfigGlobal.hxx"
Warren Dukes's avatar
Warren Dukes committed
28

29 30
#include <assert.h>
#include <string.h>
31 32 33

#ifndef WIN32
#include <pwd.h>
34 35 36 37

/**
 * Determine a given user's home directory.
 */
38
static AllocatedPath
39
GetHome(const char *user)
40
{
41
	AllocatedPath result = GetHomeDir(user);
42 43
	if (result.IsNull())
		throw FormatRuntimeError("no such user: %s", user);
44

45
	return result;
46 47 48 49 50
}

/**
 * Determine the current user's home directory.
 */
51
static AllocatedPath
52
GetHome()
53
{
54
	AllocatedPath result = GetHomeDir();
55 56
	if (result.IsNull())
		throw std::runtime_error("problems getting home for current user");
57

58
	return result;
59 60 61 62
}

/**
 * Determine the configured user's home directory.
63 64
 *
 * Throws #std::runtime_error on error.
65
 */
66
static AllocatedPath
67
GetConfiguredHome()
68
{
69
	const char *user = config_get_string(ConfigOption::USER);
70
	return user != nullptr
71 72
		? GetHome(user)
		: GetHome();
73 74
}

75
#endif
76

77
AllocatedPath
78
ParsePath(const char *path)
79
{
Max Kellermann's avatar
Max Kellermann committed
80
	assert(path != nullptr);
81

82
#ifndef WIN32
83
	if (path[0] == '~') {
84 85
		++path;

86
		if (*path == '\0')
87
			return GetConfiguredHome();
88

89
		AllocatedPath home = AllocatedPath::Null();
90 91

		if (*path == '/') {
92
			home = GetConfiguredHome();
93 94

			++path;
95
		} else {
96
			const char *slash = strchr(path, '/');
97 98 99 100
			const char *end = slash == nullptr
					? path + strlen(path)
					: slash;
			const std::string user(path, end);
101
			home = GetHome(user.c_str());
102

103 104 105 106
			if (slash == nullptr)
				return home;

			path = slash + 1;
107 108
		}

109
		if (home.IsNull())
110
			return AllocatedPath::Null();
111

112
		AllocatedPath path2 = AllocatedPath::FromUTF8Throw(path);
113
		if (path2.IsNull())
114
			return AllocatedPath::Null();
115

116
		return AllocatedPath::Build(home, path2);
117
	} else if (!PathTraitsUTF8::IsAbsolute(path)) {
118
		throw FormatRuntimeError("not an absolute path: %s", path);
119
	} else {
120
#endif
121
		return AllocatedPath::FromUTF8Throw(path);
122
#ifndef WIN32
123
	}
124
#endif
125
}