<commit_before>/**
 * Copyright (c) 2004 <NAME> <<EMAIL>>
 *
 *  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; version 2 of the License
 *
 *  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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 *  In addition, as a special exception, the copyright holders give
 *  permission to link the code of this program with any edition of
 *  the Qt library by Trolltech AS, Norway (or with modified versions
 *  of Qt that use the same license as Qt), and distribute linked
 *  combinations including the two.  You must obey the GNU General
 *  Public License in all respects for all of the code used other than
 *  Qt.  If you modify this file, you may extend this exception to
 *  your version of the file, but you are not obligated to do so.  If
 *  you do not wish to do so, delete this exception statement from
 *  your version.
 */
#include "compactionjob.h"
#include "kmfolder.h"
#include "broadcaststatus.h"
using KPIM::BroadcastStatus;
#include "kmfoldermbox.h"
#include "kmfoldermaildir.h"

#include <kdebug.h>
#include <klocale.h>

#include <qfile.h>
#include <qfileinfo.h>
#include <qdir.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>

using namespace KMail;

// Look at this number of messages in each slotDoWork call
#define COMPACTIONJOB_NRMESSAGES 100
// And wait this number of milliseconds before calling it again
#define COMPACTIONJOB_TIMERINTERVAL 100

MboxCompactionJob::MboxCompactionJob( KMFolder* folder, bool immediate )
 : ScheduledJob( folder, immediate ), mTimer( this ), mTmpFile( 0 ),
   mCurrentIndex( 0 ), mFolderOpen( false ), mSilent( false )
{
}

MboxCompactionJob::~MboxCompactionJob()
{
}

void MboxCompactionJob::kill()
{
  Q_ASSERT( mCancellable );
  // We must close the folder if we opened it and got interrupted
  if ( mFolderOpen && mSrcFolder && mSrcFolder->storage() )
    mSrcFolder->storage()->close();

  if ( mTmpFile )
    fclose( mTmpFile );
  mTmpFile = 0;
  if ( !mTempName.isEmpty() )
    QFile::remove( mTempName );
  FolderJob::kill();
}

QString MboxCompactionJob::realLocation() const
{
  QString location = mSrcFolder->location();
  QFileInfo inf( location );
  if (inf.isSymLink()) {
    KURL u; u.setPath( location );
    // follow (and resolve) symlinks so that the final ::rename() always works
    // KURL gives us support for absolute and relative links transparently.
    return KURL( u, inf.readLink() ).path();
  }
  return location;
}

int MboxCompactionJob::executeNow( bool silent )
{
  mSilent = silent;
  FolderStorage* storage = mSrcFolder->storage();
  KMFolderMbox* mbox = static_cast<KMFolderMbox *>( storage );
  if (!storage->compactable()) {
    kdDebug(5006) << storage->location() << " compaction skipped." << endl;
    if ( !mSilent ) {
      QString str = i18n( "For safety reasons, compaction has been disabled for %1" ).arg( mbox->label() );
      BroadcastStatus::instance()->setStatusMsg( str );
    }
    return 0;
  }
  kdDebug(5006) << "Compacting " << mSrcFolder->idString() << endl;

  if (KMFolderIndex::IndexOk != mbox->indexStatus()) {
      kdDebug(5006) << "Critical error: " << storage->location() <<
          " has been modified by an external application while KMail was running." << endl;
      //      exit(1); backed out due to broken nfs
  }

  mTempName = realLocation() + ".compacted";

  mode_t old_umask = umask(077);
  mTmpFile = fopen(QFile::encodeName(mTempName), "w");
  umask(old_umask);
  if (!mTmpFile) {
    kdWarning(5006) << "Couldn't start compacting " << mSrcFolder->label()
                    << " : " << strerror( errno )
                    << " while creating " << mTempName << endl;
    return errno;
  }
  mOpeningFolder = true; // Ignore open-notifications while opening the folder
  storage->open();
  mOpeningFolder = false;
  mFolderOpen = true;
  mOffset = 0;
  mCurrentIndex = 0;

  kdDebug(5006) << "MboxCompactionJob: starting to compact in folder " << mSrcFolder->location() << endl;
  connect( &mTimer, SIGNAL( timeout() ), SLOT( slotDoWork() ) );
  if ( !mImmediate )
    mTimer.start( COMPACTIONJOB_TIMERINTERVAL );
  slotDoWork();
  return mErrorCode;
}

void MboxCompactionJob::slotDoWork()
{
  // No need to worry about mSrcFolder==0 here. The FolderStorage deletes the jobs on destruction.
  KMFolderMbox* mbox = static_cast<KMFolderMbox *>( mSrcFolder->storage() );
  bool bDone = false;
  int nbMessages = mImmediate ? -1 /*all*/ : COMPACTIONJOB_NRMESSAGES;
  int rc = mbox->compact( mCurrentIndex, nbMessages,
                          mTmpFile, mOffset /*in-out*/, bDone /*out*/ );
  if ( !mImmediate )
    mCurrentIndex += COMPACTIONJOB_NRMESSAGES;
  if ( rc || bDone ) // error, or finished
    done( rc );
}

void MboxCompactionJob::done( int rc )
{
  mTimer.stop();
  mCancellable = false;
  KMFolderMbox* mbox = static_cast<KMFolderMbox *>( mSrcFolder->storage() );
  if (!rc)
      rc = fflush(mTmpFile);
  if (!rc)
      rc = fsync(fileno(mTmpFile));
  rc |= fclose(mTmpFile);
  QString str;
  if (!rc) {
    bool autoCreate = mbox->autoCreateIndex();
    QString box( realLocation() );
    ::rename(QFile::encodeName(mTempName), QFile::encodeName(box));
    mbox->writeIndex();
    mbox->writeConfig();
    mbox->setAutoCreateIndex( false );
    mbox->close(true);
    mbox->setAutoCreateIndex( autoCreate );
    mbox->setNeedsCompacting( false );            // We are clean now
    str = i18n( "Folder \"%1\" successfully compacted" ).arg( mSrcFolder->label() );
    kdDebug(5006) << str << endl;
  } else {
    mbox->close();
    str = i18n( "Error occurred while compacting \"%1\". Compaction aborted." ).arg( mSrcFolder->label() );
    kdDebug(5006) << "Error occurred while compacting " << mbox->location() << endl;
    kdDebug(5006) << "Compaction aborted." << endl;
  }
  mErrorCode = rc;

  if ( !mSilent )
    BroadcastStatus::instance()->setStatusMsg( str );

  mFolderOpen = false;
  deleteLater(); // later, because of the "return mErrorCode"
}

////

MaildirCompactionJob::MaildirCompactionJob( KMFolder* folder, bool immediate )
 : ScheduledJob( folder, immediate ), mTimer( this ),
   mCurrentIndex( 0 ), mFolderOpen( false ), mSilent( false )
{
}<commit_msg>Don't leave around mymbox.compacted when compaction fails (e.g. disk full), otherwise kmail treats it as a real mailbox on the next restart.<commit_after>/**
 * Copyright (c) 2004 <NAME> <<EMAIL>>
 *
 *  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; version 2 of the License
 *
 *  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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 *  In addition, as a special exception, the copyright holders give
 *  permission to link the code of this program with any edition of
 *  the Qt library by Trolltech AS, Norway (or with modified versions
 *  of Qt that use the same license as Qt), and distribute linked
 *  combinations including the two.  You must obey the GNU General
 *  Public License in all respects for all of the code used other than
 *  Qt.  If you modify this file, you may extend this exception to
 *  your version of the file, but you are not obligated to do so.  If
 *  you do not wish to do so, delete this exception statement from
 *  your version.
 */
#include "compactionjob.h"
#include "kmfolder.h"
#include "broadcaststatus.h"
using KPIM::BroadcastStatus;
#include "kmfoldermbox.h"
#include "kmfoldermaildir.h"

#include <kdebug.h>
#include <klocale.h>

#include <qfile.h>
#include <qfileinfo.h>
#include <qdir.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>

using namespace KMail;

// Look at this number of messages in each slotDoWork call
#define COMPACTIONJOB_NRMESSAGES 100
// And wait this number of milliseconds before calling it again
#define COMPACTIONJOB_TIMERINTERVAL 100

MboxCompactionJob::MboxCompactionJob( KMFolder* folder, bool immediate )
 : ScheduledJob( folder, immediate ), mTimer( this ), mTmpFile( 0 ),
   mCurrentIndex( 0 ), mFolderOpen( false ), mSilent( false )
{
}

MboxCompactionJob::~MboxCompactionJob()
{
}

void MboxCompactionJob::kill()
{
  Q_ASSERT( mCancellable );
  // We must close the folder if we opened it and got interrupted
  if ( mFolderOpen && mSrcFolder && mSrcFolder->storage() )
    mSrcFolder->storage()->close();

  if ( mTmpFile )
    fclose( mTmpFile );
  mTmpFile = 0;
  if ( !mTempName.isEmpty() )
    QFile::remove( mTempName );
  FolderJob::kill();
}

QString MboxCompactionJob::realLocation() const
{
  QString location = mSrcFolder->location();
  QFileInfo inf( location );
  if (inf.isSymLink()) {
    KURL u; u.setPath( location );
    // follow (and resolve) symlinks so that the final ::rename() always works
    // KURL gives us support for absolute and relative links transparently.
    return KURL( u, inf.readLink() ).path();
  }
  return location;
}

int MboxCompactionJob::executeNow( bool silent )
{
  mSilent = silent;
  FolderStorage* storage = mSrcFolder->storage();
  KMFolderMbox* mbox = static_cast<KMFolderMbox *>( storage );
  if (!storage->compactable()) {
    kdDebug(5006) << storage->location() << " compaction skipped." << endl;
    if ( !mSilent ) {
      QString str = i18n( "For safety reasons, compaction has been disabled for %1" ).arg( mbox->label() );
      BroadcastStatus::instance()->setStatusMsg( str );
    }
    return 0;
  }
  kdDebug(5006) << "Compacting " << mSrcFolder->idString() << endl;

  if (KMFolderIndex::IndexOk != mbox->indexStatus()) {
      kdDebug(5006) << "Critical error: " << storage->location() <<
          " has been modified by an external application while KMail was running." << endl;
      //      exit(1); backed out due to broken nfs
  }

  mTempName = realLocation() + ".compacted";

  mode_t old_umask = umask(077);
  mTmpFile = fopen(QFile::encodeName(mTempName), "w");
  umask(old_umask);
  if (!mTmpFile) {
    kdWarning(5006) << "Couldn't start compacting " << mSrcFolder->label()
                    << " : " << strerror( errno )
                    << " while creating " << mTempName << endl;
    return errno;
  }
  mOpeningFolder = true; // Ignore open-notifications while opening the folder
  storage->open();
  mOpeningFolder = false;
  mFolderOpen = true;
  mOffset = 0;
  mCurrentIndex = 0;

  kdDebug(5006) << "MboxCompactionJob: starting to compact in folder " << mSrcFolder->location() << endl;
  connect( &mTimer, SIGNAL( timeout() ), SLOT( slotDoWork() ) );
  if ( !mImmediate )
    mTimer.start( COMPACTIONJOB_TIMERINTERVAL );
  slotDoWork();
  return mErrorCode;
}

void MboxCompactionJob::slotDoWork()
{
  // No need to worry about mSrcFolder==0 here. The FolderStorage deletes the jobs on destruction.
  KMFolderMbox* mbox = static_cast<KMFolderMbox *>( mSrcFolder->storage() );
  bool bDone = false;
  int nbMessages = mImmediate ? -1 /*all*/ : COMPACTIONJOB_NRMESSAGES;
  int rc = mbox->compact( mCurrentIndex, nbMessages,
                          mTmpFile, mOffset /*in-out*/, bDone /*out*/ );
  if ( !mImmediate )
    mCurrentIndex += COMPACTIONJOB_NRMESSAGES;
  if ( rc || bDone ) // error, or finished
    done( rc );
}

void MboxCompactionJob::done( int rc )
{
  mTimer.stop();
  mCancellable = false;
  KMFolderMbox* mbox = static_cast<KMFolderMbox *>( mSrcFolder->storage() );
  if (!rc)
      rc = fflush(mTmpFile);
  if (!rc)
      rc = fsync(fileno(mTmpFile));
  rc |= fclose(mTmpFile);
  QString str;
  if (!rc) {
    bool autoCreate = mbox->autoCreateIndex();
    QString box( realLocation() );
    ::rename(QFile::encodeName(mTempName), QFile::encodeName(box));
    mbox->writeIndex();
    mbox->writeConfig();
    mbox->setAutoCreateIndex( false );
    mbox->close(true);
    mbox->setAutoCreateIndex( autoCreate );
    mbox->setNeedsCompacting( false );            // We are clean now
    str = i18n( "Folder \"%1\" successfully compacted" ).arg( mSrcFolder->label() );
    kdDebug(5006) << str << endl;
  } else {
    mbox->close();
    str = i18n( "Error occurred while compacting \"%1\". Compaction aborted." ).arg( mSrcFolder->label() );
    kdDebug(5006) << "Error occurred while compacting " << mbox->location() << endl;
    kdDebug(5006) << "Compaction aborted." << endl;
    QFile::remove( mTempName );
  }
  mErrorCode = rc;

  if ( !mSilent )
    BroadcastStatus::instance()->setStatusMsg( str );

  mFolderOpen = false;
  deleteLater(); // later, because of the "return mErrorCode"
}

////

MaildirCompactionJob::MaildirCompactionJob( KMFolder* folder, bool immediate )
 : ScheduledJob( folder, immediate ), mTimer( this ),
   mCurrentIndex( 0 ), mFolderOpen( false ), mSilent( false )
{
}
{'fraction_non_alphanumeric': 0.12388269188150994, 'fraction_numerical': 0.011671714560094556, 'mean_word_length': 3.653832932279134, 'pattern_counts': {'":': 0, '<': 139, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1169227', 'n_tokens_mistral': 4436, 'n_tokens_neox': 4276, 'n_words': 1357}