Path.cxx 2.78 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 "config.h"
21
#include "Path.hxx"
22
#include "Data.hxx"
23
#include "fs/AllocatedPath.hxx"
24
#include "fs/Traits.hxx"
25
#include "fs/Domain.hxx"
26
#include "fs/StandardDirectory.hxx"
27
#include "util/RuntimeError.hxx"
Warren Dukes's avatar
Warren Dukes committed
28

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

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

35 36
static const char *configured_user = nullptr;

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

47
	return result;
48 49 50 51 52
}

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

60
	return result;
61 62 63 64
}

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

76
#endif
77

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

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

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

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

100
		AllocatedPath home = nullptr;
101 102

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

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

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

			path = slash + 1;
118 119
		}

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

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