47
49
RestoreHelper* backup_helper
49
51
: q_ptr(backup_helper)
52
, server_(new QLocalServer)
51
54
// listen for inactivity from storage framework
52
55
QObject::connect(&timer_, &QTimer::timeout,
53
56
std::bind(&RestoreHelperPrivate::on_inactivity_detected, this)
59
QObject::connect(&timer2_, &QTimer::timeout,
60
std::bind(&RestoreHelperPrivate::delay_data, this)
56
63
// fire up the sockets
58
65
int rc = socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0, fds);
66
73
// helper socket is for the client.
67
helper_socket_.setSocketDescriptor(fds[0], QLocalSocket::ConnectedState, QIODevice::ReadOnly);
69
write_socket_.setSocketDescriptor(fds[1], QLocalSocket::ConnectedState, QIODevice::WriteOnly);
74
helper_socket_.setSocketDescriptor(fds[1], QLocalSocket::ConnectedState, QIODevice::ReadOnly);
76
write_socket_.setSocketDescriptor(fds[0], QLocalSocket::ConnectedState, QIODevice::WriteOnly);
79
QObject::connect(server_.data(), &QLocalServer::newConnection, std::bind(&RestoreHelperPrivate::restore_ready, this));
81
if (!server_->listen("test_helper"))
83
qWarning() << " GREP ERROR LISTENING";
72
87
~RestoreHelperPrivate() = default;
140
157
case Helper::State::DATA_COMPLETE: {
141
158
qDebug() << "Restore helper finished, calling downloader_.finish()";
142
159
write_socket_.disconnectFromServer();
160
helper_conn_->disconnectFromServer();
143
161
downloader_->finish();
144
162
downloader_.reset();
158
176
check_for_done();
181
qDebug() << "GREP (((((((((((((((((((((((((((((((((((( HELPER IS READY: Starts sending data: TOTAL TO SEND: " << upload_buffer_.size();
182
helper_is_ready_ = true;
184
helper_conn_.reset(server_->nextPendingConnection());
186
connections_.remember(QObject::connect(
187
helper_conn_.data(), &QLocalSocket::bytesWritten,
188
std::bind(&RestoreHelperPrivate::on_data_uploaded, this, std::placeholders::_1)
190
// timer2_.start(10);
163
196
void on_inactivity_detected()
205
if (upload_buffer_.size())
207
int bytes_to_send = upload_buffer_.size() < UPLOAD_BUFFER_MAX_ ? upload_buffer_.size() : UPLOAD_BUFFER_MAX_;
208
// try to empty the upload buf
209
const auto n = helper_conn_->write(upload_buffer_, bytes_to_send);
211
// THIS IS JUST FOR EXTRA DEBUG INFORMATION
212
QCryptographicHash hash(QCryptographicHash::Sha1);
213
hash.addData(upload_buffer_.left(100));
214
// qDebug() << "GREP ************************************************ Hash send: " << hash.result().toHex() << " Size: " << n << " Total: " << upload_buffer_.size();
215
// // THIS IS JUST FOR EXTRA DEBUG INFORMATION
216
upload_buffer_.remove(0, int(n));
217
qDebug() << "GREP ************************************************ Hash send: " << hash.result().toHex() << " Size: " << n << " Total: " << upload_buffer_.size();
218
// THIS IS JUST FOR EXTRA DEBUG INFORMATION
219
waiting_for_data_to_be_written_ = true;
224
qWarning() << "Write error:" << write_socket_.errorString();
228
// helper_conn_->flush();
229
// timer2_.start(10);
170
233
void on_ready_read()
175
238
void on_data_uploaded(qint64 n)
240
waiting_for_data_to_be_written_ = false;
177
241
n_uploaded_ += n;
178
242
q_ptr->record_data_transferred(n);
179
qDebug("n_read %zu n_uploaded %zu (newly uploaded %zu)", size_t(n_read_), size_t(n_uploaded_), size_t(n));
244
qDebug() << "GREP ====================================== Bytes uploaded: " << n << "Total: " << n_uploaded_;
246
// qDebug("n_read %zu n_uploaded %zu (newly uploaded %zu)", size_t(n_read_), size_t(n_uploaded_), size_t(n));
254
// qDebug("n_read %zu n_uploaded %zu (newly uploaded %zu)", size_t(n_read_), size_t(n_uploaded_), size_t(n));
181
256
check_for_done();
184
259
void process_more()
186
qDebug() << "RestoreHelper::process_more()";
187
261
if (!downloader_)
189
qDebug() << "RestoreHelper::process_more() 2";
190
263
char readbuf[UPLOAD_BUFFER_MAX_];
191
264
auto socket = downloader_->socket();
194
267
if (!socket->bytesAvailable())
196
269
// try to fill the upload buf
197
int max_bytes = (UPLOAD_BUFFER_MAX_) - upload_buffer_.size();
270
// int max_bytes = UPLOAD_BUFFER_MAX_ - upload_buffer_.size();
271
int max_bytes = UPLOAD_BUFFER_MAX_;
198
272
if (max_bytes > 0) {
199
273
const auto n = socket->read(readbuf, max_bytes);
202
276
upload_buffer_.append(readbuf, int(n));
277
QCryptographicHash hash(QCryptographicHash::Sha1);
278
hash.addData(readbuf, 100);
279
qDebug() << "GREP ########################### READ BYTES: " << n << "Total: " << n_read_ << " Hash: " << hash.result().toHex();
203
280
qDebug("upload_buffer_.size() is %zu after reading %zu from helper", size_t(upload_buffer_.size()), size_t(n));
205
282
else if (n < 0) {
213
// THIS IS JUST FOR EXTRA DEBUG INFORMATION
214
QCryptographicHash hash(QCryptographicHash::Sha1);
215
hash.addData(upload_buffer_.left(100));
216
qDebug() << "************************************************ Hash send: " << hash.result().toHex() << " Size: " << upload_buffer_.size() << " Total: " << n_read_;
217
// THIS IS JUST FOR EXTRA DEBUG INFORMATION
290
if (helper_is_ready_ && ! waiting_for_data_to_be_written_)
219
// try to empty the upload buf
220
const auto n = write_socket_.write(upload_buffer_);
222
upload_buffer_.remove(0, int(n));
223
qDebug("upload_buffer_.size() is %zu after writing %zu to cloud", size_t(upload_buffer_.size()), size_t(n));
229
qWarning() << "Write error:" << write_socket_.errorString();
295
// // THIS IS JUST FOR EXTRA DEBUG INFORMATION
296
// QCryptographicHash hash(QCryptographicHash::Sha1);
297
// hash.addData(upload_buffer_.left(100));
298
// qDebug() << "************************************************ Hash send: " << hash.result().toHex() << " Size: " << upload_buffer_.size() << " Total: " << n_read_;
299
// // THIS IS JUST FOR EXTRA DEBUG INFORMATION
301
// // try to empty the upload buf
302
// const auto n = write_socket_.write(upload_buffer_);
304
// upload_buffer_.remove(0, int(n));
305
// qDebug("upload_buffer_.size() is %zu after writing %zu to cloud", size_t(upload_buffer_.size()), size_t(n));
310
// write_error_ = true;
311
// qWarning() << "Write error:" << write_socket_.errorString();
236
318
reset_inactivity_timer();
297
381
bool cancelled_ = false;
298
382
ConnectionHelper connections_;
299
383
QString uploader_committed_file_name_;
385
bool helper_is_ready_ = false;
386
bool waiting_for_data_to_be_written_ = false;
388
QSharedPointer<QLocalServer> server_;
389
QSharedPointer<QLocalSocket> helper_conn_;