danielperna84 / hass-configurator

Configuration UI for Home Assistant
MIT License
306 stars 168 forks source link

Saving a file to a full drive destroys the file instead #240

Closed Biotronic closed 12 months ago

Biotronic commented 1 year ago

First reported this on the File Editor add-on, but they tell me the real issue is here. https://github.com/home-assistant/addons/issues/3206

So I left debug logging on for a while, and my drive filled up. When I tried to fix it, I got an error message when saving configuration.yaml (No space left on device). I then deleted the giant log files, and went back to the file I was editing. To my surprise, that file was now empty.

Clearly, discarding the contents of a file when attempting to save it, is an unfortunate effect and should be avoided if possible. In this case, I assume we could check if there's space, and if not, just not save and then return the error.

Steps to reproduce the issue

Interesting log contents:

WARNING:2023-09-07 09:51:42,551:hass_configurator.configurator:[Errno 28] No space left on device
INFO:2023-09-07 09:51:42,552:hass_configurator.configurator:172.30.32.2 - "POST /api/save HTTP/1.1" 200 -

I tried to figure out the version of Configurator, but I don't see it anywhere.

danielperna84 commented 12 months ago

I don't feel like this is an issue that should be addressed. As this tool can be run on multiple platforms, there is no consistent way to check how much space is available. And even if we would get that information, things like the blocksize of the filesystem would have to be considered as well. So the approach using the filesize is not an option.

The only workaround that could be working would be to first save the content into a temporary file, and then rename the file to the correct location, overwriting the original file. But during this so much could go wrong, that I don't think it's worth the effort as well.

The main problem is, that attempting to write to a file always truncates the content. That's just how file operations on this level work.

And not to forget: when your disk is full, a lot of the other data wouldn't be written to the disk as well. I'm actually surprised you didn't notice the disk being full in the first place, as many other parts of your installation should have given an indication, that your installation is unhealthy.

PS: You can see the version by first clicking on the gear-icon in the top-right corner, and then clicking on About HASS Configurator.