Apply upstream fix for bug #8954, to cope with non-writable directories, suitably modified.