xref: /haiku/src/add-ons/kernel/file_systems/packagefs/package/Package.h (revision 2b76973fa2401f7a5edf68e6470f3d3210cbcff3)
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/hpkg/DataReader.h>
10 #include <package/PackageArchitecture.h>
11 
12 #include <Referenceable.h>
13 
14 #include <util/DoublyLinkedList.h>
15 #include <util/khash.h>
16 #include <util/OpenHashTable.h>
17 
18 #include <lock.h>
19 
20 #include "Dependency.h"
21 #include "PackageNode.h"
22 #include "Resolvable.h"
23 #include "String.h"
24 
25 
26 using BPackageKit::BPackageArchitecture;
27 using BPackageKit::BHPKG::BAbstractBufferedDataReader;
28 
29 
30 class PackageLinkDirectory;
31 class PackageSettings;
32 class Volume;
33 class Version;
34 
35 
36 class Package : public BReferenceable,
37 	public DoublyLinkedListLinkImpl<Package> {
38 public:
39 								Package(::Volume* volume, dev_t deviceID,
40 									ino_t nodeID);
41 								~Package();
42 
43 			status_t			Init(const char* fileName);
44 			status_t			Load(const PackageSettings& settings);
45 
46 			::Volume*			Volume() const		{ return fVolume; }
47 			const String&		FileName() const	{ return fFileName; }
48 
49 			void				SetName(const String& name);
50 			const String&		Name() const		{ return fName; }
51 
52 			const String&		VersionedName() const
53 									{ return fVersionedName; }
54 
55 			dev_t				DeviceID() const
56 									{ return fDeviceID; }
57 			ino_t				NodeID() const
58 									{ return fNodeID; }
59 
60 			void				SetInstallPath(const String& installPath);
61 			const String&		InstallPath() const	{ return fInstallPath; }
62 
63 			void				SetVersion(::Version* version);
64 									// takes over object ownership
65 			::Version*			Version() const
66 									{ return fVersion; }
67 
68 			void				SetArchitecture(
69 									BPackageArchitecture architecture)
70 									{ fArchitecture = architecture; }
71 			BPackageArchitecture Architecture() const
72 									{ return fArchitecture; }
73 			const char*			ArchitectureName() const;
74 
75 			void				SetLinkDirectory(
76 									PackageLinkDirectory* linkDirectory)
77 									{ fLinkDirectory = linkDirectory; }
78 			PackageLinkDirectory* LinkDirectory() const
79 									{ return fLinkDirectory; }
80 
81 			Package*&			FileNameHashTableNext()
82 									{ return fFileNameHashTableNext; }
83 
84 			void				AddNode(PackageNode* node);
85 			void				AddResolvable(Resolvable* resolvable);
86 			void				AddDependency(Dependency* dependency);
87 
88 			int					Open();
89 			void				Close();
90 
91 			status_t			CreateDataReader(const PackageData& data,
92 									BAbstractBufferedDataReader*& _reader);
93 
94 			const PackageNodeList& Nodes() const	{ return fNodes; }
95 			const ResolvableList& Resolvables() const
96 									{ return fResolvables; }
97 			const DependencyList& Dependencies() const
98 									{ return fDependencies; }
99 
100 private:
101 			struct LoaderErrorOutput;
102 			struct LoaderContentHandler;
103 			struct LoaderContentHandlerV1;
104 			struct HeapReader;
105 			struct HeapReaderV1;
106 			struct HeapReaderV2;
107 			struct CachingPackageReader;
108 
109 private:
110 			status_t			_Load(const PackageSettings& settings);
111 			bool				_InitVersionedName();
112 
113 private:
114 			mutex				fLock;
115 			::Volume*			fVolume;
116 			String				fFileName;
117 			String				fName;
118 			String				fInstallPath;
119 			String				fVersionedName;
120 			::Version*			fVersion;
121 			BPackageArchitecture fArchitecture;
122 			PackageLinkDirectory* fLinkDirectory;
123 			int					fFD;
124 			uint32				fOpenCount;
125 			HeapReader*			fHeapReader;
126 			Package*			fFileNameHashTableNext;
127 			ino_t				fNodeID;
128 			dev_t				fDeviceID;
129 			PackageNodeList		fNodes;
130 			ResolvableList		fResolvables;
131 			DependencyList		fDependencies;
132 };
133 
134 
135 struct PackageCloser {
136 	PackageCloser(Package* package)
137 		:
138 		fPackage(package)
139 	{
140 	}
141 
142 	~PackageCloser()
143 	{
144 		if (fPackage != NULL)
145 			fPackage->Close();
146 	}
147 
148 	void Detach()
149 	{
150 		fPackage = NULL;
151 	}
152 
153 private:
154 	Package*	fPackage;
155 };
156 
157 
158 struct PackageFileNameHashDefinition {
159 	typedef const char*		KeyType;
160 	typedef	Package			ValueType;
161 
162 	size_t HashKey(const char* key) const
163 	{
164 		return hash_hash_string(key);
165 	}
166 
167 	size_t Hash(const Package* value) const
168 	{
169 		return value->FileName().Hash();
170 	}
171 
172 	bool Compare(const char* key, const Package* value) const
173 	{
174 		return strcmp(value->FileName(), key) == 0;
175 	}
176 
177 	Package*& GetLink(Package* value) const
178 	{
179 		return value->FileNameHashTableNext();
180 	}
181 };
182 
183 
184 typedef BOpenHashTable<PackageFileNameHashDefinition> PackageFileNameHashTable;
185 typedef DoublyLinkedList<Package> PackageList;
186 
187 
188 #endif	// PACKAGE_H
189