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