xref: /haiku/src/servers/package/CommitTransactionHandler.h (revision 71452e98334eaac603bf542d159e24788a46bebb)
1 /*
2  * Copyright 2013-2014, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Ingo Weinhold <ingo_weinhold@gmx.de>
7  */
8 #ifndef COMMIT_TRANSACTION_HANDLER_H
9 #define COMMIT_TRANSACTION_HANDLER_H
10 
11 
12 #include <set>
13 #include <string>
14 
15 #include <Directory.h>
16 
17 #include "FSTransaction.h"
18 #include "FSUtils.h"
19 #include "Volume.h"
20 
21 
22 typedef std::set<std::string> StringSet;
23 
24 
25 namespace BPackageKit {
26 	class BCommitTransactionResult;
27 }
28 
29 using BPackageKit::BCommitTransactionResult;
30 
31 
32 class CommitTransactionHandler {
33 public:
34 								CommitTransactionHandler(Volume* volume,
35 									PackageFileManager* packageFileManager,
36 									BCommitTransactionResult& result);
37 								~CommitTransactionHandler();
38 
39 			void				Init(VolumeState* volumeState,
40 									bool isActiveVolumeState,
41 									const PackageSet& packagesAlreadyAdded,
42 									const PackageSet& packagesAlreadyRemoved);
43 
44 			void				HandleRequest(BMessage* request);
45 			void				HandleRequest(
46 									const BActivationTransaction& transaction);
47 			void				HandleRequest();
48 									// uses packagesAlreadyAdded and
49 									// packagesAlreadyRemoved from Init()
50 
51 			void				Revert();
52 
53 			const BString&		OldStateDirectoryName() const
54 									{ return fOldStateDirectoryName; }
55 
56 			Package*			CurrentPackage() const
57 									{ return fCurrentPackage; }
58 
59 			VolumeState*		DetachVolumeState();
60 			bool				IsActiveVolumeState() const
61 									{ return fVolumeStateIsActive; }
62 
63 private:
64 			typedef BObjectList<Package> PackageList;
65 			typedef FSUtils::RelativePath RelativePath;
66 
67 			struct TransactionIssueBuilder;
68 
69 private:
70 			void				_GetPackagesToDeactivate(
71 									const BActivationTransaction& transaction);
72 			void				_ReadPackagesToActivate(
73 									const BActivationTransaction& transaction);
74 			void				_ApplyChanges();
75 			void				_CreateOldStateDirectory();
76 			void				_RemovePackagesToDeactivate();
77 			void				_AddPackagesToActivate();
78 
79 			void				_PreparePackageToActivate(Package* package);
80 			void				_AddGroup(Package* package,
81 									const BString& groupName);
82 			void				_AddUser(Package* package, const BUser& user);
83 			void				_AddGlobalWritableFiles(Package* package);
84 			void				_AddGlobalWritableFile(Package* package,
85 									const BGlobalWritableFileInfo& file,
86 									const BDirectory& rootDirectory,
87 									const BDirectory& extractedFilesDirectory);
88 			void				_AddGlobalWritableFileRecurse(Package* package,
89 									const BDirectory& sourceDirectory,
90 									FSUtils::Path& relativeSourcePath,
91 									const BDirectory& targetDirectory,
92 									const char* targetName,
93 									BWritableFileUpdateType updateType);
94 
95 			void				_RevertAddPackagesToActivate();
96 			void				_RevertRemovePackagesToDeactivate();
97 			void				_RevertUserGroupChanges();
98 
99 			void				_RunPostInstallScripts();
100 			void				_RunPostInstallScript(Package* package,
101 									const BString& script);
102 
103 			void				_QueuePostInstallScripts();
104 
105 			void				_ExtractPackageContent(Package* package,
106 									const BStringList& contentPaths,
107 									BDirectory& targetDirectory,
108 									BDirectory& _extractedFilesDirectory);
109 
110 			status_t			_OpenPackagesSubDirectory(
111 									const RelativePath& path, bool create,
112 									BDirectory& _directory);
113 			status_t			_OpenPackagesFile(
114 									const RelativePath& subDirectoryPath,
115 									const char* fileName, uint32 openMode,
116 									BFile& _file, BEntry* _entry = NULL);
117 
118 			void				_WriteActivationFile(
119 									const RelativePath& directoryPath,
120 									const char* fileName,
121 									const PackageSet& toActivate,
122 									const PackageSet& toDeactivate,
123 									BEntry& _entry);
124 			void				_CreateActivationFileContent(
125 									const PackageSet& toActivate,
126 									const PackageSet& toDeactivate,
127 									BString& _content);
128 			status_t			_WriteTextFile(
129 									const RelativePath& directoryPath,
130 									const char* fileName,
131 									const BString& content, BEntry& _entry);
132 			void				_ChangePackageActivation(
133 									const PackageSet& packagesToActivate,
134 									const PackageSet& packagesToDeactivate);
135 									// throws Exception
136 			void				_ChangePackageActivationIOCtl(
137 									const PackageSet& packagesToActivate,
138 									const PackageSet& packagesToDeactivate);
139 									// throws Exception
140 			void				_FillInActivationChangeItem(
141 									PackageFSActivationChangeItem* item,
142 									PackageFSActivationChangeType type,
143 									Package* package, char*& nameBuffer);
144 
145 			bool				_IsSystemPackage(Package* package);
146 
147 			void				_AddIssue(
148 									const TransactionIssueBuilder& builder);
149 
150 	static	BString				_GetPath(const FSUtils::Entry& entry,
151 									const BString& fallback);
152 
153 	static	void				_TagPackageEntriesRecursively(
154 									BDirectory& directory, const BString& value,
155 									bool nonDirectoriesOnly);
156 
157 private:
158 			Volume*				fVolume;
159 			PackageFileManager*	fPackageFileManager;
160 			VolumeState*		fVolumeState;
161 			bool				fVolumeStateIsActive;
162 			PackageList			fPackagesToActivate;
163 			PackageSet			fPackagesToDeactivate;
164 			PackageSet			fAddedPackages;
165 			PackageSet			fRemovedPackages;
166 			PackageSet			fPackagesAlreadyAdded;
167 			PackageSet			fPackagesAlreadyRemoved;
168 			BDirectory			fOldStateDirectory;
169 			node_ref			fOldStateDirectoryRef;
170 			BString				fOldStateDirectoryName;
171 			node_ref			fTransactionDirectoryRef;
172 			BDirectory			fWritableFilesDirectory;
173 			StringSet			fAddedGroups;
174 			StringSet			fAddedUsers;
175 			FSTransaction		fFSTransaction;
176 			BCommitTransactionResult& fResult;
177 			Package*			fCurrentPackage;
178 };
179 
180 
181 #endif	// COMMIT_TRANSACTION_HANDLER_H
182