Path.cxx 2.56 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 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/StandardDirectory.hxx"
25
#include "util/RuntimeError.hxx"
26
#include "util/StringView.hxx"
Warren Dukes's avatar
Warren Dukes committed
27

28 29
#include <cassert>

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

33 34
static const char *configured_user = nullptr;

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 70
	return configured_user != nullptr
		? GetHome(configured_user)
71
		: GetHome();
72 73
}

74
#endif
75

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

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

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

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

		if (*path == '/') {
99
			++path;
100

101 102 103 104 105
			return GetConfiguredHome() /
				AllocatedPath::FromUTF8Throw(path);
		} else {
			const auto [user, rest] =
				StringView{path}.Split('/');
106

107 108
			return GetHome(std::string{user}.c_str())
				/ AllocatedPath::FromUTF8Throw(rest);
109
		}
110
	} else if (!PathTraitsUTF8::IsAbsolute(path)) {
111
		throw FormatRuntimeError("not an absolute path: %s", path);
112
	} else {
113
#endif
114
		return AllocatedPath::FromUTF8Throw(path);
115
#ifndef _WIN32
116
	}
117
#endif
118
}