Software /
code /
prosody
Comparison
plugins/mod_http_file_share.lua @ 11405:ce8291e89d67
mod_http_file_share: Remove correct entries when not all expired files were deleted
If any of the expired files could not be deleted then we should not
forget about that, we should complain loudly and try again.
The code got this backwards and would have removed only the entries
referring to still existing files.
Test procedure:
1. Upload a file
2. chown root:root http_file_share/
3. In uploads.list, decrease 'when' enough to ensure expiry
4. Reload mod_http_file_share
5. Should see an error in the logs about failure to delete the file
6. Should see that the metadata in uploads.list is still there
7. chown http_file_share/ back to the previous owner
8. Reload mod_http_file_share
9. Should see logs about successful removal of expired file
10. Should see that the metadata in uploads.list is gone
11. Should see that the file was deleted
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 25 Feb 2021 23:58:08 +0100 |
parent | 11402:a3be7b3cf1e1 |
child | 11406:9d6545a7d483 |
comparison
equal
deleted
inserted
replaced
11404:f7704f987439 | 11405:ce8291e89d67 |
---|---|
381 return; | 381 return; |
382 end | 382 end |
383 | 383 |
384 module:log("info", "Pruning expired files uploaded earlier than %s", dt.datetime(boundary_time)); | 384 module:log("info", "Pruning expired files uploaded earlier than %s", dt.datetime(boundary_time)); |
385 | 385 |
386 local obsolete_files = array(); | 386 local obsolete_uploads = array(); |
387 local i = 0; | 387 local i = 0; |
388 for slot_id in iter do | 388 for slot_id in iter do |
389 i = i + 1; | 389 i = i + 1; |
390 obsolete_files:push(get_filename(slot_id)); | 390 obsolete_uploads:push(slot_id); |
391 upload_cache:set(slot_id, nil); | 391 upload_cache:set(slot_id, nil); |
392 end | 392 end |
393 | 393 |
394 sleep(0.1); | 394 sleep(0.1); |
395 local n = 0; | 395 local n = 0; |
396 obsolete_files:filter(function(filename) | 396 local problem_deleting = false; |
397 obsolete_uploads:filter(function(slot_id) | |
397 n = n + 1; | 398 n = n + 1; |
398 if i % 100 == 0 then sleep(0.1); end | 399 if i % 100 == 0 then sleep(0.1); end |
400 local filename = get_filename(slot_id); | |
399 local deleted, err, errno = os.remove(filename); | 401 local deleted, err, errno = os.remove(filename); |
400 if deleted or errno == ENOENT then | 402 if deleted or errno == ENOENT then |
401 return false; | 403 return true; |
402 else | 404 else |
403 module:log("error", "Could not delete file %q: %s", filename, err); | 405 module:log("error", "Could not delete file %q: %s", filename, err); |
404 return true; | 406 problem_deleting = true; |
407 return false; | |
405 end | 408 end |
406 end); | 409 end); |
410 -- obsolete_uploads now contains slot ids for which the files have been | |
411 -- deleted and that needs to be cleared from the database | |
407 | 412 |
408 local deletion_query = {["end"] = boundary_time}; | 413 local deletion_query = {["end"] = boundary_time}; |
409 if #obsolete_files == 0 then | 414 if not problem_deleting then |
410 module:log("info", "All %d expired files deleted", n); | 415 module:log("info", "All (%d) expired files successfully deleted", n); |
416 -- we can delete based on time | |
411 else | 417 else |
412 module:log("warn", "%d out of %d expired files could not be deleted", #obsolete_files, n); | 418 module:log("warn", "%d out of %d expired files could not be deleted", n-#obsolete_uploads, n); |
413 deletion_query = {ids = obsolete_files}; | 419 -- we'll need to delete only those entries where the files were |
420 -- successfully deleted, and then try again with the failed ones. | |
421 -- eventually the admin ought to notice and fix the permissions or | |
422 -- whatever the problem is. | |
423 deletion_query = {ids = obsolete_uploads}; | |
414 end | 424 end |
415 | 425 |
416 local removed, err = uploads:delete(nil, deletion_query); | 426 local removed, err = uploads:delete(nil, deletion_query); |
417 | 427 |
418 if removed == true or removed == n or removed == #obsolete_files then | 428 if removed == true or removed == n or removed == #obsolete_uploads then |
419 module:log("debug", "Removed all metadata for expired uploaded files"); | 429 module:log("debug", "Removed all metadata for expired uploaded files"); |
420 else | 430 else |
421 module:log("error", "Problem removing metadata for deleted files: %s", err); | 431 module:log("error", "Problem removing metadata for deleted files: %s", err); |
422 end | 432 end |
423 | 433 |