From 45b128c0e800faaf0a670d19ebb2cba8a5b3b053 Mon Sep 17 00:00:00 2001
From: "Rob Swindell (on Windows)" <rob@synchro.net>
Date: Thu, 2 Feb 2023 17:47:03 -0800
Subject: [PATCH] Fix uploader-notification, credit awards, download-counters
 in FTP downloads

Since v3.19 (the new filebases), when a user FTP-downloaded a file, we failed
to properly find/load that file's record from the filebase (searching for the
file's full path, rather than just the filename), so the code the increments
the file's download counter, notifies the uploader, awards credits, etc. did
not ever execute. This means that FTP-downloads for all files downloaded via
FTP were effectively "free" (and nobody noticed). No error was logged either.

I discovered this while debugging the case of "(null)" filenames in the
action/download MQTT topic messages being published by the FTP server. So
that issue is fixed as part of this commit as well.

Oh, and if this code had executed before, it would have memory-leaked the
file information, so that's fixed too (added call to smb_freefilemem). Ugh.
---
 src/sbbs3/ftpsrvr.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/src/sbbs3/ftpsrvr.c b/src/sbbs3/ftpsrvr.c
index 83ec882c3a..a8d1331915 100644
--- a/src/sbbs3/ftpsrvr.c
+++ b/src/sbbs3/ftpsrvr.c
@@ -766,7 +766,12 @@ static void send_thread(void* arg)
 
 		if(xfer.dir>=0) {
 			memset(&f,0,sizeof(f));
-			if(loadfile(&scfg, xfer.dir, xfer.filename, &f, file_detail_normal) == TRUE) {
+			if(!loadfile(&scfg, xfer.dir, getfname(xfer.filename), &f, file_detail_normal)) {
+				lprintf(LOG_ERR, "%04d <%s> DATA downloaded: %s (not found in filebase!)"
+					,xfer.ctrl_sock
+					,xfer.user->alias
+					,xfer.filename);
+			} else {
 				f.hdr.times_downloaded++;
 				f.hdr.last_downloaded = time32(NULL);
 				updatefile(&scfg, &f);
@@ -823,12 +828,11 @@ static void send_thread(void* arg)
 						putsmsg(&scfg,uploader.number,str);
 					}
 				}
+				mqtt_file_download(&mqtt, xfer.user, &f, total, xfer.client);
+				smb_freefilemem(&f);
 			}
 			if(!xfer.tmpfile && !xfer.delfile && !(scfg.dir[f.dir]->misc&DIR_NOSTAT))
 				inc_download_stats(&scfg, 1, (ulong)total);
-
-			if(!xfer.tmpfile)
-				mqtt_file_download(&mqtt, xfer.user, &f, total, xfer.client);
 		}
 
 		if(xfer.credits) {
-- 
GitLab