forked from owncloud/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpropagateremotemkdir.cpp
140 lines (116 loc) · 4.53 KB
/
propagateremotemkdir.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Copyright (C) by Olivier Goffart <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License
* for more details.
*/
#include "propagateremotemkdir.h"
#include "owncloudpropagator_p.h"
#include "account.h"
#include "common/syncjournalfilerecord.h"
#include "propagateremotedelete.h"
#include "common/asserts.h"
#include <QFile>
#include <QLoggingCategory>
namespace OCC {
Q_LOGGING_CATEGORY(lcPropagateRemoteMkdir, "sync.propagator.remotemkdir", QtInfoMsg)
void PropagateRemoteMkdir::start()
{
if (propagator()->_abortRequested)
return;
qCDebug(lcPropagateRemoteMkdir) << _item->_file;
propagator()->_activeJobList.append(this);
if (!_deleteExisting) {
return slotStartMkcolJob();
}
_job = new DeleteJob(propagator()->account(),
propagator()->fullRemotePath(_item->_file),
this);
connect(_job, SIGNAL(finishedSignal()), SLOT(slotStartMkcolJob()));
_job->start();
}
void PropagateRemoteMkdir::slotStartMkcolJob()
{
if (propagator()->_abortRequested)
return;
qCDebug(lcPropagateRemoteMkdir) << _item->_file;
_job = new MkColJob(propagator()->account(),
propagator()->fullRemotePath(_item->_file),
this);
connect(_job, SIGNAL(finished(QNetworkReply::NetworkError)), this, SLOT(slotMkcolJobFinished()));
_job->start();
}
void PropagateRemoteMkdir::abort(PropagatorJob::AbortType abortType)
{
if (_job && _job->reply())
_job->reply()->abort();
if (abortType == AbortType::Asynchronous) {
emit abortFinished();
}
}
void PropagateRemoteMkdir::setDeleteExisting(bool enabled)
{
_deleteExisting = enabled;
}
void PropagateRemoteMkdir::slotMkcolJobFinished()
{
propagator()->_activeJobList.removeOne(this);
OC_ASSERT(_job);
QNetworkReply::NetworkError err = _job->reply()->error();
_item->_httpErrorCode = _job->reply()->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
_item->_responseTimeStamp = _job->responseTimestamp();
_item->_requestId = _job->requestId();
if (_item->_httpErrorCode == 405) {
// This happens when the directory already exists. Nothing to do.
} else if (err != QNetworkReply::NoError) {
SyncFileItem::Status status = classifyError(err, _item->_httpErrorCode,
&propagator()->_anotherSyncNeeded);
done(status, _job->errorString());
return;
} else if (_item->_httpErrorCode != 201) {
// Normally we expect "201 Created"
// If it is not the case, it might be because of a proxy or gateway intercepting the request, so we must
// throw an error.
done(SyncFileItem::NormalError,
tr("Wrong HTTP code returned by server. Expected 201, but received \"%1 %2\".")
.arg(_item->_httpErrorCode)
.arg(_job->reply()->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString()));
return;
}
_item->_fileId = _job->reply()->rawHeader("OC-FileId");
propagator()->_activeJobList.append(this);
auto propfindJob = new PropfindJob(_job->account(), _job->path(), this);
propfindJob->setProperties({"http://owncloud.org/ns:permissions"});
connect(propfindJob, &PropfindJob::result, this, [this](const QVariantMap &result){
propagator()->_activeJobList.removeOne(this);
_item->_remotePerm = RemotePermissions::fromServerString(result.value(QStringLiteral("permissions")).toString());
success();
});
connect(propfindJob, &PropfindJob::finishedWithError, this, [this]{
// ignore the PROPFIND error
propagator()->_activeJobList.removeOne(this);
done(SyncFileItem::NormalError);
});
propfindJob->start();
}
void PropagateRemoteMkdir::success()
{
// Never save the etag on first mkdir.
// Only fully propagated directories should have the etag set.
auto itemCopy = *_item;
itemCopy._etag.clear();
// save the file id already so we can detect rename or remove
if (!propagator()->updateMetadata(itemCopy)) {
done(SyncFileItem::FatalError, tr("Error writing metadata to the database"));
return;
}
done(SyncFileItem::Success);
}
}