1/*
2 * Copyright 2022, Andrew Lindesay <apl@lindesay.co.nz>.
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
5
6#include "PackageUtils.h"
7
8
9using namespace BPackageKit;
10
11
12/*static*/
13status_t PackageUtils::DeriveLocalFilePath(const PackageInfo* package,
14	BPath& path)
15{
16	if (package->IsLocalFile()) {
17		path.SetTo(package->LocalFilePath());
18		return B_OK;
19	}
20
21	path.Unset();
22	BPackageInstallationLocation installationLocation
23		= DeriveInstallLocation(package);
24	directory_which which;
25	status_t result = _DeriveDirectoryWhich(installationLocation, &which);
26
27	if (result == B_OK)
28		result = find_directory(which, &path);
29
30	if (result == B_OK)
31		path.Append(package->FileName());
32
33	return result;
34}
35
36
37/*static*/ status_t
38PackageUtils::_DeriveDirectoryWhich(
39	BPackageInstallationLocation location,
40	directory_which* which)
41{
42	switch (location) {
43		case B_PACKAGE_INSTALLATION_LOCATION_SYSTEM:
44			*which = B_SYSTEM_PACKAGES_DIRECTORY;
45			break;
46		case B_PACKAGE_INSTALLATION_LOCATION_HOME:
47			*which = B_USER_PACKAGES_DIRECTORY;
48			break;
49		default:
50			debugger("illegal state: unsupported package installation"
51				"location");
52			return B_BAD_VALUE;
53	}
54	return B_OK;
55}
56
57
58/*static*/ BPackageInstallationLocation
59PackageUtils::DeriveInstallLocation(const PackageInfo* package)
60{
61	const PackageInstallationLocationSet& locations
62		= package->InstallationLocations();
63
64	// If the package is already installed, return its first installed location
65	if (locations.size() != 0)
66		return static_cast<BPackageInstallationLocation>(*locations.begin());
67
68	return B_PACKAGE_INSTALLATION_LOCATION_SYSTEM;
69}
70