Tweak file handling code, particularly to improve performance

Tidy up variables and use $this->getBackend()->fileExists()
to avoid regenerating files that already exist.

Could be annoying in the event of a hash collision (probability
2.8e14, so I'm not too concerned). Should significant improve
performance for some kinds of edits.

Change-Id: I2b43823c295a1e70084771704997ee9351230ee5
Merge "Tweak file handling code, particularly to improve performance"