135edda8fSOliver Tappe /*
235edda8fSOliver Tappe * Copyright 2011, Haiku, Inc. All Rights Reserved.
335edda8fSOliver Tappe * Distributed under the terms of the MIT License.
435edda8fSOliver Tappe *
535edda8fSOliver Tappe * Authors:
635edda8fSOliver Tappe * Oliver Tappe <zooey@hirschkaefer.de>
735edda8fSOliver Tappe */
835edda8fSOliver Tappe
935edda8fSOliver Tappe
1035edda8fSOliver Tappe #include <package/ValidateChecksumJob.h>
1135edda8fSOliver Tappe
1235edda8fSOliver Tappe #include <File.h>
1335edda8fSOliver Tappe
1435edda8fSOliver Tappe #include <package/Context.h>
1535edda8fSOliver Tappe
1635edda8fSOliver Tappe
17*7d7ed9bfSOliver Tappe namespace BPackageKit {
1835edda8fSOliver Tappe
19*7d7ed9bfSOliver Tappe namespace BPrivate {
2035edda8fSOliver Tappe
2135edda8fSOliver Tappe
ValidateChecksumJob(const BContext & context,const BString & title,ChecksumAccessor * expectedChecksumAccessor,ChecksumAccessor * realChecksumAccessor,bool failIfChecksumsDontMatch)22*7d7ed9bfSOliver Tappe ValidateChecksumJob::ValidateChecksumJob(const BContext& context,
2335edda8fSOliver Tappe const BString& title, ChecksumAccessor* expectedChecksumAccessor,
2435edda8fSOliver Tappe ChecksumAccessor* realChecksumAccessor, bool failIfChecksumsDontMatch)
2535edda8fSOliver Tappe :
2635edda8fSOliver Tappe inherited(context, title),
2735edda8fSOliver Tappe fExpectedChecksumAccessor(expectedChecksumAccessor),
2835edda8fSOliver Tappe fRealChecksumAccessor(realChecksumAccessor),
2935edda8fSOliver Tappe fFailIfChecksumsDontMatch(failIfChecksumsDontMatch),
3035edda8fSOliver Tappe fChecksumsMatch(false)
3135edda8fSOliver Tappe {
3235edda8fSOliver Tappe }
3335edda8fSOliver Tappe
3435edda8fSOliver Tappe
~ValidateChecksumJob()3535edda8fSOliver Tappe ValidateChecksumJob::~ValidateChecksumJob()
3635edda8fSOliver Tappe {
3735edda8fSOliver Tappe delete fRealChecksumAccessor;
3835edda8fSOliver Tappe delete fExpectedChecksumAccessor;
3935edda8fSOliver Tappe }
4035edda8fSOliver Tappe
4135edda8fSOliver Tappe
4235edda8fSOliver Tappe status_t
Execute()4335edda8fSOliver Tappe ValidateChecksumJob::Execute()
4435edda8fSOliver Tappe {
4535edda8fSOliver Tappe if (fExpectedChecksumAccessor == NULL || fRealChecksumAccessor == NULL)
4635edda8fSOliver Tappe return B_BAD_VALUE;
4735edda8fSOliver Tappe
4835edda8fSOliver Tappe BString expectedChecksum;
4935edda8fSOliver Tappe BString realChecksum;
5035edda8fSOliver Tappe
5135edda8fSOliver Tappe status_t result = fExpectedChecksumAccessor->GetChecksum(expectedChecksum);
5235edda8fSOliver Tappe if (result != B_OK)
5335edda8fSOliver Tappe return result;
5435edda8fSOliver Tappe
5535edda8fSOliver Tappe result = fRealChecksumAccessor->GetChecksum(realChecksum);
5635edda8fSOliver Tappe if (result != B_OK)
5735edda8fSOliver Tappe return result;
5835edda8fSOliver Tappe
5935edda8fSOliver Tappe fChecksumsMatch = expectedChecksum.ICompare(realChecksum) == 0;
6035edda8fSOliver Tappe
6135edda8fSOliver Tappe if (fFailIfChecksumsDontMatch && !fChecksumsMatch) {
6235edda8fSOliver Tappe BString error = BString("Checksum error:\n")
6335edda8fSOliver Tappe << "expected '" << expectedChecksum << "'\n"
6435edda8fSOliver Tappe << "got '" << realChecksum << "'";
6535edda8fSOliver Tappe SetErrorString(error);
6635edda8fSOliver Tappe return B_BAD_DATA;
6735edda8fSOliver Tappe }
6835edda8fSOliver Tappe
6935edda8fSOliver Tappe return B_OK;
7035edda8fSOliver Tappe }
7135edda8fSOliver Tappe
7235edda8fSOliver Tappe
7335edda8fSOliver Tappe bool
ChecksumsMatch() const7435edda8fSOliver Tappe ValidateChecksumJob::ChecksumsMatch() const
7535edda8fSOliver Tappe {
7635edda8fSOliver Tappe return fChecksumsMatch;
7735edda8fSOliver Tappe }
7835edda8fSOliver Tappe
7935edda8fSOliver Tappe
80*7d7ed9bfSOliver Tappe } // namespace BPrivate
8135edda8fSOliver Tappe
82*7d7ed9bfSOliver Tappe } // namespace BPackageKit
83