Problem/Motivation
The managed file garbage collector deletes unused temporary file entities and related files if they haven't been changed for a specified age. The entity is only deleted if the file exists. If the file does not exist then the entity is not deleted and a "Could not delete temporary file "%path" during garbage collection" message is written to the log.
It seems to me that if the desired end result is that both the entity and file be deleted then I don't see why the absence of the file should stop the entity from being deleted.
Of course, in a perfect world, this should never happen. Files and entities should not get out of sync but if it does happen, whatever caused it is lost in history as far as the file module is concerned. It happened to me when I was doing some module development with a form involving an upload. As far as I can see, as an admin, I have no way to delete those entities.
Proposed resolution
I think the entity deletion should proceed if the file (exists and is_writeable) or does not exist. I'm suggesting the is_writeable test because I think that pretty much implies "is_deleteable". If there is some sort of file permissions problem then we probably don't want to delete the entity and leave an orphan file. As it is now, we leave an orphan file if the file delete fails.
Remaining tasks
Discuss if this makes sense. I may be missing a good reason why it was coded like this in file.module.
Create a patch
I'm happy to have a go at creating a patch.
Create a test?
I'm not sure if it would be possible to create a meaningful test for the code in hook_cron.
Perhaps the code in hook_cron which decides "should this be GC'd" could be moved somewhere else. Perhaps an isGarbageCollectable($age) method in the file entity. That would be more testable.
User interface changes
None
API changes
None
Data model changes
None
Comment | File | Size | Author |
---|---|---|---|
#3 | file_garbage_collection-2725457-3.patch | 1.13 KB | Reuben Unruh |
Comments
Comment #3
Reuben Unruh CreditAttribution: Reuben Unruh commentedComment #5
gapple+1
--
Some history trawling:
Looks
file_cron
was added by #1468328: Move file entity info, managed file, and file usage functionality into File module in August 2012, before which it had the same behaviour insystem_cron()
.The particular check was added by #1401558: Remove the usage handling logic from file_delete() in May 2012 with this change:
From what I can tell, the concern that kept the
file_exists()
check is if the file used a disabled stream wrapper. If that is still a concern, I think a more targeted check could be made so that a valid stream wrapper with a non-existent file doesn't result in perpetual error messages. This was discussed briefly in that issue, but appeared to be forgotten as a concern in the final patch reviews.--
On to the patch in this issue:
- I think if the file exists but is not writable, the watchdog should still receive an error, versus the change to a warning.
- If the file doesn't exist and
$file->delete()
was successful, I don't think that a warning should be logged at all. The current message also implies that the delete was not successful.Comment #6
gapple#2802803: Temporary files whose files are missing on the disk result in never-ending error log messages is a newer issue, but appears to have more engagement.