xref: /haiku/src/kits/package/ValidateChecksumJob.cpp (revision 02354704729d38c3b078c696adc1bbbd33cbcf72)
1 /*
2  * Copyright 2011, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Oliver Tappe <zooey@hirschkaefer.de>
7  */
8 
9 
10 #include <package/ValidateChecksumJob.h>
11 
12 #include <File.h>
13 
14 #include <package/Context.h>
15 
16 
17 namespace BPackageKit {
18 
19 namespace BPrivate {
20 
21 
22 ValidateChecksumJob::ValidateChecksumJob(const BContext& context,
23 	const BString& title, ChecksumAccessor* expectedChecksumAccessor,
24 	ChecksumAccessor* realChecksumAccessor, bool failIfChecksumsDontMatch)
25 	:
26 	inherited(context, title),
27 	fExpectedChecksumAccessor(expectedChecksumAccessor),
28 	fRealChecksumAccessor(realChecksumAccessor),
29 	fFailIfChecksumsDontMatch(failIfChecksumsDontMatch),
30 	fChecksumsMatch(false)
31 {
32 }
33 
34 
35 ValidateChecksumJob::~ValidateChecksumJob()
36 {
37 	delete fRealChecksumAccessor;
38 	delete fExpectedChecksumAccessor;
39 }
40 
41 
42 status_t
43 ValidateChecksumJob::Execute()
44 {
45 	if (fExpectedChecksumAccessor == NULL || fRealChecksumAccessor == NULL)
46 		return B_BAD_VALUE;
47 
48 	BString expectedChecksum;
49 	BString realChecksum;
50 
51 	status_t result = fExpectedChecksumAccessor->GetChecksum(expectedChecksum);
52 	if (result != B_OK)
53 		return result;
54 
55 	result = fRealChecksumAccessor->GetChecksum(realChecksum);
56 	if (result != B_OK)
57 		return result;
58 
59 	fChecksumsMatch = expectedChecksum.ICompare(realChecksum) == 0;
60 
61 	if (fFailIfChecksumsDontMatch && !fChecksumsMatch) {
62 		BString error = BString("Checksum error:\n")
63 			<< "expected '"	<< expectedChecksum << "'\n"
64 			<< "got      '" << realChecksum << "'";
65 		SetErrorString(error);
66 		return B_BAD_DATA;
67 	}
68 
69 	return B_OK;
70 }
71 
72 
73 bool
74 ValidateChecksumJob::ChecksumsMatch() const
75 {
76 	return fChecksumsMatch;
77 }
78 
79 
80 }	// namespace BPrivate
81 
82 }	// namespace BPackageKit
83