From 805c497d60490be80492650b7ce8746d4e2daffd Mon Sep 17 00:00:00 2001
From: rswindell <>
Date: Mon, 30 Apr 2018 23:02:14 +0000
Subject: [PATCH] maint(): don't truncate the .sid and .hash files to 0-bytes
 before writing their new contents, Instead truncate to the new length after
 writing the new contents. This should make the files more tolerant of "out of
 disk space" situations and reduce the risk of lost data (0-byte files) due to
 low/no free disk space.

---
 src/sbbs3/smbutil.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/src/sbbs3/smbutil.c b/src/sbbs3/smbutil.c
index 199fa4b380..0196381b90 100644
--- a/src/sbbs3/smbutil.c
+++ b/src/sbbs3/smbutil.c
@@ -708,9 +708,10 @@ void maint(void)
 				hash_t*	hashes = malloc(max_hashes * SMB_HASH_SOURCE_TYPES * sizeof(hash_t));
 				if(hashes != NULL) {
 					if(fread(hashes, sizeof(hash_t), max_hashes * SMB_HASH_SOURCE_TYPES, smb.hash_fp) == max_hashes * SMB_HASH_SOURCE_TYPES) {
-						CHSIZE_FP(smb.hash_fp,0);
 						rewind(smb.hash_fp);
 						fwrite(hashes, sizeof(hash_t), max_hashes * SMB_HASH_SOURCE_TYPES, smb.hash_fp);
+						fflush(smb.hash_fp);
+						CHSIZE_FP(smb.hash_fp, sizeof(hash_t) * max_hashes * SMB_HASH_SOURCE_TYPES);
 					}
 					free(hashes);
 				}
@@ -860,15 +861,16 @@ void maint(void)
 
 	printf("Re-writing index...\n");
 	rewind(smb.sid_fp);
-	CHSIZE_FP(smb.sid_fp,0);
 	for(m=n=0;m<l;m++) {
 		if(idx[m].attr&MSG_DELETE)
 			continue;
-		printf("%lu of %lu\r",++n,l-flagged);
+		n++;
+		printf("%lu of %lu\r", n, l-flagged);
 		fwrite(&idx[m],sizeof(idxrec_t),1,smb.sid_fp); 
 	}
-	printf("\nDone.\n\n");
 	fflush(smb.sid_fp);
+	CHSIZE_FP(smb.sid_fp, n * sizeof(idxrec_t));
+	printf("\nDone.\n\n");
 
 	free(idx);
 	smb.status.total_msgs-=flagged;
-- 
GitLab