PATCH: hisv6_replace() fix

Chris Caputo ccaputo at alt.net
Mon Jun 6 23:28:30 UTC 2005


On Mon, 6 Jun 2005, Chris Caputo wrote:
> Oops.  Hold off on this patch.  Found another issue and will have better
> patch shortly.

Ok.  Here's the better patch.

This patch is needed to make prunehistory work.

The change from using "a" to using "r+" for the history file Fopen() is
because pwrite() will not be able to update to the absolute file location
if a file is opened in append mode.  "r+" enables full read/write.

What was happening prior to this patch with prunehistory is a corrupted
line was being written to the end of the history file, rather than a clean
line to the correct location.

At http://www.caputo.com/foss/inn-2.4.2-hisv6_replacefix.patch or below...

Thanks,
Chris

---

--- inn-2.4.2-stock/history/hisv6/hisv6.c	2004-12-22 04:21:19.000000000 +0000
+++ inn-2.4.2/history/hisv6/hisv6.c	2005-06-06 23:18:54.471204948 +0000
@@ -287,7 +287,7 @@ hisv6_reopen(struct hisv6 *h)
 	if (h->flags & HIS_CREAT)
 	    mode = "w";
 	else
-	    mode = "a";
+	    mode = "r+";
 	if ((h->writefp = Fopen(h->histpath, mode, INND_HISTORY)) == NULL) {
 	    hisv6_seterror(h, concat("can't fopen history ",
 				      h->histpath, " ",
@@ -295,12 +295,6 @@ hisv6_reopen(struct hisv6 *h)
 	    hisv6_closefiles(h);
 	    goto fail;
 	}
-
-	/* fseeko to the end of file because the result of ftello() is
-	   undefined for files freopen()-ed in append mode according
-	   to POSIX 1003.1.  ftello() is used later on to determine a
-	   new article's offset in the history file. Fopen() uses
-	   freopen() internally. */
 	if (fseeko(h->writefp, 0, SEEK_END) == -1) {
 	    hisv6_seterror(h, concat("can't fseek to end of ",
 				      h->histpath, " ",
@@ -924,7 +918,9 @@ hisv6_replace(void *history, const char 
 
 	    oldlen = strlen(old);
 	    newlen = strlen(new);
-	    if (new > old) {
+	    if ('\n' == new[newlen - 1])
+	      newlen--;
+	    if (newlen > oldlen) {
 		hisv6_seterror(h, concat("new history line too long ",
 					  h->histpath, NULL));
 		r = false;
@@ -932,7 +928,7 @@ hisv6_replace(void *history, const char 
 		ssize_t n;
 
 		/* space fill any excess in the tail of new */
-		memset(new + oldlen, ' ', oldlen - newlen);
+		memset(new + newlen, ' ', oldlen - newlen);
 
 		do {
 		    n = pwrite(fileno(h->writefp), new, oldlen, offset);



More information about the inn-workers mailing list