Thursday, June 22, 2017

RCA: boost::filesystem::permissions: Operation not permitted

There is a configuration file, say /our/product/config.json, in our product.  It was designed with permission 644 which means only the owner of this configuration can update its content. This makes it impossible to support multiple users since each user should be able to update this configuration file by design.

A fix was submitted to address this issue with:
    pt::write_json(...)
    changePermission(666)

The first line will create the configuration file if it does not exist or update it if it has already been created. Note that the 2nd line will set permission as 666 every time this file is created/updated -- a new problem arises:
    boost::filesystem::permissions: Operation not permitted: "/our/product/config.json"
A non-root user is not allowed to change the permission of a file owned by another user, not mention that this configuration file is created by root and owned by root.

A workaround for this is: set the permission to 666 only when it's created. Something like this:
    bool flag = isConfigFileExist(...)  
    pt::write_json(...)
    if (!flag) {
        changePermission(666)
    }

No comments: