Question

I'm trying to create xml file on POST request with fs module of node.js.

    filePath = path.normalize(path.join(profilesDirPath, name + xmlExt));

    fs.exists(filePath, function(exists) {
        if (exists) {
            callback({
                code: h.httpStatus.CONFLICT,
                resp: 'audio profile group with such name exists'
            });
            return;
        }

        fs.open(filePath, 'a', function(error) {
            if (error) {
                callback({
                    code: h.httpStatus.INTERNAL_SERVER_ERROR,
                    resp: error
                });
                return;
            }

            callback(null);
        });
    });

Which results in error:

{
    "errno": 3,
    "code": "EACCES",
    "path": "/mount/fs/folder/newGroup.xml"
}

Node process is running on Linux and folder, where I try to create such file is on the same machine.

What's more, I have no issues with mkdir.

I understand, this is related to permissions, but I cannot grasp why it's different from creating folder and how to fix this.

Was it helpful?

Solution

The first thing you should do is determine the user/group combination that your node.js app is being run as.

node.js at runtime inherits the filesystem permissions of the user & group membership much like any process in linux/unix.

You can do that by examining the running processes with lsof and ps.

For short (shows the running user of your node app):

ps xaf | grep node | awk '{ system("lsof | grep " $1) }' | awk '{ print $3 }'

The second thing you should determine is if the file in question (i.e. /mount/fs/folder/newGroup.xml) has 'write/append' permissions which you are currently specifying as the fs.open() mode.

You can determine the permissions of the file (which I am assuming is going to be a read only file share due to the mount point of /mount indicating a possible read-only filesystem such as those on a DVD/CD drive) like so:

ls -lah /mount/fs/folder/newGroup.xml

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top