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