Browse Source

- rewritten to use Manifest-constructor

- fixed order for tagmanifest call in store()
master
Andreas Romeyke 3 years ago
parent
commit
9edbd288db
6 changed files with 116 additions and 87 deletions
  1. +3
    -0
      src/include/manifest.hpp
  2. +28
    -25
      src/lib/bag.cpp
  3. +56
    -15
      src/lib/manifest.cpp
  4. +2
    -17
      src/lib/payloadmanifest.cpp
  5. +2
    -30
      src/lib/tagmanifest.cpp
  6. +25
    -0
      test/testbag.cpp

+ 3
- 0
src/include/manifest.hpp View File

@ -12,8 +12,11 @@ class Manifest{
protected:
string basedir;
stringstream log;
string base_manifest_file_prefix;
map<checksum_algorithms,filename_t> manifest_algorithm_files;
bool exist_manifest_files;
public:
Manifest( string basedir, string file_prefix );
virtual multimap<checksum_string_t,filename_t> get_checksum_file_pairs(checksum_algorithms alg);
list<filename_t> get_checksummed_files();
virtual bool validate();


+ 28
- 25
src/lib/bag.cpp View File

@ -93,55 +93,45 @@ 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;
cout << "directory '" << basedir << "' already exists" << endl;
return false;
}
fs::create_directory(p);
fs::create_directory(p);
// store payload
if (nullptr == Bag::payload_p) {
Bag::log << "Payload object needed" << endl;
cout << "Payload object needed" << endl;
return false;
}
Bag::payload_p->store( basedir );
// store payload manifest
if (nullptr == Bag::payloadmanifest_p) {
Bag::log << "Payloadmanifest object needed" << endl;
cout << "Payloadmanifest object needed" << endl;
return false;
}
list<string> payload_files =Bag::payload_p->get_all_relative_paths();
cout << "PAYLOAD" <<endl;
Bag::payloadmanifest_p->store( basedir, payload_files);
// store tagmanifest
if (nullptr == Bag::tagmanifest_p) {
Bag::log << "Tagmanifest object needed" << endl;
return false;
}
list<string> bagfiles = Bag::get_all_bag_files();
cout << "TAGMANIFEST" << endl;
list<string>::iterator it;
for (it=bagfiles.begin(); it!= bagfiles.end(); ++it) {
cout << "TAGMANIFEST-file '" << *it << "'" << endl;
}
Bag::tagmanifest_p->store( basedir, bagfiles );
// store baginfo
if (nullptr == Bag::bagmetadata_p) {
Bag::log << "Bagmetadata object needed" << endl;
cout << "Bagmetadata object needed" << endl;
return false;
}
Bag::bagmetadata_p->store( basedir );
// store fetchfile (if needed)
if (nullptr == Bag::fetchfile_p) {
Bag::log << "Fetchfile object needed" << endl;
return false;
}
cout << "Fetchfile object needed" << endl;
} else {
Bag::fetchfile_p->store( basedir );
}
// store other
if (nullptr == Bag::othertags_p) {
Bag::log << "Othertags object needed" << endl;
return false;
}
Bag::othertags_p->store( basedir );
cout << "Othertags object needed" << endl;
} else {
Bag::othertags_p->store( basedir );
}
// store bag itself
string bagit_txt_path = basedir + "bagit.txt";
ofstream bagit_txt_file;
@ -151,9 +141,22 @@ bool Bag::store( string basedir ) {
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;
cout << "file " << bagit_txt_path << "could not be open for writing" << endl;
return false;
}
// at least (!), store tagmanifest
if (nullptr == Bag::tagmanifest_p) {
cout << "Tagmanifest object needed" << endl;
return false;
}
list<string> bagfiles = Bag::get_all_bag_files();
cout << "TAGMANIFEST" << endl;
list<string>::iterator it;
for (it=bagfiles.begin(); it!= bagfiles.end(); ++it) {
cout << "TAGMANIFEST-file '" << *it << "'" << endl;
}
Bag::tagmanifest_p->store( basedir, bagfiles );
return true;
}


+ 56
- 15
src/lib/manifest.cpp View File

@ -11,6 +11,38 @@ namespace fs = boost::filesystem;
using namespace std;
Manifest::Manifest( string basedir, string file_prefix ) {
Manifest::basedir = basedir;
Manifest::base_manifest_file_prefix = file_prefix;
map<checksum_algorithms, string> possible_manifest_files;
for (checksum_algorithms alg : checksum_algorithmsList) {
possible_manifest_files[alg] = Manifest::base_manifest_file_prefix + string_of_algorithm( alg ) + ".txt";
}
for (auto & possible_manifest_file : possible_manifest_files) {
// debug
// cout << it->first << " => " << it->second << endl;
// test if file exists
//string filename = basedir + it->second;
string filename = possible_manifest_file.second;
checksum_algorithms alg = possible_manifest_file.first;
fs::path p{ filename };
fs::file_status s = fs::status( p );
cout << "path "<< p.string() << endl;
cout << "is file: "<< fs::is_regular_file( s) << endl;
if (fs::is_regular_file( s)) {
Manifest::manifest_algorithm_files[alg] = filename;
// debug
//cout << "TAGMANIFEST: alg="<< it->first << " => filename=" << it->second << endl;
//cout << "TAGMANIFEST2: alg="<< it->first << " => filename=" << filename << endl;
}
}
Manifest::exist_manifest_files = !possible_manifest_files.empty();
//cout << "TAGMANIFEST constructor, calling debug()" << endl;
//this->debug();
//cout << "TAGMANIFEST constructor, finished" << endl;
}
multimap<checksum_string_t,filename_t> Manifest::get_checksum_file_pairs(checksum_algorithms alg) {
string filename = this->manifest_algorithm_files[ alg ];
multimap<checksum_string_t,filename_t> checksum_file_pairs;
@ -89,23 +121,33 @@ list<string> Manifest::get_checksummed_files() {
}
bool Manifest::store( const string& basedir, list<string>& files ) {
map<checksum_algorithms, string> possible_manifest_files;
for (checksum_algorithms alg : checksum_algorithmsList) {
possible_manifest_files[alg] = Manifest::base_manifest_file_prefix + string_of_algorithm( alg ) + ".txt";
}
for (auto file : files) {
cout << "DEBUG manifest-files file='" << file << "'" << endl;
}
Checksum checksum;
for (auto & manifest_algorithm_file : this->manifest_algorithm_files) {
checksum_algorithms alg = manifest_algorithm_file.first;
for (auto & possible_manifest_file : possible_manifest_files) {
checksum_algorithms alg = possible_manifest_file.first;
cout << "DEBUG manifest::store alg=" << string_of_algorithm(alg) << endl;
if (!possible_manifest_file.second.empty()) {
// test if file exists
string algfilename = basedir + manifest_algorithm_file.second;
cout << "DEBUG manifest::store, file='"<<algfilename << "' (newbase='" << basedir << "', filename='" << manifest_algorithm_file.second << "'" <<endl;
ofstream alg_txt_file;
alg_txt_file.open( algfilename );
if (alg_txt_file.is_open()) {
list<string>::iterator ch;
for (ch=files.begin(); ch!=files.end(); ++ch) {
string filename = basedir + *ch;
cout << "\tfilename='"<<filename<<"'"<<endl;
string checksum_string = checksum.checksum_of_file( filename, alg);
alg_txt_file << checksum_string << " " << *ch << endl;
string algfilename = basedir + possible_manifest_file.second;
cout << "DEBUG manifest::store, alg=" << string_of_algorithm(alg) << " file='"<<algfilename << "' (newbase='" << basedir << "', filename='" << possible_manifest_file.second << "'" <<endl;
ofstream alg_txt_file;
alg_txt_file.open( algfilename );
if (alg_txt_file.is_open()) {
list<string>::iterator ch;
for (ch=files.begin(); ch!=files.end(); ++ch) {
string filename = basedir + *ch;
cout << "DEBUG manifest::store\tfilename='"<<filename<<"'"<<endl;
string checksum_string = checksum.checksum_of_file( filename, alg);
alg_txt_file << checksum_string << " " << *ch << endl;
}
alg_txt_file.close();
}
alg_txt_file.close();
}
}
return true;
@ -120,5 +162,4 @@ void Manifest::reset_logstream() {
this->log.str(std::string());
}
// vim: set tabstop=4 softtabstop=0 expandtab shiftwidth=4 smarttab

+ 2
- 17
src/lib/payloadmanifest.cpp View File

@ -12,23 +12,8 @@ namespace fs = boost::filesystem;
using namespace std;
Payloadmanifest::Payloadmanifest( string basedir ) {
Payloadmanifest::basedir = basedir;
map<checksum_algorithms, string> possible_manifest_files;
for (checksum_algorithms alg : checksum_algorithmsList) {
possible_manifest_files[alg] = "manifest-" + string_of_algorithm( alg ) + ".txt";
}
for (auto & possible_manifest_file : possible_manifest_files) {
checksum_algorithms alg = possible_manifest_file.first;
// test if file exists
//string filename = basedir + it->second;
string filename = possible_manifest_file.second;
fs::path p{ basedir + filename };
fs::file_status s = fs::status( p );
if (fs::is_regular_file( s)) {
Payloadmanifest::manifest_algorithm_files[alg] = filename;
}
}
Payloadmanifest::Payloadmanifest( string basedir ) : Manifest::Manifest( basedir, "manifest-") {
}
bool Payloadmanifest::validate() {


+ 2
- 30
src/lib/tagmanifest.cpp View File

@ -12,35 +12,7 @@ namespace fs = boost::filesystem;
using namespace std;
Tagmanifest::Tagmanifest( string basedir ) {
Tagmanifest::basedir = basedir;
map<checksum_algorithms, string> possible_manifest_files;
for (checksum_algorithms alg : checksum_algorithmsList) {
possible_manifest_files[alg] = "tagmanifest-" + string_of_algorithm( alg ) + ".txt";
}
for (auto & possible_manifest_file : possible_manifest_files) {
// debug
// cout << it->first << " => " << it->second << endl;
// test if file exists
//string filename = basedir + it->second;
string filename = possible_manifest_file.second;
checksum_algorithms alg = possible_manifest_file.first;
fs::path p{ filename };
fs::file_status s = fs::status( p );
cout << "path "<< p.string() << endl;
cout << "is file: "<< fs::is_regular_file( s) << endl;
if (fs::is_regular_file( s)) {
Tagmanifest::manifest_algorithm_files[alg] = filename;
// debug
//cout << "TAGMANIFEST: alg="<< it->first << " => filename=" << it->second << endl;
//cout << "TAGMANIFEST2: alg="<< it->first << " => filename=" << filename << endl;
}
}
this->exist_manifest_files = !possible_manifest_files.empty();
//cout << "TAGMANIFEST constructor, calling debug()" << endl;
//this->debug();
//cout << "TAGMANIFEST constructor, finished" << endl;
Tagmanifest::Tagmanifest( string basedir ) : Manifest::Manifest (basedir, "tagmanifest-") {
}
multimap<checksum_string_t,filename_t> Tagmanifest::get_checksum_file_pairs(checksum_algorithms alg) {
@ -61,7 +33,7 @@ multimap<checksum_string_t,filename_t> Tagmanifest::get_checksum_file_pairs(chec
}
bool Tagmanifest::has_tagmanifest() {
return this->exist_manifest_files;
return Tagmanifest::exist_manifest_files;
}
// vim: set tabstop=4 softtabstop=0 expandtab shiftwidth=4 smarttab


+ 25
- 0
test/testbag.cpp View File

@ -1,6 +1,7 @@
#define BOOST_TEST_MODULE Mytest
#include <boost/test/unit_test.hpp>
#include <utility>
#include <iostream>
#include "bag.hpp"
@ -26,10 +27,34 @@ bool check_store( string dir_load_from, string dir_store_to ) {
return result;
}
bool check_get_all_bag_files(string dir) {
Bag p( std::move(dir) );
list<string> results = p.get_all_bag_files();
list<string> expected {
"bagit.txt",
"bag-info.txt"
};
bool is_valid = true;
list<string>::iterator i;
list<string>::iterator j;
for (i=results.begin(), j=expected.begin(); i!=results.end() && j!=expected.end(); i++, j++) {
if ((*i) != (*j)) {
cout << "Expected: "<< (*j) << " Result:"<< (*i) << endl;
is_valid = false;
break;
}
}
return is_valid;
}
/*
BOOST_AUTO_TEST_CASE(constructor1) {BOOST_TEST(check_constructor(""));};
BOOST_AUTO_TEST_CASE(constructor2) {BOOST_TEST(check_constructor("./"));};
BOOST_AUTO_TEST_CASE(constructor3) {BOOST_TEST(check_constructor("../testbags/bag_minimal_ok/"));};
BOOST_AUTO_TEST_CASE(check_validation_ok) {BOOST_TEST(check_validation("../testbags/bag_minimal_ok/"));};
BOOST_AUTO_TEST_CASE(check_validation_buggy) {BOOST_TEST(check_validation("../testbags/1008_buggy/"));};
BOOST_AUTO_TEST_CASE(check_get_all_bag_files_ok) {BOOST_TEST(check_get_all_bag_files("../testbags/bag_minimal_ok"));};
*/
BOOST_AUTO_TEST_CASE(check_store_ok) {BOOST_TEST(check_store("../testbags/bag_minimal_ok/", "/tmp/testbag/"));};

Loading…
Cancel
Save