Refining the fix made in #5885.

It turns out that some of the assumptions in #5885 were slightly off. The previous fix relied on a call to the module to creat a tmp_path. This is insufficent as there are few cases that we need to have the tmp directory before we make the module call. If we don't have a tmp_path before we do a recursive call or when we find a file that does not match the remote md5 hash we need to create a tmp directory. Also we are not more percise when we will need to clean up the remote tmp_path.
Merge pull request #5912 from risaacson/issue5739_5

Refining the fix made in #5885.

Merging this quickly after PR as further testing of of earlier fix demonstrated flaws. Those flaws are now removed and tested to be removed.