use set_perm instead of set_metadata

All credits to "venuatu" for https://gist.github.com/venuatu/7429378