Inheriting file permissions

0

I currently have an AirPlay audio streaming service (Shairport) running on Raspbian on my Raspberry Pi. I have it configured to save the metadata for the songs that are played. For every song, it saves the album cover to the metadata folder. I then wrote another program in Java to read the album covers and display them.

The only problem is that when the files are created by shairport, the permissions are set so that only the shairport user can access them. I have tried using setfacl to change the permissions but either it doesn't work or more likely I am doing something wrong.

How can I set it so that when a file is added to the folder its permissions are automatically set to others?

I have tried every solution I can find and none of them work.

dabecks

Posted 2014-12-19T16:13:07.430

Reputation: 11

Answers

1

I found another solution before the other answers were posted so I don't know if the others work, but in case anyone else needs help with this, this is what I ended up doing:

I wrote the following bash script, which listens for new files to be added to the folder and then automatically changes the permissions for the file.

#!/bin/sh

echo "Starting change_permissions"

inotifywait -m /etc/shairport/metadata/ -e create -e moved_to |
    while read path action file; do
        echo "The file '$file' appeared in directory '$path' via '$action'"
        sudo chmod 777 /etc/shairport/metadata/$file
    done

I then created a daemon for the script to keep it running in the background. I am not sure if this is the best way to do it but I know for a fact that it works.

dabecks

Posted 2014-12-19T16:13:07.430

Reputation: 11

1

Here's a potential solution. I looked at the code within shairport that writes the album cover file. We can probably modify shairport's code to include a specific set of permissions.

I'm relatively new to all this so bear with me.

My idea is to edit the file metadata.c. Here's a link to the file at the shairport github: https://github.com/abrasive/shairport/blob/master/metadata.c

Within that file we're looking for the line that first opens the album art file for writing. In this case it looks like this:

int cover_fd = open(path, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);

This opens the file and stores the resulting code in the int cover_fd which can be used to check if the file was opened successfully.

In c, open() is called like this: open(const char *path, int oflags, mode_t mode). http://codewiki.wikidot.com/c:system-calls:open

In particular mod_t mode defines the permissions of the newly created file. Shairport then sets the permissions as S_IRUSR | S_IWUSR, or (Set read rights for the owner to true and Set write rights for the owner to true.)

Open can also use linux style permissions. This means you can change S_IRUSR | S_IWUSR to, say, 0666 or whatever you like.

Once done save the modified metadata.c and recompile like this:

cd shairport

./configure

make

make install

I don't have a pi or shairport in front of me to test this right now but it should work.

Nate

Posted 2014-12-19T16:13:07.430

Reputation: 11

I hadn't thought of editing shairports files to do that. What I ended up doing was writing a bash script that listened for files to be added and then changing the permissions. Not sure if thats a good way to do it but it works – dabecks – 2015-01-31T20:15:55.300

0

While you are there you may want to think about making a few other changes as well.

If you change the following lines:

size_t pl = strlen(dir) + 1 + strlen(prefix) + strlen(img_md5_str) + 1 + strlen(ext);

char *path = malloc(pl+1);

snprintf(path, pl+1, "%s/%s%s.%s", dir, prefix, img_md5_str, ext);

int cover_fd = open(path, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);

to:

char *cover_file_name = "/cover_art.jpg";

size_t pl = strlen(dir) + strlen(cover_file_name);

char *path = malloc(pl+1);

snprintf(path, pl+1, "%s%s", dir, cover_file_name);

int cover_fd = open(path, O_TRUNC | O_WRONLY | O_CREAT, 0666);

It should modify shairport to continually overwrite one album cover file instead of creating a new one every time. The benefit would be that your java program would always know that the name of the most recent image would be cover_art.jpg. It might be much easier to do things this way instead of having to figure out which jpg is the most recent. I mention this because, at least for me, the FIFO file shairport updates seems to always display the previous song's artwork. This is still untested but should get you on the right path. Good luck!

Nate

Posted 2014-12-19T16:13:07.430

Reputation: 11

Please merge your two accounts and then edit your first answer (and then delete this one).

– G-Man Says 'Reinstate Monica' – 2015-01-29T00:03:04.173

The only thing about that is that when I play a song that I have already played before then it doesn't have to load the album cover again. Its noticeably slower to play new songs since they have to load again. – dabecks – 2015-01-31T20:17:28.037