xref: /haiku/src/add-ons/kernel/file_systems/packagefs/resolvables/Dependency.cpp (revision 9f81ca838ce7b92b5689e57d3f86765db4705a7b)
11e7416d9SIngo Weinhold /*
21e7416d9SIngo Weinhold  * Copyright 2011, Ingo Weinhold, ingo_weinhold@gmx.de.
31e7416d9SIngo Weinhold  * Distributed under the terms of the MIT License.
41e7416d9SIngo Weinhold  */
51e7416d9SIngo Weinhold 
61e7416d9SIngo Weinhold 
71e7416d9SIngo Weinhold #include "Dependency.h"
81e7416d9SIngo Weinhold 
91e7416d9SIngo Weinhold #include <stdlib.h>
101e7416d9SIngo Weinhold #include <string.h>
111e7416d9SIngo Weinhold 
12*b8ab901eSIngo Weinhold #include <AutoDeleter.h>
13*b8ab901eSIngo Weinhold 
141e7416d9SIngo Weinhold #include "Version.h"
151e7416d9SIngo Weinhold 
161e7416d9SIngo Weinhold 
Dependency(::Package * package)171e7416d9SIngo Weinhold Dependency::Dependency(::Package* package)
181e7416d9SIngo Weinhold 	:
191e7416d9SIngo Weinhold 	fPackage(package),
201e7416d9SIngo Weinhold 	fFamily(NULL),
211e7416d9SIngo Weinhold 	fResolvable(NULL),
22d07c930cSIngo Weinhold 	fName(),
23*b8ab901eSIngo Weinhold 	fFileName(),
241e7416d9SIngo Weinhold 	fVersion(NULL),
251e7416d9SIngo Weinhold 	fVersionOperator(B_PACKAGE_RESOLVABLE_OP_EQUAL)
261e7416d9SIngo Weinhold {
271e7416d9SIngo Weinhold }
281e7416d9SIngo Weinhold 
291e7416d9SIngo Weinhold 
~Dependency()301e7416d9SIngo Weinhold Dependency::~Dependency()
311e7416d9SIngo Weinhold {
321e7416d9SIngo Weinhold 	delete fVersion;
331e7416d9SIngo Weinhold }
341e7416d9SIngo Weinhold 
351e7416d9SIngo Weinhold 
361e7416d9SIngo Weinhold status_t
Init(const char * name)371e7416d9SIngo Weinhold Dependency::Init(const char* name)
381e7416d9SIngo Weinhold {
39d07c930cSIngo Weinhold 	if (!fName.SetTo(name))
401e7416d9SIngo Weinhold 		return B_NO_MEMORY;
411e7416d9SIngo Weinhold 
42*b8ab901eSIngo Weinhold 	// If the name contains a ':', replace it with '~' in the file name. We do
43*b8ab901eSIngo Weinhold 	// that so that a path containing the symlink can be used in colon-separated
44*b8ab901eSIngo Weinhold 	// search paths.
45*b8ab901eSIngo Weinhold 	if (strchr(name, ':') != NULL) {
46*b8ab901eSIngo Weinhold 		char* fileName = strdup(name);
47*b8ab901eSIngo Weinhold 		if (fileName == NULL)
48*b8ab901eSIngo Weinhold 			return B_NO_MEMORY;
49*b8ab901eSIngo Weinhold 		MemoryDeleter fileNameDeleter(fileName);
50*b8ab901eSIngo Weinhold 
51*b8ab901eSIngo Weinhold 		char* remainder = fileName;
52*b8ab901eSIngo Weinhold 		while (char* colon = strchr(remainder, ':')) {
53*b8ab901eSIngo Weinhold 			*colon = '~';
54*b8ab901eSIngo Weinhold 			remainder = colon + 1;
55*b8ab901eSIngo Weinhold 		}
56*b8ab901eSIngo Weinhold 
57*b8ab901eSIngo Weinhold 		if (!fFileName.SetTo(fileName))
58*b8ab901eSIngo Weinhold 			return B_NO_MEMORY;
59*b8ab901eSIngo Weinhold 	} else
60*b8ab901eSIngo Weinhold 		fFileName = fName;
61*b8ab901eSIngo Weinhold 
621e7416d9SIngo Weinhold 	return B_OK;
631e7416d9SIngo Weinhold }
641e7416d9SIngo Weinhold 
651e7416d9SIngo Weinhold 
661e7416d9SIngo Weinhold void
SetVersionRequirement(BPackageResolvableOperator op,Version * version)671e7416d9SIngo Weinhold Dependency::SetVersionRequirement(BPackageResolvableOperator op,
681e7416d9SIngo Weinhold 	Version* version)
691e7416d9SIngo Weinhold {
701e7416d9SIngo Weinhold 	fVersionOperator = op;
711e7416d9SIngo Weinhold 	fVersion = version;
721e7416d9SIngo Weinhold }
731e7416d9SIngo Weinhold 
741e7416d9SIngo Weinhold 
751e7416d9SIngo Weinhold bool
ResolvableVersionMatches(Version * resolvableVersion) const761e7416d9SIngo Weinhold Dependency::ResolvableVersionMatches(Version* resolvableVersion) const
771e7416d9SIngo Weinhold {
781e7416d9SIngo Weinhold 	if (fVersion == NULL)
791e7416d9SIngo Weinhold 		return true;
801e7416d9SIngo Weinhold 
811e7416d9SIngo Weinhold 	return resolvableVersion != NULL
821e7416d9SIngo Weinhold 		&& resolvableVersion->Compare(fVersionOperator, *fVersion);
831e7416d9SIngo Weinhold }
841e7416d9SIngo Weinhold 
851e7416d9SIngo Weinhold 
861e7416d9SIngo Weinhold bool
ResolvableCompatibleVersionMatches(Version * resolvableVersion) const871e7416d9SIngo Weinhold Dependency::ResolvableCompatibleVersionMatches(Version* resolvableVersion) const
881e7416d9SIngo Weinhold {
891e7416d9SIngo Weinhold 	if (fVersion == NULL)
901e7416d9SIngo Weinhold 		return true;
911e7416d9SIngo Weinhold 
921e7416d9SIngo Weinhold 	// Only compare the versions, if the operator indicates that our version is
931e7416d9SIngo Weinhold 	// some kind of minimum required version. Only in that case the resolvable's
941e7416d9SIngo Weinhold 	// actual version is required to be greater (or equal) and the backwards
951e7416d9SIngo Weinhold 	// compatibility check makes sense.
961e7416d9SIngo Weinhold 	if (fVersionOperator == B_PACKAGE_RESOLVABLE_OP_GREATER_EQUAL
971e7416d9SIngo Weinhold 		|| fVersionOperator == B_PACKAGE_RESOLVABLE_OP_GREATER) {
981e7416d9SIngo Weinhold 		return resolvableVersion != NULL
991e7416d9SIngo Weinhold 			&& fVersion->Compare(*resolvableVersion) >= 0;
1001e7416d9SIngo Weinhold 	}
1011e7416d9SIngo Weinhold 
1021e7416d9SIngo Weinhold 	return true;
1031e7416d9SIngo Weinhold }
104