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