changeset 13947:0f482af5b882

mod_http_file_share: Check for unexpected files in the upload directory In case the metadata is lost or corrupted it can be handy to know what files might have been left behind.
author Kim Alvefur <zash@zash.se>
date Sat, 04 Dec 2021 18:57:11 +0100
parents f5e8ab42c708
children fc489086e09a
files plugins/mod_http_file_share.lua
diffstat 1 files changed, 13 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/mod_http_file_share.lua	Sat Dec 04 18:56:51 2021 +0100
+++ b/plugins/mod_http_file_share.lua	Sat Dec 04 18:57:11 2021 +0100
@@ -18,6 +18,7 @@
 local dt = require "prosody.util.datetime";
 local hi = require "prosody.util.human.units";
 local cache = require "prosody.util.cache";
+local join_path = require "util.paths".join;
 local lfs = require "lfs";
 
 local unknown = math.abs(0/0);
@@ -568,11 +569,14 @@
 	local iter = assert(uploads:find(nil));
 
 	local count, sum = 0, 0;
+	local expected_filenames = {};
 	for slot_id, file in iter do
 		count = count + 1;
+
 		local expected_size = tonumber(file.attr.size);
 		local filename = get_filename(slot_id);
 		local actual_size, err = lfs.attributes(filename, "size");
+		expected_filenames[filename] = true;
 		if not actual_size then
 			module:log("warn", "Missing file for upload slot %s: %s", slot_id, err);
 			count = count - 1;
@@ -585,6 +589,15 @@
 		end
 	end
 
+	local upload_dir = dm.getpath("", module.host, module.name, ""):gsub("/[^/]*$", "");
+	for file in lfs.dir(upload_dir) do
+		local file_path = join_path(upload_dir, file);
+		if file ~= "." and file ~= ".." and not expected_filenames[file_path] then
+			local mode, err = lfs.attributes(file_path, "mode");
+			module:log("warn", "Unexpected %s in upload storage: %s", mode or "file", err or file);
+		end
+	end
+
 	module:log("info", "Uploaded files total: %s in %d files", B(sum), count);
 	if persist_stats:set(nil, "total", sum) then
 		total_storage_usage = sum;