unsigned long -> uint64_t
and fix possible closedir when opendir failed before.