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