766047
--- procmail-3.22/src/mailfold.c.truncate	2007-03-27 13:24:05.000000000 +0200
766047
+++ procmail-3.22/src/mailfold.c	2007-03-27 13:25:06.000000000 +0200
766047
@@ -30,6 +30,7 @@
766047
 
766047
 int logopened,rawnonl;
766047
 off_t lasttell;
766047
+static int trunced;
766047
 static long lastdump;
766047
 static volatile int mailread;	/* if the mail is completely read in already */
766047
 static struct dyna_array confield;		  /* escapes, concatenations */
766047
@@ -81,6 +82,7 @@
766047
  long len;
766047
 { int i;long part;
766047
   lasttell=i= -1;SETerrno(EBADF);
766047
+  trunced=0;
766047
   if(s>=0)
766047
    { if(ft_lock(type)&&(lseek(s,(off_t)0,SEEK_END),fdlock(s)))
766047
 	nlog("Kernel-lock failed\n");
766047
@@ -120,13 +122,18 @@
766047
       }
766047
 writefin:
766047
      i=type!=ft_PIPE&&fsync(s)&&errno!=EINVAL;	  /* EINVAL => wasn't a file */
766047
+     if ((i||len)&&lasttell>=0)
766047
+      { int serrno=errno;
766047
+	if(!ftruncate(s,lasttell)) trunced=1;
766047
+	SETerrno(serrno);
766047
+      }
766047
      if(ft_lock(type))
766047
       { int serrno=errno;		       /* save any error information */
766047
 	if(fdunlock())
766047
 	   nlog("Kernel-unlock failed\n");
766047
 	SETerrno(serrno);
766047
       }
766047
-     i=rclose(s)||i;
766047
+     i=rclose(s)||i; /* if this fails, we should truncate, but it's too late */
766047
    }			   /* return an error even if nothing was to be sent */
766047
   return i&&!len?-1:len;
766047
 }
766047
@@ -237,7 +244,7 @@
766047
 #endif
766047
 	      default:writeerr(buf);
766047
 	    }
766047
-	   if(lasttell>=0&&!truncate(boxname,lasttell)&&(logopened||verbose))
766047
+	   if(lasttell>=0&&trunced&&(logopened||verbose))
766047
 	      nlog("Truncated file to former size\n");	    /* undo garbage */
766047
 ret0:	   return 0;
766047
 	 }