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