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.

83 lines
2.4 KiB

#include "payload.hpp"
#include <iostream>
#include <string>
#include <list>
#include <boost/filesystem.hpp>
//#include <filesystem> // c++17
//namespace fs = std::filesystem;
namespace fs = boost::filesystem;
using namespace std;
Payload::Payload( string basedir ) {
this->basedir = basedir;
fs::path p{ this->basedir };
fs::file_status s = fs::status( p );
// cout << "basedir "<< p.string() << endl;
// cout << "is dir: "<< fs::is_directory( s) << endl;
// cout << "exists: "<< fs::exists(s) << endl;
};
void Payload::scan_dir_recursively( fs::path directory, list<fs::path> &paths) {
fs::recursive_directory_iterator iter(directory), end;
for (; iter != end; ++iter) {
fs::path subdir = iter->path();
paths.push_back( subdir );
}
}
// get all relative paths relative to the bag base directory
list<string> Payload::get_all_relative_paths() {
list<fs::path> paths;
list<string> strpaths;
fs::path directory(this->basedir + "/data");
Payload::scan_dir_recursively( directory, paths);
list<fs::path>::iterator i;
for (i=paths.begin(); i!= paths.end(); i++) {
if (fs::is_regular_file( *i )) {
fs::path relpath = fs::relative((*i), this->basedir);
strpaths.push_back( relpath.string() );
}
}
return strpaths;
}
list<string> Payload::get_all_absolute_paths() {
list<fs::path> paths;
list<string> strpaths;
fs::path directory(this->basedir + "/data");
Payload::scan_dir_recursively( directory, paths);
list<fs::path>::iterator i;
for (i=paths.begin(); i!= paths.end(); i++) {
if (fs::is_regular_file( *i )) {
strpaths.push_back( (*i).string() );
}
}
return strpaths;
}
bool Payload::validate( list<string> & log ) {
return true;
}
bool Payload::store( string basedir ) {
string newbasedir = basedir + "data/";
fs::path p{ newbasedir };
fs::file_status s = fs::status( p );
if (fs::is_directory( s)) {
// TODO: Payload::log << "directory '" << newbasedir << "'already exists" << endl;
cout << "directory '" << newbasedir << "'already exists" << endl;
return false;
}
fs::create_directory(p);
list<string> paths = Payload::get_all_relative_paths();
for (string relpath : paths) {
string sourcepath = Payload::basedir + relpath;
string targetpath = basedir + relpath;
cout << "map '" << sourcepath << "' to '" << targetpath << "'" <<endl;
}
return false;
}
// vim: set tabstop=4