move the 'change to tmp/' code out to its own function.

Coverity #1042334