VirtualBox

Changeset 78701 in vbox for trunk/src/VBox


Ignore:
Timestamp:
May 23, 2019 5:02:30 PM (6 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
130779
Message:

FE/Qt: bugref:9470: Networking stuff: Rework GA network customer to download image to .tmp file first and then rename it carefully with rewriting confirmation if required.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/src/net/UIDownloaderAdditions.cpp

    r78668 r78701  
    5959    const QString strSource = strSourcePath + strSourceName;
    6060    const QString strPathSHA256SumsFile = QString("https://www.virtualbox.org/download/hashes/%1/SHA256SUMS").arg(strVersion);
    61     const QString strTarget = QDir(vboxGlobal().homeFolder()).absoluteFilePath(strSourceName);
     61    const QString strTarget = QDir(vboxGlobal().homeFolder()).absoluteFilePath(QString("%1.tmp").arg(strSourceName));
    6262
    6363    /* Set source/target: */
     
    144144    }
    145145
    146     /* Serialize that buffer into the file: */
     146    /* Make sure temporary file exists.  If we have
     147     * reached this place, it's already written and verified. */
     148    const QString strTempFilename = target();
     149    if (!QFile::exists(strTempFilename))
     150    {
     151        /* But still we are providing a failsafe.
     152         * Since we can try to write it again. */
     153        QFile file(strTempFilename);
     154        if (!file.open(QIODevice::WriteOnly))
     155            AssertFailedReturnVoid();
     156        file.write(m_receivedData);
     157    }
     158
     159    /* Rename temporary file to target one.  This can require a number
     160     * of tries to let user choose the place to save file to. */
     161    QString strNetTarget = target();
     162    strNetTarget.remove(QRegExp("\\.tmp$"));
     163    setTarget(strNetTarget);
    147164    while (true)
    148165    {
    149         /* Make sure the file already exists.  If we reached
    150          * this place, it's already written and checked. */
    151         QFile file(target());
    152         bool fSuccess = false;
    153         /* Check step. Try to open file for reading first. */
    154         if (file.open(QIODevice::ReadOnly))
    155             fSuccess = true;
    156         /* Failsafe step. Try to open file for writing otherwise. */
    157         if (!fSuccess && file.open(QIODevice::WriteOnly))
    158         {
    159             /* Write buffer into the file: */
    160             file.write(m_receivedData);
    161             file.close();
    162             fSuccess = true;
    163         }
    164         /* If the file already exists or was just written: */
    165         if (fSuccess)
    166         {
    167             /* Warn the user about additions-image loaded and saved, propose to mount it: */
     166        /* Make sure target file doesn't exist: */
     167        bool fTargetFileExists = QFile::exists(target());
     168        if (fTargetFileExists)
     169        {
     170            /* We should ask user about file rewriting (or exit otherwise): */
     171            if (!msgCenter().confirmOverridingFile(QDir::toNativeSeparators(target())))
     172                break;
     173            /* And remove file if rewriting confirmed: */
     174            if (QFile::remove(target()))
     175                fTargetFileExists = false;
     176        }
     177
     178        /* Try to rename temporary file to target one (would fail if target file still exists): */
     179        const bool fFileRenamed = !fTargetFileExists && QFile::rename(strTempFilename, target());
     180
     181        /* If file renamed: */
     182        if (fFileRenamed)
     183        {
     184            /* Warn the user about additions-image downloaded and saved, propose to mount it (and/or exit in any case): */
    168185            if (msgCenter().proposeMountGuestAdditions(source().toString(), QDir::toNativeSeparators(target())))
    169186                emit sigDownloadFinished(target());
    170187            break;
    171188        }
    172 
    173         /* Warn the user about additions-image was downloaded but was NOT saved: */
    174         msgCenter().cannotSaveGuestAdditions(source().toString(), QDir::toNativeSeparators(target()));
    175 
    176         /* Ask the user for another location for the additions-image file: */
    177         QString strTarget = QIFileDialog::getExistingDirectory(QFileInfo(target()).absolutePath(),
    178                                                                windowManager().networkManagerOrMainWindowShown(),
    179                                                                tr("Select folder to save Guest Additions image to"), true);
    180 
    181         /* Check if user had really set a new target: */
    182         if (!strTarget.isNull())
    183             setTarget(QDir(strTarget).absoluteFilePath(QFileInfo(target()).fileName()));
    184189        else
    185             break;
    186     }
    187 }
    188 
     190        {
     191            /* Warn the user about additions-image was downloaded but was NOT saved: */
     192            msgCenter().cannotSaveGuestAdditions(source().toString(), QDir::toNativeSeparators(target()));
     193            /* Ask the user for another location for the additions-image file: */
     194            const QString strTarget = QIFileDialog::getExistingDirectory(QFileInfo(target()).absolutePath(),
     195                                                                         windowManager().networkManagerOrMainWindowShown(),
     196                                                                         tr("Select folder to save Guest Additions image to"), true);
     197
     198            /* Check if user had really set a new target (and exit in opposite case): */
     199            if (!strTarget.isNull())
     200                setTarget(QDir(strTarget).absoluteFilePath(QFileInfo(target()).fileName()));
     201            else
     202                break;
     203        }
     204    }
     205}
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette