This should be called via a trap set on EXIT. First it removes $ROOT/install if it exists.Then if backup tar exists and install was not done properly, removes partially installed package and then restores from backup.If install was done it just removes the backup.Then it removes the temporary directory.If exit status is other than $ERR_LOCKED it calls lock -r to remove the lock.
Since behaviour of cleanup is based on $ROOT, trap should be set after ROOT is determined from environment and argument parsing, ie after a call to installpkg_parseargs.Also, since it tends to remove TMPDIR, it should be set after a call to meta_init_db.Also, as it tends to remove the lock and since lock maynot be the first thing checked it trap should be set immediately before lock is called.
Since meta_init_db is called after lock is set (we mess with file system after creating a lock).Above conditions cannot be met.Ensuring that TMPDIR has an empty value before defining meta_init_db allows and not setting TMPDIR anywhere until meta_init_db is called allows setting the trap right before calling lock and allows meta_init_db to be called after lock by ensuring that we do not remove anything else stored in environmental TMPDIR in case trap is triggered before a call to meta_init_db.