3333#include < QFileInfo>
3434#include < QDir>
3535#include < cmath>
36+ #include < optional>
3637
3738#ifdef Q_OS_UNIX
3839#include < unistd.h>
@@ -290,12 +291,29 @@ qint64 GETFileJob::writeToDevice(const QByteArray &data)
290291 return _device->write (data);
291292}
292293
294+ bool GETFileJob::writeSymlink (const QString &symlinkTarget)
295+ {
296+ auto file_device = qobject_cast<QFile*>(_device);
297+ if (!file_device) {
298+ qCWarning (lcGetJob) << " Unable to create symlink on given device!" ;
299+ return false ;
300+ }
301+ file_device->close ();
302+ FileSystem::remove (file_device->fileName ());
303+ if (!QFile::link (symlinkTarget, file_device->fileName ())) {
304+ qCWarning (lcGetJob) << " Error creating symlink '" << file_device->fileName () << ' \' ' ;
305+ return false ;
306+ }
307+ return true ;
308+ }
309+
293310void GETFileJob::slotReadyRead ()
294311{
295312 if (!reply ())
296313 return ;
297314 int bufferSize = qMin (1024 * 8ll , reply ()->bytesAvailable ());
298315 QByteArray buffer (bufferSize, Qt::Uninitialized);
316+ std::optional<QString> symlinkTarget;
299317
300318 while (reply ()->bytesAvailable () > 0 && _saveBodyToFile) {
301319 if (_bandwidthChoked) {
@@ -323,11 +341,11 @@ void GETFileJob::slotReadyRead()
323341
324342 if (reply ()->hasRawHeader (" OC-File-Type" ) &&
325343 reply ()->rawHeader (" OC-File-Type" ).toInt () == ItemTypeSoftLink) {
326- // TODO: handle non-Unix OS
327- auto file_device = static_cast <QFile*>(_device) ;
328- file_device-> close ();
329- FileSystem::remove (file_device-> fileName ()) ;
330- QFile::link ( QString (buffer), file_device-> fileName ());
344+ if (symlinkTarget) {
345+ *symlinkTarget += buffer ;
346+ } else {
347+ symlinkTarget = buffer ;
348+ }
331349 } else {
332350 const qint64 writtenBytes = writeToDevice (QByteArray::fromRawData (buffer.constData (), readBytes));
333351 if (writtenBytes != readBytes) {
@@ -340,6 +358,11 @@ void GETFileJob::slotReadyRead()
340358 }
341359 }
342360
361+ if (symlinkTarget && !writeSymlink (*symlinkTarget)) {
362+ reply ()->abort ();
363+ return ;
364+ }
365+
343366 if (reply ()->isFinished () && (reply ()->bytesAvailable () == 0 || !_saveBodyToFile)) {
344367 qCDebug (lcGetJob) << " Actually finished!" ;
345368 if (_bandwidthManager) {
0 commit comments