2 * Copyright 2013-2014, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
6 * Ingo Weinhold <ingo_weinhold@gmx.de>
10 #include <package/CommitTransactionResult.h>
14 //#include <package/DaemonDefs.h>
17 namespace BPackageKit
{
20 // #pragma mark - BTransactionIssue
23 BTransactionIssue::BTransactionIssue()
25 fType(B_WRITABLE_FILE_TYPE_MISMATCH
),
35 BTransactionIssue::BTransactionIssue(BType type
, const BString
& packageName
,
36 const BString
& path1
, const BString
& path2
, status_t systemError
,
40 fPackageName(packageName
),
43 fSystemError(systemError
),
49 BTransactionIssue::BTransactionIssue(const BTransactionIssue
& other
)
55 BTransactionIssue::~BTransactionIssue()
60 BTransactionIssue::BType
61 BTransactionIssue::Type() const
68 BTransactionIssue::PackageName() const
75 BTransactionIssue::Path1() const
82 BTransactionIssue::Path2() const
89 BTransactionIssue::SystemError() const
96 BTransactionIssue::ExitCode() const
103 BTransactionIssue::ToString() const
105 const char* messageTemplate
= "";
107 case B_WRITABLE_FILE_TYPE_MISMATCH
:
108 messageTemplate
= "\"%path1%\" cannot be updated automatically,"
109 " since its type doesn't match the type of \"%path2%\" which it"
110 " is supposed to be updated with."
111 " Please perform the update manually if needed.";
113 case B_WRITABLE_FILE_NO_PACKAGE_ATTRIBUTE
:
114 messageTemplate
= "\"%path1%\" cannot be updated automatically,"
115 " since it doesn't have a SYS:PACKAGE attribute."
116 " Please perform the update manually if needed.";
118 case B_WRITABLE_FILE_OLD_ORIGINAL_FILE_MISSING
:
119 messageTemplate
= "\"%path1%\" cannot be updated automatically,"
120 " since \"%path2%\" which we need to compare it with is"
122 " Please perform the update manually if needed.";
124 case B_WRITABLE_FILE_OLD_ORIGINAL_FILE_TYPE_MISMATCH
:
125 messageTemplate
= "\"%path1%\" cannot be updated automatically,"
126 " since its type doesn't match the type of \"%path2%\" which we"
127 " need to compare it with."
128 " Please perform the update manually if needed.";
130 case B_WRITABLE_FILE_COMPARISON_FAILED
:
131 messageTemplate
= "\"%path1%\" cannot be updated automatically,"
132 " since the comparison with \"%path2%\" failed: %error%."
133 " Please perform the update manually if needed.";
135 case B_WRITABLE_FILE_NOT_EQUAL
: // !keep old
136 messageTemplate
= "\"%path1%\" cannot be updated automatically,"
137 " since it was changed manually from previous version"
139 " Please perform the update manually if needed.";
141 case B_WRITABLE_SYMLINK_COMPARISON_FAILED
: // !keep old
142 messageTemplate
= "Symbolic link \"%path1%\" cannot be updated"
143 " automatically, since the comparison with \"%path2%\" failed:"
145 " Please perform the update manually if needed.";
147 case B_WRITABLE_SYMLINK_NOT_EQUAL
: // !keep old
148 messageTemplate
= "Symbolic link \"%path1%\" cannot be updated"
149 " automatically, since it was changed manually from previous"
150 " version \"%path2%\"."
151 " Please perform the update manually if needed.";
153 case B_POST_INSTALL_SCRIPT_NOT_FOUND
:
154 messageTemplate
= "Failed to find post-installation script "
155 " \"%path1%\": %error%.";
157 case B_STARTING_POST_INSTALL_SCRIPT_FAILED
:
158 messageTemplate
= "Failed to run post-installation script "
159 " \"%path1%\": %error%.";
161 case B_POST_INSTALL_SCRIPT_FAILED
:
162 messageTemplate
= "The post-installation script "
163 " \"%path1%\" failed with exit code %exitCode%.";
167 BString
message(messageTemplate
);
168 message
.ReplaceAll("%path1%", fPath1
)
169 .ReplaceAll("%path2%", fPath2
)
170 .ReplaceAll("%error%", strerror(fSystemError
))
171 .ReplaceAll("%exitCode%", BString() << fExitCode
);
177 BTransactionIssue::AddToMessage(BMessage
& message
) const
180 if ((error
= message
.AddInt32("type", (int32
)fType
)) != B_OK
181 || (error
= message
.AddString("package", fPackageName
)) != B_OK
182 || (error
= message
.AddString("path1", fPath1
)) != B_OK
183 || (error
= message
.AddString("path2", fPath2
)) != B_OK
184 || (error
= message
.AddInt32("system error", (int32
)fSystemError
))
186 || (error
= message
.AddInt32("exit code", (int32
)fExitCode
)) != B_OK
) {
195 BTransactionIssue::ExtractFromMessage(const BMessage
& message
)
201 if ((error
= message
.FindInt32("type", &type
)) != B_OK
202 || (error
= message
.FindString("package", &fPackageName
)) != B_OK
203 || (error
= message
.FindString("path1", &fPath1
)) != B_OK
204 || (error
= message
.FindString("path2", &fPath2
)) != B_OK
205 || (error
= message
.FindInt32("system error", &systemError
)) != B_OK
206 || (error
= message
.FindInt32("exit code", &exitCode
)) != B_OK
) {
211 fSystemError
= (status_t
)systemError
;
212 fExitCode
= (int)exitCode
;
219 BTransactionIssue::operator=(const BTransactionIssue
& other
)
222 fPackageName
= other
.fPackageName
;
223 fPath1
= other
.fPath1
;
224 fPath2
= other
.fPath2
;
225 fSystemError
= other
.fSystemError
;
226 fExitCode
= other
.fExitCode
;
232 // #pragma mark - BCommitTransactionResult
235 BCommitTransactionResult::BCommitTransactionResult()
237 fError(B_TRANSACTION_INTERNAL_ERROR
),
238 fSystemError(B_ERROR
),
244 fOldStateDirectory(),
250 BCommitTransactionResult::BCommitTransactionResult(BTransactionError error
)
253 fSystemError(B_ERROR
),
259 fOldStateDirectory(),
265 BCommitTransactionResult::BCommitTransactionResult(
266 const BCommitTransactionResult
& other
)
268 fError(B_TRANSACTION_INTERNAL_ERROR
),
269 fSystemError(B_ERROR
),
275 fOldStateDirectory(),
282 BCommitTransactionResult::~BCommitTransactionResult()
288 BCommitTransactionResult::Unset()
290 fError
= B_TRANSACTION_INTERNAL_ERROR
;
291 fSystemError
= B_ERROR
;
292 fErrorPackage
.Truncate(0);
295 fString1
.Truncate(0);
296 fString2
.Truncate(0);
297 fOldStateDirectory
.Truncate(0);
303 BCommitTransactionResult::CountIssues() const
305 return fIssues
.CountItems();
309 const BTransactionIssue
*
310 BCommitTransactionResult::IssueAt(int32 index
) const
312 if (index
< 0 || index
>= CountIssues())
314 return fIssues
.ItemAt(index
);
319 BCommitTransactionResult::AddIssue(const BTransactionIssue
& issue
)
321 BTransactionIssue
* newIssue
= new(std::nothrow
) BTransactionIssue(issue
);
322 if (newIssue
== NULL
|| !fIssues
.AddItem(newIssue
)) {
331 BCommitTransactionResult::Error() const
333 return fError
> 0 ? (BTransactionError
)fError
: B_TRANSACTION_OK
;
338 BCommitTransactionResult::SetError(BTransactionError error
)
345 BCommitTransactionResult::SystemError() const
352 BCommitTransactionResult::SetSystemError(status_t error
)
354 fSystemError
= error
;
359 BCommitTransactionResult::ErrorPackage() const
361 return fErrorPackage
;
366 BCommitTransactionResult::SetErrorPackage(const BString
& packageName
)
368 fErrorPackage
= packageName
;
373 BCommitTransactionResult::FullErrorMessage() const
378 const char* messageTemplate
= "";
379 switch ((BTransactionError
)fError
) {
380 case B_TRANSACTION_OK
:
381 messageTemplate
= "Everything went fine.";
383 case B_TRANSACTION_NO_MEMORY
:
384 messageTemplate
= "Out of memory.";
386 case B_TRANSACTION_INTERNAL_ERROR
:
387 messageTemplate
= "An internal error occurred. Specifics can be"
388 " found in the syslog.";
390 case B_TRANSACTION_INSTALLATION_LOCATION_BUSY
:
391 messageTemplate
= "Another package operation is already in"
394 case B_TRANSACTION_CHANGE_COUNT_MISMATCH
:
395 messageTemplate
= "The transaction is out of date.";
397 case B_TRANSACTION_BAD_REQUEST
:
398 messageTemplate
= "The requested transaction is invalid.";
400 case B_TRANSACTION_NO_SUCH_PACKAGE
:
401 messageTemplate
= "No such package \"%package%\".";
403 case B_TRANSACTION_PACKAGE_ALREADY_EXISTS
:
404 messageTemplate
= "The to be activated package \"%package%\" does"
407 case B_TRANSACTION_FAILED_TO_GET_ENTRY_PATH
:
408 if (fPath1
.IsEmpty()) {
409 if (fErrorPackage
.IsEmpty()) {
410 messageTemplate
= "A file path could not be determined:"
413 messageTemplate
= "While processing package \"%package%\""
414 " a file path could not be determined: %error%";
417 if (fErrorPackage
.IsEmpty()) {
418 messageTemplate
= "The file path for \"%path1%\" could not"
419 " be determined: %error%";
421 messageTemplate
= "While processing package \"%package%\""
422 " the file path for \"%path1%\" could not be"
423 " determined: %error%";
427 case B_TRANSACTION_FAILED_TO_OPEN_DIRECTORY
:
428 messageTemplate
= "Failed to open directory \"%path1%\": %error%";
430 case B_TRANSACTION_FAILED_TO_CREATE_DIRECTORY
:
431 messageTemplate
= "Failed to create directory \"%path1%\": %error%";
433 case B_TRANSACTION_FAILED_TO_REMOVE_DIRECTORY
:
434 messageTemplate
= "Failed to remove directory \"%path1%\": %error%";
436 case B_TRANSACTION_FAILED_TO_MOVE_DIRECTORY
:
437 messageTemplate
= "Failed to move directory \"%path1%\" to"
438 " \"%path2%\": %error%";
440 case B_TRANSACTION_FAILED_TO_WRITE_ACTIVATION_FILE
:
441 messageTemplate
= "Failed to write new package activation file"
442 " \"%path1%\": %error%";
444 case B_TRANSACTION_FAILED_TO_READ_PACKAGE_FILE
:
445 messageTemplate
= "Failed to read package file \"%path1%\":"
448 case B_TRANSACTION_FAILED_TO_EXTRACT_PACKAGE_FILE
:
449 messageTemplate
= "Failed to extract \"%path1%\" from package"
450 " \"%package%\": %error%";
452 case B_TRANSACTION_FAILED_TO_OPEN_FILE
:
453 messageTemplate
= "Failed to open file \"%path1%\": %error%";
455 case B_TRANSACTION_FAILED_TO_MOVE_FILE
:
456 messageTemplate
= "Failed to move file \"%path1%\" to \"%path2%\":"
459 case B_TRANSACTION_FAILED_TO_COPY_FILE
:
460 messageTemplate
= "Failed to copy file \"%path1%\" to \"%path2%\":"
463 case B_TRANSACTION_FAILED_TO_WRITE_FILE_ATTRIBUTE
:
464 messageTemplate
= "Failed to write attribute \"%string1%\" of file"
465 " \"%path1%\": %error%";
467 case B_TRANSACTION_FAILED_TO_ACCESS_ENTRY
:
468 messageTemplate
= "Failed to access entry \"%path1%\": %error%";
470 case B_TRANSACTION_FAILED_TO_ADD_GROUP
:
471 messageTemplate
= "Failed to add user group \"%string1%\" required"
472 " by package \"%package%\".";
474 case B_TRANSACTION_FAILED_TO_ADD_USER
:
475 messageTemplate
= "Failed to add user \"%string1%\" required"
476 " by package \"%package%\".";
478 case B_TRANSACTION_FAILED_TO_ADD_USER_TO_GROUP
:
479 messageTemplate
= "Failed to add user \"%string1%\" to group"
480 " \"%string2%\" as required by package \"%package%\".";
482 case B_TRANSACTION_FAILED_TO_CHANGE_PACKAGE_ACTIVATION
:
483 messageTemplate
= "Failed to change the package activation in"
484 " packagefs: %error%";
488 BString
message(messageTemplate
);
489 message
.ReplaceAll("%package%", fErrorPackage
)
490 .ReplaceAll("%path1%", fPath1
)
491 .ReplaceAll("%path2%", fPath2
)
492 .ReplaceAll("%string1%", fString1
)
493 .ReplaceAll("%string2%", fString2
)
494 .ReplaceAll("%error%", strerror(fSystemError
));
500 BCommitTransactionResult::Path1() const
507 BCommitTransactionResult::SetPath1(const BString
& path
)
514 BCommitTransactionResult::Path2() const
521 BCommitTransactionResult::SetPath2(const BString
& path
)
528 BCommitTransactionResult::String1() const
535 BCommitTransactionResult::SetString1(const BString
& string
)
542 BCommitTransactionResult::String2() const
549 BCommitTransactionResult::SetString2(const BString
& string
)
556 BCommitTransactionResult::OldStateDirectory() const
558 return fOldStateDirectory
;
563 BCommitTransactionResult::SetOldStateDirectory(const BString
& directory
)
565 fOldStateDirectory
= directory
;
570 BCommitTransactionResult::AddToMessage(BMessage
& message
) const
573 if ((error
= message
.AddInt32("error", (int32
)fError
)) != B_OK
574 || (error
= message
.AddInt32("system error", (int32
)fSystemError
))
576 || (error
= message
.AddString("error package", fErrorPackage
)) != B_OK
577 || (error
= message
.AddString("path1", fPath1
)) != B_OK
578 || (error
= message
.AddString("path2", fPath2
)) != B_OK
579 || (error
= message
.AddString("string1", fString1
)) != B_OK
580 || (error
= message
.AddString("string2", fString2
)) != B_OK
581 || (error
= message
.AddString("old state", fOldStateDirectory
))
586 int32 count
= fIssues
.CountItems();
587 for (int32 i
= 0; i
< count
; i
++) {
588 const BTransactionIssue
* issue
= fIssues
.ItemAt(i
);
589 BMessage issueMessage
;
590 if ((error
= issue
->AddToMessage(issueMessage
)) != B_OK
591 || (error
= message
.AddMessage("issues", &issueMessage
)) != B_OK
) {
601 BCommitTransactionResult::ExtractFromMessage(const BMessage
& message
)
608 if ((error
= message
.FindInt32("error", &resultError
)) != B_OK
609 || (error
= message
.FindInt32("system error", &systemError
)) != B_OK
610 || (error
= message
.FindString("error package", &fErrorPackage
)) != B_OK
611 || (error
= message
.FindString("path1", &fPath1
)) != B_OK
612 || (error
= message
.FindString("path2", &fPath2
)) != B_OK
613 || (error
= message
.FindString("string1", &fString1
)) != B_OK
614 || (error
= message
.FindString("string2", &fString2
)) != B_OK
615 || (error
= message
.FindString("old state", &fOldStateDirectory
))
620 fError
= (BTransactionError
)resultError
;
621 fSystemError
= (status_t
)systemError
;
623 BMessage issueMessage
;
624 for (int32 i
= 0; message
.FindMessage("issues", i
, &issueMessage
) == B_OK
;
626 BTransactionIssue issue
;
627 error
= issue
.ExtractFromMessage(issueMessage
);
631 if (!AddIssue(issue
))
639 BCommitTransactionResult
&
640 BCommitTransactionResult::operator=(const BCommitTransactionResult
& other
)
644 fError
= other
.fError
;
645 fSystemError
= other
.fSystemError
;
646 fErrorPackage
= other
.fErrorPackage
;
647 fPath1
= other
.fPath1
;
648 fPath2
= other
.fPath2
;
649 fString1
= other
.fString1
;
650 fString2
= other
.fString2
;
651 fOldStateDirectory
= other
.fOldStateDirectory
;
653 for (int32 i
= 0; const BTransactionIssue
* issue
= other
.fIssues
.ItemAt(i
);
662 } // namespace BPackageKit