Fixed directory file descriptor leak.
Adapted test to make comparable to master.
Fixed file descriptor close logic.
Fixed memory leak when enabled_context mappings already existed.