C++ Library to handle BagIt structures. BagIt is a standard format to create transfer packages for digital preservation purposes. See https://en.wikipedia.org/wiki/BagIt for details http://andreas-romeyke.de
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
4.2 KiB

#include "bag.hpp"
#include <iostream>
bool Bag::validate( list<string> & log ) {
bool is_valid = true;
if (this->bagit_version_major != 0) {
log.push_back( "Bagit major version 0 is expected, but got: " + to_string(this->bagit_version_major));
is_valid = false;
}
if (this->bagit_version_minor != 97) {
log.push_back( "Bagit minor version 97 is expected, but got: " + to_string(this->bagit_version_minor));
is_valid = false;
}
if (0 != tag_file_character_encoding.compare( "UTF-8" )) {
log.push_back( "Bagit character encoding UTF-8 is expected, but got: " + this->tag_file_character_encoding);
is_valid = false;
}
if (NULL == this->payload_p) {
log.push_back( "Bagit payload directory 'data/' is expected, but could not found");
is_valid = false;
} else {
bool ret = this->payload_p->validate( log );
if (ret == false) {
is_valid = false;
}
}
if (NULL == this->payloadmanifest_p) {
is_valid = false;
} else {
// checksums check
log.push_back( "Bagit payload manifest");
bool ret = this->payloadmanifest_p->validate( log );
if (ret == false) {
is_valid = false;
}
if (NULL != this->payload_p) {
// check if payload checksums missed for payload files
// HINT: not requested by draft, therefore only a warning
list<string> payload_files = this->payload_p->get_all_relative_paths();
list<string> payload_manifest_files = this->payloadmanifest_p->get_checksummed_files();
list<string> missed_files;
payload_files.sort();
payload_manifest_files.sort();
//cout << "PAYLOAD_FILES:" << endl;
//auto it = payload_files.begin();
//while( it != payload_files.end()) {
// cout << "\t"<<(*it++) << endl;
//}
//cout << "PAYLOADMANIFEST_FILES:" << endl;
//it = payload_manifest_files.begin();
//while( it != payload_manifest_files.end()) {
// cout << "\t"<<(*it++) << endl;
//}
auto it1 = payload_files.begin();
auto it2 = payload_manifest_files.begin();
while( it1 != payload_files.end() && it2 != payload_manifest_files.end() ) {
int cmp_res = (*it1).compare( *it2);
//cout << "COMP: "<<cmp_res<<" file='"<< (*it1) << "' checksummed file='" << (*it2) << "'" << endl;
if ( cmp_res < 0) {
log.push_back("Bagit warning, file '" + (*it1) + "' in payload has no checksum entry in payload manifest");
it1++;
} else if ( cmp_res > 0) {
it2++;
} else {
it1++;
it2++;
}
}
}
}
// next elements are optional
if (NULL == this->tagmanifest_p) {
} else {
log.push_back( "Bagit tag manifest");
bool ret = this->tagmanifest_p->validate( log );
if (ret == false) {
is_valid = false;
}
}
if (NULL == this->bagmetadata_p) {
} else {
bool ret = this->bagmetadata_p->validate( log );
if (ret == false) {
is_valid = false;
}
if (this->bagmetadata_p->has_oxum()) {
// check oxum of payload
Checksum c;
list<string> files = this->payload_p->get_all_relative_paths();
oxum_t expected_oxum = this->bagmetadata_p->get_oxum();
oxum_t calculated_oxum = c.oxum_of_filelist( files );
if (expected_oxum.octetcount != calculated_oxum.octetcount) {
log.push_back("Bagit payload oxum octectcount=" + to_string(expected_oxum.octetcount) + " expected, but " + to_string(calculated_oxum.octetcount) + " found");
is_valid = false;
}
if (expected_oxum.streamcount != calculated_oxum.streamcount) {
log.push_back("Bagit payload oxum streamcount=" + to_string(expected_oxum.streamcount) + " expected, but " + to_string(calculated_oxum.streamcount) + " found");
is_valid = false;
}
}
}
/*
if (NULL == this->fetchfile_p) {
} else {
bool ret = this->fetchfile_p->validate( log );
if (ret == false) {
is_valid = false;
}
}
if (NULL == this->othertags_p) {
} else {
bool ret = this->othertags_p->validate( log);
if (ret == false) {
is_valid = false;
}
}
*/
return is_valid;
}
// vim: set tabstop=4 softtabstop=0 expandtab shiftwidth=4 smarttab