[SPARK-2817] move some code out of function rewritePaths, as it may be called multiple times