code format

This commit is contained in:
2022-12-14 14:13:52 +03:00
parent 430a41fefc
commit c2b8a8d6da
297 changed files with 27331 additions and 24162 deletions

View File

@@ -1,20 +1,20 @@
/*
PIP - Platform Independent Primitives
Class for send and receive files and directories via PIBaseTransfer
Andrey Bychkov work.a.b@yandex.ru
PIP - Platform Independent Primitives
Class for send and receive files and directories via PIBaseTransfer
Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser 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 free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "pifiletransfer.h"
@@ -22,11 +22,12 @@
const char PIFileTransfer::sign[] = {'P', 'F', 'T'};
PIFileTransfer::PIFileTransfer() {
for (uint i = 0; i < sizeof(sign); i++) pftheader.sig[i] = sign[i];
pftheader.version = __PIFILETRANSFER_VERSION;
for (uint i = 0; i < sizeof(sign); i++)
pftheader.sig[i] = sign[i];
pftheader.version = __PIFILETRANSFER_VERSION;
pftheader.session_id = 0;
pftheader.step = pft_None;
dir = PIDir::current();
pftheader.step = pft_None;
dir = PIDir::current();
started_ = scanning = false;
bytes_file_all = bytes_file_cur = 0;
CONNECT1(void, bool, this, sendFinished, this, send_finished);
@@ -40,7 +41,7 @@ PIFileTransfer::~PIFileTransfer() {
}
bool PIFileTransfer::send(const PIFile& file) {
bool PIFileTransfer::send(const PIFile & file) {
return send(file.path());
}
@@ -50,9 +51,10 @@ bool PIFileTransfer::send(const PIString & file) {
}
bool PIFileTransfer::send(const PIStringList& files) {
bool PIFileTransfer::send(const PIStringList & files) {
PIVector<PIFile::FileInfo> fil;
piForeachC(PIString &file, files) fil << PIFile::fileInfo(file);
piForeachC(PIString & file, files)
fil << PIFile::fileInfo(file);
return send(fil);
}
@@ -66,16 +68,16 @@ bool PIFileTransfer::send(PIVector<PIFile::FileInfo> entries) {
if (entries[i].isDir()) {
cur_file_string = "scanning ... ";
scan_dir.setDir(entries[i].path);
scanning = true;
scanning = true;
PIVector<PIFile::FileInfo> fls = scan_dir.allEntries();
scanning = false;
scanning = false;
scan_dir.up();
//piCout << "files rel to" << d.absolutePath();
// piCout << "files rel to" << d.absolutePath();
for (uint j = 0; j < fls.size(); j++) {
allEntries << PFTFileInfo(fls[j]);
allEntries.back().dest_path = scan_dir.relative(fls[j].path);
//piCout << " abs path" << fls[j].path;
//piCout << " rel path" << allEntries.back().dest_path;
// piCout << " abs path" << fls[j].path;
// piCout << " rel path" << allEntries.back().dest_path;
}
}
}
@@ -83,19 +85,18 @@ bool PIFileTransfer::send(PIVector<PIFile::FileInfo> entries) {
}
bool PIFileTransfer::sendFiles(const PIVector<PFTFileInfo> &files) {
bool PIFileTransfer::sendFiles(const PIVector<PFTFileInfo> & files) {
files_ = files;
PIStringList names;
//piCoutObj << "prepare to send" << files_.size() << "files";
// piCoutObj << "prepare to send" << files_.size() << "files";
for (int i = 0; i < files_.size_s(); i++) {
if (names.contains(files_[i].path)) {
files_.remove(i);
--i;
}
else names << files_[i].path;
if (files_[i].isDir())
files_[i].size = 0;
//piCout << "prepare" << i << files_[i].path << files_[i].dest_path << files_[i].name();
} else
names << files_[i].path;
if (files_[i].isDir()) files_[i].size = 0;
// piCout << "prepare" << i << files_[i].path << files_[i].dest_path << files_[i].name();
}
randomize();
step_mutex.lock();
@@ -129,15 +130,15 @@ bool PIFileTransfer::sendFiles(const PIVector<PFTFileInfo> &files) {
void PIFileTransfer::processFile(int id, ullong start, PIByteArray & data) {
//piCout << "processFile" << id << files_.size();
PFTFileInfo fi = files_[id-1];
bytes_file_all = fi.size;
bytes_file_cur = start;
// piCout << "processFile" << id << files_.size();
PFTFileInfo fi = files_[id - 1];
bytes_file_all = fi.size;
bytes_file_cur = start;
cur_file_string = fi.dest_path;
PIString path = dir.absolutePath() + dir.separator + fi.dest_path;
//piCout << "receive" << path << fi.size << data.size();
PIString path = dir.absolutePath() + dir.separator + fi.dest_path;
// piCout << "receive" << path << fi.size << data.size();
if (fi.isDir()) {
//piCoutObj << "make dir" << fi.entry.path;
// piCoutObj << "make dir" << fi.entry.path;
if (!PIDir::make(path)) {
cur_file_string = "Error: While create directory \"" + path + "\"";
piCoutObj << cur_file_string;
@@ -147,9 +148,9 @@ void PIFileTransfer::processFile(int id, ullong start, PIByteArray & data) {
}
if (fi.isFile()) {
if (work_file.path() != path || !work_file.isOpened()) {
//piCout << "file close";
// piCout << "file close";
work_file.close();
//piCout << "new file" << path << work_file.path() << work_file.isOpened();
// piCout << "new file" << path << work_file.path() << work_file.isOpened();
if (!work_file.open(path, PIIODevice::ReadWrite)) {
cur_file_string = "Error: While open file \"" + path + "\"";
piCoutObj << cur_file_string;
@@ -158,17 +159,18 @@ void PIFileTransfer::processFile(int id, ullong start, PIByteArray & data) {
}
PIFile::applyFileInfo(path, fi);
if (work_file.size() > fi.size) {
//piCoutObj << "error size" << work_file.size() << fi.size;
// piCoutObj << "error size" << work_file.size() << fi.size;
work_file.clear();
work_file.resize(fi.size);
//piCoutObj << "correct size" << work_file.size() << fi.size;
// piCoutObj << "correct size" << work_file.size() << fi.size;
}
}
//piCoutObj << "write file" << path << work_file.path() << work_file.size() << fi.entry.size << work_file.pos() << fi.fstart << fi.fsize;
// piCoutObj << "write file" << path << work_file.path() << work_file.size() << fi.entry.size << work_file.pos() << fi.fstart <<
// fi.fsize;
if (work_file.size() < (llong)start) {
//piCoutObj << "error pos size" << work_file.pos() << fi.fstart;
// piCoutObj << "error pos size" << work_file.pos() << fi.fstart;
work_file.resize(start);
//piCoutObj << "correct pos size" << work_file.pos() << fi.fstart;
// piCoutObj << "correct pos size" << work_file.pos() << fi.fstart;
}
if (work_file.size() > fi.size) {
piCoutObj << "****** error size" << work_file.size() << fi.size;
@@ -176,11 +178,12 @@ void PIFileTransfer::processFile(int id, ullong start, PIByteArray & data) {
work_file.resize(fi.size);
piCoutObj << "****** correct size" << work_file.size() << fi.size;
}
//if (fi.fstart != work_file.pos()) piCoutObj << "error pos" << work_file.pos() << fi.fstart;
// if (fi.fstart != work_file.pos()) piCoutObj << "error pos" << work_file.pos() << fi.fstart;
work_file.seek(start);
int rs = work_file.write(data.data(), data.size());
if (rs != data.size_s()) {
cur_file_string = "Error: While write file \"" + fi.path + "\" (must " + PIString::fromNumber(data.size()) + ", but write " + PIString::fromNumber(rs) + ")";
cur_file_string = "Error: While write file \"" + fi.path + "\" (must " + PIString::fromNumber(data.size()) + ", but write " +
PIString::fromNumber(rs) + ")";
piCoutObj << cur_file_string;
stopReceive();
return;
@@ -190,46 +193,45 @@ void PIFileTransfer::processFile(int id, ullong start, PIByteArray & data) {
PIByteArray PIFileTransfer::buildPacket(Part p) {
//piCoutObj << "Step" << pftheader.step;
//piCoutObj << "session id" << pftheader.session_id;
// piCoutObj << "Step" << pftheader.step;
// piCoutObj << "session id" << pftheader.session_id;
PIByteArray ba;
switch(pftheader.step) {
case pft_None:
stopSend();
return PIByteArray();
case pft_Description:
ba.resize(p.size);
memcpy(ba.data(), desc.data(p.start), p.size);
switch (pftheader.step) {
case pft_None: stopSend(); return PIByteArray();
case pft_Description:
ba.resize(p.size);
memcpy(ba.data(), desc.data(p.start), p.size);
return ba;
case pft_Data:
//piCout << "send data" << p.id << files_.size();
PIFile::FileInfo fi = files_[p.id-1];
if (fi.isFile()) {
//piCout << "send file" << fi.name() << fi.size;
PIString path = fi.path;
if (work_file.path() != path || !work_file.isOpened()) {
if (!work_file.open(path, PIIODevice::ReadOnly)) {
break_ = true;
cur_file_string = "Error: While open file \"" + fi.path + "\"";
piCoutObj << cur_file_string;
stopSend();
return PIByteArray();
}
}
work_file.seek(p.start);
ba.resize(p.size);
int rs = work_file.read(ba.data(), ba.size());
if ((llong)rs != (llong)p.size) {
break_ = true;
cur_file_string = "Error: While read file \"" + fi.path + "\" (must " + PIString::fromNumber(p.size) + ", but read " + PIString::fromNumber(rs) + ")";
case pft_Data:
// piCout << "send data" << p.id << files_.size();
PIFile::FileInfo fi = files_[p.id - 1];
if (fi.isFile()) {
// piCout << "send file" << fi.name() << fi.size;
PIString path = fi.path;
if (work_file.path() != path || !work_file.isOpened()) {
if (!work_file.open(path, PIIODevice::ReadOnly)) {
break_ = true;
cur_file_string = "Error: While open file \"" + fi.path + "\"";
piCoutObj << cur_file_string;
stopSend();
return PIByteArray();
}
}
cur_file_string = fi.path;
bytes_file_all = fi.size;
bytes_file_cur = p.start;
work_file.seek(p.start);
ba.resize(p.size);
int rs = work_file.read(ba.data(), ba.size());
if ((llong)rs != (llong)p.size) {
break_ = true;
cur_file_string = "Error: While read file \"" + fi.path + "\" (must " + PIString::fromNumber(p.size) + ", but read " +
PIString::fromNumber(rs) + ")";
piCoutObj << cur_file_string;
stopSend();
return PIByteArray();
}
}
cur_file_string = fi.path;
bytes_file_all = fi.size;
bytes_file_cur = p.start;
return ba;
}
return ba;
@@ -242,7 +244,7 @@ void PIFileTransfer::receivePart(PIBaseTransfer::Part fi, PIByteArray ba, PIByte
pheader >> h;
// piCout << h.session_id;
PIMutexLocker ml(step_mutex);
StepType st = (StepType)h.step;
StepType st = (StepType)h.step;
pftheader.step = st;
if (!h.check_sig()) {
cur_file_string = "Error: Check signature: File Transfer incompatable or invalid version!";
@@ -250,29 +252,27 @@ void PIFileTransfer::receivePart(PIBaseTransfer::Part fi, PIByteArray ba, PIByte
stopReceive();
return;
}
switch(st) {
case pft_None:
switch (st) {
case pft_None: break;
case pft_Description:
pftheader.session_id = h.session_id;
if (desc.size() < fi.start + fi.size) desc.resize(fi.start + fi.size);
memcpy(desc.data(fi.start), ba.data(), ba.size_s());
break;
case pft_Description:
pftheader.session_id = h.session_id;
if (desc.size() < fi.start + fi.size) desc.resize(fi.start + fi.size);
memcpy(desc.data(fi.start), ba.data(), ba.size_s());
break;
case pft_Data:
if (h.session_id == pftheader.session_id)
processFile(fi.id, fi.start, ba);
else stopReceive();
break;
default:
case pft_Data:
if (h.session_id == pftheader.session_id)
processFile(fi.id, fi.start, ba);
else
stopReceive();
break;
default: break;
}
}
PIString PIFileTransfer::curFile() const {
PIString s = cur_file_string;
if (scanning)
return s + scan_dir.scanDir();
if (scanning) return s + scan_dir.scanDir();
return s;
}
@@ -287,7 +287,7 @@ PIByteArray PIFileTransfer::customHeader() {
void PIFileTransfer::beginReceive() {
if (pftheader.step == pft_None) {
files_.clear();
// piCoutObj << "start receive"
// piCoutObj << "start receive"
started_ = true;
receiveFilesStarted();
}
@@ -301,11 +301,12 @@ void PIFileTransfer::receive_finished(bool ok) {
if (st == pft_Description) {
bool user_ok = true;
if (ok) {
// piCoutObj << desc.size() << PICoutManipulators::Hex << desc;
// piCoutObj << desc.size() << PICoutManipulators::Hex << desc;
desc >> files_;
// piCoutObj << files_;
// piCoutObj << files_;
PIStringList files;
piForeachC(PFTFileInfo &fi, files_) files << fi.dest_path;
piForeachC(PFTFileInfo & fi, files_)
files << fi.dest_path;
pause();
receiveFilesRequest(files, bytesAll(), &user_ok);
}
@@ -314,10 +315,11 @@ void PIFileTransfer::receive_finished(bool ok) {
piCoutObj << "receive aborted";
receiveFilesFinished(false);
started_ = false;
} else resume();
} else
resume();
}
if (st == pft_Data) {
//piCoutObj << "file close";
// piCoutObj << "file close";
work_file.close();
receiveFilesFinished(ok);
started_ = false;
@@ -326,7 +328,7 @@ void PIFileTransfer::receive_finished(bool ok) {
void PIFileTransfer::send_finished(bool ok) {
// piCoutObj << "file close";
// piCoutObj << "file close";
if (pftheader.step == pft_Data) {
work_file.close();
}