1/*
2 * Copyright 2009-2011, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef PACKAGE_H
6#define PACKAGE_H
7
8
9#include <package/PackageArchitecture.h>
10
11#include <Referenceable.h>
12
13#include <util/DoublyLinkedList.h>
14#include <util/khash.h>
15#include <util/OpenHashTable.h>
16
17#include <lock.h>
18
19#include "Dependency.h"
20#include "PackageNode.h"
21#include "Resolvable.h"
22
23
24using BPackageKit::BPackageArchitecture;
25
26
27class PackageDomain;
28class PackageLinkDirectory;
29class Version;
30
31
32class Package : public BReferenceable,
33	public DoublyLinkedListLinkImpl<Package> {
34public:
35								Package(PackageDomain* domain, dev_t deviceID,
36									ino_t nodeID);
37								~Package();
38
39			status_t			Init(const char* fileName);
40
41			PackageDomain*		Domain() const		{ return fDomain; }
42			const char*			FileName() const	{ return fFileName; }
43
44			status_t			SetName(const char* name);
45			const char*			Name() const		{ return fName; }
46
47			status_t			SetInstallPath(const char* installPath);
48			const char*			InstallPath() const	{ return fInstallPath; }
49
50			void				SetVersion(::Version* version);
51									// takes over object ownership
52			::Version*			Version() const
53									{ return fVersion; }
54
55			void				SetArchitecture(
56									BPackageArchitecture architecture)
57									{ fArchitecture = architecture; }
58			BPackageArchitecture Architecture() const
59									{ return fArchitecture; }
60			const char*			ArchitectureName() const;
61
62			void				SetLinkDirectory(
63									PackageLinkDirectory* linkDirectory)
64									{ fLinkDirectory = linkDirectory; }
65			PackageLinkDirectory* LinkDirectory() const
66									{ return fLinkDirectory; }
67
68			Package*&			FileNameHashTableNext()
69									{ return fFileNameHashTableNext; }
70
71			void				AddNode(PackageNode* node);
72			void				AddResolvable(Resolvable* resolvable);
73			void				AddDependency(Dependency* dependency);
74
75			int					Open();
76			void				Close();
77
78			const PackageNodeList& Nodes() const	{ return fNodes; }
79			const ResolvableList& Resolvables() const
80									{ return fResolvables; }
81			const DependencyList& Dependencies() const
82									{ return fDependencies; }
83
84private:
85			mutex				fLock;
86			PackageDomain*		fDomain;
87			char*				fFileName;
88			char*				fName;
89			char*				fInstallPath;
90			::Version*			fVersion;
91			BPackageArchitecture fArchitecture;
92			PackageLinkDirectory* fLinkDirectory;
93			int					fFD;
94			uint32				fOpenCount;
95			Package*			fFileNameHashTableNext;
96			ino_t				fNodeID;
97			dev_t				fDeviceID;
98			PackageNodeList		fNodes;
99			ResolvableList		fResolvables;
100			DependencyList		fDependencies;
101};
102
103
104struct PackageCloser {
105	PackageCloser(Package* package)
106		:
107		fPackage(package)
108	{
109	}
110
111	~PackageCloser()
112	{
113		if (fPackage != NULL)
114			fPackage->Close();
115	}
116
117	void Detach()
118	{
119		fPackage = NULL;
120	}
121
122private:
123	Package*	fPackage;
124};
125
126
127struct PackageFileNameHashDefinition {
128	typedef const char*		KeyType;
129	typedef	Package			ValueType;
130
131	size_t HashKey(const char* key) const
132	{
133		return hash_hash_string(key);
134	}
135
136	size_t Hash(const Package* value) const
137	{
138		return HashKey(value->FileName());
139	}
140
141	bool Compare(const char* key, const Package* value) const
142	{
143		return strcmp(value->FileName(), key) == 0;
144	}
145
146	Package*& GetLink(Package* value) const
147	{
148		return value->FileNameHashTableNext();
149	}
150};
151
152
153typedef BOpenHashTable<PackageFileNameHashDefinition> PackageFileNameHashTable;
154typedef DoublyLinkedList<Package> PackageList;
155
156
157#endif	// PACKAGE_H
158