Browse Source

- joined load_bag.cpp make_bag.cpp modify_bag.cpp store_bag.cpp validate_bag.cpp to bag.cpp

master
Andreas Romeyke 3 years ago
parent
commit
727256bf19
6 changed files with 265 additions and 275 deletions
  1. +265
    -0
      src/lib/bag.cpp
  2. +0
    -75
      src/lib/load_bag.cpp
  3. +0
    -0
      src/lib/make_bag.cpp
  4. +0
    -0
      src/lib/modify_bag.cpp
  5. +0
    -70
      src/lib/store_bag.cpp
  6. +0
    -130
      src/lib/validate_bag.cpp

+ 265
- 0
src/lib/bag.cpp View File

@ -0,0 +1,265 @@
#include "bag.hpp"
#include <boost/filesystem.hpp>
#include <fstream>
#include <iostream>
#include "payload.hpp"
#include <sstream>
#include <string>
//#include <filesystem> // c++17
//namespace fs = std::filesystem;
namespace fs = boost::filesystem;
using namespace std;
Bag::Bag( string dfname ) {
// cout << "load constructor (" << dfname << ")" << endl;
// read in file bagit.txt
string bagit_txt_path = dfname + "bagit.txt";
//log << "parse " << bagit_txt_path << endl;
ifstream bagit_txt_file;
bagit_txt_file.open( bagit_txt_path );
if (bagit_txt_file.is_open()) {
string version_line;
string utf8_line;
getline(bagit_txt_file, version_line);
getline(bagit_txt_file, utf8_line);
bagit_txt_file.close();
stringstream version_ss ( version_line );
string major;
string minor;
string vprefix;
getline(version_ss, vprefix, ' ');
getline(version_ss, major ,'.');
getline(version_ss, minor, '.');
if (0 != vprefix.compare("BagIt-Version:")) {
// log << "wrong vprefix='" << vprefix << "', but 'BagIt-Version:' expected" << endl;
}
//cout << "major:'"<<major<<"'"<<endl;
//cout << "minor:'"<<minor<<"'"<<endl;
Bag::bagit_version_major = stoi(major);
Bag::bagit_version_minor = stoi(minor);
stringstream utf8_ss (utf8_line);
string uprefix;
string uvalue;
getline(utf8_ss, uprefix, ' ');
getline(utf8_ss, uvalue, ' ');
if (0 != uprefix.compare("Tag-File-Character-Encoding:")) {
//log << "wrong uprefix='" << uprefix << "', but 'Tag-File-Character-Encoding:' expected" << endl;
}
Bag::tag_file_character_encoding = uvalue;
//cout << "Bagit Version ("<< version_line << ") major=" << Bag::bagit_version_major << " minor=" << Bag::bagit_version_minor << endl;
} else {
Bag::log << "file " << bagit_txt_path << " could not be opened" <<endl;
}
// read in payload
Bag::payload_p = new Payload( dfname ) ;
list<string> files = Bag::payload_p->get_all_relative_paths();
// read in payload manifest
Bag::payloadmanifest_p = new Payloadmanifest(dfname);
// read in tagmanifest
Bag::tagmanifest_p = new Tagmanifest(dfname);
// read in baginfo
Bag::bagmetadata_p = new Bagmetadata(dfname);
map<string,string> md = Bag::bagmetadata_p->get_metadata();
map<string,string>::iterator m;
for (m=md.begin(); m!=md.end(); m++) {
// cout << m->first << " = " << m->second <<endl;
}
list<string>::iterator i;
for (i=files.begin(); i!=files.end(); i++) {
// cout << "file/dir (rel):" << (*i) << endl;
//cout << "file/dir (abs):" << (*i) << endl;
}
Bag::payloadmanifest_p->get_checksum_file_pairs( md5 );
Bag::tagmanifest_p->get_checksum_file_pairs( md5 );
}
bool Bag::store( string basedir ) {
fs::path p{ basedir };
fs::file_status s = fs::status( p );
if (fs::is_directory( s)) {
Bag::log << "directory '" << basedir << "' already exists" << endl;
return false;
}
fs::create_directory(p);
// store payload
if (NULL == Bag::payload_p) {
Bag::log << "Payload object needed" << endl;
return false;
}
Bag::payload_p->store( basedir );
// store payload manifest
if (NULL == Bag::payloadmanifest_p) {
Bag::log << "Payloadmanifest object needed" << endl;
return false;
}
Bag::payloadmanifest_p->store( basedir );
// store tagmanifest
if (NULL == Bag::tagmanifest_p) {
Bag::log << "Tagmanifest object needed" << endl;
return false;
}
Bag::tagmanifest_p->store( basedir );
// store baginfo
if (NULL == Bag::bagmetadata_p) {
Bag::log << "Bagmetadata object needed" << endl;
return false;
}
Bag::bagmetadata_p->store( basedir );
// store fetchfile (if needed)
if (NULL == Bag::fetchfile_p) {
Bag::log << "Fetchfile object needed" << endl;
return false;
}
Bag::fetchfile_p->store( basedir );
// store other
if (NULL == Bag::othertags_p) {
Bag::log << "Othertags object needed" << endl;
return false;
}
Bag::othertags_p->store( basedir );
// store bag itself
string bagit_txt_path = basedir + "bagit.txt";
ofstream bagit_txt_file;
bagit_txt_file.open( bagit_txt_path );
if (bagit_txt_file.is_open()) {
bagit_txt_file << ("BagIt-Version: " + to_string(Bag::bagit_version_major) + "." + to_string(Bag::bagit_version_minor)) << endl;
bagit_txt_file << ("Tag-File-Character-Encoding: " + Bag::tag_file_character_encoding) << endl;
bagit_txt_file.close();
} else {
Bag::log << "file " << bagit_txt_path << "could not be open for writing" << endl;
}
}
bool Bag::validate() {
bool is_valid = true;
if (this->bagit_version_major != 0) {
this->log << "Bagit major version 0 is expected, but got: " << to_string(this->bagit_version_major) << endl;
is_valid = false;
}
if (this->bagit_version_minor != 97) {
this->log << "Bagit minor version 97 is expected, but got: " << to_string(this->bagit_version_minor) << endl;
is_valid = false;
}
if (0 != tag_file_character_encoding.compare( "UTF-8" )) {
this->log << "Bagit character encoding UTF-8 is expected, but got: " << this->tag_file_character_encoding << endl;
is_valid = false;
}
if (NULL == this->payload_p) {
this->log << "Bagit payload directory 'data/' is expected, but could not found" << endl;
is_valid = false;
} else {
bool ret = this->payload_p->validate();
if (ret == false) {
is_valid = false;
}
}
if (NULL == this->payloadmanifest_p) {
is_valid = false;
} else {
// checksums check
this->log << "Bagit payload manifest" << endl;
bool ret = this->payloadmanifest_p->validate();
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) {
this->log << "Bagit warning, file '" << (*it1) << "' in payload has no checksum entry in payload manifest" << endl;
it1++;
} else if ( cmp_res > 0) {
it2++;
} else {
it1++;
it2++;
}
}
}
}
// next elements are optional
if (NULL == this->tagmanifest_p) {
} else {
this->log << "Bagit tag manifest" << endl;
bool ret = this->tagmanifest_p->validate();
if (ret == false) {
is_valid = false;
}
}
if (NULL == this->bagmetadata_p) {
} else {
bool ret = this->bagmetadata_p->validate();
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_absolute_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) {
this->log << "Bagit payload oxum octectcount=" << to_string(expected_oxum.octetcount) << " expected, but " << to_string(calculated_oxum.octetcount) << " found" << endl;
is_valid = false;
}
if (expected_oxum.streamcount != calculated_oxum.streamcount) {
this->log << "Bagit payload oxum streamcount=" << to_string(expected_oxum.streamcount) << " expected, but " << to_string(calculated_oxum.streamcount) << " found" << endl;
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

+ 0
- 75
src/lib/load_bag.cpp View File

@ -1,75 +0,0 @@
#include "bag.hpp"
#include "payload.hpp"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
Bag::Bag( string dfname ) {
// cout << "load constructor (" << dfname << ")" << endl;
// read in file bagit.txt
string bagit_txt_path = dfname + "bagit.txt";
//log << "parse " << bagit_txt_path << endl;
ifstream bagit_txt_file;
bagit_txt_file.open( bagit_txt_path );
if (bagit_txt_file.is_open()) {
string version_line;
string utf8_line;
getline(bagit_txt_file, version_line);
getline(bagit_txt_file, utf8_line);
bagit_txt_file.close();
stringstream version_ss ( version_line );
string major;
string minor;
string vprefix;
getline(version_ss, vprefix, ' ');
getline(version_ss, major ,'.');
getline(version_ss, minor, '.');
if (0 != vprefix.compare("BagIt-Version:")) {
// log << "wrong vprefix='" << vprefix << "', but 'BagIt-Version:' expected" << endl;
}
//cout << "major:'"<<major<<"'"<<endl;
//cout << "minor:'"<<minor<<"'"<<endl;
Bag::bagit_version_major = stoi(major);
Bag::bagit_version_minor = stoi(minor);
stringstream utf8_ss (utf8_line);
string uprefix;
string uvalue;
getline(utf8_ss, uprefix, ' ');
getline(utf8_ss, uvalue, ' ');
if (0 != uprefix.compare("Tag-File-Character-Encoding:")) {
//log << "wrong uprefix='" << uprefix << "', but 'Tag-File-Character-Encoding:' expected" << endl;
}
Bag::tag_file_character_encoding = uvalue;
//cout << "Bagit Version ("<< version_line << ") major=" << Bag::bagit_version_major << " minor=" << Bag::bagit_version_minor << endl;
} else {
Bag::log << "file " << bagit_txt_path << " could not be opened" <<endl;
}
// read in payload
Bag::payload_p = new Payload( dfname ) ;
list<string> files = Bag::payload_p->get_all_relative_paths();
// read in payload manifest
Bag::payloadmanifest_p = new Payloadmanifest(dfname);
// read in tagmanifest
Bag::tagmanifest_p = new Tagmanifest(dfname);
// read in baginfo
Bag::bagmetadata_p = new Bagmetadata(dfname);
map<string,string> md = Bag::bagmetadata_p->get_metadata();
map<string,string>::iterator m;
for (m=md.begin(); m!=md.end(); m++) {
// cout << m->first << " = " << m->second <<endl;
}
list<string>::iterator i;
for (i=files.begin(); i!=files.end(); i++) {
// cout << "file/dir (rel):" << (*i) << endl;
//cout << "file/dir (abs):" << (*i) << endl;
}
Bag::payloadmanifest_p->get_checksum_file_pairs( md5 );
Bag::tagmanifest_p->get_checksum_file_pairs( md5 );
}
// vim: set tabstop=4 softtabstop=0 expandtab shiftwidth=4 smarttab

+ 0
- 0
src/lib/make_bag.cpp View File


+ 0
- 0
src/lib/modify_bag.cpp View File


+ 0
- 70
src/lib/store_bag.cpp View File

@ -1,70 +0,0 @@
#include "bag.hpp"
#include <iostream>
#include <fstream>
#include <string>
#include <boost/filesystem.hpp>
// #include <boost/interprocess/detail/os_file_functions.hpp>
//#include <filesystem> // c++17
//namespace fs = std::filesystem;
namespace fs = boost::filesystem;
using namespace std;
bool Bag::store( string basedir ) {
fs::path p{ basedir };
fs::file_status s = fs::status( p );
if (fs::is_directory( s)) {
Bag::log << "directory '" << basedir << "' already exists" << endl;
return false;
}
fs::create_directory(p);
// store payload
if (NULL == Bag::payload_p) {
Bag::log << "Payload object needed" << endl;
return false;
}
Bag::payload_p->store( basedir );
// store payload manifest
if (NULL == Bag::payloadmanifest_p) {
Bag::log << "Payloadmanifest object needed" << endl;
return false;
}
Bag::payloadmanifest_p->store( basedir );
// store tagmanifest
if (NULL == Bag::tagmanifest_p) {
Bag::log << "Tagmanifest object needed" << endl;
return false;
}
Bag::tagmanifest_p->store( basedir );
// store baginfo
if (NULL == Bag::bagmetadata_p) {
Bag::log << "Bagmetadata object needed" << endl;
return false;
}
Bag::bagmetadata_p->store( basedir );
// store fetchfile (if needed)
if (NULL == Bag::fetchfile_p) {
Bag::log << "Fetchfile object needed" << endl;
return false;
}
Bag::fetchfile_p->store( basedir );
// store other
if (NULL == Bag::othertags_p) {
Bag::log << "Othertags object needed" << endl;
return false;
}
Bag::othertags_p->store( basedir );
// store bag itself
string bagit_txt_path = basedir + "bagit.txt";
ofstream bagit_txt_file;
bagit_txt_file.open( bagit_txt_path );
if (bagit_txt_file.is_open()) {
bagit_txt_file << ("BagIt-Version: " + to_string(Bag::bagit_version_major) + "." + to_string(Bag::bagit_version_minor)) << endl;
bagit_txt_file << ("Tag-File-Character-Encoding: " + Bag::tag_file_character_encoding) << endl;
bagit_txt_file.close();
} else {
Bag::log << "file " << bagit_txt_path << "could not be open for writing" << endl;
}
}

+ 0
- 130
src/lib/validate_bag.cpp View File

@ -1,130 +0,0 @@
#include "bag.hpp"
#include <iostream>
#include <sstream>
bool Bag::validate() {
bool is_valid = true;
if (this->bagit_version_major != 0) {
this->log << "Bagit major version 0 is expected, but got: " << to_string(this->bagit_version_major) << endl;
is_valid = false;
}
if (this->bagit_version_minor != 97) {
this->log << "Bagit minor version 97 is expected, but got: " << to_string(this->bagit_version_minor) << endl;
is_valid = false;
}
if (0 != tag_file_character_encoding.compare( "UTF-8" )) {
this->log << "Bagit character encoding UTF-8 is expected, but got: " << this->tag_file_character_encoding << endl;
is_valid = false;
}
if (NULL == this->payload_p) {
this->log << "Bagit payload directory 'data/' is expected, but could not found" << endl;
is_valid = false;
} else {
bool ret = this->payload_p->validate();
if (ret == false) {
is_valid = false;
}
}
if (NULL == this->payloadmanifest_p) {
is_valid = false;
} else {
// checksums check
this->log << "Bagit payload manifest" << endl;
bool ret = this->payloadmanifest_p->validate();
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) {
this->log << "Bagit warning, file '" << (*it1) << "' in payload has no checksum entry in payload manifest" << endl;
it1++;
} else if ( cmp_res > 0) {
it2++;
} else {
it1++;
it2++;
}
}
}
}
// next elements are optional
if (NULL == this->tagmanifest_p) {
} else {
this->log << "Bagit tag manifest" << endl;
bool ret = this->tagmanifest_p->validate();
if (ret == false) {
is_valid = false;
}
}
if (NULL == this->bagmetadata_p) {
} else {
bool ret = this->bagmetadata_p->validate();
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_absolute_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) {
this->log << "Bagit payload oxum octectcount=" << to_string(expected_oxum.octetcount) << " expected, but " << to_string(calculated_oxum.octetcount) << " found" << endl;
is_valid = false;
}
if (expected_oxum.streamcount != calculated_oxum.streamcount) {
this->log << "Bagit payload oxum streamcount=" << to_string(expected_oxum.streamcount) << " expected, but " << to_string(calculated_oxum.streamcount) << " found" << endl;
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

Loading…
Cancel
Save