Why do you need to do this programmatically? Seems a bit of a dodgy thing to do, to have a program changing permissions on folders.

Why not just use the supplied OS tools (e.g. subinacl, etc.)?