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.

98 lines
2.8 KiB

#include "payload.hpp"
#include <iostream>
#include <boost/filesystem.hpp>
#include <boost/filesystem/operations.hpp>
#include <list>
#include <string>
//#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( const fs::path& directory, list<fs::path> &paths) {
fs::recursive_directory_iterator iter(directory);
fs::recursive_directory_iterator 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);
for (auto & path : paths) {
if (fs::is_regular_file( path )) {
fs::path relpath = fs::relative(path, 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);
for (auto & path : paths) {
if (fs::is_regular_file( path )) {
strpaths.push_back( path.string() );
}
}
return strpaths;
}
bool Payload::validate() {
return true;
}
bool Payload::store( const string& basedir ) {
string newbasedir = basedir + "data/";
fs::path p{ newbasedir };
fs::file_status s = fs::status( p );
if (fs::is_directory( s)) {
// TODO(art1): 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 (const string& relpath : paths) {
string sourcepath = Payload::basedir + relpath;
string targetpath = basedir + relpath;
cout << "map '" << sourcepath << "' to '" << targetpath << "'" <<endl;
fs::path from_fp{ sourcepath };
fs::path to_fp{ targetpath };
fs::path parentdir = to_fp.parent_path();
if (! fs::is_directory (parentdir)) {
fs::create_directory( parentdir );
}
fs::copy_file (from_fp, to_fp);
}
return false;
}
void Payload::get_logstream( stringstream & log ) {
log << this->log.rdbuf();
}
void Payload::reset_logstream() {
this->log.str(std::string());
}
// vim: set tabstop=4 softtabstop=0 expandtab shiftwidth=4 smarttab