xref: /haiku/src/add-ons/kernel/file_systems/packagefs/resolvables/Dependency.cpp (revision 1e7416d9b3846396e2f7d6552e37d72664cd6853)
1*1e7416d9SIngo Weinhold /*
2*1e7416d9SIngo Weinhold  * Copyright 2011, Ingo Weinhold, ingo_weinhold@gmx.de.
3*1e7416d9SIngo Weinhold  * Distributed under the terms of the MIT License.
4*1e7416d9SIngo Weinhold  */
5*1e7416d9SIngo Weinhold 
6*1e7416d9SIngo Weinhold 
7*1e7416d9SIngo Weinhold #include "Dependency.h"
8*1e7416d9SIngo Weinhold 
9*1e7416d9SIngo Weinhold #include <stdlib.h>
10*1e7416d9SIngo Weinhold #include <string.h>
11*1e7416d9SIngo Weinhold 
12*1e7416d9SIngo Weinhold #include "Version.h"
13*1e7416d9SIngo Weinhold 
14*1e7416d9SIngo Weinhold 
15*1e7416d9SIngo Weinhold Dependency::Dependency(::Package* package)
16*1e7416d9SIngo Weinhold 	:
17*1e7416d9SIngo Weinhold 	fPackage(package),
18*1e7416d9SIngo Weinhold 	fFamily(NULL),
19*1e7416d9SIngo Weinhold 	fResolvable(NULL),
20*1e7416d9SIngo Weinhold 	fName(NULL),
21*1e7416d9SIngo Weinhold 	fVersion(NULL),
22*1e7416d9SIngo Weinhold 	fVersionOperator(B_PACKAGE_RESOLVABLE_OP_EQUAL)
23*1e7416d9SIngo Weinhold {
24*1e7416d9SIngo Weinhold }
25*1e7416d9SIngo Weinhold 
26*1e7416d9SIngo Weinhold 
27*1e7416d9SIngo Weinhold Dependency::~Dependency()
28*1e7416d9SIngo Weinhold {
29*1e7416d9SIngo Weinhold 	free(fName);
30*1e7416d9SIngo Weinhold 	delete fVersion;
31*1e7416d9SIngo Weinhold }
32*1e7416d9SIngo Weinhold 
33*1e7416d9SIngo Weinhold 
34*1e7416d9SIngo Weinhold status_t
35*1e7416d9SIngo Weinhold Dependency::Init(const char* name)
36*1e7416d9SIngo Weinhold {
37*1e7416d9SIngo Weinhold 	fName = strdup(name);
38*1e7416d9SIngo Weinhold 	if (fName == NULL)
39*1e7416d9SIngo Weinhold 		return B_NO_MEMORY;
40*1e7416d9SIngo Weinhold 
41*1e7416d9SIngo Weinhold 	return B_OK;
42*1e7416d9SIngo Weinhold }
43*1e7416d9SIngo Weinhold 
44*1e7416d9SIngo Weinhold 
45*1e7416d9SIngo Weinhold void
46*1e7416d9SIngo Weinhold Dependency::SetVersionRequirement(BPackageResolvableOperator op,
47*1e7416d9SIngo Weinhold 	Version* version)
48*1e7416d9SIngo Weinhold {
49*1e7416d9SIngo Weinhold 	fVersionOperator = op;
50*1e7416d9SIngo Weinhold 	fVersion = version;
51*1e7416d9SIngo Weinhold }
52*1e7416d9SIngo Weinhold 
53*1e7416d9SIngo Weinhold 
54*1e7416d9SIngo Weinhold bool
55*1e7416d9SIngo Weinhold Dependency::ResolvableVersionMatches(Version* resolvableVersion) const
56*1e7416d9SIngo Weinhold {
57*1e7416d9SIngo Weinhold 	if (fVersion == NULL)
58*1e7416d9SIngo Weinhold 		return true;
59*1e7416d9SIngo Weinhold 
60*1e7416d9SIngo Weinhold 	return resolvableVersion != NULL
61*1e7416d9SIngo Weinhold 		&& resolvableVersion->Compare(fVersionOperator, *fVersion);
62*1e7416d9SIngo Weinhold }
63*1e7416d9SIngo Weinhold 
64*1e7416d9SIngo Weinhold 
65*1e7416d9SIngo Weinhold bool
66*1e7416d9SIngo Weinhold Dependency::ResolvableCompatibleVersionMatches(Version* resolvableVersion) const
67*1e7416d9SIngo Weinhold {
68*1e7416d9SIngo Weinhold 	if (fVersion == NULL)
69*1e7416d9SIngo Weinhold 		return true;
70*1e7416d9SIngo Weinhold 
71*1e7416d9SIngo Weinhold 	// Only compare the versions, if the operator indicates that our version is
72*1e7416d9SIngo Weinhold 	// some kind of minimum required version. Only in that case the resolvable's
73*1e7416d9SIngo Weinhold 	// actual version is required to be greater (or equal) and the backwards
74*1e7416d9SIngo Weinhold 	// compatibility check makes sense.
75*1e7416d9SIngo Weinhold 	if (fVersionOperator == B_PACKAGE_RESOLVABLE_OP_GREATER_EQUAL
76*1e7416d9SIngo Weinhold 		|| fVersionOperator == B_PACKAGE_RESOLVABLE_OP_GREATER) {
77*1e7416d9SIngo Weinhold 		return resolvableVersion != NULL
78*1e7416d9SIngo Weinhold 			&& fVersion->Compare(*resolvableVersion) >= 0;
79*1e7416d9SIngo Weinhold 	}
80*1e7416d9SIngo Weinhold 
81*1e7416d9SIngo Weinhold 	return true;
82*1e7416d9SIngo Weinhold }
83