xref: /haiku/headers/os/package/PackageInfo.h (revision fce4895d1884da5ae6fb299d23c735c598e690b1)
1 /*
2  * Copyright 2011, Haiku, Inc.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _PACKAGE__PACKAGE_INFO_H_
6 #define _PACKAGE__PACKAGE_INFO_H_
7 
8 
9 #include <Archivable.h>
10 #include <ObjectList.h>
11 #include <String.h>
12 #include <StringList.h>
13 
14 #include <package/GlobalWritableFileInfo.h>
15 #include <package/PackageArchitecture.h>
16 #include <package/PackageFlags.h>
17 #include <package/PackageInfoAttributes.h>
18 #include <package/PackageResolvable.h>
19 #include <package/PackageResolvableExpression.h>
20 #include <package/PackageVersion.h>
21 #include <package/User.h>
22 #include <package/UserSettingsFileInfo.h>
23 
24 
25 class BEntry;
26 class BFile;
27 
28 
29 namespace BPackageKit {
30 
31 
32 /*
33  * Keeps a list of package info elements (e.g. name, version, vendor, ...) which
34  * will be converted to package attributes when creating a package. Usually,
35  * these elements have been parsed from a ".PackageInfo" file.
36  * Alternatively, they can be read from an existing package file.
37  */
38 class BPackageInfo : public BArchivable {
39 public:
40 			struct ParseErrorListener {
41 				virtual	~ParseErrorListener();
42 				virtual void OnError(const BString& msg, int line, int col) = 0;
43 			};
44 
45 public:
46 								BPackageInfo();
47 								BPackageInfo(BMessage* archive,
48 									status_t* _error = NULL);
49 	virtual						~BPackageInfo();
50 
51 			status_t			ReadFromConfigFile(
52 									const BEntry& packageInfoEntry,
53 									ParseErrorListener* listener = NULL);
54 			status_t			ReadFromConfigFile(
55 									BFile& packageInfoFile,
56 									ParseErrorListener* listener = NULL);
57 			status_t			ReadFromConfigString(
58 									const BString& packageInfoString,
59 									ParseErrorListener* listener = NULL);
60 			status_t			ReadFromPackageFile(const char* path);
61 			status_t			ReadFromPackageFile(int fd);
62 
63 			status_t			InitCheck() const;
64 
65 			const BString&		Name() const;
66 			const BString&		Summary() const;
67 			const BString&		Description() const;
68 			const BString&		Vendor() const;
69 			const BString&		Packager() const;
70 			const BString&		BasePackage() const;
71 			const BString&		Checksum() const;
72 			const BString&		InstallPath() const;
73 			BString				FileName() const;
74 									// the explicitly set file name, if any, or
75 									// CanonicalFileName() otherwise
76 
77 			uint32				Flags() const;
78 
79 			BPackageArchitecture	Architecture() const;
80 			const char*			ArchitectureName() const;
81 									// NULL, if invalid
82 
83 			const BPackageVersion&	Version() const;
84 
85 			const BStringList&	CopyrightList() const;
86 			const BStringList&	LicenseList() const;
87 			const BStringList&	URLList() const;
88 			const BStringList&	SourceURLList() const;
89 
90 			const BObjectList<BGlobalWritableFileInfo>&
91 									GlobalWritableFileInfos() const;
92 			const BObjectList<BUserSettingsFileInfo>&
93 									UserSettingsFileInfos() const;
94 
95 			const BObjectList<BUser>& Users() const;
96 			const BStringList&	Groups() const;
97 
98 			const BStringList&	PostInstallScripts() const;
99 
100 			const BObjectList<BPackageResolvable>&	ProvidesList() const;
101 			const BObjectList<BPackageResolvableExpression>&
102 								RequiresList() const;
103 			const BObjectList<BPackageResolvableExpression>&
104 								SupplementsList() const;
105 			const BObjectList<BPackageResolvableExpression>&
106 								ConflictsList() const;
107 			const BObjectList<BPackageResolvableExpression>&
108 								FreshensList() const;
109 			const BStringList&	ReplacesList() const;
110 
111 			BString				CanonicalFileName() const;
112 
113 			bool				Matches(const BPackageResolvableExpression&
114 									expression) const;
115 
116 			void				SetName(const BString& name);
117 			void				SetSummary(const BString& summary);
118 			void				SetDescription(const BString& description);
119 			void				SetVendor(const BString& vendor);
120 			void				SetPackager(const BString& packager);
121 			void				SetBasePackage(const BString& basePackage);
122 			void				SetChecksum(const BString& checksum);
123 			void				SetInstallPath(const BString& installPath);
124 			void				SetFileName(const BString& fileName);
125 
126 			void				SetFlags(uint32 flags);
127 
128 			void				SetArchitecture(
129 									BPackageArchitecture architecture);
130 
131 			void				SetVersion(const BPackageVersion& version);
132 
133 			void				ClearCopyrightList();
134 			status_t			AddCopyright(const BString& copyright);
135 
136 			void				ClearLicenseList();
137 			status_t			AddLicense(const BString& license);
138 
139 			void				ClearURLList();
140 			status_t			AddURL(const BString& url);
141 
142 			void				ClearSourceURLList();
143 			status_t			AddSourceURL(const BString& url);
144 
145 			void				ClearGlobalWritableFileInfos();
146 			status_t			AddGlobalWritableFileInfo(
147 									const BGlobalWritableFileInfo& info);
148 
149 			void				ClearUserSettingsFileInfos();
150 			status_t			AddUserSettingsFileInfo(
151 									const BUserSettingsFileInfo& info);
152 
153 			void				ClearUsers();
154 			status_t			AddUser(const BUser& user);
155 
156 			void				ClearGroups();
157 			status_t			AddGroup(const BString& group);
158 
159 			void				ClearPostInstallScripts();
160 			status_t			AddPostInstallScript(const BString& path);
161 
162 			void				ClearProvidesList();
163 			status_t			AddProvides(const BPackageResolvable& provides);
164 
165 			void				ClearRequiresList();
166 			status_t			AddRequires(
167 									const BPackageResolvableExpression& expr);
168 
169 			void				ClearSupplementsList();
170 			status_t			AddSupplements(
171 									const BPackageResolvableExpression& expr);
172 
173 			void				ClearConflictsList();
174 			status_t			AddConflicts(
175 									const BPackageResolvableExpression& expr);
176 
177 			void				ClearFreshensList();
178 			status_t			AddFreshens(
179 									const BPackageResolvableExpression& expr);
180 
181 			void				ClearReplacesList();
182 			status_t			AddReplaces(const BString& replaces);
183 
184 			void				Clear();
185 
186 	virtual	status_t 			Archive(BMessage* archive,
187 									bool deep = true) const;
188 	static 	BArchivable*		Instantiate(BMessage* archive);
189 
190 			status_t			GetConfigString(BString& _string) const;
191 			BString				ToString() const;
192 
193 public:
194 	static	status_t			GetArchitectureByName(const BString& name,
195 									BPackageArchitecture& _architecture);
196 
197 	static	status_t			ParseVersionString(const BString& string,
198 									bool revisionIsOptional,
199 									BPackageVersion& _version,
200 									ParseErrorListener* listener = NULL);
201 	static	status_t			ParseResolvableExpressionString(
202 									const BString& string,
203 									BPackageResolvableExpression& _expression,
204 									ParseErrorListener* listener = NULL);
205 
206 public:
207 	static	const char*	const	kElementNames[];
208 	static	const char*	const	kArchitectureNames[];
209 	static	const char* const	kWritableFileUpdateTypes[];
210 
211 private:
212 			class Parser;
213 			friend class Parser;
214 			struct StringBuilder;
215 			struct FieldName;
216 			struct PackageFileLocation;
217 
218 			typedef BObjectList<BPackageResolvable> ResolvableList;
219 			typedef BObjectList<BPackageResolvableExpression>
220 				ResolvableExpressionList;
221 
222 			typedef BObjectList<BGlobalWritableFileInfo>
223 				GlobalWritableFileInfoList;
224 			typedef BObjectList<BUserSettingsFileInfo>
225 				UserSettingsFileInfoList;
226 
227 			typedef BObjectList<BUser> UserList;
228 
229 private:
230 			status_t			_ReadFromPackageFile(
231 									const PackageFileLocation& fileLocation);
232 
233 	static	status_t			_AddVersion(BMessage* archive,
234 									const char* field,
235 									const BPackageVersion& version);
236 	static	status_t			_AddResolvables(BMessage* archive,
237 									const char* field,
238 									const ResolvableList& resolvables);
239 	static	status_t			_AddResolvableExpressions(BMessage* archive,
240 									const char* field,
241 									const ResolvableExpressionList&
242 										expressions);
243 	static	status_t			_AddGlobalWritableFileInfos(BMessage* archive,
244 									const char* field,
245 									const GlobalWritableFileInfoList&
246 										infos);
247 	static	status_t			_AddUserSettingsFileInfos(BMessage* archive,
248 									const char* field,
249 									const UserSettingsFileInfoList&
250 										infos);
251 	static	status_t			_AddUsers(BMessage* archive, const char* field,
252 									const UserList& users);
253 
254 	static	status_t			_ExtractVersion(BMessage* archive,
255 									const char* field, int32 index,
256 									BPackageVersion& _version);
257 	static	status_t			_ExtractStringList(BMessage* archive,
258 									const char* field, BStringList& _list);
259 	static	status_t			_ExtractResolvables(BMessage* archive,
260 									const char* field,
261 									ResolvableList& _resolvables);
262 	static	status_t			_ExtractResolvableExpressions(BMessage* archive,
263 									const char* field,
264 									ResolvableExpressionList& _expressions);
265 	static	status_t			_ExtractGlobalWritableFileInfos(
266 									BMessage* archive, const char* field,
267 									GlobalWritableFileInfoList& _infos);
268 	static	status_t			_ExtractUserSettingsFileInfos(
269 									BMessage* archive, const char* field,
270 									UserSettingsFileInfoList& _infos);
271 	static	status_t			_ExtractUsers(BMessage* archive,
272 									const char* field, UserList& _users);
273 
274 private:
275 			BString				fName;
276 			BString				fSummary;
277 			BString				fDescription;
278 			BString				fVendor;
279 			BString				fPackager;
280 			BString				fBasePackage;
281 
282 			uint32				fFlags;
283 
284 			BPackageArchitecture fArchitecture;
285 
286 			BPackageVersion		fVersion;
287 
288 			BStringList			fCopyrightList;
289 			BStringList			fLicenseList;
290 			BStringList			fURLList;
291 			BStringList			fSourceURLList;
292 
293 			BObjectList<BGlobalWritableFileInfo> fGlobalWritableFileInfos;
294 			BObjectList<BUserSettingsFileInfo> fUserSettingsFileInfos;
295 
296 			UserList			fUsers;
297 			BStringList			fGroups;
298 
299 			BStringList			fPostInstallScripts;
300 
301 			ResolvableList		fProvidesList;
302 
303 			ResolvableExpressionList fRequiresList;
304 			ResolvableExpressionList fSupplementsList;
305 			ResolvableExpressionList fConflictsList;
306 			ResolvableExpressionList fFreshensList;
307 
308 			BStringList			fReplacesList;
309 
310 			BString				fChecksum;
311 			BString				fInstallPath;
312 			BString				fFileName;
313 };
314 
315 
316 }	// namespace BPackageKit
317 
318 
319 #endif	// _PACKAGE__PACKAGE_INFO_H_
320