Browse Source

- added new checksum.hpp

- added new tagmanifest.cpp
- test_bag expects now a bagit-dir
master
parent
commit
240adcfeac
8 changed files with 128 additions and 14 deletions
  1. +1
    -1
      src/include/bag.hpp
  2. +5
    -0
      src/include/checksum.hpp
  3. +2
    -3
      src/include/manifest.hpp
  4. +12
    -3
      src/include/tagmanifest.hpp
  5. +34
    -3
      src/lib/load_bag.cpp
  6. +1
    -1
      src/lib/manifest.cpp
  7. +64
    -0
      src/lib/tagmanifest.cpp
  8. +9
    -3
      src/test/test_bag.cpp

+ 1
- 1
src/include/bag.hpp View File

@ -32,4 +32,4 @@ class Bag {
bool validate();
};
#endif
// vim: set tabstop=4
// vim: set tabstop=4 softtabstop=0 expandtab shiftwidth=4 smarttab

+ 5
- 0
src/include/checksum.hpp View File

@ -0,0 +1,5 @@
#ifndef LIBCBAG_CHECKSUM
#define LIBCBAG_CHECKSUM
enum checksum_algorithms {md5=1, sha1};
#endif
// vim: set tabstop=4 softtabstop=0 expandtab shiftwidth=4 smarttab

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

@ -2,10 +2,9 @@
#define LIBCBAG_MANIFEST
#include <string>
#include <map>
#include "checksum.hpp"
using namespace std;
enum checksum_algorithms {md5=1, sha1};
class Manifest{
private:
@ -16,4 +15,4 @@ class Manifest{
};
#endif
// vim: set tabstop=4
// vim: set tabstop=4 softtabstop=0 expandtab shiftwidth=4 smarttab

+ 12
- 3
src/include/tagmanifest.hpp View File

@ -1,11 +1,20 @@
#ifndef LIBCBAG_TAGMANIFEST
#define LIBCBAG_TAGMANIFEST
#include <string>
#include <map>
#include "checksum.hpp"
using namespace std;
class tagmanifest{
class Tagmanifest {
private:
map<checksum_algorithms,string> manifest_algorithm_files;
public:
tagmanifest();
Tagmanifest( string basedir );
bool has_tagmanifest();
map<string,string> get_checksum_file_pairs(checksum_algorithms algorithm);
};
#endif
// vim: set tabstop=4
// vim: set tabstop=4 softtabstop=0 expandtab shiftwidth=4 smarttab

+ 34
- 3
src/lib/load_bag.cpp View File

@ -19,8 +19,35 @@ Bag::Bag( string dfname ) {
getline(bagit_txt_file, utf8_line);
bagit_txt_file.close();
stringstream version_ss ( version_line );
version_ss >> Bag::bagit_version_major;
version_ss >> Bag::bagit_version_minor;
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;
}
@ -30,6 +57,8 @@ Bag::Bag( string dfname ) {
list<string> files = Bag::payload_p->get_all_relative_paths();
// read in payload manifest
Bag::manifest_p = new Manifest(dfname);
// read in tagmanifest
Bag::tagmanifest_p = new Tagmanifest(dfname);
list<string>::iterator i;
for (i=files.begin(); i!=files.end(); i++) {
@ -37,7 +66,9 @@ Bag::Bag( string dfname ) {
//cout << "file/dir (abs):" << (*i) << endl;
}
Bag::manifest_p->get_checksum_file_pairs( md5 );
Bag::tagmanifest_p->get_checksum_file_pairs( md5 );
}
// vim: set tabstop=4
// vim: set tabstop=4 softtabstop=0 expandtab shiftwidth=4 smarttab

+ 1
- 1
src/lib/manifest.cpp View File

@ -52,4 +52,4 @@ map<string,string> Manifest::get_checksum_file_pairs(checksum_algorithms alg) {
}
return checksum_file_pairs;
}
// vim: set tabstop=4
// vim: set tabstop=4 softtabstop=0 expandtab shiftwidth=4 smarttab

+ 64
- 0
src/lib/tagmanifest.cpp View File

@ -0,0 +1,64 @@
#include "tagmanifest.hpp"
#include "manifest.hpp"
#include <iostream>
#include <fstream>
#include <sstream>
#include <boost/filesystem.hpp>
//#include <filesystem> // c++17
//namespace fs = std::filesystem;
namespace fs = boost::filesystem;
using namespace std;
Tagmanifest::Tagmanifest( string basedir ) {
map<checksum_algorithms, string> possible_manifest_files;
possible_manifest_files[md5] = "tagmanifest-md5.txt";
possible_manifest_files[sha1] = "tagmanifest-sha1.txt";
for (map<checksum_algorithms, string>::iterator it=possible_manifest_files.begin(); it!=possible_manifest_files.end(); ++it) {
// debug
cout << it->first << " => " << it->second << endl;
// test if file exists
string filename = basedir + it->second;
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[it->first] = filename;
}
}
}
map<string,string> Tagmanifest::get_checksum_file_pairs(checksum_algorithms alg) {
string filename = Tagmanifest::manifest_algorithm_files[ alg ];
map<string,string> checksum_file_pairs;
cout << "using file " << filename << endl;
ifstream file;
file.open( filename );
if (file.is_open()) {
string line;
while (getline(file, line)) {
stringstream line_ss ( line );
string checksum;
string subfile;
line_ss >> checksum;
line_ss >> subfile;
checksum_file_pairs[checksum]=subfile;
cout << "checksum="<<checksum<<" file="<<subfile<<endl;
}
file.close();
}
for (map<string,string>::iterator it=checksum_file_pairs.begin(); it!=checksum_file_pairs.end(); ++it) {
if (it->second.find( "data/" ) == 0) {
cout << "found 'data/' in the beginning of file '"<<it->second<<"' in tagmanifest file '"<< this->manifest_algorithm_files[ alg] << endl;
}
}
}
// vim: set tabstop=4 softtabstop=0 expandtab shiftwidth=4 smarttab

+ 9
- 3
src/test/test_bag.cpp View File

@ -2,10 +2,16 @@
using namespace std;
#include <iostream>
#include <string>
int main() {
Bag bag( "../" );
int main(int argc, const char *argv[]) {
if (argc != 2) {
cout << "you need at least one argument (a bagit dir)" << endl;
exit(-1);
}
const string dir = argv[1];
Bag bag( dir );
return 0;
}
// vim: set tabstop=4
// vim: set tabstop=4 softtabstop=0 expandtab shiftwidth=4 smarttab

Loading…
Cancel
Save