refactor dir traversal and do check on errno