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