Browse Source

- added import_data_dir()

master
Andreas Romeyke 2 years ago
parent
commit
a32729223f
2 changed files with 46 additions and 8 deletions
  1. +4
    -3
      src/include/payload.hpp
  2. +42
    -5
      src/lib/payload.cpp

+ 4
- 3
src/include/payload.hpp View File

@@ -1,15 +1,15 @@
// Copyright (C) 2018 Andreas Romeyke (art1@andreas-romeyke.de), 2018.
//
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#ifndef LIBCBAG_PAYLOAD
@@ -34,6 +34,7 @@ class Payload{
bool has_file( string filename );
bool add_file( string sourcefile, string target_file );
bool remove_file( string file );
bool import_data_dir( const string& dirname ); /** imports a given dir as data/-dir */
bool validate();
bool store( const string& basedir);
void get_logstream( stringstream & log);


+ 42
- 5
src/lib/payload.cpp View File

@@ -1,15 +1,15 @@
// Copyright (C) 2018 Andreas Romeyke (art1@andreas-romeyke.de), 2018.
//
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "payload.hpp"
@@ -73,6 +73,43 @@ list<string> Payload::get_all_absolute_paths() {
return strpaths;
}

bool Payload::import_data_dir( const string& importdir ) {
fs::path b{ basedir };
fs::file_status bs = fs::status( b );
if (! fs::is_directory( bs ) ) {
fs::create_directory(b);
}
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<fs::path> paths;
Payload::scan_dir_recursively( importdir, paths);
for (auto & path : paths) {
fs::path relpath = fs::relative(path, importdir);
string sourcepath = path.string();
string targetpath = newbasedir + relpath.string();
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 );
}
if (fs::is_directory( from_fp ) ) {
fs::create_directory( to_fp );
} else {
fs::copy_file (from_fp, to_fp);
}
}
return true;
}

bool Payload::validate() {
fs::path directory(this->basedir + "/data");
return fs::is_directory( directory );
@@ -80,7 +117,7 @@ bool Payload::validate() {


bool Payload::store( const string& basedir ) {
string newbasedir = basedir + "data/";
string newbasedir = basedir + "data/";

fs::path p{ newbasedir };
fs::file_status s = fs::status( p );
@@ -89,7 +126,7 @@ bool Payload::store( const string& basedir ) {
log << "directory '" << newbasedir << "'already exists" << endl;
return false;
}
fs::create_directory(p);
fs::create_directory(p);
list<string> paths = Payload::get_all_relative_paths();
for (const string& relpath : paths) {
string sourcepath = Payload::basedir + relpath;


Loading…
Cancel
Save