1ère version fonctionnelle

This commit is contained in:
2023-01-12 09:23:45 +01:00
parent a76f32ea30
commit 789317ea12
9 changed files with 273 additions and 150 deletions
+24 -40
View File
@@ -14,6 +14,8 @@
#include <memory>
#include <stdexcept>
#include <array>
#include <unistd.h>
#include <sys/types.h>
using namespace std;
@@ -21,66 +23,47 @@ downloadFile::downloadFile()
{
}
void downloadFile::cancelled()
void downloadFile::cancelled(int pid)
{
this->canceled = true;
if (kill(pid, SIGTERM) == -1)
{
//TODO gestion erreur kill
}
}
void downloadFile::download(QString savePath, MainWindow *mw)
void downloadFile::download(MainWindow *mw)
{
string server;
string service;
string path;
string line;
string errorRsync;
char cmd[4096];
int portN;
int pos;
int pid;
QTreeWidgetItem * item;
array<string, 7> argv;
stringstream output;
// QString savePath = ".";
vector<string> v;
int value;
//char command_out[1024] = {0};
array<char, 2048> buffer;
char buffer[4096];
argv[0] = "/usr/bin/rsync";
argv[1] = "--bwlimit=" + mw->connexion.bandwidthLimit;
argv[2] = "--port=" + to_string(mw->connexion.port);
argv[3] = "-P";
argv[4] = mw->connexion.server + "::" + mw->downloading.service + "/" + mw->downloading.path;
argv[5] = mw->downloading.savePath + "/";
server.assign(mw->ui->khistorycombobox->currentText().toStdString());
portN = mw->ui->portEdit->text().toInt();
service = mw->ui->listWidget->currentItem()->text().toStdString();
pos = service.find_first_of('\n');
service.resize(pos);
item = mw->ui->treeWidget->currentItem();
path = item->text(0).toStdString();
while(item->parent() != NULL)
{
item = item->parent();
path = item->text(0).toStdString() + "/" + path;
};
sprintf(cmd, "rsync --bwlimit=100K --port %i -P %s::\"%s/%s\" \"%s/\" 2>&1", portN, server.c_str(), service.c_str(), path.c_str(), savePath.toStdString().c_str());
//unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
FILE * fp = popen2(cmd, "r", pid);
//unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd "r"), pclose);
FILE * fp = popen2(argv, "r", mw->downloading.pid);
if (!fp)
{
throw runtime_error("popen2() failed!");
return;
}
while (fgets(buffer.data(), buffer.size(), fp) != nullptr)
while (fgets(buffer, 4096, fp) != nullptr)
{
buffer.data();
if (this->canceled == true)
{
return;
}
line = buffer.data();
line = buffer;
//cout << line << endl;
pos = line.find('%');
if (pos != -1)
@@ -95,9 +78,10 @@ void downloadFile::download(QString savePath, MainWindow *mw)
emit progressSignal(value);
}
}
buffer.empty();
}
pclose2(fp, mw->downloading.pid);
emit progressSignal(100);
emit finishedSignal(true);
//cout << path << endl;
}