Index: kmail/kmailicalifaceimpl.cpp
===================================================================
--- kmail/kmailicalifaceimpl.cpp	(wersja 799355)
+++ kmail/kmailicalifaceimpl.cpp	(kopia robocza)
@@ -467,6 +467,9 @@
     msg->cleanupHeader();
     //debugBodyParts( "after cleanup", *msg );
     msg->touch();
+#ifdef KMAIL_INDEX_DEBUG
+    kDebug() << "***<msg>***" << msg->asString() << "***</msg>***";
+#endif
     if ( folder.addMsg( msg ) == 0 )
       // Message stored
       sernum = msg->getMsgSerNum();
@@ -1734,6 +1737,9 @@
     mContacts = initFolder( KMail::ContentsTypeContact );
     mNotes    = initFolder( KMail::ContentsTypeNote );
 
+    if ( !mCalendar || !mTasks || !mJournals || !mContacts || !mNotes )
+      return;
+
     // Store final annotation (with .default) so that we won't ask again on next startup
     if ( mCalendar->folderType() == KMFolderTypeCachedImap )
       static_cast<KMFolderCachedImap *>( mCalendar->storage() )->updateAnnotationFolderType();
@@ -1932,7 +1938,11 @@
   folder->storage()->setContentsType( contentsType );
   folder->setSystemFolder( true );
   folder->storage()->writeConfig();
-  folder->open( "ifacefolder" );
+  if ( 0 != folder->open( "ifacefolder" ) ) {
+    kWarning() << "folder->open( \"ifacefolder\" ) ) FAILED";
+    // TODO: remove data structures?
+    return 0;
+  }
   connectFolder( folder );
   return folder;
 }
Index: kmail/kmfoldermaildir.cpp
===================================================================
--- kmail/kmfoldermaildir.cpp	(wersja 799355)
+++ kmail/kmfoldermaildir.cpp	(kopia robocza)
@@ -136,11 +136,15 @@
       emit statusMsg(str);
     } else {
       mIndexStream = KDE_fopen(QFile::encodeName(indexLocation()), "r+"); // index file
+#ifdef KMAIL_INDEX_DEBUG
+      kDebug() << "KDE_fopen(indexLocation()=" << indexLocation() << ", \"r+\") == mIndexStream == " << mIndexStream;
+#endif
       if ( mIndexStream ) {
 #ifndef Q_WS_WIN
         fcntl(fileno(mIndexStream), F_SETFD, FD_CLOEXEC);
 #endif
-        updateIndexStreamPtr();
+        if (!updateIndexStreamPtr())
+          return 1;
       }
     }
 
@@ -222,8 +226,13 @@
   {
     old_umask = umask(077);
     mIndexStream = KDE_fopen(QFile::encodeName(indexLocation()), "w+"); //sven; open RW
-    updateIndexStreamPtr(true);
+#ifdef KMAIL_INDEX_DEBUG
+    kDebug() << "KDE_fopen(indexLocation()=" << indexLocation() << ", \"w+\") == mIndexStream == " << mIndexStream;
+#endif
+    const bool updateIndexStreamPtrResult = updateIndexStreamPtr(true);
     umask(old_umask);
+    if ( !updateIndexStreamPtrResult )
+      return 1;
 
     if (!mIndexStream) return errno;
 #ifndef Q_WS_WIN
@@ -270,6 +279,9 @@
 
     if (mIndexStream) {
 	fclose(mIndexStream);
+#ifdef KMAIL_INDEX_DEBUG
+      kDebug() << "fclose(mIndexStream = " << mIndexStream << ")";
+#endif
 	updateIndexStreamPtr(true);
     }
 
@@ -589,11 +601,17 @@
   if (fi.exists() && fi.isFile() && fi.isWritable() && fi.size() > 0)
   {
     FILE* stream = KDE_fopen(QFile::encodeName(abs_file), "r+");
+#ifdef KMAIL_INDEX_DEBUG
+    kDebug() << "KDE_fopen(abs_file=" << abs_file << ", \"r+\") == stream == " << stream;
+#endif
     if (stream) {
       size_t msgSize = fi.size();
       char* msgText = new char[ msgSize + 1 ];
       fread(msgText, msgSize, 1, stream);
       fclose( stream );
+#ifdef KMAIL_INDEX_DEBUG
+      kDebug() << "fclose(mIndexStream = " << stream << ")";
+#endif
       msgText[msgSize] = '\0';
       size_t newMsgSize = KMail::Util::crlf2lf( msgText, msgSize );
       DwString str;
Index: kmail/kmkernel.cpp
===================================================================
--- kmail/kmkernel.cpp	(wersja 799355)
+++ kmail/kmkernel.cpp	(kopia robocza)
@@ -2135,16 +2135,21 @@
   KConfigGroup generalGroup( config(), "General" );
 
   if ( generalGroup.readEntry( "auto-expiring", true ) ) {
-    the_folderMgr->expireAllFolders( false /*scheduled, not immediate*/ );
-    the_imapFolderMgr->expireAllFolders( false /*scheduled, not immediate*/ );
-    the_dimapFolderMgr->expireAllFolders( false /*scheduled, not immediate*/ );
+    if ( the_folderMgr )
+      the_folderMgr->expireAllFolders( false /*scheduled, not immediate*/ );
+    if ( the_imapFolderMgr )
+      the_imapFolderMgr->expireAllFolders( false /*scheduled, not immediate*/ );
+    if ( the_dimapFolderMgr )
+      the_dimapFolderMgr->expireAllFolders( false /*scheduled, not immediate*/ );
     // the_searchFolderMgr: no expiry there
   }
 
   if ( generalGroup.readEntry( "auto-compaction", true ) ) {
-    the_folderMgr->compactAllFolders( false /*scheduled, not immediate*/ );
+    if ( the_folderMgr )
+      the_folderMgr->compactAllFolders( false /*scheduled, not immediate*/ );
     // the_imapFolderMgr: no compaction
-    the_dimapFolderMgr->compactAllFolders( false /*scheduled, not immediate*/ );
+    if ( the_dimapFolderMgr )
+      the_dimapFolderMgr->compactAllFolders( false /*scheduled, not immediate*/ );
     // the_searchFolderMgr: no compaction
   }
 
Index: kmail/kmfoldermbox.cpp
===================================================================
--- kmail/kmfoldermbox.cpp	(wersja 799355)
+++ kmail/kmfoldermbox.cpp	(kopia robocza)
@@ -183,7 +183,8 @@
 #ifndef Q_WS_WIN
          fcntl( fileno( mIndexStream ), F_SETFD, FD_CLOEXEC );
 #endif
-         updateIndexStreamPtr();
+         if ( !updateIndexStreamPtr() )
+           return 1;
        }
      }
 
@@ -254,8 +255,10 @@
   {
     old_umask = umask(077);
     mIndexStream = KDE_fopen(QFile::encodeName(indexLocation()), "w+"); //sven; open RW
-    updateIndexStreamPtr(true);
+    const bool updateIndexStreamPtrResult = updateIndexStreamPtr(true);
     umask(old_umask);
+    if ( !updateIndexStreamPtrResult )
+      return 1;
 
     if (!mIndexStream) return errno;
 #ifndef Q_WS_WIN
Index: kmail/kmfolderindex.cpp
===================================================================
--- kmail/kmfolderindex.cpp	(wersja 799355)
+++ kmail/kmfolderindex.cpp	(kopia robocza)
@@ -134,7 +134,11 @@
 
   indexName = indexLocation();
   tempName = indexName + ".temp";
-  unlink( QFile::encodeName( tempName ) );
+  {
+    int result = unlink( QFile::encodeName( tempName ) );
+    if ( ! ( result == 0 || (result == -1 && errno == ENOENT ) ) )
+      return errno;
+  }
 
   // We touch the folder, otherwise the index is regenerated, if KMail is
   // running, while the clock switches from daylight savings time to normal time
@@ -142,6 +146,9 @@
 
   old_umask = umask( 077 );
   FILE *tmpIndexStream = KDE_fopen( QFile::encodeName( tempName ), "w" );
+#ifdef KMAIL_INDEX_DEBUG
+  kDebug() << "KDE_fopen(tempName=" << tempName << ", \"w\") == tmpIndexStream == " << tmpIndexStream;
+#endif
   umask( old_umask );
   if ( !tmpIndexStream ) {
     return errno;
@@ -183,20 +190,38 @@
   int fError = ferror( tmpIndexStream );
   if( fError != 0 ) {
     fclose( tmpIndexStream );
+#ifdef KMAIL_INDEX_DEBUG
+    kDebug() << "fclose(tmpIndexStream = " << tmpIndexStream << ")";
+#endif
     return fError;
   }
   if(    ( fflush( tmpIndexStream ) != 0 )
       || ( fsync( fileno( tmpIndexStream ) ) != 0 ) ) {
     int errNo = errno;
     fclose( tmpIndexStream );
+    kWarning() << "fflush() or fsync() failed; fclose(tmpIndexStream = " << tmpIndexStream << ")";
     return errNo;
   }
-  if( fclose( tmpIndexStream ) != 0 )
+#ifdef KMAIL_INDEX_DEBUG
+  kDebug() << "fclose(tmpIndexStream = " << tmpIndexStream << ")";
+#endif
+  if( fclose( tmpIndexStream ) != 0 ) {
+    kWarning() << "fclose() failed";
     return errno;
+  }
 
 #ifdef Q_WS_WIN
-  if (mIndexStream) // close before renaming
-    fclose(mIndexStream);
+  if (mIndexStream) { // close before renaming
+    bool ok = fclose( mIndexStream ) == 0;
+# ifdef KMAIL_INDEX_DEBUG
+    kDebug() << "fclose(mIndexStream = " << mIndexStream << ")";
+# endif
+    mIndexStream = 0;
+    if ( !ok ) {
+      kWarning() << "fclose() failed";
+      return errno;
+    }
+  }
 #endif
 
   if ( KDE_rename(QFile::encodeName(tempName), QFile::encodeName(indexName)) != 0 )
@@ -204,23 +229,31 @@
   mHeaderOffset = nho;
 
 #ifndef Q_WS_WIN
-  if (mIndexStream)
+  if (mIndexStream) {
       fclose(mIndexStream);
+# ifdef KMAIL_INDEX_DEBUG
+      kDebug() << "fclose(mIndexStream = " << mIndexStream << ")";
+# endif
+  }
 #endif
 
   if ( createEmptyIndex )
     return 0;
 
   mIndexStream = KDE_fopen(QFile::encodeName(indexName), "r+"); // index file
+#ifdef KMAIL_INDEX_DEBUG
+  kDebug() << "KDE_fopen(indexName=" << indexName << ", \"r+\") == mIndexStream == " << mIndexStream;
+#endif
   assert( mIndexStream );
 #ifndef Q_WS_WIN
   fcntl(fileno(mIndexStream), F_SETFD, FD_CLOEXEC);
 #endif
 
-  updateIndexStreamPtr();
+  if ( !updateIndexStreamPtr() )
+    return 1;
+  if ( 0 != writeFolderIdsFile() )
+    return 1;
 
-  writeFolderIdsFile();
-
   setDirty( false );
   return 0;
 }
@@ -268,6 +301,9 @@
       if (feof(mIndexStream)) break;
       if (*line.data() == '\0') {
 	  fclose(mIndexStream);
+#ifdef KMAIL_INDEX_DEBUG
+    kDebug() << "fclose(mIndexStream = " << mIndexStream << ")";
+#endif
 	  mIndexStream = 0;
 	  clearIndex();
 	  return false;
@@ -410,18 +446,22 @@
 {
     // We touch the folder, otherwise the index is regenerated, if KMail is
     // running, while the clock switches from daylight savings time to normal time
-    utime(QFile::encodeName( QDir::toNativeSeparators(location()) ), 0);
-    utime(QFile::encodeName( QDir::toNativeSeparators(indexLocation()) ), 0);
-    utime(QFile::encodeName( QDir::toNativeSeparators(KMMsgDict::getFolderIdsLocation( *this )) ), 0);
+    if (0 != utime(QFile::encodeName( QDir::toNativeSeparators(location()) ), 0))
+      kWarning() << "utime(" << QDir::toNativeSeparators(location()) << ", 0) failed (location())";
+    if (0 != utime(QFile::encodeName( QDir::toNativeSeparators(indexLocation()) ), 0))
+      kWarning() << "utime(" << QDir::toNativeSeparators(indexLocation()) << ", 0) failed (indexLocation())";
+    if (0 != utime(QFile::encodeName( QDir::toNativeSeparators(KMMsgDict::getFolderIdsLocation( *this )) ), 0))
+      kWarning() << "utime(" << QDir::toNativeSeparators(KMMsgDict::getFolderIdsLocation( *this )) << ", 0) failed (KMMsgDict::getFolderIdsLocation( *this ))";
 
   mIndexSwapByteOrder = false;
 #ifdef HAVE_MMAP
     if(just_close) {
+      bool munmapResult = true;
 	if(mIndexStreamPtr)
-	    munmap((char *)mIndexStreamPtr, mIndexStreamPtrLength);
+	     munmapResult = 0 == munmap((char *)mIndexStreamPtr, mIndexStreamPtrLength);
 	mIndexStreamPtr = 0;
 	mIndexStreamPtrLength = 0;
-	return true;
+	return munmapResult;
     }
 
     assert(mIndexStream);
@@ -434,7 +474,8 @@
 	return false;
     }
     if(mIndexStreamPtr)
-	munmap((char *)mIndexStreamPtr, mIndexStreamPtrLength);
+      if(0 != munmap((char *)mIndexStreamPtr, mIndexStreamPtrLength))
+        return false;
     mIndexStreamPtrLength = stat_buf.st_size;
     mIndexStreamPtr = (uchar *)mmap(0, mIndexStreamPtrLength, PROT_READ, MAP_SHARED,
 				    fileno(mIndexStream), 0);
Index: kmail/CMakeLists.txt
===================================================================
--- kmail/CMakeLists.txt	(wersja 799355)
+++ kmail/CMakeLists.txt	(kopia robocza)
@@ -23,6 +23,7 @@
 
 add_definitions (-DQT3_SUPPORT -DQT3_SUPPORT_WARNINGS)
 add_definitions ( -DKDE_DEFAULT_DEBUG_AREA=5006 )
+add_definitions (-DKMAIL_INDEX_DEBUG)
 
 qt4_generate_dbus_interface( kmkernel.h org.kde.kmail.kmail.xml OPTIONS -a )
 qt4_generate_dbus_interface( kmcomposewin.h org.kde.kmail.mailcomposer.xml OPTIONS -a )
