Browse Source

- added method has_fetchfile()

- implemented constructor
- implemented get_entries()
- implemented fetch_all_entries()
- implemented store()
master
Andreas Romeyke 3 years ago
parent
commit
3c34cd7f7d
3 changed files with 131 additions and 4 deletions
  1. +2
    -0
      src/include/fetchfile.hpp
  2. +90
    -4
      src/lib/fetchfile.cpp
  3. +39
    -0
      test/test_fetchfile.cpp

+ 2
- 0
src/include/fetchfile.hpp View File

@ -16,8 +16,10 @@ class Fetchfile{
string basedir;
list<fetch_t> entries;
stringstream log;
bool exist_fetchfile;
public:
Fetchfile( string basedir );
bool has_fetchfile();
list<fetch_t> get_entries();
bool fetch(fetch_t);
bool fetch_all_entries();


+ 90
- 4
src/lib/fetchfile.cpp View File

@ -3,11 +3,56 @@
#include <memory>
#include <curl/curl.h>
#include "fetchfile.hpp"
#include <sstream>
#include <boost/filesystem.hpp>
//#include <filesystem> // c++17
//namespace fs = std::filesystem;
namespace fs = boost::filesystem;
using namespace std;
Fetchfile::Fetchfile( string basedir ) {
Fetchfile::basedir = basedir;
// test if file exists
string filename = basedir + "fetch.txt";
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)) {
this->exist_fetchfile = true;
// map entries
// from spec: A metadata element MUST consist of a label, a colon, and a value,
// each separated by optional whitespace
// Long values may be continued
// onto the next line by inserting a newline (LF), a carriage return
// (CR), or carriage return plus newline (CRLF) and indenting the next
// line with linear white space (spaces or tabs)
ifstream file;
file.open( filename );
if (file.is_open()) {
string line;
while (getline(file, line)) {
stringstream line_ss ( line );
string url;
string length;
string subfile;
line_ss >> url;
line_ss >> length;
line_ss >> subfile;
fetch_t entry;
entry.url = url;
entry.size = stoi( length );
entry.filename = subfile;
this->entries.push_back( entry );
}
file.close();
}
} else { // no file
this->exist_fetchfile = false;
}
}
bool Fetchfile::fetch( fetch_t fetch ) {
@ -15,7 +60,7 @@ bool Fetchfile::fetch( fetch_t fetch ) {
CURLcode result;
curl = curl_easy_init();
if (curl) {
FILE *fp;
FILE *fp;
fp = fopen( fetch.filename.c_str(), "wb");
curl_easy_setopt(curl, CURLOPT_URL, fetch.url.c_str());
// FIXME: curl needs a write function if WIN32,
@ -26,18 +71,59 @@ bool Fetchfile::fetch( fetch_t fetch ) {
curl_easy_cleanup(curl);
fclose(fp);
} else {
return false;
return false;
}
return true;
}
bool Fetchfile::has_fetchfile() {
return this->exist_fetchfile;
}
list<fetch_t> Fetchfile::get_entries() {
return this->entries;
}
bool Fetchfile::fetch_all_entries() {
bool is_successful = true;
for (list<fetch_t>::iterator it=this->entries.begin(); it!=this->entries.end(); ++it) {
bool result = this->fetch( *it );
if (false == result) {
is_successful = false;
}
}
return is_successful;
}
bool Fetchfile::validate() {
return true;
bool is_valid = true;
if (this->has_fetchfile()) {
// TODO:
}
return is_valid;
}
bool Fetchfile::store( string basedir ) {
return false;
fs::path p{ basedir };
fs::file_status s = fs::status( p );
if (! fs::is_directory( s)) {
Fetchfile::log << "directory '" << basedir << "' does not exist" << endl;
return false;
}
string filename = basedir + "fetch.txt";
ofstream fetchfile_txt_file;
fetchfile_txt_file.open( filename );
if (fetchfile_txt_file.is_open()) {
for (list<fetch_t>::iterator it=this->entries.begin(); it!=this->entries.end(); ++it) {
fetchfile_txt_file << it->url << " " << it->size << " " << it->filename << endl;
}
fetchfile_txt_file.close();
}
return true;
}
void Fetchfile::get_logstream( stringstream & log ) {
log << this->log.rdbuf();
}


+ 39
- 0
test/test_fetchfile.cpp View File

@ -2,6 +2,7 @@
#include <boost/test/unit_test.hpp>
#include <string>
#include <iostream>
#include <list>
namespace utf = boost::unit_test;
#include "fetchfile.hpp"
@ -22,11 +23,49 @@ bool check_validation(string dir) {
return p.validate();
}
bool check_store( string dir_load_from, string dir_store_to ) {
Fetchfile p( dir_load_from );
bool result = p.store( dir_store_to );
return result;
}
bool check_get_fetchfile(string dir) {
Fetchfile p( dir );
list<fetch_t> results = p.get_entries();
list<fetch_t> expected;
list<fetch_t>::iterator i;
list<fetch_t>::iterator j;
bool is_valid = true;
for (i=expected.begin(), j=results.begin(); i!=expected.end() && j!=results.end(); i++,j++) {
if (
( i->url != j-> url) ||
( i->size != j->size) ||
( i->filename != j->filename)
) {
cout << "Expected: (" << (i->url) << ";" << (i->size) << ";"<< (i->filename)<< ")" << endl;
cout << "Result: ("<< (j->url) << ";" << (j->size) << ";" << (j->filename)<< ")" << endl;
is_valid=false; break;
}
}
return is_valid;
}
bool check_has_fetchfile(string dir) {
Fetchfile p( dir );
return p.has_fetchfile();
}
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("/tmp/"));};
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_fetchfile_exists) {BOOST_TEST(! check_has_fetchfile("../testbags/bag_minimal_ok/")); };
BOOST_AUTO_TEST_CASE(check_fetchfile) {BOOST_TEST( check_get_fetchfile("../testbags/bag_minimal_ok/")); };
BOOST_AUTO_TEST_CASE(check_store_ok) {BOOST_TEST(check_store("../testbags/bag_minimal_ok/", "/tmp/testbag/"));};
// vim: set tabstop=4 softtabstop=0 expandtab shiftwidth=4 smarttab

Loading…
Cancel
Save