xref: /haiku/headers/os/package/PackageInfo.h (revision 9f3bdf3d039430b5172c424def20ce5d9f7367d4)
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 			const BStringList&	PreUninstallScripts() const;
100 
101 			const BObjectList<BPackageResolvable>&	ProvidesList() const;
102 			const BObjectList<BPackageResolvableExpression>&
103 								RequiresList() const;
104 			const BObjectList<BPackageResolvableExpression>&
105 								SupplementsList() const;
106 			const BObjectList<BPackageResolvableExpression>&
107 								ConflictsList() const;
108 			const BObjectList<BPackageResolvableExpression>&
109 								FreshensList() const;
110 			const BStringList&	ReplacesList() const;
111 
112 			BString				CanonicalFileName() const;
113 
114 			bool				Matches(const BPackageResolvableExpression&
115 									expression) const;
116 
117 			void				SetName(const BString& name);
118 			void				SetSummary(const BString& summary);
119 			void				SetDescription(const BString& description);
120 			void				SetVendor(const BString& vendor);
121 			void				SetPackager(const BString& packager);
122 			void				SetBasePackage(const BString& basePackage);
123 			void				SetChecksum(const BString& checksum);
124 			void				SetInstallPath(const BString& installPath);
125 			void				SetFileName(const BString& fileName);
126 
127 			void				SetFlags(uint32 flags);
128 
129 			void				SetArchitecture(
130 									BPackageArchitecture architecture);
131 
132 			void				SetVersion(const BPackageVersion& version);
133 
134 			void				ClearCopyrightList();
135 			status_t			AddCopyright(const BString& copyright);
136 
137 			void				ClearLicenseList();
138 			status_t			AddLicense(const BString& license);
139 
140 			void				ClearURLList();
141 			status_t			AddURL(const BString& url);
142 
143 			void				ClearSourceURLList();
144 			status_t			AddSourceURL(const BString& url);
145 
146 			void				ClearGlobalWritableFileInfos();
147 			status_t			AddGlobalWritableFileInfo(
148 									const BGlobalWritableFileInfo& info);
149 
150 			void				ClearUserSettingsFileInfos();
151 			status_t			AddUserSettingsFileInfo(
152 									const BUserSettingsFileInfo& info);
153 
154 			void				ClearUsers();
155 			status_t			AddUser(const BUser& user);
156 
157 			void				ClearGroups();
158 			status_t			AddGroup(const BString& group);
159 
160 			void				ClearPostInstallScripts();
161 			status_t			AddPostInstallScript(const BString& path);
162 
163 			void				ClearPreUninstallScripts();
164 			status_t			AddPreUninstallScript(const BString& path);
165 
166 			void				ClearProvidesList();
167 			status_t			AddProvides(const BPackageResolvable& provides);
168 
169 			void				ClearRequiresList();
170 			status_t			AddRequires(
171 									const BPackageResolvableExpression& expr);
172 
173 			void				ClearSupplementsList();
174 			status_t			AddSupplements(
175 									const BPackageResolvableExpression& expr);
176 
177 			void				ClearConflictsList();
178 			status_t			AddConflicts(
179 									const BPackageResolvableExpression& expr);
180 
181 			void				ClearFreshensList();
182 			status_t			AddFreshens(
183 									const BPackageResolvableExpression& expr);
184 
185 			void				ClearReplacesList();
186 			status_t			AddReplaces(const BString& replaces);
187 
188 			void				Clear();
189 
190 	virtual	status_t 			Archive(BMessage* archive,
191 									bool deep = true) const;
192 	static 	BArchivable*		Instantiate(BMessage* archive);
193 
194 			status_t			GetConfigString(BString& _string) const;
195 			BString				ToString() const;
196 
197 public:
198 	static	status_t			GetArchitectureByName(const BString& name,
199 									BPackageArchitecture& _architecture);
200 
201 	static	status_t			ParseVersionString(const BString& string,
202 									bool revisionIsOptional,
203 									BPackageVersion& _version,
204 									ParseErrorListener* listener = NULL);
205 	static	status_t			ParseResolvableString(
206 									const BString& string,
207 									BPackageResolvable& _expression,
208 									ParseErrorListener* listener = NULL);
209 	static	status_t			ParseResolvableExpressionString(
210 									const BString& string,
211 									BPackageResolvableExpression& _expression,
212 									ParseErrorListener* listener = NULL);
213 
214 public:
215 	static	const char*	const	kElementNames[];
216 	static	const char*	const	kArchitectureNames[];
217 	static	const char* const	kWritableFileUpdateTypes[];
218 
219 private:
220 			class Parser;
221 			friend class Parser;
222 			struct StringBuilder;
223 			struct FieldName;
224 			struct PackageFileLocation;
225 
226 			typedef BObjectList<BPackageResolvable> ResolvableList;
227 			typedef BObjectList<BPackageResolvableExpression>
228 				ResolvableExpressionList;
229 
230 			typedef BObjectList<BGlobalWritableFileInfo>
231 				GlobalWritableFileInfoList;
232 			typedef BObjectList<BUserSettingsFileInfo>
233 				UserSettingsFileInfoList;
234 
235 			typedef BObjectList<BUser> UserList;
236 
237 private:
238 			status_t			_ReadFromPackageFile(
239 									const PackageFileLocation& fileLocation);
240 
241 	static	status_t			_AddVersion(BMessage* archive,
242 									const char* field,
243 									const BPackageVersion& version);
244 	static	status_t			_AddResolvables(BMessage* archive,
245 									const char* field,
246 									const ResolvableList& resolvables);
247 	static	status_t			_AddResolvableExpressions(BMessage* archive,
248 									const char* field,
249 									const ResolvableExpressionList&
250 										expressions);
251 	static	status_t			_AddGlobalWritableFileInfos(BMessage* archive,
252 									const char* field,
253 									const GlobalWritableFileInfoList&
254 										infos);
255 	static	status_t			_AddUserSettingsFileInfos(BMessage* archive,
256 									const char* field,
257 									const UserSettingsFileInfoList&
258 										infos);
259 	static	status_t			_AddUsers(BMessage* archive, const char* field,
260 									const UserList& users);
261 
262 	static	status_t			_ExtractVersion(BMessage* archive,
263 									const char* field, int32 index,
264 									BPackageVersion& _version);
265 	static	status_t			_ExtractStringList(BMessage* archive,
266 									const char* field, BStringList& _list);
267 	static	status_t			_ExtractResolvables(BMessage* archive,
268 									const char* field,
269 									ResolvableList& _resolvables);
270 	static	status_t			_ExtractResolvableExpressions(BMessage* archive,
271 									const char* field,
272 									ResolvableExpressionList& _expressions);
273 	static	status_t			_ExtractGlobalWritableFileInfos(
274 									BMessage* archive, const char* field,
275 									GlobalWritableFileInfoList& _infos);
276 	static	status_t			_ExtractUserSettingsFileInfos(
277 									BMessage* archive, const char* field,
278 									UserSettingsFileInfoList& _infos);
279 	static	status_t			_ExtractUsers(BMessage* archive,
280 									const char* field, UserList& _users);
281 
282 private:
283 			BString				fName;
284 			BString				fSummary;
285 			BString				fDescription;
286 			BString				fVendor;
287 			BString				fPackager;
288 			BString				fBasePackage;
289 
290 			uint32				fFlags;
291 
292 			BPackageArchitecture fArchitecture;
293 
294 			BPackageVersion		fVersion;
295 
296 			BStringList			fCopyrightList;
297 			BStringList			fLicenseList;
298 			BStringList			fURLList;
299 			BStringList			fSourceURLList;
300 
301 			BObjectList<BGlobalWritableFileInfo> fGlobalWritableFileInfos;
302 			BObjectList<BUserSettingsFileInfo> fUserSettingsFileInfos;
303 
304 			UserList			fUsers;
305 			BStringList			fGroups;
306 
307 			BStringList			fPostInstallScripts;
308 			BStringList			fPreUninstallScripts;
309 
310 			ResolvableList		fProvidesList;
311 
312 			ResolvableExpressionList fRequiresList;
313 			ResolvableExpressionList fSupplementsList;
314 			ResolvableExpressionList fConflictsList;
315 			ResolvableExpressionList fFreshensList;
316 
317 			BStringList			fReplacesList;
318 
319 			BString				fChecksum;
320 			BString				fInstallPath;
321 			BString				fFileName;
322 };
323 
324 
325 }	// namespace BPackageKit
326 
327 
328 #endif	// _PACKAGE__PACKAGE_INFO_H_
329