encode dirname safely