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