Path.cxx 2.76 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 "Path.hxx"
21
#include "Data.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"
Warren Dukes's avatar
Warren Dukes committed
27

28 29
#include <assert.h>
#include <string.h>
30

31
#ifndef _WIN32
32
#include <pwd.h>
33

34 35
static const char *configured_user = nullptr;

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

46
	return result;
47 48 49 50 51
}

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

59
	return result;
60 61 62 63
}

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

75
#endif
76

77 78 79 80 81 82 83 84 85 86
void
InitPathParser(const ConfigData &config) noexcept
{
#ifdef _WIN32
	(void)config;
#else
	configured_user = config.GetString(ConfigOption::USER);
#endif
}

87
AllocatedPath
88
ParsePath(const char *path)
89
{
Max Kellermann's avatar
Max Kellermann committed
90
	assert(path != nullptr);
91

92
#ifndef _WIN32
93
	if (path[0] == '~') {
94 95
		++path;

96
		if (*path == '\0')
97
			return GetConfiguredHome();
98

99
		AllocatedPath home = nullptr;
100 101

		if (*path == '/') {
102
			home = GetConfiguredHome();
103 104

			++path;
105
		} else {
106
			const char *slash = strchr(path, '/');
107 108 109 110
			const char *end = slash == nullptr
					? path + strlen(path)
					: slash;
			const std::string user(path, end);
111
			home = GetHome(user.c_str());
112

113 114 115 116
			if (slash == nullptr)
				return home;

			path = slash + 1;
117 118
		}

119
		if (home.IsNull())
120
			return nullptr;
121

122
		return home / AllocatedPath::FromUTF8Throw(path);
123
	} else if (!PathTraitsUTF8::IsAbsolute(path)) {
124
		throw FormatRuntimeError("not an absolute path: %s", path);
125
	} else {
126
#endif
127
		return AllocatedPath::FromUTF8Throw(path);
128
#ifndef _WIN32
129
	}
130
#endif
131
}