rsyncui/downloadfile.cpp

88 lines
2.0 KiB
C++
Raw Normal View History

2023-01-07 12:44:45 +01:00
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "downloadfile.h"
2023-01-07 12:44:45 +01:00
#include "tools.h"
#include <pstreams/pstream.h>
#include <sstream>
#include <string>
#include <cstring>
#include <iostream>
#include <stdio.h>
#include <QMessageBox>
#include <vector>
#include <boost/algorithm/string/replace.hpp>
#include <memory>
#include <stdexcept>
#include <array>
2023-01-12 09:23:45 +01:00
#include <unistd.h>
#include <sys/types.h>
2023-01-07 12:44:45 +01:00
using namespace std;
downloadFile::downloadFile()
{
}
2023-01-12 09:23:45 +01:00
void downloadFile::cancelled(int pid)
2023-01-07 12:44:45 +01:00
{
2023-01-12 09:23:45 +01:00
if (kill(pid, SIGTERM) == -1)
{
//TODO gestion erreur kill
}
2023-01-07 12:44:45 +01:00
}
2023-01-12 09:23:45 +01:00
void downloadFile::download(MainWindow *mw)
2023-01-07 12:44:45 +01:00
{
string line;
string errorRsync;
int pos;
2023-01-12 09:23:45 +01:00
array<string, 7> argv;
2023-01-07 12:44:45 +01:00
stringstream output;
vector<string> v;
int value;
2023-01-12 09:23:45 +01:00
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 + "/";
2023-01-07 12:44:45 +01:00
2023-01-12 09:23:45 +01:00
//unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd "r"), pclose);
FILE * fp = popen2(argv, "r", mw->downloading.pid);
2023-01-07 12:44:45 +01:00
if (!fp)
{
throw runtime_error("popen2() failed!");
2023-01-12 09:23:45 +01:00
return;
2023-01-07 12:44:45 +01:00
}
2023-01-12 09:23:45 +01:00
while (fgets(buffer, 4096, fp) != nullptr)
2023-01-07 12:44:45 +01:00
{
if (this->canceled == true)
{
return;
}
2023-01-12 09:23:45 +01:00
line = buffer;
2023-01-07 12:44:45 +01:00
//cout << line << endl;
pos = line.find('%');
if (pos != -1)
{
line.erase(pos);
pos = line.find_last_of(' ');
if (pos != -1)
{
line.erase(0, pos);
value = stoi(line);
//cout << value << endl;
2023-01-07 12:44:45 +01:00
emit progressSignal(value);
}
}
}
2023-01-12 09:23:45 +01:00
pclose2(fp, mw->downloading.pid);
emit progressSignal(100);
emit finishedSignal(true);
2023-01-07 12:44:45 +01:00
//cout << path << endl;
}