Skip to content

Commit 62b164b

Browse files
committed
BPathFinder: Add BPackageResolvableExpression initialization
Add a constructor and a SetTo() method with a BPackageResolvableExpression parameter instead of a path. The path of the package satisfying the expression is used. The new functionality lives in libpackage as it uses the package kit.
1 parent ee8e915 commit 62b164b

File tree

4 files changed

+116
-1
lines changed

4 files changed

+116
-1
lines changed

headers/os/storage/PathFinder.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,37 @@ class BStringList;
1515
struct entry_ref;
1616

1717

18+
namespace BPackageKit {
19+
class BPackageResolvableExpression;
20+
}
21+
22+
1823
class BPathFinder {
24+
public:
25+
typedef BPackageKit::BPackageResolvableExpression
26+
BResolvableExpression;
27+
1928
public:
2029
BPathFinder(const void* codePointer = NULL,
2130
const char* dependency = NULL);
2231
BPathFinder(const char* path,
2332
const char* dependency = NULL);
2433
BPathFinder(const entry_ref& ref,
2534
const char* dependency = NULL);
35+
BPathFinder(
36+
const BResolvableExpression& expression,
37+
const char* dependency = NULL);
38+
// requires libpackage
2639

2740
status_t SetTo(const void* codePointer = NULL,
2841
const char* dependency = NULL);
2942
status_t SetTo(const char* path,
3043
const char* dependency = NULL);
3144
status_t SetTo(const entry_ref& ref,
3245
const char* dependency = NULL);
46+
status_t SetTo(const BResolvableExpression& expression,
47+
const char* dependency = NULL);
48+
// requires libpackage
3349

3450
status_t FindPath(const char* architecture,
3551
path_base_directory baseDirectory,
@@ -64,7 +80,7 @@ class BPathFinder {
6480
BString fPath;
6581
BString fDependency;
6682
status_t fInitStatus;
67-
uint32 fReserved[4];
83+
addr_t fReserved[4];
6884
};
6985

7086

src/kits/package/Jamfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ UsePrivateHeaders
55
shared
66
storage
77
;
8+
UsePrivateSystemHeaders ;
89

910
HPKG_SOURCES =
1011
AttributeDataReader.cpp
@@ -103,6 +104,7 @@ for architectureObject in [ MultiArchSubDirSetup ] {
103104
PackageResolvableExpression.cpp
104105
PackageRoster.cpp
105106
PackageVersion.cpp
107+
PathFinder.cpp
106108
RefreshRepositoryRequest.cpp
107109
RemoveRepositoryJob.cpp
108110
RepositoryCache.cpp

src/kits/package/PathFinder.cpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 2013, Ingo Weinhold, [email protected].
3+
* Distributed under the terms of the MIT License.
4+
*/
5+
6+
7+
#include <PathFinder.h>
8+
9+
#include <package/PackageResolvableExpression.h>
10+
#include <package/solver/SolverPackage.h>
11+
12+
#include <directories.h>
13+
#include <package/manager/PackageManager.h>
14+
15+
16+
// NOTE: This is only the package kit specific part of BPathFinder. Everything
17+
// else is implemented in the storage kit.
18+
19+
20+
using namespace BPackageKit;
21+
using namespace BPackageKit::BPrivate;
22+
using namespace BPackageKit::BManager::BPrivate;
23+
24+
25+
static status_t
26+
find_package(const BPackageResolvableExpression& expression,
27+
BString& _versionedPackageName)
28+
{
29+
if (expression.InitCheck() != B_OK)
30+
return B_BAD_VALUE;
31+
32+
// create the package manager -- we only want to use its solver
33+
BPackageManager::ClientInstallationInterface installationInterface;
34+
BPackageManager::UserInteractionHandler userInteractionHandler;
35+
BPackageManager packageManager(B_PACKAGE_INSTALLATION_LOCATION_HOME,
36+
&installationInterface, &userInteractionHandler);
37+
packageManager.Init(BPackageManager::B_ADD_INSTALLED_REPOSITORIES);
38+
39+
// search
40+
BObjectList<BSolverPackage> packages;
41+
status_t error = packageManager.Solver()->FindPackages(expression.Name(),
42+
BSolver::B_FIND_IN_NAME | BSolver::B_FIND_IN_PROVIDES, packages);
43+
if (error != B_OK)
44+
return B_ENTRY_NOT_FOUND;
45+
46+
// find the newest matching package
47+
BSolverPackage* foundPackage = NULL;
48+
for (int32 i = 0; BSolverPackage* package = packages.ItemAt(i); i++) {
49+
if (package->Info().Matches(expression)
50+
&& (foundPackage == NULL
51+
|| package->Info().Version().Compare(
52+
foundPackage->Info().Version()) > 0)) {
53+
foundPackage = package;
54+
}
55+
}
56+
57+
if (foundPackage == NULL)
58+
return B_ENTRY_NOT_FOUND;
59+
60+
BString version = foundPackage->Info().Version().ToString();
61+
_versionedPackageName = foundPackage->VersionedName();
62+
return _versionedPackageName.IsEmpty() ? B_NO_MEMORY : B_OK;
63+
}
64+
65+
66+
BPathFinder::BPathFinder(const BResolvableExpression& expression,
67+
const char* dependency)
68+
{
69+
SetTo(expression, dependency);
70+
}
71+
72+
73+
status_t
74+
BPathFinder::SetTo(const BResolvableExpression& expression,
75+
const char* dependency)
76+
{
77+
BString versionedPackageName;
78+
fInitStatus = find_package(expression, versionedPackageName);
79+
if (fInitStatus != B_OK)
80+
return fInitStatus;
81+
82+
BString packageLinksPath;
83+
packageLinksPath.SetToFormat(kSystemPackageLinksDirectory "/%s",
84+
versionedPackageName.String());
85+
if (packageLinksPath.IsEmpty())
86+
return fInitStatus = B_NO_MEMORY;
87+
88+
struct stat st;
89+
if (lstat(packageLinksPath, &st) < 0)
90+
return fInitStatus = B_ENTRY_NOT_FOUND;
91+
92+
return _SetTo(NULL, packageLinksPath, dependency);
93+
}

src/kits/storage/PathFinder.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
#include <StringList.h>
1212

1313

14+
// NOTE: The package kit specific part of BPathFinder (BResolvableExpression
15+
// constructor and SetTo()) is implemented in the package kit.
16+
17+
1418
BPathFinder::BPathFinder(const void* codePointer, const char* dependency)
1519
{
1620
_SetTo(codePointer, NULL, dependency);

0 commit comments

Comments
 (0)