Ticket #8094: file_preallocation_for_windows.patch

File file_preallocation_for_windows.patch, 2.6 KB (added by Underground78, 5 years ago)

Patch: Preallocate files on Windows (v1b)

  • src/engine/ftpcontrolsocket.cpp

     
    26332633
    26342634                if (pData->resume)
    26352635                {
    2636                     if (!pFile->Open(pData->localFile, wxFile::write_append))
     2636                    // Open the file in read_write mode since we don't want to truncate the file
     2637                    // nor to always write at the end of the file due to possible preallocation
     2638                    if (!pFile->Open(pData->localFile, wxFile::read_write))
    26372639                    {
    26382640                        delete pFile;
    26392641                        LogMessage(MessageType::Error, _("Failed to open \"%s\" for appending/writing"), pData->localFile);
     
    26922694                    pData->resumeOffset = 0;
    26932695
    26942696                InitTransferStatus(pData->remoteFileSize, startOffset, false);
     2697
     2698#ifdef __WXMSW__
     2699                // Try to preallocate the file on Windows in order to reduce fragmentation
     2700                wxFileOffset sizeToPreallocate = pData->remoteFileSize - startOffset;
     2701                if (sizeToPreallocate > 0)
     2702                {
     2703                    LogMessage(MessageType::Debug_Info, _T("Preallocating %") wxFileOffsetFmtSpec _T("d bytes for the file \"%s\""), sizeToPreallocate, pData->localFile);
     2704                    wxFileOffset oldPos = pFile->Tell();
     2705                    if (pFile->SeekEnd(sizeToPreallocate) == pData->remoteFileSize)
     2706                    {
     2707                        if (!SetEndOfFile((HANDLE)_get_osfhandle(pFile->fd())))
     2708                            LogMessage(MessageType::Debug_Warning, _T("Impossible to preallocate the file"));
     2709                    }
     2710                    pFile->Seek(oldPos);
     2711                }
     2712#endif
    26952713            }
    26962714            else
    26972715            {
  • src/engine/iothread.cpp

     
    4141
    4242CIOThread::~CIOThread()
    4343{
    44     delete m_pFile;
     44    if (m_pFile)
     45    {
     46#ifdef __WXMSW__
     47        // The file might have been preallocated and the transfer stopped before being completed
     48        // so always truncate the file to the actually written size before closing it.
     49        if (!m_read)
     50            SetEndOfFile((HANDLE)_get_osfhandle(m_pFile->fd()));
     51#endif
     52        delete m_pFile;
     53    }
    4554
    4655    for (unsigned int i = 0; i < BUFFERCOUNT; i++)
    4756        delete [] m_buffers[i];
     
    5261bool CIOThread::Create(wxFile* pFile, bool read, bool binary)
    5362{
    5463    wxASSERT(pFile);
    55     delete m_pFile;
     64
     65    if (m_pFile)
     66    {
     67#ifdef __WXMSW__
     68        // The file might have been preallocated and the transfer stopped before being completed
     69        // so always truncate the file to the actually written size before closing it.
     70        if (!m_read)
     71            SetEndOfFile((HANDLE)_get_osfhandle(m_pFile->fd()));
     72#endif
     73        delete m_pFile;
     74    }
     75
    5676    m_pFile = pFile;
    5777    m_read = read;
    5878    m_binary = binary;